Skip to content

Commit 7ad3d30

Browse files
committed
Append an underscore if model name is a reserved word (#562, #570)
Remove 'esmd' lang option and standardize 'useDefine' behavior
1 parent 728b240 commit 7ad3d30

File tree

4 files changed

+24
-44
lines changed

4 files changed

+24
-44
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"@types/chai": "^4.2.17",
7474
"@types/lodash": "^4.14.168",
7575
"@types/mocha": "^8.2.2",
76+
"@types/reserved-words": "0.1.0",
7677
"chai": "^4.3.4",
7778
"coveralls": "^3.1.0",
7879
"crlf": "^1.1.1",
@@ -87,6 +88,7 @@
8788
"nyc": "^15.1.0",
8889
"pg": "^8.5.1",
8990
"pg-hstore": "^2.3.3",
91+
"reserved-words": "^0.1.2",
9092
"rimraf": "^3.0.2",
9193
"sequelize": "^6.11",
9294
"sqlite3": "5.0.2",

src/auto-generator.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from "lodash";
22
import { ColumnDescription } from "sequelize/types";
33
import { DialectOptions, FKSpec } from "./dialects/dialect-options";
4-
import { AutoOptions, CaseFileOption, CaseOption, Field, IndexSpec, LangOption, pluralize, qNameJoin, qNameSplit, recase, Relation, singularize, TableData, TSField } from "./types";
4+
import { AutoOptions, CaseFileOption, CaseOption, Field, IndexSpec, LangOption, makeTableName, pluralize, qNameJoin, qNameSplit, recase, Relation, singularize, TableData, TSField } from "./types";
55

66
/** Generates text from each table in TableData */
77
export class AutoGenerator {
@@ -69,12 +69,11 @@ export class AutoGenerator {
6969
} else if (this.options.lang === 'esm') {
7070
header += "import _sequelize from 'sequelize';\n";
7171
header += "const { Model, Sequelize } = _sequelize;\n\n";
72+
header += "export default class #TABLE# extends Model {\n";
73+
header += sp + "static init(sequelize, DataTypes) {\n";
7274
if (this.options.useDefine) {
73-
header += "export default function(sequelize, DataTypes) {\n";
7475
header += sp + "return sequelize.define('#TABLE#', {\n";
7576
} else {
76-
header += "export default class #TABLE# extends Model {\n";
77-
header += sp + "static init(sequelize, DataTypes) {\n";
7877
header += sp + "return super.init({\n";
7978
}
8079
} else {
@@ -94,7 +93,7 @@ export class AutoGenerator {
9493
tableNames.forEach(table => {
9594
let str = header;
9695
const [schemaName, tableNameOrig] = qNameSplit(table);
97-
const tableName = recase(this.options.caseModel, tableNameOrig, this.options.singularize);
96+
const tableName = makeTableName(this.options.caseModel, tableNameOrig, this.options.singularize, this.options.lang);
9897

9998
if (this.options.lang === 'ts') {
10099
const associations = this.addTypeScriptAssociationMixins(table);
@@ -173,7 +172,6 @@ export class AutoGenerator {
173172
private addTable(table: string) {
174173

175174
const [schemaName, tableNameOrig] = qNameSplit(table);
176-
const tableName = recase(this.options.caseModel, tableNameOrig, this.options.singularize);
177175
const space = this.space;
178176
let timestamps = (this.options.additional && this.options.additional.timestamps === true) || false;
179177
let paranoid = (this.options.additional && this.options.additional.paranoid === true) || false;

src/auto-writer.ts

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import _ from "lodash";
33
import path from "path";
44
import util from "util";
55
import { FKSpec, TableData } from ".";
6-
import { AutoOptions, CaseFileOption, CaseOption, LangOption, pluralize, qNameSplit, recase, Relation } from "./types";
6+
import { AutoOptions, CaseFileOption, CaseOption, LangOption, makeTableName, pluralize, qNameSplit, recase, Relation } from "./types";
77
const mkdirp = require('mkdirp');
88

99
/** Writes text into files from TableData.text, and writes init-models */
@@ -72,8 +72,6 @@ export class AutoWriter {
7272
return this.createTsInitString(tableNames, assoc);
7373
case 'esm':
7474
return this.createESMInitString(tableNames, assoc);
75-
case 'esmd':
76-
return this.createESMDInitString(tableNames, assoc);
7775
case 'es6':
7876
return this.createES5InitString(tableNames, assoc, "const");
7977
default:
@@ -127,7 +125,7 @@ export class AutoWriter {
127125
// import statements
128126
tables.forEach(t => {
129127
const fileName = recase(this.options.caseFile, t, this.options.singularize);
130-
const modelName = recase(this.options.caseModel, t, this.options.singularize);
128+
const modelName = makeTableName(this.options.caseModel, t, this.options.singularize, this.options.lang);
131129
modelNames.push(modelName);
132130
str += `import { ${modelName} as _${modelName} } from "./${fileName}";\n`;
133131
str += `import type { ${modelName}Attributes, ${modelName}CreationAttributes } from "./${fileName}";\n`;
@@ -174,7 +172,7 @@ export class AutoWriter {
174172
// import statements
175173
tables.forEach(t => {
176174
const fileName = recase(this.options.caseFile, t, this.options.singularize);
177-
const modelName = recase(this.options.caseModel, t, this.options.singularize);
175+
const modelName = makeTableName(this.options.caseModel, t, this.options.singularize, this.options.lang);
178176
modelNames.push(modelName);
179177
str += `${vardef} _${modelName} = require("./${fileName}");\n`;
180178
});
@@ -200,34 +198,7 @@ export class AutoWriter {
200198
str += 'module.exports.default = initModels;\n';
201199
return str;
202200
}
203-
// create the ES6 init-models file to load all the models (with define-syntax instead of classes) into Sequelize
204-
createESMDInitString(tables: string[], assoc: string) {
205-
let str = 'import _sequelize from "sequelize";\n';
206-
str += 'const DataTypes = _sequelize.DataTypes;\n';
207-
const modelNames: string[] = [];
208-
// import statements
209-
tables.forEach(t => {
210-
const fileName = recase(this.options.caseFile, t, this.options.singularize);
211-
const modelName = recase(this.options.caseModel, t, this.options.singularize);
212-
modelNames.push(modelName);
213-
str += `import _${modelName} from "./${fileName}.js";\n`;
214-
});
215-
// create the initialization function
216-
str += '\nexport default function initModels(sequelize) {\n';
217-
modelNames.forEach(m => {
218-
str += ` const ${m} = _${m}(sequelize, DataTypes);\n`;
219-
});
220-
// add the asociations
221-
str += "\n" + assoc;
222-
// return the models
223-
str += "\n return {\n";
224-
modelNames.forEach(m => {
225-
str += ` ${m},\n`;
226-
});
227-
str += ' };\n';
228-
str += '}\n';
229-
return str;
230-
}
201+
231202
// create the ESM init-models file to load all the models into Sequelize
232203
private createESMInitString(tables: string[], assoc: string) {
233204
let str = 'import _sequelize from "sequelize";\n';
@@ -236,18 +207,17 @@ export class AutoWriter {
236207
// import statements
237208
tables.forEach(t => {
238209
const fileName = recase(this.options.caseFile, t, this.options.singularize);
239-
const modelName = recase(this.options.caseModel, t, this.options.singularize);
210+
const modelName = makeTableName(this.options.caseModel, t, this.options.singularize, this.options.lang);
240211
modelNames.push(modelName);
241212
str += `import _${modelName} from "./${fileName}.js";\n`;
242213
});
243-
244214
// create the initialization function
245215
str += '\nexport default function initModels(sequelize) {\n';
246216
modelNames.forEach(m => {
247217
str += ` const ${m} = _${m}.init(sequelize, DataTypes);\n`;
248218
});
249219

250-
// add the asociations
220+
// add the associations
251221
str += "\n" + assoc;
252222

253223
// return the models

src/types.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import _ from "lodash";
2+
import { check as isReserved } from "reserved-words";
23
import { Utils } from "sequelize";
34
import { ColumnDescription, Dialect } from "sequelize/types";
45
import { FKSpec } from "./dialects/dialect-options";
@@ -107,7 +108,7 @@ export function qNameJoin(schema: string | undefined, table: string | undefined)
107108
}
108109

109110
/** Language of output model files */
110-
export declare type LangOption = "es5" | "es6" | "esm" | "ts" | "esmd";
111+
export declare type LangOption = "es5" | "es6" | "esm" | "ts";
111112

112113
/** "c" camelCase |
113114
* "l" lower_case |
@@ -180,7 +181,7 @@ export interface AutoOptions {
180181
views?: boolean;
181182
/** Primary Key Suffixes to trim (default "id") */
182183
pkSuffixes?: string[];
183-
/** Use `sequelize.define` instead of `init` for model initialization */
184+
/** Use `sequelize.define` instead of `init` for model initialization. See issues #527, #559, #573 */
184185
useDefine: boolean;
185186
}
186187

@@ -227,3 +228,12 @@ export function recase(opt: CaseOption | CaseFileOption | undefined, val: string
227228
return val;
228229
}
229230

231+
const tsNames = ["DataTypes", "Model", "Optional", "Sequelize"];
232+
export function makeTableName(opt: CaseOption | undefined, tableNameOrig: string | null, singular = false, lang = "es5") {
233+
let name = recase(opt, tableNameOrig, singular);
234+
if (isReserved(name) || (lang == "ts" && tsNames.includes(name))) {
235+
name += "_";
236+
}
237+
return name;
238+
}
239+

0 commit comments

Comments
 (0)