masterrecord 0.0.27 → 0.0.28

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/Masterrecord.js CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  // https://github.com/kriasoft/node-sqlite
3
3
  // https://www.learnentityframeworkcore.com/dbset/deleting-data
4
- // version 1.0.14
4
+ // version 1.0.16
5
5
 
6
6
  var modelBuilder = require('./Entity/EntityModelBuilder');
7
7
  var query = require('masterrecord/QueryLanguage/queryMethods');
@@ -12,6 +12,7 @@ var insertManager = require('./InsertManager');
12
12
  var deleteManager = require('./DeleteManager');
13
13
  var globSearch = require("glob");
14
14
 
15
+
15
16
  class Context {
16
17
  _isModelValid = {
17
18
  isValid: true,
@@ -21,12 +22,13 @@ class Context {
21
22
  __builderEntities = [];
22
23
  __trackedEntities = [];
23
24
  __relationshipModels = [];
25
+ __enviornment = "";
26
+ __name = "";
24
27
 
25
28
  constructor(){
26
- // TODO when we build the sql engine it depends on the which type.
27
-
28
- this._SQLEngine = "";
29
+ this. __enviornment = process.env.master;
29
30
  this.__name = this.constructor.name;
31
+ this._SQLEngine = "";
30
32
  }
31
33
 
32
34
  /*
@@ -41,7 +43,8 @@ class Context {
41
43
  __SQLiteInit(env, sqlName){
42
44
  try{
43
45
  const sqlite3 = require(sqlName);
44
- let DBAddress = env.connection;
46
+ console.log("sdsdsds", env)
47
+ let DBAddress = env.completeConnection;
45
48
  var db = new sqlite3(DBAddress, env);
46
49
  db.__name = sqlName;
47
50
  this._SQLEngine = new SQLLiteEngine();
@@ -86,28 +89,63 @@ class Context {
86
89
  };
87
90
  };
88
91
 
92
+ __findSettings(root, rootFolderLocation, envType){
93
+
94
+ var rootFolder = `${root}/${rootFolderLocation}`;
95
+ var search = `${rootFolder}/**/*env.${envType}.json`;
96
+ var files = globSearch.sync(search, rootFolder);
97
+ var file = files[0];
98
+ if(file === undefined){
99
+ root = tools.removeBackwardSlashSection(root, 1, "/");
100
+ rootFolder = `${root}/${rootFolderLocation}`;
101
+ var search = `${rootFolder}/**/*env.${envType}.json`;
102
+ var files = globSearch.sync(search,rootFolder);
103
+ file = files[0];
104
+ if(file === undefined){
105
+ root = tools.removeBackwardSlashSection(root, 1, "/");
106
+ rootFolder = `${root}/${rootFolderLocation}`;
107
+ var search = `${rootFolder}/**/*env.${envType}.json`;
108
+ var files = globSearch.sync(search,rootFolder);
109
+ file = files[0];
110
+ if(file === undefined){
111
+ console.log(`could not find file - ${rootFolder}/env.${envType}.json`);
112
+ throw error(`could not find file - ${rootFolder}/env.${envType}.json`);
113
+ }
114
+
115
+ }
116
+
117
+ }
118
+
119
+ return {
120
+ file: file,
121
+ rootFolder : root
122
+ };
123
+ }
124
+
89
125
  useSqlite(rootFolderLocation){
90
- var contextName = this.constructor.name;
91
- var envType = process.env.master;
92
- var search = `${rootFolderLocation}/**/*env.${envType}.json`;
93
- var files = globSearch.sync(search, rootFolderLocation);
94
- var file = files[0];
95
- var settings = require(file);
96
- var options = settings[contextName][contextName];
126
+ var root = process.cwd();
127
+ var envType = this.__enviornment;
128
+ var contextName = this.__name;
129
+ var file = this.__findSettings(root, rootFolderLocation, envType);
130
+ var settings = require(file.file);
131
+ var options = settings[contextName];
132
+ if(options === undefined){
133
+ console.log("settings missing context name settings");
134
+ throw error("settings missing context name settings");
135
+ }
136
+ this.validateSQLiteOptions(options);
137
+ options.completeConnection = `${file.rootFolder}${options.connection}`;
97
138
  this.db = this.__SQLiteInit(options, "better-sqlite3");
98
139
  this._SQLEngine.setDB(this.db, "better-sqlite3");
99
140
  return this;
100
141
  }
101
142
 
102
- useSqlite(options){
103
- if(options !== undefined){
104
- this.db = this.__SQLiteInit(options, "better-sqlite3");
105
- this._SQLEngine.setDB(this.db, "better-sqlite3");
106
- return this;
107
- }
108
- else{
109
- console.log("database options not defined - Master Record");
143
+ validateSQLiteOptions(options){
144
+ if(options.hasOwnProperty('connect') === undefined){
145
+ console.log("connnect string settings is missing")
146
+ throw error("connection string settings is missing");
110
147
  }
148
+
111
149
  }
112
150
 
113
151
  useMySql(options, rootFolderLocation){
package/Migrations/cli.js CHANGED
@@ -21,20 +21,19 @@ program
21
21
  .alias('em')
22
22
  .description('Enables the migration in your project by creating a configuration class called ContextSnapShot.json')
23
23
  .action(function(contextFileName){
24
+
24
25
  var migration = new Migration();
25
26
  // location of folder where command is being executed..
26
27
  var executedLocation = process.cwd();
27
28
  // find context file from main folder location
28
- var search = `${executedLocation}/**/*${contextFileName}.js`
29
-
30
- var files = globSearch.sync(search, executedLocation);
31
- var file = files[0];
32
- var contextInstance = require(file);
29
+ var contextInstance = migration.getContext(executedLocation, contextFileName);
33
30
 
34
31
  var snap = {
35
- file : files[0],
32
+ file : contextInstance.fileLocation,
36
33
  executedLocation : executedLocation,
37
- context : new contextInstance(),
34
+ context : new contextInstance.context({
35
+ root: executedLocation
36
+ }),
38
37
  contextFileName: contextFileName.toLowerCase()
39
38
  }
40
39
 
@@ -50,15 +49,16 @@ program
50
49
  .action(function(name, contextFileName){
51
50
  var executedLocation = process.cwd();
52
51
  contextFileName = contextFileName.toLowerCase();
52
+ var migration = new Migration();
53
53
  try{
54
54
  // find context file from main folder location
55
55
  var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
56
56
 
57
57
  var files = globSearch.sync(search, executedLocation);
58
58
  var contextSnapshot = require(files[0]);
59
- var migration = new Migration();
60
59
  var context = require(contextSnapshot.contextLocation);
61
- var newEntity = migration.buildMigrationTemplate(name, contextSnapshot.schema, context.__entities);
60
+ var contextInstance = new context();
61
+ var newEntity = migration.buildMigrationTemplate(name, contextSnapshot.schema, contextInstance .__entities);
62
62
  var migrationDate = Date.now();
63
63
  var file = `${contextSnapshot.migrationFolder}/${migrationDate}_${name}_migration.js`
64
64
  fs.writeFile(file, newEntity, 'utf8', function (err) {
@@ -73,10 +73,11 @@ program
73
73
  });
74
74
 
75
75
  program
76
- .command('update-database <contextFileName>')
76
+ .command('update-database <contextFileName> <environment>')
77
77
  .alias('ud')
78
78
  .description('Apply pending migrations to database')
79
- .action(function(contextFileName){
79
+ .action(function(contextFileName, environment){
80
+ console.log("NODE_ENV", process.NODE_ENV)
80
81
  var executedLocation = process.cwd();
81
82
  contextFileName = contextFileName.toLowerCase();
82
83
 
@@ -86,16 +87,19 @@ program
86
87
  var files = globSearch.sync(search, executedLocation);
87
88
  var file = files[0];
88
89
  var contextSnapshot = require(file);
89
- var searchMigration = `**/*_migration.js`
90
+
91
+ var searchMigration = `**/*_migration.js`;
90
92
  var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
91
93
  if( migrationFiles){
94
+ // find newest migration file
92
95
  var mFiles = migrationFiles.sort(function(x, y){
93
96
  return new Date(x.timestamp) < new Date(y.timestamp) ? 1 : -1
94
97
  });
95
98
  mFiles = mFiles[0];
96
99
  var migration = require(mFiles);
97
- var settings = Migration.getSettings(executedLocation);
98
- var newMigrationInstance = new migration(settings);
100
+ var context = require(contextSnapshot.contextLocation);
101
+ var contextInstance = new context();
102
+ var newMigrationInstance = new migration(context);
99
103
  newMigrationInstance.up();
100
104
  }
101
105
  }catch (e){
@@ -12,8 +12,8 @@ class MigrationTemplate {
12
12
  get(){
13
13
  return `
14
14
  class ${this.name} extends Schema {
15
- constructor(settings){
16
- super(settings);
15
+ constructor(context){
16
+ super(context);
17
17
  }
18
18
 
19
19
  up(table){
@@ -1,26 +1,25 @@
1
- // version 0.0.2
1
+ // version 0.0.3
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');
5
5
  var diff = require("deep-object-diff");
6
6
  var MigrationTemplate = require("./migrationTemplate");
7
-
7
+ var globSearch = require("glob");
8
8
 
9
9
  // https://blog.tekspace.io/code-first-multiple-db-context-migration/
10
10
 
11
11
  // node masterrecord add-migration josh C:\Users\rbatista\Downloads\kollege\freshmen\app\models\context
12
12
  class Migrations{
13
13
 
14
- getSettings(rootLocation){
15
- var envType = process.env.master;
16
- var search = `${rootFolderLocation}/**/*env.${envType}.json`;
17
- var files = globSearch.sync(search, rootFolderLocation);
14
+ getContext(executedLocation, contextFileName){
15
+ var search = `${executedLocation}/**/*${contextFileName}.js`
16
+ var files = globSearch.sync(search, executedLocation);
18
17
  var file = files[0];
19
- var settings = require(file);
20
- options = settings[contextName];
21
- this.db = this.__SQLiteInit(options, "better-sqlite3");
22
- this._SQLEngine.setDB(this.db, "better-sqlite3");
23
- return this;
18
+ var context = require(file);
19
+ return {
20
+ context : context,
21
+ fileLocation : file
22
+ }
24
23
  }
25
24
 
26
25
  createSnapShot(snap){
@@ -33,7 +32,6 @@ class Migrations{
33
32
  seed : function(seed){
34
33
  this.seed(this);
35
34
  },
36
- database: {},
37
35
  contextLocation: snap.file,
38
36
  migrationFolder: `${snap.executedLocation}/db/migrations`,
39
37
  snapShotLocation: `${snap.executedLocation}/db/migrations/${snap.contextFileName}_contextSnapShot.json`,
@@ -133,7 +131,7 @@ class Migrations{
133
131
 
134
132
 
135
133
  buildMigrationTemplate(name, oldSchema, newSchema){
136
-
134
+
137
135
  var MT = new MigrationTemplate(name);
138
136
  var tables = this.organizeSchemaByTables(oldSchema, newSchema);
139
137
  tables = this.findNewColumnsInTables(tables);
package/Tools.js CHANGED
@@ -4,6 +4,17 @@ class Tools{
4
4
  return entityList[name];
5
5
  }
6
6
 
7
+
8
+ // this will remove everthing from back slash amount
9
+ static removeBackwardSlashSection(string, amount, type){
10
+ type = type === undefined ? "\\" : type;
11
+ var stringArray = string.split(type);
12
+ for(var i = 0; i < amount; i++){
13
+ stringArray.pop();
14
+ }
15
+ return stringArray.join(type);
16
+ }
17
+
7
18
  static removePrimarykeyandVirtual(currentModel, modelEntity){
8
19
  var newCurrentModel = Object.create(currentModel);
9
20
 
package/package.json CHANGED
@@ -4,9 +4,9 @@
4
4
  "better-sqlite3": "^7.6.2",
5
5
  "commander": "^9.4.0",
6
6
  "glob" : "^8.0.3",
7
- "deep-object-diff" : "1.1.7"
7
+ "deep-object-diff" : "^1.1.7"
8
8
  },
9
- "version": "0.0.27",
9
+ "version": "0.0.28",
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": {