masterrecord 0.0.29 → 0.0.30
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 +3 -1
- package/Migrations/cli.js +21 -15
- package/Migrations/migrationSQLiteQuery.js +92 -0
- package/Migrations/migrationTemplate.js +15 -15
- package/Migrations/migrations.js +52 -18
- package/Migrations/schema.js +32 -6
- package/Migrations/test.js +34 -0
- package/package.json +1 -1
package/Masterrecord.js
CHANGED
|
@@ -24,6 +24,8 @@ class Context {
|
|
|
24
24
|
__relationshipModels = [];
|
|
25
25
|
__enviornment = "";
|
|
26
26
|
__name = "";
|
|
27
|
+
isSQite = false;
|
|
28
|
+
isMySQL = false;
|
|
27
29
|
|
|
28
30
|
constructor(){
|
|
29
31
|
this. __enviornment = process.env.master;
|
|
@@ -42,7 +44,6 @@ class Context {
|
|
|
42
44
|
*/
|
|
43
45
|
__SQLiteInit(env, sqlName){
|
|
44
46
|
try{
|
|
45
|
-
console.log("===========+++++++++++++++")
|
|
46
47
|
const sqlite3 = require(sqlName);
|
|
47
48
|
let DBAddress = env.completeConnection;
|
|
48
49
|
var db = new sqlite3(DBAddress, env);
|
|
@@ -123,6 +124,7 @@ class Context {
|
|
|
123
124
|
}
|
|
124
125
|
|
|
125
126
|
useSqlite(rootFolderLocation){
|
|
127
|
+
this.isSQite = true;
|
|
126
128
|
var root = process.cwd();
|
|
127
129
|
var envType = this.__enviornment;
|
|
128
130
|
var contextName = this.__name;
|
package/Migrations/cli.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
// version 0.0.4
|
|
4
|
+
// https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/
|
|
5
|
+
// how to add environment variables on cli call example - master=development masterrecord add-migration auth authContext
|
|
6
|
+
|
|
3
7
|
const program = require('commander');
|
|
4
8
|
let fs = require('fs');
|
|
5
9
|
let path = require('path');
|
|
@@ -12,7 +16,7 @@ const [,, ...args] = process.argv
|
|
|
12
16
|
|
|
13
17
|
program
|
|
14
18
|
.version('0.0.2')
|
|
15
|
-
.option('-v, --version', '0.0.
|
|
19
|
+
.option('-v, --version', '0.0.30')
|
|
16
20
|
.description('A ORM framework that facilitates the creation and use of business objects whose data requires persistent storage to a database');
|
|
17
21
|
|
|
18
22
|
// Instructions : to run command you must go to main project folder is located and run the command using the context file name.
|
|
@@ -31,9 +35,7 @@ program
|
|
|
31
35
|
var snap = {
|
|
32
36
|
file : contextInstance.fileLocation,
|
|
33
37
|
executedLocation : executedLocation,
|
|
34
|
-
context : new contextInstance.context(
|
|
35
|
-
root: executedLocation
|
|
36
|
-
}),
|
|
38
|
+
context : new contextInstance.context(),
|
|
37
39
|
contextFileName: contextFileName.toLowerCase()
|
|
38
40
|
}
|
|
39
41
|
|
|
@@ -73,14 +75,13 @@ program
|
|
|
73
75
|
});
|
|
74
76
|
|
|
75
77
|
program
|
|
76
|
-
.command('update-database <contextFileName>
|
|
78
|
+
.command('update-database <contextFileName>')
|
|
77
79
|
.alias('ud')
|
|
78
80
|
.description('Apply pending migrations to database')
|
|
79
|
-
.action(function(contextFileName
|
|
80
|
-
console.log("NODE_ENV", process.NODE_ENV)
|
|
81
|
+
.action(function(contextFileName){
|
|
81
82
|
var executedLocation = process.cwd();
|
|
82
83
|
contextFileName = contextFileName.toLowerCase();
|
|
83
|
-
|
|
84
|
+
var migration = new Migration();
|
|
84
85
|
try{
|
|
85
86
|
// find context file from main folder location
|
|
86
87
|
var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
|
|
@@ -88,24 +89,29 @@ program
|
|
|
88
89
|
var file = files[0];
|
|
89
90
|
var contextSnapshot = require(file);
|
|
90
91
|
|
|
91
|
-
var searchMigration =
|
|
92
|
+
var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
|
|
92
93
|
var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
|
|
93
94
|
if( migrationFiles){
|
|
94
95
|
// find newest migration file
|
|
95
96
|
var mFiles = migrationFiles.sort(function(x, y){
|
|
96
97
|
return new Date(x.timestamp) < new Date(y.timestamp) ? 1 : -1
|
|
97
98
|
});
|
|
98
|
-
|
|
99
|
-
var
|
|
99
|
+
|
|
100
|
+
var mFile = mFiles[0];
|
|
101
|
+
console.log("ontextSnapshot -------------",mFile);
|
|
102
|
+
var migrationFile = require(mFile);
|
|
100
103
|
var context = require(contextSnapshot.contextLocation);
|
|
101
104
|
var contextInstance = new context();
|
|
102
|
-
|
|
103
|
-
newMigrationInstance
|
|
105
|
+
|
|
106
|
+
var newMigrationInstance = new migrationFile(context);
|
|
107
|
+
|
|
108
|
+
var tableList = migration.callMigrationUp(contextSnapshot.schema, contextInstance.__entities);
|
|
109
|
+
//newMigrationInstance.up(tableList);
|
|
104
110
|
}
|
|
105
111
|
}catch (e){
|
|
106
112
|
console.log("Cannot read or find file ", e);
|
|
107
113
|
}
|
|
108
|
-
console.log("
|
|
114
|
+
console.log("databasedsdsd updated");
|
|
109
115
|
});
|
|
110
116
|
|
|
111
117
|
// we will find the migration folder inside the nearest app folder if no migration folder is location is added
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
class migrationSQLiteQuery {
|
|
4
|
+
|
|
5
|
+
addColum(table){
|
|
6
|
+
var buildDefinations = this.buildDefinations(table);
|
|
7
|
+
return `ALTER TABLE ${table.name}
|
|
8
|
+
ADD ${table.column} ${buildDefinations}`;
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
column definations
|
|
12
|
+
NULL
|
|
13
|
+
TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
|
|
14
|
+
BLOB. The value is a blob of data, stored exactly as it was input
|
|
15
|
+
INTEGER,
|
|
16
|
+
real
|
|
17
|
+
*/
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
buildDefinations(definations){
|
|
21
|
+
return "";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
createTable(){
|
|
25
|
+
`CREATE TABLE devices (
|
|
26
|
+
name TEXT NOT NULL,
|
|
27
|
+
model TEXT NOT NULL,
|
|
28
|
+
Serial INTEGER NOT NULL UNIQUE
|
|
29
|
+
);`
|
|
30
|
+
|
|
31
|
+
/*
|
|
32
|
+
INTEGER PRIMARY KEY AUTOINCREMENT
|
|
33
|
+
all these are equal to interger
|
|
34
|
+
INT
|
|
35
|
+
INTEGER
|
|
36
|
+
TINYINT
|
|
37
|
+
SMALLINT
|
|
38
|
+
MEDIUMINT
|
|
39
|
+
BIGINT
|
|
40
|
+
UNSIGNED BIG INT
|
|
41
|
+
INT2
|
|
42
|
+
INT8
|
|
43
|
+
*/
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
dropTable(name){
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
dropIndex(){
|
|
52
|
+
|
|
53
|
+
}
|
|
54
|
+
//"dbo.People", "Location"
|
|
55
|
+
alterColumn(){
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
renameColumn(){
|
|
60
|
+
`ALTER TABLE existing_table
|
|
61
|
+
RENAME TO new_table;`
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
/*
|
|
69
|
+
ADDING NEW COLUMN SQLITE
|
|
70
|
+
There are some restrictions on the new column:
|
|
71
|
+
The new column cannot have a UNIQUE or PRIMARY KEY constraint.
|
|
72
|
+
If the new column has a NOT NULL constraint, you must specify a default value for the column other than a NULL value.
|
|
73
|
+
The new column cannot have a default of CURRENT_TIMESTAMP, CURRENT_DATE, and CURRENT_TIME, or an expression.
|
|
74
|
+
If the new column is a foreign key and the foreign key constraint check is enabled, the new column must accept a default value NULL.
|
|
75
|
+
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/*
|
|
79
|
+
|
|
80
|
+
DROPING A COLUMN SQLITE
|
|
81
|
+
Possible reasons why the DROP COLUMN command can fail include:
|
|
82
|
+
|
|
83
|
+
The column is a PRIMARY KEY or part of one.
|
|
84
|
+
The column has a UNIQUE constraint.
|
|
85
|
+
The column is indexed.
|
|
86
|
+
The column is named in the WHERE clause of a partial index.
|
|
87
|
+
The column is named in a table or column CHECK constraint not associated with the column being dropped.
|
|
88
|
+
The column is used in a foreign key constraint.
|
|
89
|
+
The column is used in the expression of a generated column.
|
|
90
|
+
The column appears in a trigger or view.
|
|
91
|
+
|
|
92
|
+
*/
|
|
@@ -28,48 +28,48 @@ module.exports = ${this.name};
|
|
|
28
28
|
`
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
alterColumn(){
|
|
31
|
+
alterColumn(type, name){
|
|
32
32
|
if(type === "up"){
|
|
33
|
-
this.up += os.EOL + ` this.alterColumn(table
|
|
33
|
+
this.up += os.EOL + ` this.alterColumn(table.${name});`
|
|
34
34
|
}
|
|
35
35
|
else{
|
|
36
|
-
this.down += os.EOL + ` this.alterColumn(table
|
|
36
|
+
this.down += os.EOL + ` this.alterColumn(table.${name});`
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
createTable(){
|
|
39
|
+
createTable(type, name){
|
|
40
40
|
if(type === "up"){
|
|
41
|
-
this.up += os.EOL + ` this.createTable(table
|
|
41
|
+
this.up += os.EOL + ` this.createTable(table.${name});`
|
|
42
42
|
}
|
|
43
43
|
else{
|
|
44
|
-
this.down += os.EOL + ` this.createTable(table
|
|
44
|
+
this.down += os.EOL + ` this.createTable(table.${name});`
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
addColumn(type){
|
|
48
|
+
addColumn(type, name){
|
|
49
49
|
if(type === "up"){
|
|
50
|
-
this.up += os.EOL + ` this.addColumn(table
|
|
50
|
+
this.up += os.EOL + ` this.addColumn(table.${name});`
|
|
51
51
|
}
|
|
52
52
|
else{
|
|
53
|
-
this.down += os.EOL + ` this.addColumn(table
|
|
53
|
+
this.down += os.EOL + ` this.addColumn(table.${name});`
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
|
|
58
|
-
dropTable(type){
|
|
58
|
+
dropTable(type, name){
|
|
59
59
|
if(type === "up"){
|
|
60
|
-
this.down += os.EOL + ` this.droptable(table
|
|
60
|
+
this.down += os.EOL + ` this.droptable(table.${name});`
|
|
61
61
|
}
|
|
62
62
|
else{
|
|
63
|
-
this.down += os.EOL + ` this.droptable(table
|
|
63
|
+
this.down += os.EOL + ` this.droptable(table.${name});`
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
dropColumn(type){
|
|
67
|
+
dropColumn(type, name){
|
|
68
68
|
if(type === "up"){
|
|
69
|
-
this.up += os.EOL + ` this.dropColumn(table
|
|
69
|
+
this.up += os.EOL + ` this.dropColumn(table.${name});`
|
|
70
70
|
}
|
|
71
71
|
else{
|
|
72
|
-
this.down += os.EOL + ` this.dropColumn(table
|
|
72
|
+
this.down += os.EOL + ` this.dropColumn(table.${name});`
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
package/Migrations/migrations.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// version 0.0.
|
|
1
|
+
// version 0.0.4
|
|
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');
|
|
@@ -29,9 +29,6 @@ class Migrations{
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
var content = {
|
|
32
|
-
seed : function(seed){
|
|
33
|
-
this.seed(this);
|
|
34
|
-
},
|
|
35
32
|
contextLocation: snap.file,
|
|
36
33
|
migrationFolder: `${snap.executedLocation}/db/migrations`,
|
|
37
34
|
snapShotLocation: `${snap.executedLocation}/db/migrations/${snap.contextFileName}_contextSnapShot.json`,
|
|
@@ -129,39 +126,76 @@ class Migrations{
|
|
|
129
126
|
return tables;
|
|
130
127
|
}
|
|
131
128
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
var MT = new MigrationTemplate(name);
|
|
129
|
+
// build table to build new migration snapshot
|
|
130
|
+
buildMigrationObject(oldSchema, newSchema){
|
|
136
131
|
var tables = this.organizeSchemaByTables(oldSchema, newSchema);
|
|
137
132
|
tables = this.findNewColumnsInTables(tables);
|
|
138
133
|
tables = this.findDeletedColumnsInTables(tables);
|
|
139
134
|
tables = this.findUpdatedColumns(tables);
|
|
135
|
+
return tables;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
//
|
|
139
|
+
callMigrationUp(oldSchema, newSchema){
|
|
140
|
+
var tableArray =[];
|
|
141
|
+
var tables = this.buildMigrationObject(oldSchema, newSchema);
|
|
142
|
+
tables.forEach(function (item, index) {
|
|
143
|
+
|
|
144
|
+
// add new columns for table
|
|
145
|
+
item.newColumns.forEach(function (column, index) {
|
|
146
|
+
tableArray.push(tables[column]);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
item.deletedColumns.forEach(function (column, index) {
|
|
150
|
+
tableArray.push(tables[column]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
item.updatedColumns.forEach(function (column, index) {
|
|
154
|
+
tableArray.push(tables[column]);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
if(item.old === null){
|
|
158
|
+
tableArray.push(tables[column]);
|
|
159
|
+
|
|
160
|
+
}
|
|
161
|
+
if(item.new === null){
|
|
162
|
+
tableArray.push(tables[column]);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
return tableArray;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
buildMigrationTemplate(name, oldSchema, newSchema){
|
|
171
|
+
|
|
172
|
+
var MT = new MigrationTemplate(name);
|
|
173
|
+
var tables = this.buildMigrationObject(oldSchema, newSchema);
|
|
140
174
|
tables.forEach(function (item, index) {
|
|
141
175
|
// add new columns for table
|
|
142
176
|
item.newColumns.forEach(function (column, index) {
|
|
143
|
-
MT.addColumn();
|
|
144
|
-
MT.dropColumn("down");
|
|
177
|
+
MT.addColumn("up", column);
|
|
178
|
+
MT.dropColumn("down", column);
|
|
145
179
|
});
|
|
146
180
|
|
|
147
181
|
item.deletedColumns.forEach(function (column, index) {
|
|
148
|
-
MT.dropColumn("up");
|
|
149
|
-
MT.addColumn("down");
|
|
182
|
+
MT.dropColumn("up", column);
|
|
183
|
+
MT.addColumn("down",column);
|
|
150
184
|
});
|
|
151
185
|
|
|
152
186
|
item.updatedColumns.forEach(function (column, index) {
|
|
153
|
-
MT.alterColumn();
|
|
154
|
-
MT.alterColumn("down")
|
|
187
|
+
MT.alterColumn("up", column);
|
|
188
|
+
MT.alterColumn("down", column)
|
|
155
189
|
});
|
|
156
190
|
|
|
157
191
|
if(item.old === null){
|
|
158
|
-
MT.createTable();
|
|
159
|
-
MT.dropTable("down");
|
|
192
|
+
MT.createTable("up", column);
|
|
193
|
+
MT.dropTable("down", column);
|
|
160
194
|
|
|
161
195
|
}
|
|
162
196
|
if(item.new === null){
|
|
163
|
-
MT.dropTable("up");
|
|
164
|
-
MT.createTable("down");
|
|
197
|
+
MT.dropTable("up", column);
|
|
198
|
+
MT.createTable("down", column);
|
|
165
199
|
}
|
|
166
200
|
|
|
167
201
|
});
|
package/Migrations/schema.js
CHANGED
|
@@ -1,15 +1,41 @@
|
|
|
1
1
|
// version 1
|
|
2
2
|
var fs = require('fs');
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
var table = {
|
|
5
|
+
email : {
|
|
6
|
+
name: "auth",
|
|
7
|
+
column : "email",
|
|
8
|
+
rules: {
|
|
9
|
+
"type": "integer",
|
|
10
|
+
"primary": true,
|
|
11
|
+
"nullable": false,
|
|
12
|
+
"unique": true,
|
|
13
|
+
"auto": true,
|
|
14
|
+
"cascadeOnDelete": true,
|
|
15
|
+
"lazyLoading": true,
|
|
16
|
+
"isNavigational": false
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class schema{
|
|
24
|
+
// TODO : check what database we are using
|
|
25
|
+
// based on the database you can make the call to update the database.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
constructor(context){
|
|
29
|
+
this.context = context;
|
|
8
30
|
}
|
|
9
31
|
|
|
10
32
|
// create obj to convert into create sql
|
|
11
|
-
addColumn(
|
|
12
|
-
|
|
33
|
+
addColumn(table){
|
|
34
|
+
if(this.context.isSQite){
|
|
35
|
+
var sqliteQuery = require("./migrationSQLiteQuery");
|
|
36
|
+
var query = sqliteQuery.addColumn(table);
|
|
37
|
+
this.context.db.prepare(query).all();
|
|
38
|
+
}
|
|
13
39
|
// add column to database
|
|
14
40
|
}
|
|
15
41
|
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
class temp {
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
hatml =
|
|
5
|
+
buildMOdal(className, header, body){
|
|
6
|
+
|
|
7
|
+
`
|
|
8
|
+
Copy
|
|
9
|
+
<div class="modal" tabindex="-1" role="dialog" ${className}>
|
|
10
|
+
<div class="modal-dialog" role="document">
|
|
11
|
+
<div class="modal-content">
|
|
12
|
+
<div class="modal-header">
|
|
13
|
+
${ buildheader(header)}
|
|
14
|
+
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
|
15
|
+
<span aria-hidden="true">×</span>
|
|
16
|
+
</button>
|
|
17
|
+
</div>
|
|
18
|
+
<div class="modal-body">
|
|
19
|
+
${body}
|
|
20
|
+
</div>
|
|
21
|
+
<div class="modal-footer">
|
|
22
|
+
<button type="button" class="btn btn-primary">Save changes</button>
|
|
23
|
+
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
|
24
|
+
</div>
|
|
25
|
+
</div>
|
|
26
|
+
</div>
|
|
27
|
+
</div>`
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
buildheader(title){
|
|
32
|
+
return ` <h5 class="modal-title">${title}</h5>`
|
|
33
|
+
}
|
|
34
|
+
}
|
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.
|
|
9
|
+
"version": "0.0.30",
|
|
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": {
|