masterrecord 0.0.31 → 0.0.32
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/Entity/entityModel.js +1 -1
- package/Entity/entityModelBuilder.js +1 -1
- package/Masterrecord.js +1 -1
- package/Migrations/cli.js +93 -59
- package/Migrations/migrationSQLiteQuery.js +105 -18
- package/Migrations/migrationTemplate.js +6 -1
- package/Migrations/migrations.js +31 -17
- package/Migrations/schema.js +36 -34
- package/SQLLiteEngine.js +1 -0
- package/context.js +46 -36
- package/package.json +1 -1
package/Entity/entityModel.js
CHANGED
package/Masterrecord.js
CHANGED
package/Migrations/cli.js
CHANGED
|
@@ -9,6 +9,7 @@ let fs = require('fs');
|
|
|
9
9
|
let path = require('path');
|
|
10
10
|
var Migration = require('./migrations');
|
|
11
11
|
var globSearch = require("glob");
|
|
12
|
+
const { table } = require('console');
|
|
12
13
|
|
|
13
14
|
const [,, ...args] = process.argv
|
|
14
15
|
|
|
@@ -16,10 +17,10 @@ const [,, ...args] = process.argv
|
|
|
16
17
|
|
|
17
18
|
program
|
|
18
19
|
.version('0.0.2')
|
|
19
|
-
.option('-v, --version', '0.0.
|
|
20
|
+
.option('-v, --version', '0.0.32')
|
|
20
21
|
.description('A ORM framework that facilitates the creation and use of business objects whose data requires persistent storage to a database');
|
|
21
22
|
|
|
22
|
-
// Instructions : to run command you must go to main project folder is located and run the command using the
|
|
23
|
+
// Instructions : to run command you must go to main project folder is located and run the command using the context file name.
|
|
23
24
|
program
|
|
24
25
|
.command('enable-migrations <contextFileName>')
|
|
25
26
|
.alias('em')
|
|
@@ -54,30 +55,33 @@ program
|
|
|
54
55
|
var migration = new Migration();
|
|
55
56
|
try{
|
|
56
57
|
// find context file from main folder location
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
58
|
+
var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
|
|
59
|
+
var files = globSearch.sync(search, executedLocation);
|
|
60
|
+
var file = files[0];
|
|
61
|
+
if(file){
|
|
62
|
+
var contextSnapshot = require(files[0]);
|
|
63
|
+
var context = require(contextSnapshot.contextLocation);
|
|
64
|
+
var contextInstance = new context();
|
|
65
|
+
var newEntity = migration.buildMigrationTemplate(name, contextSnapshot.schema, contextInstance .__entities);
|
|
66
|
+
var migrationDate = Date.now();
|
|
67
|
+
var file = `${contextSnapshot.migrationFolder}/${migrationDate}_${name}_migration.js`
|
|
68
|
+
fs.writeFile(file, newEntity, 'utf8', function (err) {
|
|
69
|
+
if (err) return console.log("--- Error running cammand, rlease run command add-migration ---- ", err);
|
|
70
|
+
});
|
|
71
|
+
console.log(`${name} migration file created`);
|
|
72
|
+
}
|
|
73
|
+
else{
|
|
74
|
+
console.log("Error - Cannot read or find Context file");
|
|
75
|
+
}
|
|
70
76
|
}catch (e){
|
|
71
|
-
console.log("Cannot read or find file ", e);
|
|
77
|
+
console.log("Error - Cannot read or find file ", e);
|
|
72
78
|
}
|
|
73
|
-
|
|
74
|
-
console.log(`${name} migration file created`);
|
|
75
79
|
});
|
|
76
80
|
|
|
77
81
|
program
|
|
78
82
|
.command('update-database <contextFileName>')
|
|
79
83
|
.alias('ud')
|
|
80
|
-
.description('Apply pending migrations to database')
|
|
84
|
+
.description('Apply pending migrations to database - up method call')
|
|
81
85
|
.action(function(contextFileName){
|
|
82
86
|
var executedLocation = process.cwd();
|
|
83
87
|
contextFileName = contextFileName.toLowerCase();
|
|
@@ -87,54 +91,84 @@ program
|
|
|
87
91
|
var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
|
|
88
92
|
var files = globSearch.sync(search, executedLocation);
|
|
89
93
|
var file = files[0];
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
94
|
+
if(file){
|
|
95
|
+
var contextSnapshot = require(file);
|
|
96
|
+
var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
|
|
97
|
+
var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
|
|
98
|
+
if( migrationFiles){
|
|
99
|
+
|
|
100
|
+
// find newest migration file
|
|
101
|
+
var mFiles = migrationFiles.sort(function(x, y){
|
|
102
|
+
return new Date(x.timestamp) < new Date(y.timestamp) ? 1 : -1
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
var mFile = mFiles[0];
|
|
106
|
+
var migrationFile = require(mFile);
|
|
107
|
+
var context = require(contextSnapshot.contextLocation);
|
|
108
|
+
var contextInstance = new context();
|
|
109
|
+
var newMigrationInstance = new migrationFile(context);
|
|
110
|
+
|
|
111
|
+
var tableObj = migration.callMigrationUp(contextSnapshot.schema, contextInstance.__entities);
|
|
112
|
+
newMigrationInstance.up(tableObj);
|
|
113
|
+
// TODO create a new snapshot
|
|
114
|
+
|
|
115
|
+
var snap = {
|
|
116
|
+
file : contextInstance.fileLocation,
|
|
117
|
+
executedLocation : executedLocation,
|
|
118
|
+
context : contextInstance,
|
|
119
|
+
contextFileName: contextFileName
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
migration.createSnapShot(snap);
|
|
123
|
+
console.log("database updated");
|
|
124
|
+
}
|
|
125
|
+
else{
|
|
126
|
+
console.log("Error - Cannot read or find migration file");
|
|
127
|
+
}
|
|
128
|
+
|
|
110
129
|
}
|
|
130
|
+
else{
|
|
131
|
+
console.log("Error - Cannot read or find Context file");
|
|
132
|
+
}
|
|
111
133
|
}catch (e){
|
|
112
|
-
console.log("Cannot read or find file ", e);
|
|
134
|
+
console.log("Error - Cannot read or find file ", e);
|
|
113
135
|
}
|
|
114
|
-
console.log("databasedsdsd updated");
|
|
115
136
|
});
|
|
116
137
|
|
|
117
|
-
// we will find the migration folder inside the nearest app folder if no migration folder is location is added
|
|
118
|
-
program
|
|
119
|
-
.command('remove-migration <name> <migrationFolderLocation>')
|
|
120
|
-
.alias('rm')
|
|
121
|
-
.description('Removes the last migration that has not been applied')
|
|
122
|
-
.action(function(name){
|
|
123
|
-
// remove migrations call the down method of a migration newMigrationInstance.down();
|
|
124
|
-
// find migration file using name and delete it.
|
|
125
|
-
});
|
|
126
138
|
|
|
139
|
+
program
|
|
140
|
+
.command('get-migrations <contextFileName>')
|
|
141
|
+
.alias('gm')
|
|
142
|
+
.description('Get a list of migration file names using the context')
|
|
143
|
+
.action(function(contextFileName){
|
|
144
|
+
var executedLocation = process.cwd();
|
|
145
|
+
contextFileName = contextFileName.toLowerCase();
|
|
146
|
+
var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
|
|
147
|
+
var files = globSearch.sync(search, executedLocation);
|
|
148
|
+
var file = files[0];
|
|
149
|
+
if(file){
|
|
150
|
+
var contextSnapshot = require(file);
|
|
151
|
+
var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
|
|
152
|
+
var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
|
|
153
|
+
if( migrationFiles){
|
|
154
|
+
console.log("MIgration File List", migrationFiles);
|
|
155
|
+
return migrationFiles;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else{
|
|
159
|
+
console.log("Error - Cannot read or find Context file");
|
|
160
|
+
}
|
|
161
|
+
});
|
|
127
162
|
|
|
128
|
-
|
|
163
|
+
// we will find the migration folder inside the nearest app folder if no migration folder is location is added
|
|
129
164
|
program
|
|
130
|
-
.command('
|
|
131
|
-
.alias('
|
|
132
|
-
.description('
|
|
133
|
-
.action(function(
|
|
134
|
-
|
|
135
|
-
console.log("starting server");
|
|
136
|
-
require(dir + '/server.js');
|
|
137
|
-
//return "node c:\node\server.js"
|
|
165
|
+
.command('update-database-target <migrationFileName>')
|
|
166
|
+
.alias('udt')
|
|
167
|
+
.description('Apply pending migrations to database - down method call')
|
|
168
|
+
.action(function(migrationFileName){
|
|
169
|
+
// this will call all the down methods until it gets to the one your looking for. First it needs to validate that there is such a file.
|
|
138
170
|
});
|
|
139
171
|
|
|
172
|
+
|
|
173
|
+
|
|
140
174
|
program.parse(process.argv);
|
|
@@ -1,11 +1,28 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
// verison 0.0.2
|
|
3
3
|
class migrationSQLiteQuery {
|
|
4
4
|
|
|
5
|
+
tempTableName = "_temp_alter_column_update"
|
|
6
|
+
|
|
7
|
+
alterColumn(fullTable, table){
|
|
8
|
+
if(table){
|
|
9
|
+
table.newName = this.tempTableName;
|
|
10
|
+
return {
|
|
11
|
+
1 : this.renameTable(table),
|
|
12
|
+
2 : this.createTable(table.tableName, fullTable),
|
|
13
|
+
3 : this.insertInto(table.tableName, fullTable),
|
|
14
|
+
4 : this.dropTable(this.tempTableName)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
else{
|
|
18
|
+
console.log("table information is null")
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
5
23
|
addColum(table){
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
ADD ${table.column} ${buildDefinations}`;
|
|
24
|
+
return `ALTER TABLE ${table.tableName}
|
|
25
|
+
ADD COLUMN ${table.name}`;
|
|
9
26
|
|
|
10
27
|
/*
|
|
11
28
|
column definations
|
|
@@ -17,16 +34,66 @@ class migrationSQLiteQuery {
|
|
|
17
34
|
*/
|
|
18
35
|
}
|
|
19
36
|
|
|
20
|
-
|
|
21
|
-
return
|
|
37
|
+
insertInto(name, table){
|
|
38
|
+
return `INSERT INTO ${name} (${this.getTableColumns(table)})
|
|
39
|
+
SELECT ${this.getTableColumns(table)} FROM ${this.tempTableName}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
dropColumn(table){
|
|
43
|
+
/*
|
|
44
|
+
COLUMNS CANNOT BE DROPPED - RULES
|
|
45
|
+
has unique constraint
|
|
46
|
+
is indexed
|
|
47
|
+
appears in a view
|
|
48
|
+
*/
|
|
49
|
+
return `ALTER TABLE ${table.tableName} DROP COLUMN ${table.name}`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
columnMapping(table){
|
|
53
|
+
/*
|
|
54
|
+
var mapping = {
|
|
55
|
+
"name": "id", // if this chnages then call rename column
|
|
56
|
+
"type": "integer", // if this changes then call altercolumn
|
|
57
|
+
"primary": false, // is primary key
|
|
58
|
+
"nullable": false, // is nullable
|
|
59
|
+
"unique": true, // vlaue has to be uniqe
|
|
60
|
+
"auto": true, // sets the value to AUTOINCREMENT
|
|
61
|
+
"cascadeOnDelete": true,
|
|
62
|
+
"lazyLoading": true,
|
|
63
|
+
"isNavigational": false
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
*/
|
|
67
|
+
// name TEXT NOT NULL,
|
|
68
|
+
|
|
69
|
+
var auto = table.auto ? " AUTOINCREMENT":"";
|
|
70
|
+
var primaryKey = table.primary ? " PRIMARY KEY" : "";
|
|
71
|
+
var nullName = table.nullable ? "" : " NOT NULL";
|
|
72
|
+
var unique = table.unique ? " UNIQUE" : "";
|
|
73
|
+
var type = this.typeManager(table.type);
|
|
74
|
+
|
|
75
|
+
return `${table.name} ${type}${nullName}${unique}${primaryKey}${auto}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
getTableColumns(table){
|
|
79
|
+
var columnList = [];
|
|
80
|
+
for (var key in table) {
|
|
81
|
+
if(typeof table[key] === "object"){
|
|
82
|
+
columnList.push(table[key].name);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return columnList.join(',');;
|
|
22
86
|
}
|
|
23
87
|
|
|
24
|
-
createTable(){
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
88
|
+
createTable(name, table){
|
|
89
|
+
var queryVar = "";
|
|
90
|
+
for (var key in table) {
|
|
91
|
+
if(typeof table[key] === "object"){
|
|
92
|
+
queryVar += `${this.columnMapping(table[key])}, `;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return `CREATE TABLE ${name} (${queryVar.replace(/,\s*$/, "")});`;
|
|
30
97
|
|
|
31
98
|
/*
|
|
32
99
|
INTEGER PRIMARY KEY AUTOINCREMENT
|
|
@@ -45,26 +112,46 @@ class migrationSQLiteQuery {
|
|
|
45
112
|
|
|
46
113
|
|
|
47
114
|
dropTable(name){
|
|
48
|
-
|
|
115
|
+
return `DROP TABLE ${name}`
|
|
49
116
|
}
|
|
50
117
|
|
|
51
118
|
dropIndex(){
|
|
52
119
|
|
|
53
120
|
}
|
|
54
|
-
//"dbo.People", "Location"
|
|
55
|
-
alterColumn(){
|
|
56
121
|
|
|
122
|
+
renameTable(table){
|
|
123
|
+
return `ALTER TABLE ${table.tableName} RENAME TO ${table.newName}`;
|
|
57
124
|
}
|
|
58
125
|
|
|
59
|
-
renameColumn(){
|
|
60
|
-
`ALTER TABLE
|
|
61
|
-
RENAME TO new_table;`
|
|
126
|
+
renameColumn(table){
|
|
127
|
+
return `ALTER TABLE ${table.tableName} RENAME COLUMN ${table.name} TO ${table.newName}`
|
|
62
128
|
}
|
|
63
129
|
|
|
130
|
+
typeManager(type){
|
|
131
|
+
switch(type) {
|
|
132
|
+
case "string":
|
|
133
|
+
return "TEXT"
|
|
134
|
+
break;
|
|
135
|
+
case "time":
|
|
136
|
+
return "TEXT"
|
|
137
|
+
break;
|
|
138
|
+
case "boolean":
|
|
139
|
+
return "INTEGER"
|
|
140
|
+
break;
|
|
141
|
+
case "integer":
|
|
142
|
+
return "INTEGER"
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
}
|
|
147
|
+
|
|
64
148
|
|
|
65
149
|
}
|
|
66
150
|
|
|
67
151
|
|
|
152
|
+
module.exports = migrationSQLiteQuery;
|
|
153
|
+
|
|
154
|
+
|
|
68
155
|
/*
|
|
69
156
|
ADDING NEW COLUMN SQLITE
|
|
70
157
|
There are some restrictions on the new column:
|
|
@@ -11,16 +11,21 @@ class MigrationTemplate {
|
|
|
11
11
|
|
|
12
12
|
get(){
|
|
13
13
|
return `
|
|
14
|
-
|
|
14
|
+
|
|
15
|
+
var masterrecord = require('masterrecord');
|
|
16
|
+
|
|
17
|
+
class ${this.name} extends masterrecord.schema {
|
|
15
18
|
constructor(context){
|
|
16
19
|
super(context);
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
up(table){
|
|
23
|
+
this.addTable(table);
|
|
20
24
|
${this.up}
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
down(table){
|
|
28
|
+
this.addTable(table);
|
|
24
29
|
${this.down}
|
|
25
30
|
}
|
|
26
31
|
}
|
package/Migrations/migrations.js
CHANGED
|
@@ -5,6 +5,7 @@ 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
|
+
const { table } = require('console');
|
|
8
9
|
|
|
9
10
|
// https://blog.tekspace.io/code-first-multiple-db-context-migration/
|
|
10
11
|
|
|
@@ -97,7 +98,14 @@ class Migrations{
|
|
|
97
98
|
var UD = diff.updatedDiff(item.old, item.new);
|
|
98
99
|
const isEmpty = Object.keys(UD).length === 0;
|
|
99
100
|
if(!isEmpty){
|
|
100
|
-
|
|
101
|
+
for (var key in UD) {
|
|
102
|
+
var tableChanges = {
|
|
103
|
+
changes : UD[key],
|
|
104
|
+
table : item.new[key],
|
|
105
|
+
tableName : item.name
|
|
106
|
+
};
|
|
107
|
+
item.updatedColumns.push(tableChanges);
|
|
108
|
+
}
|
|
101
109
|
}
|
|
102
110
|
|
|
103
111
|
});
|
|
@@ -137,34 +145,37 @@ class Migrations{
|
|
|
137
145
|
|
|
138
146
|
//
|
|
139
147
|
callMigrationUp(oldSchema, newSchema){
|
|
140
|
-
var
|
|
148
|
+
var tableObj = {}
|
|
141
149
|
var tables = this.buildMigrationObject(oldSchema, newSchema);
|
|
142
150
|
tables.forEach(function (item, index) {
|
|
143
|
-
|
|
144
151
|
// add new columns for table
|
|
145
|
-
|
|
146
|
-
|
|
152
|
+
var columnInfo = tables[index];
|
|
153
|
+
item.newColumns.forEach(function (column, ind) {
|
|
154
|
+
columnInfo.new[column].tableName = item.name;
|
|
155
|
+
tableObj[column] = columnInfo.new[column];
|
|
147
156
|
});
|
|
148
157
|
|
|
149
|
-
item.deletedColumns.forEach(function (column,
|
|
150
|
-
|
|
158
|
+
item.deletedColumns.forEach(function (column, ind) {
|
|
159
|
+
columnInfo.old[column].tableName = item.name;
|
|
160
|
+
tableObj[column] = columnInfo.old[column];
|
|
151
161
|
});
|
|
152
162
|
|
|
153
|
-
item.updatedColumns.forEach(function (column,
|
|
154
|
-
|
|
163
|
+
item.updatedColumns.forEach(function (column, ind) {
|
|
164
|
+
tableObj[column.table.name] = column;
|
|
155
165
|
});
|
|
156
166
|
|
|
157
167
|
if(item.old === null){
|
|
158
|
-
|
|
159
|
-
|
|
168
|
+
columnInfo.new.tableName = item.name;
|
|
169
|
+
tableObj[column] = columnInfo.new;
|
|
160
170
|
}
|
|
171
|
+
|
|
161
172
|
if(item.new === null){
|
|
162
|
-
|
|
173
|
+
columnInfo.old.tableName = item.name;
|
|
174
|
+
tableObj[column] = columnInfo.old;
|
|
163
175
|
}
|
|
164
|
-
|
|
176
|
+
tableObj.___table = item;
|
|
165
177
|
});
|
|
166
|
-
|
|
167
|
-
return tableArray;
|
|
178
|
+
return tableObj;
|
|
168
179
|
}
|
|
169
180
|
|
|
170
181
|
buildMigrationTemplate(name, oldSchema, newSchema){
|
|
@@ -184,8 +195,11 @@ class Migrations{
|
|
|
184
195
|
});
|
|
185
196
|
|
|
186
197
|
item.updatedColumns.forEach(function (column, index) {
|
|
187
|
-
|
|
188
|
-
|
|
198
|
+
const isEmpty = Object.keys(column).length === 0;
|
|
199
|
+
if(!isEmpty){
|
|
200
|
+
MT.alterColumn("up", column.table.name);
|
|
201
|
+
MT.alterColumn("down", column.table.name);
|
|
202
|
+
}
|
|
189
203
|
});
|
|
190
204
|
|
|
191
205
|
if(item.old === null){
|
package/Migrations/schema.js
CHANGED
|
@@ -1,63 +1,65 @@
|
|
|
1
1
|
// version 1
|
|
2
|
-
var fs = require('fs');
|
|
3
|
-
|
|
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
2
|
|
|
23
3
|
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
4
|
|
|
28
5
|
constructor(context){
|
|
29
|
-
this.context = context;
|
|
6
|
+
this.context = new context();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
addTable(table){
|
|
11
|
+
this.fullTable = table.___table;
|
|
30
12
|
}
|
|
31
13
|
|
|
32
14
|
// create obj to convert into create sql
|
|
33
15
|
addColumn(table){
|
|
16
|
+
console.log("----------addColumn ------");
|
|
34
17
|
if(this.context.isSQite){
|
|
35
18
|
var sqliteQuery = require("./migrationSQLiteQuery");
|
|
36
|
-
var query = sqliteQuery.addColumn(table);
|
|
37
|
-
this.context.db.prepare(query).all();
|
|
38
19
|
}
|
|
39
20
|
// add column to database
|
|
40
21
|
}
|
|
41
22
|
|
|
42
|
-
createTable(
|
|
23
|
+
createTable(table){
|
|
43
24
|
|
|
44
25
|
}
|
|
45
26
|
|
|
46
|
-
dropColumn(
|
|
47
|
-
|
|
48
|
-
|
|
27
|
+
dropColumn(table){
|
|
28
|
+
if(this.fullTable){
|
|
29
|
+
// drop column
|
|
30
|
+
if(this.context.isSQite){
|
|
31
|
+
var sqliteQuery = require("./migrationSQLiteQuery");
|
|
32
|
+
var queryBuilder = new sqliteQuery();
|
|
33
|
+
var query = queryBuilder.dropColumn(table);
|
|
34
|
+
this.context._execute(query);
|
|
35
|
+
}
|
|
36
|
+
}else{
|
|
37
|
+
console.log("Must call the addTable function.");
|
|
38
|
+
}
|
|
49
39
|
}
|
|
50
40
|
|
|
51
|
-
dropTable(
|
|
41
|
+
dropTable(table){
|
|
52
42
|
|
|
53
43
|
}
|
|
54
44
|
|
|
55
45
|
dropIndex(){
|
|
56
46
|
|
|
57
47
|
}
|
|
58
|
-
//"dbo.People", "Location"
|
|
59
|
-
alterColumn(){
|
|
60
|
-
|
|
48
|
+
//"dbo.People", "Location"
|
|
49
|
+
alterColumn(table){
|
|
50
|
+
if(this.fullTable){
|
|
51
|
+
if(this.context.isSQite){
|
|
52
|
+
var sqliteQuery = require("./migrationSQLiteQuery");
|
|
53
|
+
var queryBuilder = new sqliteQuery();
|
|
54
|
+
var queryObj = queryBuilder.alterColumn(this.fullTable.new, table);
|
|
55
|
+
for (var key in queryObj) {
|
|
56
|
+
var query = queryObj[key];
|
|
57
|
+
this.context._execute(query);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}else{
|
|
61
|
+
console.log("Must call the addTable function.");
|
|
62
|
+
}
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
renameColumn(){
|
package/SQLLiteEngine.js
CHANGED
package/context.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// Version 0.0.1
|
|
1
2
|
|
|
2
3
|
var modelBuilder = require('./Entity/EntityModelBuilder');
|
|
3
4
|
var query = require('masterrecord/QueryLanguage/queryMethods');
|
|
@@ -9,7 +10,7 @@ var deleteManager = require('./DeleteManager');
|
|
|
9
10
|
var globSearch = require("glob");
|
|
10
11
|
|
|
11
12
|
|
|
12
|
-
class
|
|
13
|
+
class context {
|
|
13
14
|
_isModelValid = {
|
|
14
15
|
isValid: true,
|
|
15
16
|
errors: []
|
|
@@ -68,7 +69,7 @@ class Context {
|
|
|
68
69
|
const connection = mysql.createConnection(env);
|
|
69
70
|
connection.connect();
|
|
70
71
|
db.__name = sqlName;
|
|
71
|
-
this.
|
|
72
|
+
this._MYSQLEngine = new MYSQLEngine();
|
|
72
73
|
return connection;
|
|
73
74
|
|
|
74
75
|
}
|
|
@@ -149,7 +150,7 @@ class Context {
|
|
|
149
150
|
useMySql(options, rootFolderLocation){
|
|
150
151
|
if(options !== undefined){
|
|
151
152
|
this.db = this.__mysqlInit(options, "mysql");
|
|
152
|
-
this.
|
|
153
|
+
this._MYSQLEngine.setDB(this.db, "mysql");
|
|
153
154
|
return this;
|
|
154
155
|
}
|
|
155
156
|
else{
|
|
@@ -175,39 +176,41 @@ class Context {
|
|
|
175
176
|
try{
|
|
176
177
|
var tracked = this.__trackedEntities;
|
|
177
178
|
// start transaction
|
|
178
|
-
this.
|
|
179
|
-
|
|
180
|
-
var
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
179
|
+
if(this.isSQite){
|
|
180
|
+
this._SQLEngine.startTransaction();
|
|
181
|
+
for (var model in tracked) {
|
|
182
|
+
var currentModel = tracked[model];
|
|
183
|
+
switch(currentModel.__state) {
|
|
184
|
+
case "insert":
|
|
185
|
+
var insert = new insertManager(this._SQLEngine, this._isModelValid, this.__entities);
|
|
186
|
+
insert.init(currentModel);
|
|
187
|
+
|
|
188
|
+
break;
|
|
189
|
+
case "modified":
|
|
190
|
+
if(currentModel.__dirtyFields.length > 0){
|
|
191
|
+
var cleanCurrentModel = tools.removePrimarykeyandVirtual(currentModel, currentModel._entity);
|
|
192
|
+
// build columns equal to value string
|
|
193
|
+
var argu = this._SQLEngine._buildSQLEqualTo(cleanCurrentModel);
|
|
194
|
+
var primaryKey = tools.getPrimaryKeyObject(cleanCurrentModel.__entity);
|
|
195
|
+
var sqlUpdate = {tableName: cleanCurrentModel.__entity.__name, arg: argu, primaryKey : primaryKey, primaryKeyValue : cleanCurrentModel[primaryKey] };
|
|
196
|
+
this._SQLEngine.update(sqlUpdate);
|
|
197
|
+
}
|
|
198
|
+
else{
|
|
199
|
+
console.log("Tracked entity modified with no values being changed");
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// code block
|
|
203
|
+
break;
|
|
204
|
+
case "delete":
|
|
205
|
+
var deleteObject = new deleteManager(this._SQLEngine, this.__entities);
|
|
206
|
+
deleteObject.init(currentModel);
|
|
207
|
+
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
this.__clearErrorHandler();
|
|
212
|
+
this._SQLEngine.endTransaction();
|
|
208
213
|
}
|
|
209
|
-
this.__clearErrorHandler();
|
|
210
|
-
this._SQLEngine.endTransaction();
|
|
211
214
|
}
|
|
212
215
|
|
|
213
216
|
catch(error){
|
|
@@ -222,6 +225,13 @@ class Context {
|
|
|
222
225
|
return true;
|
|
223
226
|
}
|
|
224
227
|
|
|
228
|
+
|
|
229
|
+
_execute(query){
|
|
230
|
+
if(this.isSQite){
|
|
231
|
+
this._SQLEngine._execute(query);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
225
235
|
// TODO: WHY WE HAVE DOUBLE TRACKED OBJECTS - LOOP THROUGH ALL TRACKED OBJECTS
|
|
226
236
|
__track(model){
|
|
227
237
|
this.__trackedEntities.push(model);
|
|
@@ -245,4 +255,4 @@ class Context {
|
|
|
245
255
|
}
|
|
246
256
|
|
|
247
257
|
|
|
248
|
-
module.exports =
|
|
258
|
+
module.exports = context;
|
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.32",
|
|
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": {
|