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

Method createESMInitString

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

Source from the content-addressed store, hash-verified

208
209 // create the ESM init-models file to load all the models into Sequelize
210 private createESMInitString(tables: string[], assoc: string) {
211 let str = 'import _sequelize from "sequelize";\n';
212 str += 'const DataTypes = _sequelize.DataTypes;\n';
213 const sp = this.space[1];
214 const modelNames: string[] = [];
215 // import statements
216 tables.forEach(t => {
217 const fileName = recase(this.options.caseFile, t, this.options.singularize);
218 const modelName = makeTableName(this.options.caseModel, t, this.options.singularize, this.options.lang);
219 modelNames.push(modelName);
220 str += `import _${modelName} from "./${fileName}.js";\n`;
221 });
222 // create the initialization function
223 str += '\nexport default function initModels(sequelize) {\n';
224 modelNames.forEach(m => {
225 str += `${sp}const ${m} = _${m}.init(sequelize, DataTypes);\n`;
226 });
227
228 // add the associations
229 str += "\n" + assoc;
230
231 // return the models
232 str += `\n${sp}return {\n`;
233 modelNames.forEach(m => {
234 str += `${this.space[2]}${m},\n`;
235 });
236 str += `${sp}};\n`;
237 str += '}\n';
238 return str;
239 }
240}

Callers 1

createInitStringMethod · 0.95

Calls 2

recaseFunction · 0.90
makeTableNameFunction · 0.90

Tested by

no test coverage detected