gina-sequelize 0.0.11 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.ts +29 -17
- package/dist/gina.js +119 -8
- package/dist/gina.js.map +1 -1
- package/package.json +3 -2
package/bin/index.ts
CHANGED
|
@@ -113,22 +113,31 @@ export { initializeModels };
|
|
|
113
113
|
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
+
async function loadGinaLib(pathName) {
|
|
117
|
+
try {
|
|
118
|
+
const ginaLib = await import(pathName + '/node_modules/gina-sequelize/dist/gina.js');
|
|
119
|
+
return ginaLib.default.default;
|
|
120
|
+
} catch {
|
|
121
|
+
const ginaLib = await import(pathName + '/lib/gina.ts');
|
|
122
|
+
return ginaLib.default;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
116
126
|
async function generateMigration(migrationName) {
|
|
117
127
|
try {
|
|
118
128
|
const pathName = process.cwd();
|
|
119
129
|
const initFile = await import(pathName + '/gina/initializeModels.ts');
|
|
120
130
|
|
|
121
131
|
console.log('loading models...');
|
|
122
|
-
|
|
132
|
+
let initializeModels = initFile.initializeModels;
|
|
133
|
+
if (!initializeModels) {
|
|
134
|
+
initializeModels = initFile.default.initializeModels;
|
|
135
|
+
}
|
|
136
|
+
const sequelize = await initializeModels();
|
|
123
137
|
|
|
124
138
|
console.log('loading gina lib...');
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
await ginaLib.default.createMigration(sequelize, migrationName);
|
|
128
|
-
} catch (err) {
|
|
129
|
-
const ginaLib = await import(pathName + '/lib/gina.ts');
|
|
130
|
-
await ginaLib.default.createMigration(sequelize, migrationName);
|
|
131
|
-
}
|
|
139
|
+
const ginaLib = await loadGinaLib(pathName);
|
|
140
|
+
await ginaLib.createMigration(sequelize, migrationName);
|
|
132
141
|
|
|
133
142
|
process.exit();
|
|
134
143
|
} catch (err) {
|
|
@@ -144,7 +153,11 @@ async function upgrade() {
|
|
|
144
153
|
const initFile = await import(pathName + '/gina/initializeModels.ts');
|
|
145
154
|
|
|
146
155
|
console.log('loading models...');
|
|
147
|
-
|
|
156
|
+
let initializeModels = initFile.initializeModels;
|
|
157
|
+
if (!initializeModels) {
|
|
158
|
+
initializeModels = initFile.default.initializeModels;
|
|
159
|
+
}
|
|
160
|
+
const sequelize = await initializeModels();
|
|
148
161
|
|
|
149
162
|
console.log('loading migrations...');
|
|
150
163
|
const files = (await readdir(pathName + '/gina/migrations')).filter(file => !file.includes('.d.ts') && !file.includes('.js.map'));
|
|
@@ -154,17 +167,16 @@ async function upgrade() {
|
|
|
154
167
|
|
|
155
168
|
for (const file of files) {
|
|
156
169
|
const filePath = await import(pathName + '/gina/migrations/' + file);
|
|
157
|
-
|
|
170
|
+
if (filePath.default) {
|
|
171
|
+
migrations[file.replace('.ts', '')] = filePath.default;
|
|
172
|
+
} else {
|
|
173
|
+
migrations[file.replace('.ts', '')] = filePath;
|
|
174
|
+
}
|
|
158
175
|
}
|
|
159
176
|
|
|
160
177
|
console.log('loading gina lib...');
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
await ginaLib.default.runMigrations(sequelize, migrations);
|
|
164
|
-
} catch (err) {
|
|
165
|
-
const ginaLib = await import(pathName + '/lib/gina.ts');
|
|
166
|
-
await ginaLib.default.runMigrations(sequelize, migrations);
|
|
167
|
-
}
|
|
178
|
+
const ginaLib = await loadGinaLib(pathName);
|
|
179
|
+
await ginaLib.runMigrations(sequelize, migrations);
|
|
168
180
|
|
|
169
181
|
process.exit();
|
|
170
182
|
} catch (err) {
|
package/dist/gina.js
CHANGED
|
@@ -53,6 +53,7 @@ class Migration {
|
|
|
53
53
|
this.downFields = '';
|
|
54
54
|
this.upIndexes = '';
|
|
55
55
|
this.downIndexes = '';
|
|
56
|
+
this.upForeignKey = '';
|
|
56
57
|
/**
|
|
57
58
|
*
|
|
58
59
|
*/
|
|
@@ -99,6 +100,9 @@ class Migration {
|
|
|
99
100
|
if (model.type instanceof sequelize_1.DataTypes.TEXT) {
|
|
100
101
|
return 'DataTypes.TEXT';
|
|
101
102
|
}
|
|
103
|
+
if (model.type instanceof sequelize_1.DataTypes.BOOLEAN) {
|
|
104
|
+
return 'DataTypes.BOOLEAN';
|
|
105
|
+
}
|
|
102
106
|
return '';
|
|
103
107
|
}
|
|
104
108
|
getDefaultValue(model) {
|
|
@@ -112,6 +116,9 @@ class Migration {
|
|
|
112
116
|
else if (typeof model.defaultValue == 'number') {
|
|
113
117
|
return String(model.defaultValue);
|
|
114
118
|
}
|
|
119
|
+
else if (typeof model.defaultValue == 'boolean') {
|
|
120
|
+
return String(model.defaultValue);
|
|
121
|
+
}
|
|
115
122
|
return `'${model.defaultValue}'`;
|
|
116
123
|
}
|
|
117
124
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -129,8 +136,39 @@ class Migration {
|
|
|
129
136
|
if (attribute.type instanceof sequelize_1.DataTypes.VIRTUAL) {
|
|
130
137
|
continue;
|
|
131
138
|
}
|
|
139
|
+
if (modelAttrs[modelAttrsKey].references) {
|
|
140
|
+
const references = modelAttrs[modelAttrsKey].references;
|
|
141
|
+
if (typeof references !== 'string') {
|
|
142
|
+
this.upForeignKey += `
|
|
143
|
+
await queryInterface.addConstraint('${model.getTableName()}', {
|
|
144
|
+
type: 'foreign key',
|
|
145
|
+
fields: ['${modelAttrs[modelAttrsKey].field}'],
|
|
146
|
+
onDelete: '${modelAttrs[modelAttrsKey].onDelete}',
|
|
147
|
+
onUpdate: '${modelAttrs[modelAttrsKey].onUpdate}',
|
|
148
|
+
references: {
|
|
149
|
+
table: '${references?.model}',
|
|
150
|
+
field: '${references?.key}'
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
`;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
132
156
|
this.upTables += tabsToSpace(`\t\t\t${modelAttrsKey}: {${this.attributeProps(modelAttrs[modelAttrsKey], 4)}\n\t\t\t},\n`);
|
|
133
157
|
}
|
|
158
|
+
const modelIndexes = model.options.indexes;
|
|
159
|
+
if (modelIndexes) {
|
|
160
|
+
for (const modelIndex of modelIndexes) {
|
|
161
|
+
this.upIndexes += `
|
|
162
|
+
await queryInterface.addIndex('${model.getTableName()}', ['${modelIndex.fields?.join('\', \'')}'], {
|
|
163
|
+
name: '${modelIndex.name}',
|
|
164
|
+
unique: ${modelIndex.unique},
|
|
165
|
+
});
|
|
166
|
+
`;
|
|
167
|
+
this.downIndexes = `
|
|
168
|
+
await queryInterface.removeIndex('${model.getTableName()}', '${modelIndex.name}');
|
|
169
|
+
` + this.downIndexes;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
134
172
|
this.upTables += tabsToSpace(`\t\t});`);
|
|
135
173
|
this.downTables = tabsToSpace(`\n\t\tawait queryInterface.dropTable('${model.getTableName()}');\n`) + this.downTables;
|
|
136
174
|
}
|
|
@@ -167,8 +205,14 @@ class Migration {
|
|
|
167
205
|
this.imports.push('DataTypes');
|
|
168
206
|
}
|
|
169
207
|
let attrProps = '';
|
|
170
|
-
|
|
171
|
-
|
|
208
|
+
let dataTypes = attribute.type;
|
|
209
|
+
if (dataTypes.includes('VARCHAR(')) {
|
|
210
|
+
dataTypes = dataTypes.replace('VARCHAR(', 'STRING(');
|
|
211
|
+
}
|
|
212
|
+
else if (dataTypes == 'INT') {
|
|
213
|
+
dataTypes = 'INTEGER';
|
|
214
|
+
}
|
|
215
|
+
attrProps += tabsToSpace(`\n${tabs(3)}type: DataTypes.${dataTypes},`);
|
|
172
216
|
if (attribute.autoIncrement !== null) {
|
|
173
217
|
attrProps += tabsToSpace(`\n${tabs(3)}autoIncrement: ${this.getAutoIncrement(attribute)},`);
|
|
174
218
|
}
|
|
@@ -203,22 +247,87 @@ class Migration {
|
|
|
203
247
|
const queryInterface = this.sequelize.getQueryInterface();
|
|
204
248
|
const attrs = await queryInterface.describeTable(modelTable);
|
|
205
249
|
const modelFields = [];
|
|
250
|
+
// const modelRefs: string[] = [];
|
|
251
|
+
// for (const modelAttr of Object.keys(modelAttrs)) {
|
|
252
|
+
// if (modelAttrs[modelAttr].references) {
|
|
253
|
+
// modelRefs.push(modelAttrs[modelAttr].field || '');
|
|
254
|
+
// }
|
|
255
|
+
// }
|
|
206
256
|
// check each field
|
|
207
257
|
for (const modelAttr of Object.keys(modelAttrs)) {
|
|
208
258
|
modelFields.push(modelAttrs[modelAttr].field || modelAttr);
|
|
209
259
|
if (!Object.keys(attrs).includes(modelAttrs[modelAttr].field || modelAttr) && !(modelAttrs[modelAttr].type instanceof sequelize_1.DataTypes.VIRTUAL)) {
|
|
210
260
|
this.newColumn(modelTable, modelAttrs[modelAttr]);
|
|
261
|
+
if (modelAttrs[modelAttr].references) {
|
|
262
|
+
const references = modelAttrs[modelAttr].references;
|
|
263
|
+
if (typeof references !== 'string') {
|
|
264
|
+
this.upForeignKey += `
|
|
265
|
+
await queryInterface.addConstraint('${modelTable}', {
|
|
266
|
+
type: 'foreign key',
|
|
267
|
+
fields: ['${modelAttrs[modelAttr].field}'],
|
|
268
|
+
onDelete: '${modelAttrs[modelAttr].onDelete}',
|
|
269
|
+
onUpdate: '${modelAttrs[modelAttr].onUpdate}',
|
|
270
|
+
references: {
|
|
271
|
+
table: '${references?.model}',
|
|
272
|
+
field: '${references?.key}'
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
`;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
211
278
|
}
|
|
212
279
|
}
|
|
280
|
+
// drop column
|
|
213
281
|
for (const attr of Object.keys(attrs)) {
|
|
214
282
|
if (!modelFields.includes(attr)) {
|
|
215
283
|
this.dropColumn(modelTable, attr, attrs[attr]);
|
|
216
284
|
}
|
|
217
285
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
286
|
+
const indexes = await queryInterface.showIndex(modelTable);
|
|
287
|
+
const modelIndexes = this.sequelize.models[model].options.indexes;
|
|
288
|
+
if (modelIndexes) {
|
|
289
|
+
for (const modelIndex of modelIndexes) {
|
|
290
|
+
if (!indexes.find((index) => index.name == modelIndex.name)) {
|
|
291
|
+
this.upIndexes += `
|
|
292
|
+
await queryInterface.addIndex('${modelTable}', ['${modelIndex.fields?.join('\', \'')}'], {
|
|
293
|
+
name: '${modelIndex.name}',
|
|
294
|
+
unique: ${modelIndex.unique},
|
|
295
|
+
});
|
|
296
|
+
`;
|
|
297
|
+
this.downIndexes = `
|
|
298
|
+
await queryInterface.removeIndex('${modelTable}', '${modelIndex.name}');
|
|
299
|
+
` + this.downIndexes;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (indexes) {
|
|
304
|
+
for (const index of indexes) {
|
|
305
|
+
if (index.name === 'PRIMARY') {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (index.name.endsWith('_fk')) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (index.name.includes('_ibfk')) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
// if (modelRefs.includes(index.name)) {
|
|
315
|
+
// continue;
|
|
316
|
+
// }
|
|
317
|
+
if (modelIndexes === undefined || !modelIndexes.find((modelIndex) => index.name == modelIndex.name)) {
|
|
318
|
+
this.upIndexes += `
|
|
319
|
+
await queryInterface.removeIndex('${modelTable}', '${index.name}');
|
|
320
|
+
|
|
321
|
+
`;
|
|
322
|
+
this.downIndexes = `
|
|
323
|
+
await queryInterface.addIndex('${modelTable}', ['${index.fields?.map(field => field.attribute).join('\', \'')}'], {
|
|
324
|
+
name: '${index.name}',
|
|
325
|
+
unique: ${index.unique},
|
|
326
|
+
});
|
|
327
|
+
` + this.downIndexes;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
222
331
|
}
|
|
223
332
|
for (const table of this.tables) {
|
|
224
333
|
if (!modelTableNames.includes(table) && table != 'gina_version') {
|
|
@@ -233,9 +342,12 @@ export const migration = {
|
|
|
233
342
|
async up(queryInterface: QueryInterface) {
|
|
234
343
|
${this.upTables}
|
|
235
344
|
${this.upFields}
|
|
345
|
+
${this.upForeignKey}
|
|
346
|
+
${this.upIndexes}
|
|
236
347
|
},
|
|
237
348
|
|
|
238
349
|
async down(queryInterface: QueryInterface) {
|
|
350
|
+
${this.downIndexes}
|
|
239
351
|
${this.downFields}
|
|
240
352
|
${this.downTables}
|
|
241
353
|
}
|
|
@@ -247,9 +359,8 @@ async function createMigration(sequelize, migrationName) {
|
|
|
247
359
|
const migration = new Migration(sequelize);
|
|
248
360
|
await migration.loadDatabaseTables();
|
|
249
361
|
await migration.checkDiffs();
|
|
250
|
-
if (!migration.upTables && !migration.upFields && !migration.upIndexes) {
|
|
362
|
+
if (!migration.upTables && !migration.upFields && !migration.upIndexes && !migration.upForeignKey) {
|
|
251
363
|
console.info('There\'s no changes between your models and the database.');
|
|
252
|
-
return;
|
|
253
364
|
}
|
|
254
365
|
await migration.generateFile(migrationName);
|
|
255
366
|
}
|
package/dist/gina.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gina.js","sourceRoot":"/","sources":["gina.ts"],"names":[],"mappings":";;;;;AAAA,yCAA+H;AAC/H,2DAA6B;AAC7B,oDAA4B;AAO5B,KAAK,UAAU,aAAa,CAAC,SAAoB,EAAE,cAA8B;IAC7E,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,iBAAiB,EAAE,CAAC,aAAa,EAAE,CAAC;IAEnE,IAAI,OAAO,GAAG,EAAE,CAAC;IAEjB,IAAI,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE;QACjC,MAAM,QAAQ,GAA2B,MAAM,SAAS,CAAC,KAAK,CAAC,mCAAmC,EAAE;YAChG,IAAI,EAAE,sBAAU,CAAC,MAAM;SAC1B,CAAC,CAAC;QAEH,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;SAC9E;QAED,OAAO,GAAG,QAAQ,C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K,UAAU,eAAe,CAAC,SAAoB,EAAE,aAAqB;IAEtE,MAAM,SAAS,GAAG,IAAI,SAAS,CAAC,SAAS,CAAC,CAAC;IAC3C,MAAM,SAAS,CAAC,kBAAkB,EAAE,CAAC;IAErC,MAAM,SAAS,CAAC,UAAU,EAAE,CAAC;IAE7B,IAAI,CAAC,SAAS,CAAC,QAAQ,IAAI,CAAC,SAAS,CAAC,QAAQ,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE;QACpE,OAAO,CAAC,IAAI,CAAC,2DAA2D,CAAC,CAAC;QAC1E,OAAO;KACV;IACD,MAAM,SAAS,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC;AAEhD,CAAC;AAED,MAAM,IAAI,GAAG;IACT,aAAa;IACb,eAAe;CAClB,CAAC;AAEF,kBAAe,IAAI,CAAC","sourcesContent":["import { DataTypes, ModelCtor, Model, Sequelize, ModelAttributeColumnOptions, QueryTypes, ColumnDescription } from 'sequelize';\nimport fs from 'fs/promises';\nimport moment from 'moment';\nimport { MigrationFile } from './typings/migration';\n\ninterface MigrationFiles {\n [version: string]: { migration: MigrationFile; };\n}\n\nasync function runMigrations(sequelize: Sequelize, migrationFiles: MigrationFiles) {\n const tables = await sequelize.getQueryInterface().showAllTables();\n\n let version = '';\n\n if (tables.includes('gina_version')) {\n const versions: { version: string; }[] = await sequelize.query('SELECT version FROM gina_version;', {\n type: QueryTypes.SELECT,\n });\n\n if (versions.length > 1) {\n throw new Error('There must be only one version on `gina_version` table.');\n }\n\n version = versions.length == 1 ? versions[0].version : '';\n }\n\n console.info('current version:', version);\n\n for (const migrationFile of Object.keys(migrationFiles)) {\n if (migrationFile > version) {\n console.info('applying migration:', migrationFile);\n await migrationFiles[migrationFile].migration.up(sequelize.getQueryInterface());\n if (version) {\n version = migrationFile;\n await sequelize.query(`UPDATE gina_version SET version = '${version}', updatedAt = NOW();`);\n } else {\n version = migrationFile;\n await sequelize.query(`INSERT INTO gina_version (updatedAt, version) VALUES (NOW(),'${version}');`);\n }\n console.info('applied version:', migrationFile);\n }\n }\n}\n\nfunction tabsToSpace(str: string) {\n return str.replaceAll('\\t', ' ');\n}\n\nfunction tabs(qty: number) {\n let tabText = '';\n\n for (let i = 0; i < qty; i++) {\n tabText += '\\t';\n }\n\n return tabText;\n}\n\nclass Migration {\n upTables: string;\n downTables: string;\n upFields: string;\n downFields: string;\n upIndexes: string;\n downIndexes: string;\n private sequelize: Sequelize;\n tables: string[];\n imports: string[];\n\n constructor(sequelize: Sequelize) {\n this.upTables = '';\n this.downTables = '';\n this.upFields = '';\n this.downFields = '';\n this.upIndexes = '';\n this.downIndexes = '';\n\n /**\n *\n */\n this.tables = [];\n this.sequelize = sequelize;\n this.imports = ['QueryInterface'];\n }\n\n async loadDatabaseTables() {\n const queryInterface = this.sequelize.getQueryInterface();\n this.tables = await queryInterface.showAllTables();\n }\n\n dropTable(tableName: string) {\n // TODO: create proper logic to delete table\n console.debug(`drop table ${tableName} `);\n\n this.upTables += `\n await queryInterface.dropTable('${tableName}');\n`;\n\n this.downTables = `\n await queryInterface.createTable('${tableName}', {});\n` + this.downTables;\n }\n\n getAllowNull(model: { allowNull?: boolean; }) {\n return model.allowNull ?? 'true' ? 'true' : 'false';\n }\n\n getAutoIncrement(model: { autoIncrement?: boolean; }) {\n return model.autoIncrement ?? 'true' ? 'true' : 'false';\n }\n\n getPrimaryKey(model: { primaryKey?: boolean; }) {\n return model.primaryKey ?? 'true' ? 'true' : 'false';\n }\n\n getType(model: { type?: DataTypes.DataType; }) {\n if (model.type instanceof DataTypes.BIGINT) {\n return 'DataTypes.BIGINT';\n }\n if (model.type instanceof DataTypes.INTEGER) {\n return 'DataTypes.INTEGER';\n }\n if (model.type instanceof DataTypes.STRING) {\n return 'DataTypes.STRING';\n }\n if (model.type instanceof DataTypes.DATE) {\n return 'DataTypes.DATE';\n }\n if (model.type instanceof DataTypes.TEXT) {\n return 'DataTypes.TEXT';\n }\n return '';\n }\n\n getDefaultValue(model: { defaultValue?: unknown; }) {\n\n const val = (model.defaultValue as { val?: string; }).val;\n\n if (val) {\n if (!this.imports.includes('Sequelize')) {\n this.imports.push('Sequelize');\n }\n return `Sequelize.literal('${val}')`;\n } else if (typeof model.defaultValue == 'number') {\n return String(model.defaultValue);\n }\n\n return `'${model.defaultValue}'`;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n createTable(model: ModelCtor<Model<any, any>>) {\n // TODO: create proper logic to create table\n console.debug(`new table ${model.getTableName()}`);\n const modelAttrs = model.getAttributes();\n const modelAttrsKeys = Object.keys(modelAttrs);\n this.upTables += tabsToSpace(`\\n\\t\\tawait queryInterface.createTable('${model.getTableName()}', {\\n`);\n\n if (!this.imports.includes('DataTypes')) {\n this.imports.push('DataTypes');\n }\n\n for (const modelAttrsKey of modelAttrsKeys) {\n\n const attribute = modelAttrs[modelAttrsKey];\n\n if (attribute.type instanceof DataTypes.VIRTUAL) {\n continue;\n }\n\n this.upTables += tabsToSpace(`\\t\\t\\t${modelAttrsKey}: {${this.attributeProps(modelAttrs[modelAttrsKey], 4)}\\n\\t\\t\\t},\\n`);\n }\n\n this.upTables += tabsToSpace(`\\t\\t});`);\n\n this.downTables = tabsToSpace(`\\n\\t\\tawait queryInterface.dropTable('${model.getTableName()}');\\n`) + this.downTables;\n }\n\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n attributeProps(attribute: ModelAttributeColumnOptions<Model<any, any>>, tabsQty: number) {\n\n if (!this.imports.includes('DataTypes')) {\n this.imports.push('DataTypes');\n }\n\n let attrProps = '';\n\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}type: ${this.getType(attribute)},`);\n if (attribute.autoIncrement !== undefined) {\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}autoIncrement: ${this.getAutoIncrement(attribute)},`);\n }\n if (attribute.allowNull !== undefined) {\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}allowNull: ${this.getAllowNull(attribute)},`);\n }\n if (attribute.primaryKey !== undefined) {\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}primaryKey: ${this.getPrimaryKey(attribute)},`);\n }\n if (attribute.defaultValue !== undefined) {\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}defaultValue: ${this.getDefaultValue(attribute)},`);\n }\n\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}field: '${attribute.field}'`);\n\n return attrProps;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n newColumn(tableName: string, attribute: ModelAttributeColumnOptions<Model<any, any>>) {\n console.debug(`new column ${tableName} ${attribute.field}`);\n this.upFields += tabsToSpace(`\\n\\t\\tawait queryInterface.addColumn('${tableName}', '${attribute.field}', {${this.attributeProps(attribute, 3)}\\n\\t\\t});\\n`);\n this.downFields = tabsToSpace(`\\n\\t\\tawait queryInterface.removeColumn('${tableName}', '${attribute.field}');\\n`) + this.downFields;\n }\n\n columnAttributeProps(attribute: ColumnDescription) {\n if (!this.imports.includes('DataTypes')) {\n this.imports.push('DataTypes');\n }\n\n let attrProps = '';\n\n console.log('attribute.defaultValue', attribute.defaultValue);\n\n attrProps += tabsToSpace(`\\n${tabs(3)}type: DataTypes.${attribute.type.replace('VARCHAR(', 'STRING(')},`);\n if (attribute.autoIncrement !== null) {\n attrProps += tabsToSpace(`\\n${tabs(3)}autoIncrement: ${this.getAutoIncrement(attribute)},`);\n }\n if (attribute.allowNull !== null) {\n attrProps += tabsToSpace(`\\n${tabs(3)}allowNull: ${this.getAllowNull(attribute)},`);\n }\n if (attribute.primaryKey !== null) {\n attrProps += tabsToSpace(`\\n${tabs(3)}primaryKey: ${this.getPrimaryKey(attribute)},`);\n }\n if (attribute.defaultValue !== null) {\n attrProps += tabsToSpace(`\\n${tabs(3)}defaultValue: ${this.getDefaultValue(attribute)},`);\n }\n\n return attrProps;\n }\n\n dropColumn(tableName: string, field: string, attribute: ColumnDescription) {\n console.debug(`drop column ${tableName} ${field} `);\n this.upFields += tabsToSpace(`\\n\\t\\tawait queryInterface.removeColumn('${tableName}', '${field}');\\n`);\n this.downFields = tabsToSpace(`\\n\\t\\tawait queryInterface.addColumn('${tableName}', '${field}', {${this.columnAttributeProps(attribute)}\\n\\t\\t});`) + this.downFields;\n }\n\n async checkDiffs() {\n const models = Object.keys(this.sequelize.models);\n const modelTableNames: string[] = [];\n for (const model of models) {\n\n const modelTable = this.sequelize.models[model].getTableName() as string;\n modelTableNames.push(modelTable);\n\n // new table\n if (!this.tables.includes(modelTable)) {\n this.createTable(this.sequelize.models[model]);\n continue;\n }\n\n const modelAttrs = this.sequelize.models[model].getAttributes();\n const queryInterface = this.sequelize.getQueryInterface();\n const attrs = await queryInterface.describeTable(modelTable);\n\n const modelFields: string[] = [];\n\n // check each field\n for (const modelAttr of Object.keys(modelAttrs)) {\n modelFields.push(modelAttrs[modelAttr].field || modelAttr);\n if (!Object.keys(attrs).includes(modelAttrs[modelAttr].field || modelAttr) && !(modelAttrs[modelAttr].type instanceof DataTypes.VIRTUAL)) {\n this.newColumn(modelTable, modelAttrs[modelAttr]);\n }\n }\n\n for (const attr of Object.keys(attrs)) {\n if (!modelFields.includes(attr)) {\n this.dropColumn(modelTable, attr, attrs[attr]);\n }\n }\n\n // TODO: add index and constraints\n // sequelize.models[model].\n // console.debug('sequelize.models.GinaVersion.options.indexes', sequelize.models.GinaVersion.options.indexes);\n // console.log(sequelize.models[model]);\n\n }\n\n for (const table of this.tables) {\n if (!modelTableNames.includes(table) && table != 'gina_version') {\n this.dropTable(table);\n }\n }\n }\n\n async generateFile(migrationName: string) {\n const migration = `import { ${this.imports.join(', ')} } from 'sequelize';\n\nexport const migration = {\n async up(queryInterface: QueryInterface) {\n${this.upTables}\n${this.upFields}\n },\n\n async down(queryInterface: QueryInterface) {\n${this.downFields}\n${this.downTables}\n }\n};`;\n\n await fs.writeFile(`./gina/migrations/${moment().format('YYYYMMDDHHmmss')}-${migrationName.toLowerCase().replaceAll(' ', '-')}.ts`, migration);\n }\n}\n\nasync function createMigration(sequelize: Sequelize, migrationName: string) {\n\n const migration = new Migration(sequelize);\n await migration.loadDatabaseTables();\n\n await migration.checkDiffs();\n\n if (!migration.upTables && !migration.upFields && !migration.upIndexes) {\n console.info('There\\'s no changes between your models and the database.');\n return;\n }\n await migration.generateFile(migrationName);\n\n}\n\nconst gina = {\n runMigrations,\n createMigration,\n};\n\nexport default gina;\n"]}
|
|
1
|
+
{"version":3,"file":"gina.js","sourceRoot":"/","sources":["gina.ts"],"names":[],"mappings":";;;;;AAAA,yCAA+H;AAC/H,2DAA6B;AAC7B,oDAA4B;AAO5B,KAAK,UAAU,aAAa,CAAC,SAAoB,EAAE,cAA8B;IAC7E,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,iBAAiB,EAAE,CAAC,aAAa,EAAE,CAAC;IAEnE,IAAI,OAAO,GAAG,EAAE,CAAC;IAEjB,IAAI,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE;QACjC,MAAM,QAAQ,GAA2B,MAAM,SAAS,CAAC,KAAK,CAAC,mCAAmC,EAAE;YAChG,IAAI,EAAE,sBAAU,CAAC,MAAM;SAC1B,CAAC,CAAC;QAEH,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;SAC9E;QAED,OAAO,GAAG,QAAQ,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;KAC7D;IAED,OAAO,CAAC,IAAI,CAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;IAE1C,KAAK,MAAM,aAAa,IAAI,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE;QACrD,IAAI,aAAa,GAAG,OAAO,EAAE;YACzB,OAAO,CAAC,IAAI,CAAC,qBAAqB,EAAE,aAAa,CAAC,CAAC;YACnD,MAAM,cAAc,CAAC,aAAa,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC,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AAC,IAAI,CAAC,IAAI,CAAC;;;;EAI3D,IAAI,CAAC,QAAQ;EACb,IAAI,CAAC,QAAQ;EACb,IAAI,CAAC,YAAY;EACjB,IAAI,CAAC,SAAS;;;;EAId,IAAI,CAAC,WAAW;EAChB,IAAI,CAAC,UAAU;EACf,IAAI,CAAC,UAAU;;GAEd,CAAC;QAEI,MAAM,kBAAE,CAAC,SAAS,CAAC,qBAAqB,IAAA,gBAAM,GAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,IAAI,aAAa,CAAC,WAAW,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IACnJ,CAAC;CACJ;AAED,KAAK,UAAU,eAAe,CAAC,SAAoB,EAAE,aAAqB;IAEtE,MAAM,SAAS,GAAG,IAAI,SAAS,CAAC,SAAS,CAAC,CAAC;IAC3C,MAAM,SAAS,CAAC,kBAAkB,EAAE,CAAC;IAErC,MAAM,SAAS,CAAC,UAAU,EAAE,CAAC;IAE7B,IAAI,CAAC,SAAS,CAAC,QAAQ,IAAI,CAAC,SAAS,CAAC,QAAQ,IAAI,CAAC,SAAS,CAAC,SAAS,IAAI,CAAC,SAAS,CAAC,YAAY,EAAE;QAC/F,OAAO,CAAC,IAAI,CAAC,2DAA2D,CAAC,CAAC;KAC7E;IACD,MAAM,SAAS,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC;AAEhD,CAAC;AAED,MAAM,IAAI,GAAG;IACT,aAAa;IACb,eAAe;CAClB,CAAC;AAEF,kBAAe,IAAI,CAAC","sourcesContent":["import { DataTypes, ModelCtor, Model, Sequelize, ModelAttributeColumnOptions, QueryTypes, ColumnDescription } from 'sequelize';\nimport fs from 'fs/promises';\nimport moment from 'moment';\nimport { MigrationFile } from './typings/migration';\n\ninterface MigrationFiles {\n [version: string]: { migration: MigrationFile; };\n}\n\nasync function runMigrations(sequelize: Sequelize, migrationFiles: MigrationFiles) {\n const tables = await sequelize.getQueryInterface().showAllTables();\n\n let version = '';\n\n if (tables.includes('gina_version')) {\n const versions: { version: string; }[] = await sequelize.query('SELECT version FROM gina_version;', {\n type: QueryTypes.SELECT,\n });\n\n if (versions.length > 1) {\n throw new Error('There must be only one version on `gina_version` table.');\n }\n\n version = versions.length == 1 ? versions[0].version : '';\n }\n\n console.info('current version:', version);\n\n for (const migrationFile of Object.keys(migrationFiles)) {\n if (migrationFile > version) {\n console.info('applying migration:', migrationFile);\n await migrationFiles[migrationFile].migration.up(sequelize.getQueryInterface());\n if (version) {\n version = migrationFile;\n await sequelize.query(`UPDATE gina_version SET version = '${version}', updatedAt = NOW();`);\n } else {\n version = migrationFile;\n await sequelize.query(`INSERT INTO gina_version (updatedAt, version) VALUES (NOW(),'${version}');`);\n }\n console.info('applied version:', migrationFile);\n }\n }\n}\n\nfunction tabsToSpace(str: string) {\n return str.replaceAll('\\t', ' ');\n}\n\nfunction tabs(qty: number) {\n let tabText = '';\n\n for (let i = 0; i < qty; i++) {\n tabText += '\\t';\n }\n\n return tabText;\n}\n\nclass Migration {\n upTables: string;\n downTables: string;\n upFields: string;\n downFields: string;\n upIndexes: string;\n downIndexes: string;\n private sequelize: Sequelize;\n tables: string[];\n imports: string[];\n upForeignKey: string;\n\n constructor(sequelize: Sequelize) {\n this.upTables = '';\n this.downTables = '';\n this.upFields = '';\n this.downFields = '';\n this.upIndexes = '';\n this.downIndexes = '';\n this.upForeignKey = '';\n\n /**\n *\n */\n this.tables = [];\n this.sequelize = sequelize;\n this.imports = ['QueryInterface'];\n }\n\n async loadDatabaseTables() {\n const queryInterface = this.sequelize.getQueryInterface();\n this.tables = await queryInterface.showAllTables();\n }\n\n dropTable(tableName: string) {\n // TODO: create proper logic to delete table\n console.debug(`drop table ${tableName} `);\n\n this.upTables += `\n await queryInterface.dropTable('${tableName}');\n`;\n\n this.downTables = `\n await queryInterface.createTable('${tableName}', {});\n` + this.downTables;\n }\n\n getAllowNull(model: { allowNull?: boolean; }) {\n return model.allowNull ?? 'true' ? 'true' : 'false';\n }\n\n getAutoIncrement(model: { autoIncrement?: boolean; }) {\n return model.autoIncrement ?? 'true' ? 'true' : 'false';\n }\n\n getPrimaryKey(model: { primaryKey?: boolean; }) {\n return model.primaryKey ?? 'true' ? 'true' : 'false';\n }\n\n getType(model: { type?: DataTypes.DataType; }) {\n if (model.type instanceof DataTypes.BIGINT) {\n return 'DataTypes.BIGINT';\n }\n if (model.type instanceof DataTypes.INTEGER) {\n return 'DataTypes.INTEGER';\n }\n if (model.type instanceof DataTypes.STRING) {\n return 'DataTypes.STRING';\n }\n if (model.type instanceof DataTypes.DATE) {\n return 'DataTypes.DATE';\n }\n if (model.type instanceof DataTypes.TEXT) {\n return 'DataTypes.TEXT';\n }\n if (model.type instanceof DataTypes.BOOLEAN) {\n return 'DataTypes.BOOLEAN';\n }\n return '';\n }\n\n getDefaultValue(model: { defaultValue?: unknown; }) {\n\n const val = (model.defaultValue as { val?: string; }).val;\n\n if (val) {\n if (!this.imports.includes('Sequelize')) {\n this.imports.push('Sequelize');\n }\n return `Sequelize.literal('${val}')`;\n } else if (typeof model.defaultValue == 'number') {\n return String(model.defaultValue);\n } else if (typeof model.defaultValue == 'boolean') {\n return String(model.defaultValue);\n }\n\n return `'${model.defaultValue}'`;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n createTable(model: ModelCtor<Model<any, any>>) {\n // TODO: create proper logic to create table\n console.debug(`new table ${model.getTableName()}`);\n const modelAttrs = model.getAttributes();\n const modelAttrsKeys = Object.keys(modelAttrs);\n this.upTables += tabsToSpace(`\\n\\t\\tawait queryInterface.createTable('${model.getTableName()}', {\\n`);\n\n if (!this.imports.includes('DataTypes')) {\n this.imports.push('DataTypes');\n }\n\n for (const modelAttrsKey of modelAttrsKeys) {\n\n const attribute = modelAttrs[modelAttrsKey];\n\n if (attribute.type instanceof DataTypes.VIRTUAL) {\n continue;\n }\n\n if (modelAttrs[modelAttrsKey].references) {\n const references = modelAttrs[modelAttrsKey].references;\n if (typeof references !== 'string') {\n this.upForeignKey += `\n await queryInterface.addConstraint('${model.getTableName()}', {\n type: 'foreign key',\n fields: ['${modelAttrs[modelAttrsKey].field}'],\n onDelete: '${modelAttrs[modelAttrsKey].onDelete}',\n onUpdate: '${modelAttrs[modelAttrsKey].onUpdate}',\n references: {\n table: '${references?.model}',\n field: '${references?.key}'\n }\n });\n`;\n }\n\n }\n\n this.upTables += tabsToSpace(`\\t\\t\\t${modelAttrsKey}: {${this.attributeProps(modelAttrs[modelAttrsKey], 4)}\\n\\t\\t\\t},\\n`);\n\n }\n\n const modelIndexes = model.options.indexes;\n\n if (modelIndexes) {\n for (const modelIndex of modelIndexes) {\n this.upIndexes += `\n await queryInterface.addIndex('${model.getTableName()}', ['${modelIndex.fields?.join('\\', \\'')}'], {\n name: '${modelIndex.name}',\n unique: ${modelIndex.unique},\n });\n`;\n this.downIndexes = `\n await queryInterface.removeIndex('${model.getTableName()}', '${modelIndex.name}');\n` + this.downIndexes;\n }\n }\n this.upTables += tabsToSpace(`\\t\\t});`);\n\n this.downTables = tabsToSpace(`\\n\\t\\tawait queryInterface.dropTable('${model.getTableName()}');\\n`) + this.downTables;\n }\n\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n attributeProps(attribute: ModelAttributeColumnOptions<Model<any, any>>, tabsQty: number) {\n\n if (!this.imports.includes('DataTypes')) {\n this.imports.push('DataTypes');\n }\n\n let attrProps = '';\n\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}type: ${this.getType(attribute)},`);\n if (attribute.autoIncrement !== undefined) {\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}autoIncrement: ${this.getAutoIncrement(attribute)},`);\n }\n if (attribute.allowNull !== undefined) {\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}allowNull: ${this.getAllowNull(attribute)},`);\n }\n if (attribute.primaryKey !== undefined) {\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}primaryKey: ${this.getPrimaryKey(attribute)},`);\n }\n if (attribute.defaultValue !== undefined) {\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}defaultValue: ${this.getDefaultValue(attribute)},`);\n }\n\n attrProps += tabsToSpace(`\\n${tabs(tabsQty)}field: '${attribute.field}'`);\n\n return attrProps;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n newColumn(tableName: string, attribute: ModelAttributeColumnOptions<Model<any, any>>) {\n console.debug(`new column ${tableName} ${attribute.field}`);\n this.upFields += tabsToSpace(`\\n\\t\\tawait queryInterface.addColumn('${tableName}', '${attribute.field}', {${this.attributeProps(attribute, 3)}\\n\\t\\t});\\n`);\n this.downFields = tabsToSpace(`\\n\\t\\tawait queryInterface.removeColumn('${tableName}', '${attribute.field}');\\n`) + this.downFields;\n }\n\n columnAttributeProps(attribute: ColumnDescription) {\n if (!this.imports.includes('DataTypes')) {\n this.imports.push('DataTypes');\n }\n\n let attrProps = '';\n\n let dataTypes = attribute.type;\n\n if (dataTypes.includes('VARCHAR(')) {\n dataTypes = dataTypes.replace('VARCHAR(', 'STRING(');\n } else if (dataTypes == 'INT') {\n dataTypes = 'INTEGER';\n }\n\n attrProps += tabsToSpace(`\\n${tabs(3)}type: DataTypes.${dataTypes},`);\n if (attribute.autoIncrement !== null) {\n attrProps += tabsToSpace(`\\n${tabs(3)}autoIncrement: ${this.getAutoIncrement(attribute)},`);\n }\n if (attribute.allowNull !== null) {\n attrProps += tabsToSpace(`\\n${tabs(3)}allowNull: ${this.getAllowNull(attribute)},`);\n }\n if (attribute.primaryKey !== null) {\n attrProps += tabsToSpace(`\\n${tabs(3)}primaryKey: ${this.getPrimaryKey(attribute)},`);\n }\n if (attribute.defaultValue !== null) {\n attrProps += tabsToSpace(`\\n${tabs(3)}defaultValue: ${this.getDefaultValue(attribute)},`);\n }\n\n return attrProps;\n }\n\n dropColumn(tableName: string, field: string, attribute: ColumnDescription) {\n console.debug(`drop column ${tableName} ${field} `);\n this.upFields += tabsToSpace(`\\n\\t\\tawait queryInterface.removeColumn('${tableName}', '${field}');\\n`);\n this.downFields = tabsToSpace(`\\n\\t\\tawait queryInterface.addColumn('${tableName}', '${field}', {${this.columnAttributeProps(attribute)}\\n\\t\\t});`) + this.downFields;\n }\n\n async checkDiffs() {\n const models = Object.keys(this.sequelize.models);\n const modelTableNames: string[] = [];\n for (const model of models) {\n\n const modelTable = this.sequelize.models[model].getTableName() as string;\n modelTableNames.push(modelTable);\n\n // new table\n if (!this.tables.includes(modelTable)) {\n this.createTable(this.sequelize.models[model]);\n continue;\n }\n\n const modelAttrs = this.sequelize.models[model].getAttributes();\n const queryInterface = this.sequelize.getQueryInterface();\n const attrs = await queryInterface.describeTable(modelTable);\n\n const modelFields: string[] = [];\n\n // const modelRefs: string[] = [];\n\n // for (const modelAttr of Object.keys(modelAttrs)) {\n // if (modelAttrs[modelAttr].references) {\n // modelRefs.push(modelAttrs[modelAttr].field || '');\n // }\n // }\n\n // check each field\n for (const modelAttr of Object.keys(modelAttrs)) {\n modelFields.push(modelAttrs[modelAttr].field || modelAttr);\n if (!Object.keys(attrs).includes(modelAttrs[modelAttr].field || modelAttr) && !(modelAttrs[modelAttr].type instanceof DataTypes.VIRTUAL)) {\n this.newColumn(modelTable, modelAttrs[modelAttr]);\n if (modelAttrs[modelAttr].references) {\n const references = modelAttrs[modelAttr].references;\n if (typeof references !== 'string') {\n\n this.upForeignKey += `\n await queryInterface.addConstraint('${modelTable}', {\n type: 'foreign key',\n fields: ['${modelAttrs[modelAttr].field}'],\n onDelete: '${modelAttrs[modelAttr].onDelete}',\n onUpdate: '${modelAttrs[modelAttr].onUpdate}',\n references: {\n table: '${references?.model}',\n field: '${references?.key}'\n }\n });\n`;\n }\n\n }\n }\n }\n\n // drop column\n for (const attr of Object.keys(attrs)) {\n if (!modelFields.includes(attr)) {\n this.dropColumn(modelTable, attr, attrs[attr]);\n }\n }\n\n\n const indexes: { name: string; fields: { attribute: string; }[]; unique: boolean; }[] = await queryInterface.showIndex(modelTable) as { name: string; fields: { attribute: string; }[]; unique: boolean; }[];\n const modelIndexes = this.sequelize.models[model].options.indexes;\n\n if (modelIndexes) {\n for (const modelIndex of modelIndexes) {\n if (!indexes.find((index) => index.name == modelIndex.name)) {\n this.upIndexes += `\n await queryInterface.addIndex('${modelTable}', ['${modelIndex.fields?.join('\\', \\'')}'], {\n name: '${modelIndex.name}',\n unique: ${modelIndex.unique},\n });\n`;\n this.downIndexes = `\n await queryInterface.removeIndex('${modelTable}', '${modelIndex.name}');\n` + this.downIndexes;\n }\n }\n }\n\n if (indexes) {\n for (const index of indexes) {\n if (index.name === 'PRIMARY') {\n continue;\n }\n if (index.name.endsWith('_fk')) {\n continue;\n }\n if (index.name.includes('_ibfk')) {\n continue;\n }\n\n // if (modelRefs.includes(index.name)) {\n // continue;\n // }\n\n if (modelIndexes === undefined || !modelIndexes.find((modelIndex) => index.name == modelIndex.name)) {\n this.upIndexes += `\n await queryInterface.removeIndex('${modelTable}', '${index.name}');\n\n`;\n\n this.downIndexes = `\n await queryInterface.addIndex('${modelTable}', ['${index.fields?.map(field => field.attribute).join('\\', \\'')}'], {\n name: '${index.name}',\n unique: ${index.unique},\n });\n` + this.downIndexes;\n }\n }\n }\n\n }\n\n for (const table of this.tables) {\n if (!modelTableNames.includes(table) && table != 'gina_version') {\n this.dropTable(table);\n }\n }\n }\n\n async generateFile(migrationName: string) {\n const migration = `import { ${this.imports.join(', ')} } from 'sequelize';\n\nexport const migration = {\n async up(queryInterface: QueryInterface) {\n${this.upTables}\n${this.upFields}\n${this.upForeignKey}\n${this.upIndexes}\n },\n\n async down(queryInterface: QueryInterface) {\n${this.downIndexes}\n${this.downFields}\n${this.downTables}\n }\n};`;\n\n await fs.writeFile(`./gina/migrations/${moment().format('YYYYMMDDHHmmss')}-${migrationName.toLowerCase().replaceAll(' ', '-')}.ts`, migration);\n }\n}\n\nasync function createMigration(sequelize: Sequelize, migrationName: string) {\n\n const migration = new Migration(sequelize);\n await migration.loadDatabaseTables();\n\n await migration.checkDiffs();\n\n if (!migration.upTables && !migration.upFields && !migration.upIndexes && !migration.upForeignKey) {\n console.info('There\\'s no changes between your models and the database.');\n }\n await migration.generateFile(migrationName);\n\n}\n\nconst gina = {\n runMigrations,\n createMigration,\n};\n\nexport default gina;\n"]}
|
package/package.json
CHANGED
|
@@ -5,12 +5,13 @@
|
|
|
5
5
|
"gina-cli": "./bin/index.ts"
|
|
6
6
|
},
|
|
7
7
|
"types": "./dist/gina.d.ts",
|
|
8
|
-
"version": "0.0.
|
|
8
|
+
"version": "0.0.13",
|
|
9
9
|
"description": "Gina is a tool to auto-generate and run migrations based on Sequelize ORM.",
|
|
10
10
|
"scripts": {
|
|
11
11
|
"eslint": "eslint .",
|
|
12
12
|
"eslint_fix": "eslint . --fix",
|
|
13
|
-
"build": "tsc"
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"gina-cli": "ts-node --esm ./bin/index.ts"
|
|
14
15
|
},
|
|
15
16
|
"repository": {
|
|
16
17
|
"type": "git",
|