masterrecord 1.1.5 → 1.1.6

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.
@@ -5,16 +5,16 @@ class migrationMySQLQuery {
5
5
  #tempTableName = "_temp_alter_column_update"
6
6
 
7
7
  #getTableColumns(table){
8
- var columnList = [];
9
- for (var key in table) {
8
+ const columnList = [];
9
+ for (const key in table) {
10
10
  if(typeof table[key] === "object"){
11
- var col = table[key];
11
+ const col = table[key];
12
12
  // Skip relationship-only fields
13
13
  if(col.type === 'hasOne' || col.type === 'hasMany' || col.type === 'hasManyThrough'){
14
14
  continue;
15
15
  }
16
16
  // Map belongsTo to its foreignKey name
17
- var name = (col.relationshipType === 'belongsTo' && col.foreignKey) ? col.foreignKey : col.name;
17
+ const name = (col.relationshipType === 'belongsTo' && col.foreignKey) ? col.foreignKey : col.name;
18
18
  columnList.push(`\`${name}\``);
19
19
  }
20
20
  }
@@ -38,16 +38,16 @@ class migrationMySQLQuery {
38
38
  */
39
39
  // name TEXT NOT NULL,
40
40
 
41
- var auto = table.auto ? " AUTO_INCREMENT":"";
42
- var primaryKey = table.primary ? " PRIMARY KEY" : "";
43
- var nullName = table.nullable ? "" : " NOT NULL";
44
- var unique = table.unique ? " UNIQUE" : "";
45
- var type = this.typeManager(table.type);
46
- var tableName = table.name;
41
+ const auto = table.auto ? " AUTO_INCREMENT":"";
42
+ const primaryKey = table.primary ? " PRIMARY KEY" : "";
43
+ const nullName = table.nullable ? "" : " NOT NULL";
44
+ const unique = table.unique ? " UNIQUE" : "";
45
+ const type = this.typeManager(table.type);
46
+ let tableName = table.name;
47
47
  if(table.relationshipType === 'belongsTo' && table.foreignKey){
48
48
  tableName = table.foreignKey;
49
49
  }
50
- var defaultValue = "";
50
+ let defaultValue = "";
51
51
  if(table.default !== undefined && table.default !== null){
52
52
  let def = table.default;
53
53
  if(table.type === 'boolean'){
@@ -69,18 +69,12 @@ class migrationMySQLQuery {
69
69
  boolType(type){
70
70
  switch(type) {
71
71
  case "true":
72
- return "1"
73
- break;
74
- case "false":
75
- return "0"
76
- break;
77
- case true:
78
- return "1"
79
- break;
80
- case false:
81
- return "0"
82
- break;
83
- default:
72
+ case true:
73
+ return "1";
74
+ case "false":
75
+ case false:
76
+ return "0";
77
+ default:
84
78
  return type;
85
79
  }
86
80
  }
@@ -197,16 +191,16 @@ class migrationMySQLQuery {
197
191
 
198
192
  createTable(table){
199
193
 
200
- var queryVar = "";
194
+ let queryVar = "";
201
195
  //console.log("Dsfdsfdsf---------", table)
202
- for (var key in table) {
196
+ for (const key in table) {
203
197
  // Skip metadata properties (indexes, __compositeIndexes, __name, etc.)
204
198
  if(key === 'indexes' || key.startsWith('__')){
205
199
  continue;
206
200
  }
207
201
 
208
202
  if(typeof table[key] === "object"){
209
- var col = table[key];
203
+ const col = table[key];
210
204
 
211
205
  if(col.type !== "hasOne" && col.type !== "hasMany" && col.type !== "hasManyThrough"){
212
206
  // Whitelist: Only process objects that look like column definitions
@@ -220,7 +214,7 @@ class migrationMySQLQuery {
220
214
  }
221
215
  }
222
216
 
223
- var completeQuery = `CREATE TABLE IF NOT EXISTS \`${table.__name}\` (${queryVar.replace(/,\s*$/, "")});`;
217
+ const completeQuery = `CREATE TABLE IF NOT EXISTS \`${table.__name}\` (${queryVar.replace(/,\s*$/, "")});`;
224
218
  return completeQuery;
225
219
 
226
220
  /*
@@ -4,16 +4,16 @@ class migrationPostgresQuery {
4
4
  #tempTableName = "_temp_alter_column_update"
5
5
 
6
6
  #getTableColumns(table){
7
- var columnList = [];
8
- for (var key in table) {
7
+ const columnList = [];
8
+ for (const key in table) {
9
9
  if(typeof table[key] === "object"){
10
- var col = table[key];
10
+ const col = table[key];
11
11
  // Skip relationship-only fields
12
12
  if(col.type === 'hasOne' || col.type === 'hasMany' || col.type === 'hasManyThrough'){
13
13
  continue;
14
14
  }
15
15
  // Map belongsTo to its foreignKey name
16
- var name = (col.relationshipType === 'belongsTo' && col.foreignKey) ? col.foreignKey : col.name;
16
+ const name = (col.relationshipType === 'belongsTo' && col.foreignKey) ? col.foreignKey : col.name;
17
17
  columnList.push(`"${name}"`);
18
18
  }
19
19
  }
@@ -36,13 +36,13 @@ class migrationPostgresQuery {
36
36
  */
37
37
 
38
38
  // PostgreSQL uses SERIAL for auto-increment, not separate AUTO_INCREMENT keyword
39
- var auto = "";
40
- var primaryKey = table.primary ? " PRIMARY KEY" : "";
41
- var nullName = table.nullable ? "" : " NOT NULL";
42
- var unique = table.unique ? " UNIQUE" : "";
39
+ let auto = "";
40
+ let primaryKey = table.primary ? " PRIMARY KEY" : "";
41
+ const nullName = table.nullable ? "" : " NOT NULL";
42
+ const unique = table.unique ? " UNIQUE" : "";
43
43
 
44
44
  // For PostgreSQL, if auto-increment primary key, use SERIAL or BIGSERIAL
45
- var type;
45
+ let type;
46
46
  if(table.auto && table.primary && (table.type === 'integer' || table.type === 'int')){
47
47
  type = "SERIAL"; // Auto-incrementing integer
48
48
  auto = "";
@@ -55,12 +55,12 @@ class migrationPostgresQuery {
55
55
  type = this.typeManager(table.type);
56
56
  }
57
57
 
58
- var tableName = table.name;
58
+ let tableName = table.name;
59
59
  if(table.relationshipType === 'belongsTo' && table.foreignKey){
60
60
  tableName = table.foreignKey;
61
61
  }
62
62
 
63
- var defaultValue = "";
63
+ let defaultValue = "";
64
64
  if(table.default !== undefined && table.default !== null){
65
65
  let def = table.default;
66
66
  if(table.type === 'boolean' || table.type === 'bool'){
@@ -135,7 +135,7 @@ class migrationPostgresQuery {
135
135
  const type = this.typeManager(table.table.type);
136
136
 
137
137
  // Build ALTER statements - PostgreSQL requires separate statements for different changes
138
- let statements = [];
138
+ const statements = [];
139
139
 
140
140
  // Change type
141
141
  statements.push(`ALTER TABLE "${tableName}" ALTER COLUMN "${colName}" TYPE ${type}`);
@@ -200,16 +200,16 @@ class migrationPostgresQuery {
200
200
  }
201
201
 
202
202
  createTable(table){
203
- var queryVar = "";
203
+ let queryVar = "";
204
204
 
205
- for (var key in table) {
205
+ for (const key in table) {
206
206
  // Skip metadata properties (indexes, __compositeIndexes, __name, etc.)
207
207
  if(key === 'indexes' || key.startsWith('__')){
208
208
  continue;
209
209
  }
210
210
 
211
211
  if(typeof table[key] === "object"){
212
- var col = table[key];
212
+ const col = table[key];
213
213
 
214
214
  if(col.type !== "hasOne" && col.type !== "hasMany" && col.type !== "hasManyThrough"){
215
215
  // Whitelist: Only process objects that look like column definitions
@@ -223,7 +223,7 @@ class migrationPostgresQuery {
223
223
  }
224
224
  }
225
225
 
226
- var completeQuery = `CREATE TABLE IF NOT EXISTS "${table.__name}" (${queryVar.replace(/,\s*$/, "")});`;
226
+ const completeQuery = `CREATE TABLE IF NOT EXISTS "${table.__name}" (${queryVar.replace(/,\s*$/, "")});`;
227
227
  return completeQuery;
228
228
  }
229
229
 
@@ -5,16 +5,16 @@ class migrationSQLiteQuery {
5
5
  #tempTableName = "_temp_alter_column_update"
6
6
 
7
7
  #getTableColumns(table){
8
- var columnList = [];
9
- for (var key in table) {
8
+ const columnList = [];
9
+ for (const key in table) {
10
10
  if(typeof table[key] === "object"){
11
- var col = table[key];
11
+ const col = table[key];
12
12
  // Skip relationship-only fields
13
13
  if(col.type === 'hasOne' || col.type === 'hasMany' || col.type === 'hasManyThrough'){
14
14
  continue;
15
15
  }
16
16
  // Map belongsTo to its foreignKey name
17
- var name = (col.relationshipType === 'belongsTo' && col.foreignKey) ? col.foreignKey : col.name;
17
+ const name = (col.relationshipType === 'belongsTo' && col.foreignKey) ? col.foreignKey : col.name;
18
18
  columnList.push(name);
19
19
  }
20
20
  }
@@ -38,17 +38,17 @@ class migrationSQLiteQuery {
38
38
  */
39
39
  // name TEXT NOT NULL,
40
40
 
41
- var auto = table.auto ? " AUTOINCREMENT":"";
42
- var primaryKey = table.primary ? " PRIMARY KEY" : "";
43
- var nullName = table.nullable ? "" : " NOT NULL";
44
- var unique = table.unique ? " UNIQUE" : "";
45
- var type = this.#typeManager(table.type);
46
- var colName = table.name;
41
+ const auto = table.auto ? " AUTOINCREMENT":"";
42
+ const primaryKey = table.primary ? " PRIMARY KEY" : "";
43
+ const nullName = table.nullable ? "" : " NOT NULL";
44
+ const unique = table.unique ? " UNIQUE" : "";
45
+ const type = this.#typeManager(table.type);
46
+ let colName = table.name;
47
47
  if(table.relationshipType === 'belongsTo' && table.foreignKey){
48
48
  colName = table.foreignKey;
49
49
  }
50
50
  // DEFAULT clause
51
- var defaultClause = "";
51
+ let defaultClause = "";
52
52
  if(table.default !== undefined && table.default !== null){
53
53
  let def = table.default;
54
54
  if(table.type === 'boolean'){
@@ -163,15 +163,15 @@ class migrationSQLiteQuery {
163
163
  }
164
164
 
165
165
  createTable(table){
166
- var queryVar = "";
167
- for (var key in table) {
166
+ let queryVar = "";
167
+ for (const key in table) {
168
168
  // Skip metadata properties (indexes, __compositeIndexes, __name, etc.)
169
169
  if(key === 'indexes' || key.startsWith('__')){
170
170
  continue;
171
171
  }
172
172
 
173
173
  if(typeof table[key] === "object"){
174
- var col = table[key];
174
+ const col = table[key];
175
175
  // Skip relationship-only fields
176
176
  if(col.type === 'hasOne' || col.type === 'hasMany' || col.type === 'hasManyThrough'){
177
177
  continue;
@@ -15,12 +15,12 @@ import { resolveMigrationsDirectory } from './pathUtils.js';
15
15
  class Migrations{
16
16
 
17
17
  #organizeSchemaByTables(oldSchema, newSchema){
18
- var tables = []
19
- var seenTableNames = new Set(); // Track processed table names to prevent duplicates
18
+ const tables = []
19
+ const seenTableNames = new Set(); // Track processed table names to prevent duplicates
20
20
 
21
21
  if(oldSchema.length === 0){
22
22
  newSchema.forEach(function (item, index) {
23
- var tableName = item["__name"];
23
+ const tableName = item["__name"];
24
24
 
25
25
  // Skip if we've already processed this table name
26
26
  if(seenTableNames.has(tableName)){
@@ -29,7 +29,7 @@ class Migrations{
29
29
  }
30
30
  seenTableNames.add(tableName);
31
31
 
32
- var table = {
32
+ const table = {
33
33
  name: tableName,
34
34
  new :item,
35
35
  old : {},
@@ -48,7 +48,7 @@ class Migrations{
48
48
  }
49
49
  else{
50
50
  newSchema.forEach(function (item, index) {
51
- var tableName = item["__name"];
51
+ const tableName = item["__name"];
52
52
 
53
53
  // Skip if we've already processed this table name
54
54
  if(seenTableNames.has(tableName)){
@@ -57,7 +57,7 @@ class Migrations{
57
57
  }
58
58
  seenTableNames.add(tableName);
59
59
 
60
- var table = {
60
+ const table = {
61
61
  name: tableName,
62
62
  old: {},
63
63
  new :item,
@@ -73,7 +73,7 @@ class Migrations{
73
73
  }
74
74
 
75
75
  oldSchema.forEach(function (oldItem, index) {
76
- var oldItemName = oldItem["__name"];
76
+ const oldItemName = oldItem["__name"];
77
77
  if(table.name === oldItemName){
78
78
  table.old = oldItem;
79
79
  }
@@ -88,13 +88,13 @@ class Migrations{
88
88
 
89
89
  #findDeletedColumns(tables){
90
90
  tables.forEach(function (item, index) {
91
- var deletedColumn = null;
91
+ let deletedColumn = null;
92
92
  if(item.new && item.old){
93
93
  Object.keys(item.old).forEach(function (key) {
94
- var value = item.old[key].name;
94
+ const value = item.old[key].name;
95
95
  deletedColumn = null;
96
96
  Object.keys(item.new).forEach(function (newKey) {
97
- var newValue = item.new[newKey].name;
97
+ const newValue = item.new[newKey].name;
98
98
  if(value === newValue){
99
99
  deletedColumn = value;
100
100
  }
@@ -113,11 +113,11 @@ class Migrations{
113
113
 
114
114
  tables.forEach(function (item, index) {
115
115
 
116
- var UD = diff.updatedDiff(item.old, item.new);
116
+ const UD = diff.updatedDiff(item.old, item.new);
117
117
  const isEmpty = Object.keys(UD).length === 0;
118
118
  if(!isEmpty){
119
- for (var key in UD) {
120
- var tableChanges = {
119
+ for (const key in UD) {
120
+ const tableChanges = {
121
121
  changes : UD[key],
122
122
  table : item.new[key],
123
123
  tableName : item.name
@@ -135,11 +135,11 @@ class Migrations{
135
135
  if(item.new && item.old){
136
136
  Object.keys(item.new).forEach(function (key) {
137
137
  if(typeof item.new[key] === "object"){
138
- var value = item.new[key].name;
139
- var columnNotFound = false;
138
+ const value = item.new[key].name;
139
+ let columnNotFound = false;
140
140
  Object.keys(item.old).forEach(function (oldKey) {
141
141
  if(typeof item.old[oldKey] === "object"){
142
- var oldValue = item.old[oldKey].name;
142
+ const oldValue = item.old[oldKey].name;
143
143
  if(value === oldValue){
144
144
  columnNotFound = true;
145
145
  }
@@ -186,7 +186,7 @@ class Migrations{
186
186
  // build table to build new migration snapshot
187
187
  #buildMigrationObject(oldSchema, newSchema, newSeedData = {}){
188
188
 
189
- var tables = this.#organizeSchemaByTables(oldSchema, newSchema);
189
+ let tables = this.#organizeSchemaByTables(oldSchema, newSchema);
190
190
 
191
191
  tables = this.#findNewTables(tables);
192
192
  tables = this.#findNewColumns(tables);
@@ -208,11 +208,11 @@ class Migrations{
208
208
  if(item.new && item.old){
209
209
  Object.keys(item.new).forEach(function (key) {
210
210
  if(typeof item.new[key] === "object" && item.new[key].indexes){
211
- var columnName = item.new[key].name;
212
- var newIndexes = item.new[key].indexes;
211
+ const columnName = item.new[key].name;
212
+ const newIndexes = item.new[key].indexes;
213
213
 
214
214
  // Check if this column existed before
215
- var oldColumn = null;
215
+ let oldColumn = null;
216
216
  Object.keys(item.old).forEach(function (oldKey) {
217
217
  if(typeof item.old[oldKey] === "object" && item.old[oldKey].name === columnName){
218
218
  oldColumn = item.old[oldKey];
@@ -252,11 +252,11 @@ class Migrations{
252
252
  if(item.new && item.old){
253
253
  Object.keys(item.old).forEach(function (key) {
254
254
  if(typeof item.old[key] === "object" && item.old[key].indexes){
255
- var columnName = item.old[key].name;
256
- var oldIndexes = item.old[key].indexes;
255
+ const columnName = item.old[key].name;
256
+ const oldIndexes = item.old[key].indexes;
257
257
 
258
258
  // Check if this column still exists
259
- var newColumn = null;
259
+ let newColumn = null;
260
260
  Object.keys(item.new).forEach(function (newKey) {
261
261
  if(typeof item.new[newKey] === "object" && item.new[newKey].name === columnName){
262
262
  newColumn = item.new[newKey];
@@ -358,22 +358,22 @@ class Migrations{
358
358
 
359
359
 
360
360
  findContextFile(executedLocation, contextFileName){
361
- var files = globSync(`**/*${contextFileName}.js`, {
361
+ const files = globSync(`**/*${contextFileName}.js`, {
362
362
  cwd: executedLocation,
363
363
  dot: true,
364
364
  windowsPathsNoEscape: true
365
365
  });
366
- var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
366
+ const file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
367
367
  return file;
368
368
  }
369
369
 
370
370
  async findContext(executedLocation, contextFileName){
371
- var files = globSync(`**/*${contextFileName}.js`, {
371
+ const files = globSync(`**/*${contextFileName}.js`, {
372
372
  cwd: executedLocation,
373
373
  dot: true,
374
374
  windowsPathsNoEscape: true
375
375
  });
376
- var file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
376
+ const file = files && files[0] ? path.resolve(executedLocation, files[0]) : null;
377
377
  if(!file){
378
378
  return null;
379
379
  }
@@ -387,12 +387,12 @@ class Migrations{
387
387
 
388
388
  // remove hasMany and hasOne and hasManyThrough
389
389
  cleanEntities(entities){
390
- var newEntity = [];
390
+ const newEntity = [];
391
391
  for (let i = 0; i < entities.length; i++) {
392
- var entity = entities[i];
393
- var newObj = {}
392
+ const entity = entities[i];
393
+ const newObj = {}
394
394
 
395
- for (let key in entity) {
395
+ for (const key in entity) {
396
396
  if (entity.hasOwnProperty(key)) {
397
397
 
398
398
  if(entity[key].type !== "hasOne" && entity[key].type !== "hasMany" && entity[key].type !== "hasManyThrough"){
@@ -454,12 +454,12 @@ class Migrations{
454
454
 
455
455
  // validate if schema has changed based on new and old
456
456
  buildUpObject(oldSchema, newSchema){
457
- var tableObj = {}
458
- var tables = this.#buildMigrationObject(oldSchema, newSchema);
457
+ const tableObj = {}
458
+ const tables = this.#buildMigrationObject(oldSchema, newSchema);
459
459
 
460
460
  tables.forEach(function (item, index) {
461
461
  // add new columns for table
462
- var columnInfo = tables[index];
462
+ const columnInfo = tables[index];
463
463
  // Always expose each table under its name so templates like
464
464
  // this.createTable(table.TableName) can safely access it.
465
465
  tableObj[item.name] = columnInfo.new;
@@ -514,23 +514,19 @@ class Migrations{
514
514
  }
515
515
 
516
516
  template(name, oldSchema, newSchema, newSeedData = {}, seedConfig = {}, currentEnv = null){
517
- var MT = new MigrationTemplate(name);
517
+ const MT = new MigrationTemplate(name);
518
518
  // Determine current environment if not provided
519
519
  if (!currentEnv) {
520
520
  currentEnv = process.env.NODE_ENV || process.env.master || 'development';
521
521
  }
522
- var tables = this.#buildMigrationObject(oldSchema, newSchema, newSeedData);
522
+ const tables = this.#buildMigrationObject(oldSchema, newSchema, newSeedData);
523
523
 
524
524
  tables.forEach(function (item, index) {
525
- if(item.old === null){
526
- MT.createTable("up", column, item.name);
527
- MT.dropTable("down", column, item.name);
528
- }
529
-
530
- if(item.new === null){
531
- MT.dropTable("up", column, item.name);
532
- MT.createTable("down", column, item.name);
533
- }
525
+ // (Whole-table create/drop is handled below via item.newTables /
526
+ // item.deletedColumns. The previous `if (item.old === null)` /
527
+ // `if (item.new === null)` branches here referenced an undefined
528
+ // `column` and never ran — old/new are always {} or an object,
529
+ // never null — so they were dead, broken code and were removed.)
534
530
 
535
531
  item.newTables.forEach(function (column, ind) {
536
532
  MT.createTable("up", item.name);
@@ -538,9 +538,9 @@ class schema{
538
538
  if(this.fullTable){
539
539
  if(this.context.isSQLite){
540
540
  var queryBuilder = new sqliteQuery();
541
- var tableSchema = (this._tableObj && this._tableObj[table.tableName]) || this.fullTable.new;
542
- var queryObj = queryBuilder.alterColumn(tableSchema, table);
543
- for (var key in queryObj) {
541
+ const tableSchema = (this._tableObj && this._tableObj[table.tableName]) || this.fullTable.new;
542
+ const queryObj = queryBuilder.alterColumn(tableSchema, table);
543
+ for (const key in queryObj) {
544
544
  var query = queryObj[key];
545
545
  await this.context._execute(query);
546
546
  }