masterrecord 0.0.33 → 0.0.34

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/Migrations/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- // version 0.0.5
3
+ // version 0.0.7
4
4
  // https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/
5
5
  // how to add environment variables on cli call example - master=development masterrecord add-migration auth authContext
6
6
 
@@ -16,7 +16,7 @@ const [,, ...args] = process.argv
16
16
 
17
17
  program
18
18
  .version('0.0.2')
19
- .option('-v, --version', '0.0.33')
19
+ .option('-v, --version', '0.0.34')
20
20
  .description('A ORM framework that facilitates the creation and use of business objects whose data requires persistent storage to a database');
21
21
 
22
22
  // Instructions : to run command you must go to main project folder is located and run the command using the context file name.
@@ -46,7 +46,7 @@ program
46
46
  });
47
47
 
48
48
  // Instructions : to run command you must go to folder where migration file is located.
49
- program
49
+ program //// TODO ---- WHY MIGRATION IS CREATING ADD COLUMSN AS WELL AS NEW TABLE
50
50
  .command('add-migration <name> <contextFileName>')
51
51
  .alias('am')
52
52
  .action(function(name, contextFileName){
@@ -62,7 +62,7 @@ program
62
62
  var contextSnapshot = require(files[0]);
63
63
  var context = require(contextSnapshot.contextLocation);
64
64
  var contextInstance = new context();
65
- var newEntity = migration.template(name, contextSnapshot.schema, contextInstance .__entities);
65
+ var newEntity = migration.template(name, contextSnapshot.schema, contextInstance.__entities);
66
66
  var migrationDate = Date.now();
67
67
  var file = `${contextSnapshot.migrationFolder}/${migrationDate}_${name}_migration.js`
68
68
  fs.writeFile(file, newEntity, 'utf8', function (err) {
@@ -103,13 +103,12 @@ program
103
103
  });
104
104
 
105
105
  var mFile = mFiles[0];
106
- var migrationFile = require(mFile);
106
+ var migrationProjectFile = require(mFile);
107
107
  var context = require(contextSnapshot.contextLocation);
108
108
  var contextInstance = new context();
109
- var newMigrationInstance = new migrationFile(context);
110
-
111
- var tableObj = migration.up(contextSnapshot.schema, contextInstance.__entities);
112
- newMigrationInstance.up(tableObj);
109
+ var newMigrationProjectInstance = new migrationProjectFile(context);
110
+ var tableObj = migration.buildUpObject(contextSnapshot.schema, contextInstance.__entities);
111
+ newMigrationProjectInstance.up(tableObj);
113
112
 
114
113
  var snap = {
115
114
  file : contextSnapshot.contextLocation,
@@ -1,5 +1,5 @@
1
1
 
2
- // verison 0.0.3
2
+ // verison 0.0.5
3
3
  class migrationSQLiteQuery {
4
4
 
5
5
  #tempTableName = "_temp_alter_column_update"
@@ -60,12 +60,12 @@ class migrationSQLiteQuery {
60
60
 
61
61
  alterColumn(fullTable, table){
62
62
  if(table){
63
- table.newName = this.tempTableName;
63
+ table.newName = this.#tempTableName;
64
64
  return {
65
65
  1 : this.renameTable(table),
66
- 2 : this.createTable(table.tableName, fullTable),
66
+ 2 : this.createTable(fullTable),
67
67
  3 : this.insertInto(table.tableName, fullTable),
68
- 4 : this.dropTable(this.tempTableName)
68
+ 4 : this.dropTable(this.#tempTableName)
69
69
  }
70
70
  }
71
71
  else{
@@ -100,7 +100,7 @@ class migrationSQLiteQuery {
100
100
 
101
101
  insertInto(name, table){
102
102
  return `INSERT INTO ${name} (${this.#getTableColumns(table)})
103
- SELECT ${this.#getTableColumns(table)} FROM ${this.tempTableName}`;
103
+ SELECT ${this.#getTableColumns(table)} FROM ${this.#tempTableName}`;
104
104
  }
105
105
 
106
106
  createTable(table){
@@ -110,21 +110,21 @@ class migrationSQLiteQuery {
110
110
  queryVar += `${this.#columnMapping(table[key])}, `;
111
111
  }
112
112
  }
113
-
113
+
114
114
  return `CREATE TABLE ${table.__name} (${queryVar.replace(/,\s*$/, "")});`;
115
115
 
116
116
  /*
117
- INTEGER PRIMARY KEY AUTOINCREMENT
118
- all these are equal to interger
119
- INT
120
- INTEGER
121
- TINYINT
122
- SMALLINT
123
- MEDIUMINT
124
- BIGINT
125
- UNSIGNED BIG INT
126
- INT2
127
- INT8
117
+ INTEGER PRIMARY KEY AUTOINCREMENT
118
+ all these are equal to interger
119
+ INT
120
+ INTEGER
121
+ TINYINT
122
+ SMALLINT
123
+ MEDIUMINT
124
+ BIGINT
125
+ UNSIGNED BIG INT
126
+ INT2
127
+ INT8
128
128
  */
129
129
  }
130
130
 
@@ -1,4 +1,4 @@
1
- // version 0.0.5
1
+ // version 0.0.7
2
2
  // learn more about seeding info - https://www.pauric.blog/Database-Updates-and-Migrations-with-Entity-Framework/
3
3
 
4
4
  var fs = require('fs');
@@ -186,22 +186,22 @@ class Migrations{
186
186
  }
187
187
 
188
188
  //
189
- up(oldSchema, newSchema){
189
+ buildUpObject(oldSchema, newSchema){
190
190
  var tableObj = {}
191
191
  var tables = this.#buildMigrationObject(oldSchema, newSchema);
192
192
  tables.forEach(function (item, index) {
193
193
  // add new columns for table
194
194
  var columnInfo = tables[index];
195
195
 
196
+ item.newTables.forEach(function (column, ind) {
197
+ tableObj[item.name] = columnInfo.new;
198
+ });
199
+
196
200
  item.newColumns.forEach(function (column, ind) {
197
201
  columnInfo.new[column].tableName = item.name;
198
202
  tableObj[column] = columnInfo.new[column];
199
203
  });
200
204
 
201
- item.newTables.forEach(function (column, ind) {
202
- tableObj[item.name] = columnInfo.new;
203
- });
204
-
205
205
  item.deletedColumns.forEach(function (column, ind) {
206
206
  columnInfo.old[column].tableName = item.name;
207
207
  tableObj[column] = columnInfo.old[column];
@@ -218,24 +218,35 @@ class Migrations{
218
218
 
219
219
  tableObj.___table = item;
220
220
  });
221
- return tableObj;
221
+ return tableObj;
222
222
  }
223
223
 
224
224
  template(name, oldSchema, newSchema){
225
+
225
226
  var MT = new MigrationTemplate(name);
226
227
  var tables = this.#buildMigrationObject(oldSchema, newSchema);
227
228
  tables.forEach(function (item, index) {
228
- // add new columns for table
229
- item.newColumns.forEach(function (column, index) {
230
- MT.addColumn("up", column, item.name);
231
- MT.dropColumn("down", column, item.name);
232
- });
229
+ if(item.old === null){
230
+ MT.createTable("up", column, item.name);
231
+ MT.dropTable("down", column, item.name);
232
+ }
233
+
234
+ if(item.new === null){
235
+ MT.dropTable("up", column, item.name);
236
+ MT.createTable("down", column, item.name);
237
+ }
233
238
 
234
239
  item.newTables.forEach(function (column, ind) {
235
240
  MT.createTable("up", item.name);
236
241
  MT.dropTable("down", item.name);
237
242
  });
238
243
 
244
+ // add new columns for table
245
+ item.newColumns.forEach(function (column, index) {
246
+ MT.addColumn("up", column, item.name);
247
+ MT.dropColumn("down", column, item.name);
248
+ });
249
+
239
250
  item.deletedColumns.forEach(function (column, index) {
240
251
  MT.dropColumn("up", column, item.name);
241
252
  MT.addColumn("down",column, item.name);
@@ -249,16 +260,6 @@ class Migrations{
249
260
  }
250
261
  });
251
262
 
252
- if(item.old === null){
253
- MT.createTable("up", column, item.name);
254
- MT.dropTable("down", column, item.name);
255
-
256
- }
257
- if(item.new === null){
258
- MT.dropTable("up", column, item.name);
259
- MT.createTable("down", column, item.name);
260
- }
261
-
262
263
  });
263
264
 
264
265
  return MT.get();
package/context.js CHANGED
@@ -1,4 +1,4 @@
1
- // Version 0.0.1
1
+ // Version 0.0.3
2
2
 
3
3
  var modelBuilder = require('./Entity/EntityModelBuilder');
4
4
  var query = require('masterrecord/QueryLanguage/queryMethods');
@@ -8,7 +8,7 @@ var MYSQLEngine = require('masterrecord/MYSQLEngine');
8
8
  var insertManager = require('./InsertManager');
9
9
  var deleteManager = require('./DeleteManager');
10
10
  var globSearch = require("glob");
11
-
11
+ var fs = require('fs');
12
12
 
13
13
  class context {
14
14
  _isModelValid = {
@@ -41,6 +41,7 @@ class context {
41
41
  */
42
42
  __SQLiteInit(env, sqlName){
43
43
  try{
44
+
44
45
  const sqlite3 = require(sqlName);
45
46
  let DBAddress = env.completeConnection;
46
47
  var db = new sqlite3(DBAddress, env);
@@ -65,6 +66,7 @@ class context {
65
66
  */
66
67
  __mysqlInit(env, sqlName){
67
68
  try{
69
+
68
70
  const mysql = require(sqlName);
69
71
  const connection = mysql.createConnection(env);
70
72
  connection.connect();
@@ -121,22 +123,26 @@ class context {
121
123
  }
122
124
 
123
125
  useSqlite(rootFolderLocation){
124
- this.isSQite = true;
125
- var root = process.cwd();
126
- var envType = this.__enviornment;
127
- var contextName = this.__name;
128
- var file = this.__findSettings(root, rootFolderLocation, envType);
129
- var settings = require(file.file);
130
- var options = settings[contextName];
131
- if(options === undefined){
132
- console.log("settings missing context name settings");
133
- throw error("settings missing context name settings");
134
- }
135
- this.validateSQLiteOptions(options);
136
- options.completeConnection = `${file.rootFolder}${options.connection}`;
137
- this.db = this.__SQLiteInit(options, "better-sqlite3");
138
- this._SQLEngine.setDB(this.db, "better-sqlite3");
139
- return this;
126
+ this.isSQite = true;
127
+ var root = process.cwd();
128
+ var envType = this.__enviornment;
129
+ var contextName = this.__name;
130
+ var file = this.__findSettings(root, rootFolderLocation, envType);
131
+ var settings = require(file.file);
132
+ var options = settings[contextName];
133
+ if(options === undefined){
134
+ console.log("settings missing context name settings");
135
+ throw error("settings missing context name settings");
136
+ }
137
+ this.validateSQLiteOptions(options);
138
+ options.completeConnection = `${file.rootFolder}${options.connection}`;
139
+ var dbDirectory = options.completeConnection.substr(0, options.completeConnection.lastIndexOf("\/"));
140
+ if (!fs.existsSync(dbDirectory)){
141
+ fs.mkdirSync(dbDirectory);
142
+ }
143
+ this.db = this.__SQLiteInit(options, "better-sqlite3");
144
+ this._SQLEngine.setDB(this.db, "better-sqlite3");
145
+ return this;
140
146
  }
141
147
 
142
148
  validateSQLiteOptions(options){
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "glob" : "^8.0.3",
7
7
  "deep-object-diff" : "^1.1.7"
8
8
  },
9
- "version": "0.0.33",
9
+ "version": "0.0.34",
10
10
  "description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
11
11
  "homepage": "https://github.com/Tailor/MasterRecord#readme",
12
12
  "repository": {