masterrecord 0.0.27 → 0.0.29
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 +58 -20
- package/Migrations/cli.js +18 -14
- package/Migrations/migrationTemplate.js +2 -2
- package/Migrations/migrations.js +11 -13
- package/Tools.js +11 -0
- package/package.json +3 -3
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.
|
|
4
|
+
// version 1.0.19
|
|
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
|
-
|
|
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
|
/*
|
|
@@ -40,8 +42,9 @@ class Context {
|
|
|
40
42
|
*/
|
|
41
43
|
__SQLiteInit(env, sqlName){
|
|
42
44
|
try{
|
|
45
|
+
console.log("===========+++++++++++++++")
|
|
43
46
|
const sqlite3 = require(sqlName);
|
|
44
|
-
let DBAddress = 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
|
|
91
|
-
var envType =
|
|
92
|
-
var
|
|
93
|
-
var
|
|
94
|
-
var
|
|
95
|
-
var
|
|
96
|
-
|
|
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
|
-
|
|
103
|
-
if(options
|
|
104
|
-
|
|
105
|
-
|
|
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
|
|
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 :
|
|
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
|
|
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
|
-
|
|
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
|
|
98
|
-
var
|
|
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){
|
package/Migrations/migrations.js
CHANGED
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
// version 0.0.
|
|
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
|
-
|
|
15
|
-
var
|
|
16
|
-
var
|
|
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
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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,16 +4,16 @@
|
|
|
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.
|
|
9
|
+
"version": "0.0.29",
|
|
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": {
|
|
13
13
|
"type": "git",
|
|
14
14
|
"url": "git+https://github.com/Tailor/Masterrecord.git"
|
|
15
15
|
},
|
|
16
|
-
"main": "
|
|
16
|
+
"main": "MasterRecord.js",
|
|
17
17
|
"scripts": {
|
|
18
18
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
19
19
|
},
|