MCPcopy
hub / github.com/sequelize/sequelize-auto / createTsInitString

Method createTsInitString

src/auto-writer.ts:127–172  ·  view source on GitHub ↗
(tables: string[], assoc: string)

Source from the content-addressed store, hash-verified

125
126 // create the TypeScript init-models file to load all the models into Sequelize
127 private createTsInitString(tables: string[], assoc: string) {
128 let str = 'import type { Sequelize } from "sequelize";\n';
129 const sp = this.space[1];
130 const modelNames: string[] = [];
131 // import statements
132 tables.forEach(t => {
133 const fileName = recase(this.options.caseFile, t, this.options.singularize);
134 const modelName = makeTableName(this.options.caseModel, t, this.options.singularize, this.options.lang);
135 modelNames.push(modelName);
136 str += `import { ${modelName} as _${modelName} } from "./${fileName}";\n`;
137 str += `import type { ${modelName}Attributes, ${modelName}CreationAttributes } from "./${fileName}";\n`;
138 });
139 // re-export the model classes
140 str += '\nexport {\n';
141 modelNames.forEach(m => {
142 str += `${sp}_${m} as ${m},\n`;
143 });
144 str += '};\n';
145
146 // re-export the model attirbutes
147 str += '\nexport type {\n';
148 modelNames.forEach(m => {
149 str += `${sp}${m}Attributes,\n`;
150 str += `${sp}${m}CreationAttributes,\n`;
151 });
152 str += '};\n\n';
153
154 // create the initialization function
155 str += 'export function initModels(sequelize: Sequelize) {\n';
156 modelNames.forEach(m => {
157 str += `${sp}const ${m} = _${m}.initModel(sequelize);\n`;
158 });
159
160 // add the asociations
161 str += "\n" + assoc;
162
163 // return the models
164 str += `\n${sp}return {\n`;
165 modelNames.forEach(m => {
166 str += `${this.space[2]}${m}: ${m},\n`;
167 });
168 str += `${sp}};\n`;
169 str += '}\n';
170
171 return str;
172 }
173
174 // create the ES5 init-models file to load all the models into Sequelize
175 private createES5InitString(tables: string[], assoc: string, vardef: string) {

Callers 1

createInitStringMethod · 0.95

Calls 2

recaseFunction · 0.90
makeTableNameFunction · 0.90

Tested by

no test coverage detected