masterrecord 0.0.46 → 0.0.48
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 +18 -7
- package/Entity/entityTrackerModel.js +3 -3
- package/Migrations/cli.js +116 -11
- package/Migrations/migrationMySQLQuery.js +299 -0
- package/Migrations/migrationTemplate.js +7 -7
- package/Migrations/migrations.js +51 -8
- package/Migrations/schema.js +93 -38
- package/QueryLanguage/queryMethods.js +25 -0
- package/context.js +32 -24
- package/insertManager.js +22 -15
- package/mySQLEngine.js +124 -41
- package/mySQLSyncConnect.js +39 -0
- package/package.json +3 -3
- package/readme.md +1 -1
package/Entity/entityModel.js
CHANGED
|
@@ -24,11 +24,12 @@ class EntityModel {
|
|
|
24
24
|
this.obj = {
|
|
25
25
|
name: name,
|
|
26
26
|
type: null,
|
|
27
|
+
relationshipType: null,
|
|
28
|
+
typeSize : null,
|
|
27
29
|
primary : null,
|
|
28
30
|
default : null,
|
|
29
31
|
virtual : null,
|
|
30
32
|
foreignKey : null,
|
|
31
|
-
maxLength : null,
|
|
32
33
|
nullable : true, // no
|
|
33
34
|
unique : false,
|
|
34
35
|
auto : false,
|
|
@@ -41,6 +42,12 @@ class EntityModel {
|
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
type(type, size){
|
|
46
|
+
this.obj.type = type;
|
|
47
|
+
this.obj.typeSize = size;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
|
|
44
51
|
string(){
|
|
45
52
|
this.obj.type = "string";
|
|
46
53
|
return this;
|
|
@@ -61,10 +68,10 @@ class EntityModel {
|
|
|
61
68
|
return this;
|
|
62
69
|
}
|
|
63
70
|
|
|
64
|
-
maxLength(amount){
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
71
|
+
// maxLength(amount){
|
|
72
|
+
// this.obj.maxLength = amount;
|
|
73
|
+
// return this;
|
|
74
|
+
// }
|
|
68
75
|
|
|
69
76
|
// will stop cascade delete which means it will stop not auto delete relationship
|
|
70
77
|
stopCascadeOnDelete(){
|
|
@@ -140,6 +147,7 @@ class EntityModel {
|
|
|
140
147
|
if(foreignKey === undefined){
|
|
141
148
|
foreignKey = `${this.obj.name.toLowerCase()}_id`;
|
|
142
149
|
}
|
|
150
|
+
this.obj.relationshipType = "hasMany";
|
|
143
151
|
this.obj.type = "hasMany";
|
|
144
152
|
this.obj.foreignTable = foreignTable;
|
|
145
153
|
this.obj.foreignKey = foreignKey;
|
|
@@ -153,6 +161,7 @@ class EntityModel {
|
|
|
153
161
|
if(foreignKey === undefined){
|
|
154
162
|
foreignKey = `${this.obj.name.toLowerCase()}_id`;
|
|
155
163
|
}
|
|
164
|
+
this.obj.relationshipType = "hasOne";
|
|
156
165
|
this.obj.type = "hasOne";
|
|
157
166
|
this.obj.foreignTable = foreignTable;
|
|
158
167
|
this.obj.foreignKey = foreignKey;
|
|
@@ -166,7 +175,8 @@ class EntityModel {
|
|
|
166
175
|
hasManyThrough(foreignTable, foreignKey ){
|
|
167
176
|
if(foreignKey === undefined){
|
|
168
177
|
foreignKey = `${this.obj.name.toLowerCase()}_id`;
|
|
169
|
-
}
|
|
178
|
+
};
|
|
179
|
+
this.obj.relationshipType = "hasManyThrough";
|
|
170
180
|
this.obj.type = "hasManyThrough";
|
|
171
181
|
this.obj.foreignTable = foreignTable;// if joinKey is undefined then use name of object.
|
|
172
182
|
this.obj.foreignKey = foreignKey; // Foreign Key table
|
|
@@ -181,7 +191,8 @@ class EntityModel {
|
|
|
181
191
|
foreignKey = `${foreignTable.toLowerCase()}_id`;
|
|
182
192
|
}
|
|
183
193
|
// will use table name to find forien key
|
|
184
|
-
this.obj.type = "
|
|
194
|
+
this.obj.type = "integer";
|
|
195
|
+
this.obj.relationshipType = "belongsTo";
|
|
185
196
|
this.obj.foreignTable = foreignTable; // this is the table name of the current table if diffrent from the object name
|
|
186
197
|
this.obj.foreignKey = foreignKey; // this is the table name of the joining table
|
|
187
198
|
this.obj.nullable = false; // this means it cannot be null
|
|
@@ -74,7 +74,7 @@ class EntityTrackerModel {
|
|
|
74
74
|
|
|
75
75
|
_isRelationship(entity){
|
|
76
76
|
if(entity){
|
|
77
|
-
if(entity.type === "hasOne" || entity.type === "hasMany" || entity.
|
|
77
|
+
if(entity.type === "hasOne" || entity.type === "hasMany" || entity.relationshipType === "belongsTo" || entity.type === "hasManyThrough"){
|
|
78
78
|
return true;
|
|
79
79
|
}
|
|
80
80
|
else{
|
|
@@ -122,7 +122,7 @@ class EntityTrackerModel {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
|
|
125
|
-
if(currentEntity[entityField].
|
|
125
|
+
if(currentEntity[entityField].relationshipType === "belongsTo"){
|
|
126
126
|
if(currentEntity[entityField].lazyLoading){
|
|
127
127
|
// TODO: UPDATE THIS CODE TO USE SOMETHING ELSE - THIS WILL NOT WORK WHEN USING DIFFERENT DATABASES BECAUSE THIS IS USING SQLITE CODE.
|
|
128
128
|
|
|
@@ -216,7 +216,7 @@ class EntityTrackerModel {
|
|
|
216
216
|
//return console.log("make db call to get value", entityField);
|
|
217
217
|
});
|
|
218
218
|
|
|
219
|
-
if(currentEntity[entityField].
|
|
219
|
+
if(currentEntity[entityField].relationshipType === "belongsTo"){
|
|
220
220
|
// check if entity has a value if so then return that value
|
|
221
221
|
if(currentModel[entityField]){
|
|
222
222
|
modelClass[entityField] = currentModel[entityField];
|
package/Migrations/cli.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
// version 0.0.
|
|
3
|
+
// version 0.0.4
|
|
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
|
|
|
@@ -10,13 +10,13 @@ let path = require('path');
|
|
|
10
10
|
var Migration = require('./migrations');
|
|
11
11
|
var globSearch = require("glob");
|
|
12
12
|
|
|
13
|
+
|
|
13
14
|
const [,, ...args] = process.argv
|
|
14
15
|
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
program
|
|
18
|
-
.version('0.0.
|
|
19
|
-
.option('-v, --version', '0.0.34')
|
|
19
|
+
.version('0.0.3', '-v, --version', '0.0.3')
|
|
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.
|
|
@@ -45,8 +45,42 @@ program
|
|
|
45
45
|
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
+
// program
|
|
49
|
+
// .command('create-database <contextFileName> <dbName>')
|
|
50
|
+
// .alias('cd')
|
|
51
|
+
// .description('allows you to create a database')
|
|
52
|
+
// .action(function(contextFileName, dbName){
|
|
53
|
+
// var executedLocation = process.cwd();
|
|
54
|
+
// contextFileName = contextFileName.toLowerCase();
|
|
55
|
+
|
|
56
|
+
// try{
|
|
57
|
+
// // find context file from main folder location
|
|
58
|
+
// // find context file from main folder location
|
|
59
|
+
// var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
|
|
60
|
+
// var files = globSearch.sync(search, executedLocation);
|
|
61
|
+
// var file = files[0];
|
|
62
|
+
|
|
63
|
+
// if(file){
|
|
64
|
+
// var contextSnapshot = require(file);
|
|
65
|
+
// var context = require(contextSnapshot.contextLocation);
|
|
66
|
+
// var newSchema = new schema(context);
|
|
67
|
+
// newSchema.createDatabase(dbName);
|
|
68
|
+
// }
|
|
69
|
+
// else{
|
|
70
|
+
// console.log("Error - Cannot read or find Context file");
|
|
71
|
+
// }
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
// }catch (e){
|
|
75
|
+
// console.log("Error - Cannot read or find file ", e);
|
|
76
|
+
// }
|
|
77
|
+
// console.log("Database Created");
|
|
78
|
+
|
|
79
|
+
// });
|
|
80
|
+
|
|
81
|
+
|
|
48
82
|
// Instructions : to run command you must go to folder where migration file is located.
|
|
49
|
-
program
|
|
83
|
+
program
|
|
50
84
|
.command('add-migration <name> <contextFileName>')
|
|
51
85
|
.alias('am')
|
|
52
86
|
.action(function(name, contextFileName){
|
|
@@ -62,11 +96,12 @@ program
|
|
|
62
96
|
var contextSnapshot = require(files[0]);
|
|
63
97
|
var context = require(contextSnapshot.contextLocation);
|
|
64
98
|
var contextInstance = new context();
|
|
65
|
-
var
|
|
99
|
+
var cleanEntities = migration.cleanEntities(contextInstance.__entities);
|
|
100
|
+
var newEntity = migration.template(name, contextSnapshot.schema, cleanEntities);
|
|
66
101
|
var migrationDate = Date.now();
|
|
67
102
|
var file = `${contextSnapshot.migrationFolder}/${migrationDate}_${name}_migration.js`
|
|
68
103
|
fs.writeFile(file, newEntity, 'utf8', function (err) {
|
|
69
|
-
if (err) return console.log("--- Error running cammand,
|
|
104
|
+
if (err) return console.log("--- Error running cammand, re-run command add-migration ---- ", err);
|
|
70
105
|
});
|
|
71
106
|
console.log(`${name} migration file created`);
|
|
72
107
|
}
|
|
@@ -98,23 +133,29 @@ program
|
|
|
98
133
|
if( migrationFiles){
|
|
99
134
|
|
|
100
135
|
// find newest migration file
|
|
136
|
+
// THIS DOESNT WORK BUG - hack to fix this I just take the last file which I am asuming is the newest one
|
|
101
137
|
var mFiles = migrationFiles.sort(function(x, y){
|
|
102
138
|
return new Date(x.timestamp) < new Date(y.timestamp) ? 1 : -1
|
|
103
139
|
});
|
|
104
|
-
|
|
105
|
-
|
|
140
|
+
/** ENd of BUG */
|
|
141
|
+
|
|
142
|
+
var mFile = mFiles[mFiles.length -1];
|
|
143
|
+
|
|
106
144
|
var migrationProjectFile = require(mFile);
|
|
107
145
|
var context = require(contextSnapshot.contextLocation);
|
|
108
146
|
var contextInstance = new context();
|
|
109
147
|
var newMigrationProjectInstance = new migrationProjectFile(context);
|
|
148
|
+
|
|
110
149
|
var tableObj = migration.buildUpObject(contextSnapshot.schema, contextInstance.__entities);
|
|
150
|
+
|
|
151
|
+
var cleanEntities = migration.cleanEntities(contextInstance.__entities);
|
|
111
152
|
newMigrationProjectInstance.up(tableObj);
|
|
112
153
|
|
|
113
154
|
var snap = {
|
|
114
155
|
file : contextSnapshot.contextLocation,
|
|
115
156
|
executedLocation : executedLocation,
|
|
116
157
|
context : contextInstance,
|
|
117
|
-
contextEntities :
|
|
158
|
+
contextEntities : cleanEntities,
|
|
118
159
|
contextFileName: contextFileName
|
|
119
160
|
}
|
|
120
161
|
|
|
@@ -135,6 +176,69 @@ program
|
|
|
135
176
|
});
|
|
136
177
|
|
|
137
178
|
|
|
179
|
+
program
|
|
180
|
+
.command('update-database-restart <contextFileName>')
|
|
181
|
+
.alias('udr')
|
|
182
|
+
.description('Apply pending migrations to database - up method call')
|
|
183
|
+
.action(function(contextFileName){
|
|
184
|
+
var executedLocation = process.cwd();
|
|
185
|
+
contextFileName = contextFileName.toLowerCase();
|
|
186
|
+
var migration = new Migration();
|
|
187
|
+
try{
|
|
188
|
+
// find context file from main folder location
|
|
189
|
+
var search = `${executedLocation}/**/*${contextFileName}_contextSnapShot.json`;
|
|
190
|
+
var files = globSearch.sync(search, executedLocation);
|
|
191
|
+
var file = files[0];
|
|
192
|
+
if(file){
|
|
193
|
+
var contextSnapshot = require(file);
|
|
194
|
+
var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
|
|
195
|
+
var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
|
|
196
|
+
if( migrationFiles){
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
// organize by time
|
|
200
|
+
var mFiles = migrationFiles.sort(function(x, y){
|
|
201
|
+
return new Date(x.timestamp) < new Date(y.timestamp) ? 1 : -1;
|
|
202
|
+
});
|
|
203
|
+
var context = require(contextSnapshot.contextLocation);
|
|
204
|
+
|
|
205
|
+
for (let i = 0; i < mFiles.length; i++) {
|
|
206
|
+
var file = mFiles[i];
|
|
207
|
+
var migrationProjectFile = require(file);
|
|
208
|
+
|
|
209
|
+
var contextInstance = new context();
|
|
210
|
+
var newMigrationProjectInstance = new migrationProjectFile(context);
|
|
211
|
+
var tableObj = migration.buildUpObject(contextSnapshot.schema, contextInstance.__entities);
|
|
212
|
+
var cleanEntities = migration.cleanEntities(contextInstance.__entities);
|
|
213
|
+
newMigrationProjectInstance.up(tableObj);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
var snap = {
|
|
218
|
+
file : contextSnapshot.contextLocation,
|
|
219
|
+
executedLocation : executedLocation,
|
|
220
|
+
context : contextInstance,
|
|
221
|
+
contextEntities : cleanEntities,
|
|
222
|
+
contextFileName: contextFileName
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
migration.createSnapShot(snap);
|
|
226
|
+
console.log("database updated");
|
|
227
|
+
}
|
|
228
|
+
else{
|
|
229
|
+
console.log("Error - Cannot read or find migration file");
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
}
|
|
233
|
+
else{
|
|
234
|
+
console.log("Error - Cannot read or find Context file");
|
|
235
|
+
}
|
|
236
|
+
}catch (e){
|
|
237
|
+
console.log("Error - Cannot read or find file ", e);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
|
|
138
242
|
program
|
|
139
243
|
.command('get-migrations <contextFileName>')
|
|
140
244
|
.alias('gm')
|
|
@@ -150,7 +254,6 @@ program
|
|
|
150
254
|
var searchMigration = `${contextSnapshot.migrationFolder}/**/*_migration.js`;
|
|
151
255
|
var migrationFiles = globSearch.sync(searchMigration, contextSnapshot.migrationFolder);
|
|
152
256
|
if( migrationFiles){
|
|
153
|
-
console.log("MIgration File List", migrationFiles);
|
|
154
257
|
return migrationFiles;
|
|
155
258
|
}
|
|
156
259
|
}
|
|
@@ -165,7 +268,9 @@ program
|
|
|
165
268
|
.alias('udt')
|
|
166
269
|
.description('Apply pending migrations to database - down method call')
|
|
167
270
|
.action(function(migrationFileName){
|
|
168
|
-
// 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.
|
|
271
|
+
// 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.
|
|
272
|
+
// TODO:
|
|
273
|
+
|
|
169
274
|
});
|
|
170
275
|
|
|
171
276
|
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
|
|
2
|
+
// verison 0.0.1
|
|
3
|
+
class migrationMySQLQuery {
|
|
4
|
+
|
|
5
|
+
#tempTableName = "_temp_alter_column_update"
|
|
6
|
+
|
|
7
|
+
#getTableColumns(table){
|
|
8
|
+
var columnList = [];
|
|
9
|
+
for (var key in table) {
|
|
10
|
+
if(typeof table[key] === "object"){
|
|
11
|
+
columnList.push(table[key].name);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return columnList.join(',');;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#columnMapping(table){
|
|
18
|
+
/*
|
|
19
|
+
var mapping = {
|
|
20
|
+
"name": "id", // if this changes then call rename column
|
|
21
|
+
"type": "integer", // if this changes then call altercolumn
|
|
22
|
+
"primary": false, // is primary key
|
|
23
|
+
"nullable": false, // is nullable
|
|
24
|
+
"unique": true, // vlaue has to be uniqe
|
|
25
|
+
"auto": true, // sets the value to AUTOINCREMENT
|
|
26
|
+
"cascadeOnDelete": true,
|
|
27
|
+
"lazyLoading": true,
|
|
28
|
+
"isNavigational": false
|
|
29
|
+
|
|
30
|
+
}
|
|
31
|
+
*/
|
|
32
|
+
// name TEXT NOT NULL,
|
|
33
|
+
|
|
34
|
+
var auto = table.auto ? " AUTO_INCREMENT":"";
|
|
35
|
+
var primaryKey = table.primary ? " PRIMARY KEY" : "";
|
|
36
|
+
var nullName = table.nullable ? "" : " NOT NULL";
|
|
37
|
+
var unique = table.unique ? " UNIQUE" : "";
|
|
38
|
+
var type = this.typeManager(table.type);
|
|
39
|
+
var tableName = table.name;
|
|
40
|
+
var defaultValue = "";
|
|
41
|
+
if(table.default){
|
|
42
|
+
defaultValue = ` DEFAULT ${this.boolType(table.default)}`
|
|
43
|
+
}
|
|
44
|
+
if(table.relationshipType === 'belongsTo'){
|
|
45
|
+
tableName = table.foreignKey;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return `${tableName} ${type}${nullName}${defaultValue}${unique}${primaryKey}${auto}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
boolType(type){
|
|
52
|
+
switch(type) {
|
|
53
|
+
case "true":
|
|
54
|
+
return "1"
|
|
55
|
+
break;
|
|
56
|
+
case "false":
|
|
57
|
+
return "0"
|
|
58
|
+
break;
|
|
59
|
+
default:
|
|
60
|
+
return type;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
typeManager(type){
|
|
65
|
+
switch(type) {
|
|
66
|
+
case "string":
|
|
67
|
+
return "VARCHAR(255)"
|
|
68
|
+
break;
|
|
69
|
+
case "text":
|
|
70
|
+
return "TEXT"
|
|
71
|
+
break;
|
|
72
|
+
case "float":
|
|
73
|
+
return "fLOAT(24)"
|
|
74
|
+
break;
|
|
75
|
+
case "decimal":
|
|
76
|
+
return "DECIMAL"
|
|
77
|
+
break;
|
|
78
|
+
case "datetime":
|
|
79
|
+
return "DATETIME"
|
|
80
|
+
break;
|
|
81
|
+
case "timestamp":
|
|
82
|
+
return "TIMESTAMP"
|
|
83
|
+
break;
|
|
84
|
+
case "date":
|
|
85
|
+
return "DATE"
|
|
86
|
+
break;
|
|
87
|
+
case "time":
|
|
88
|
+
return "TIME"
|
|
89
|
+
break;
|
|
90
|
+
case "boolean":
|
|
91
|
+
return "TINYINT"
|
|
92
|
+
break;
|
|
93
|
+
case "integer":
|
|
94
|
+
return "INTEGER"
|
|
95
|
+
break;
|
|
96
|
+
case "binary":
|
|
97
|
+
return "BLOB"
|
|
98
|
+
break;
|
|
99
|
+
case "blob":
|
|
100
|
+
return "BLOB"
|
|
101
|
+
break;
|
|
102
|
+
case "json":
|
|
103
|
+
return "JSON"
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
// :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// table is the altered field
|
|
110
|
+
alterColumn( table){
|
|
111
|
+
|
|
112
|
+
if(table){
|
|
113
|
+
|
|
114
|
+
return `ALTER TABLE ${table.tableName} MODIFY COLUMN ${this.#columnMapping(table.table)} `;
|
|
115
|
+
}
|
|
116
|
+
else{
|
|
117
|
+
console.log("table information is null");
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
alterNullable(table){
|
|
123
|
+
// check if has value
|
|
124
|
+
if(typeof table.changes.nullable !== 'undefined'){
|
|
125
|
+
// if it does we want to add that to the alter statment
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
addColum(table){
|
|
131
|
+
return `ALTER TABLE ${table.tableName}
|
|
132
|
+
ADD ${table.name} ${table.realDataType}`;
|
|
133
|
+
|
|
134
|
+
/*
|
|
135
|
+
column definations
|
|
136
|
+
NULL
|
|
137
|
+
TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
|
|
138
|
+
BLOB. The value is a blob of data, stored exactly as it was input
|
|
139
|
+
INTEGER,
|
|
140
|
+
real
|
|
141
|
+
*/
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
dropColumn(table){
|
|
145
|
+
/*
|
|
146
|
+
COLUMNS CANNOT BE DROPPED - RULES
|
|
147
|
+
has unique constraint
|
|
148
|
+
is indexed
|
|
149
|
+
appears in a view
|
|
150
|
+
*/
|
|
151
|
+
return `ALTER TABLE ${table.tableName} DROP COLUMN ${table.name}`;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
insertInto(name, table){
|
|
155
|
+
return `INSERT INTO ${name} (${this.#getTableColumns(table)})
|
|
156
|
+
SELECT ${this.#getTableColumns(table)} FROM ${this.#tempTableName}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
createTable(table){
|
|
160
|
+
|
|
161
|
+
var queryVar = "";
|
|
162
|
+
//console.log("Dsfdsfdsf---------", table)
|
|
163
|
+
for (var key in table) {
|
|
164
|
+
if(typeof table[key] === "object"){
|
|
165
|
+
|
|
166
|
+
if(table[key].type !== "hasOne" && table[key].type !== "hasMany" && table[key].type !== "hasManyThrough"){
|
|
167
|
+
queryVar += `${this.#columnMapping(table[key])}, `;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
var completeQuery = `CREATE TABLE ${table.__name} (${queryVar.replace(/,\s*$/, "")});`;
|
|
173
|
+
return completeQuery;
|
|
174
|
+
|
|
175
|
+
/*
|
|
176
|
+
INTEGER PRIMARY KEY AUTOINCREMENT
|
|
177
|
+
all these are equal to interger
|
|
178
|
+
INT
|
|
179
|
+
INTEGER
|
|
180
|
+
TINYINT
|
|
181
|
+
SMALLINT
|
|
182
|
+
MEDIUMINT
|
|
183
|
+
BIGINT
|
|
184
|
+
UNSIGNED BIG INT
|
|
185
|
+
INT2
|
|
186
|
+
INT8
|
|
187
|
+
*/
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
dropTable(name){
|
|
192
|
+
return `DROP TABLE ${name}`
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
renameTable(table){
|
|
196
|
+
return `ALTER TABLE ${table.tableName} RENAME TO ${table.newName}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
renameColumn(table){
|
|
200
|
+
return `ALTER TABLE ${table.tableName} RENAME COLUMN ${table.name} TO ${table.newName}`
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
module.exports = migrationMySQLQuery;
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
*
|
|
211
|
+
*
|
|
212
|
+
*
|
|
213
|
+
*
|
|
214
|
+
MySQL Data Types NATIVE_DATABASE_TYPES = {
|
|
215
|
+
primary_key: "bigint auto_increment PRIMARY KEY",
|
|
216
|
+
string: { name: "varchar", limit: 255 },
|
|
217
|
+
text: { name: "text" },
|
|
218
|
+
integer: { name: "int", limit: 4 },
|
|
219
|
+
float: { name: "float", limit: 24 },
|
|
220
|
+
decimal: { name: "decimal" },
|
|
221
|
+
datetime: { name: "datetime" },
|
|
222
|
+
timestamp: { name: "timestamp" },
|
|
223
|
+
time: { name: "time" },
|
|
224
|
+
date: { name: "date" },
|
|
225
|
+
binary: { name: "blob" },
|
|
226
|
+
blob: { name: "blob" },
|
|
227
|
+
boolean: { name: "tinyint", limit: 1 },
|
|
228
|
+
json: { name: "json" },
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
PostgreSQL Data Types NATIVE_DATABASE_TYPES = {
|
|
233
|
+
primary_key: "bigserial primary key",
|
|
234
|
+
string: { name: "character varying" },
|
|
235
|
+
text: { name: "text" },
|
|
236
|
+
integer: { name: "integer", limit: 4 },
|
|
237
|
+
float: { name: "float" },
|
|
238
|
+
decimal: { name: "decimal" },
|
|
239
|
+
datetime: {}, # set dynamically based on datetime_type
|
|
240
|
+
timestamp: { name: "timestamp" },
|
|
241
|
+
timestamptz: { name: "timestamptz" },
|
|
242
|
+
time: { name: "time" },
|
|
243
|
+
date: { name: "date" },
|
|
244
|
+
daterange: { name: "daterange" },
|
|
245
|
+
numrange: { name: "numrange" },
|
|
246
|
+
tsrange: { name: "tsrange" },
|
|
247
|
+
tstzrange: { name: "tstzrange" },
|
|
248
|
+
int4range: { name: "int4range" },
|
|
249
|
+
int8range: { name: "int8range" },
|
|
250
|
+
binary: { name: "bytea" },
|
|
251
|
+
boolean: { name: "boolean" },
|
|
252
|
+
xml: { name: "xml" },
|
|
253
|
+
tsvector: { name: "tsvector" },
|
|
254
|
+
hstore: { name: "hstore" },
|
|
255
|
+
inet: { name: "inet" },
|
|
256
|
+
cidr: { name: "cidr" },
|
|
257
|
+
macaddr: { name: "macaddr" },
|
|
258
|
+
uuid: { name: "uuid" },
|
|
259
|
+
json: { name: "json" },
|
|
260
|
+
jsonb: { name: "jsonb" },
|
|
261
|
+
ltree: { name: "ltree" },
|
|
262
|
+
citext: { name: "citext" },
|
|
263
|
+
point: { name: "point" },
|
|
264
|
+
line: { name: "line" },
|
|
265
|
+
lseg: { name: "lseg" },
|
|
266
|
+
box: { name: "box" },
|
|
267
|
+
path: { name: "path" },
|
|
268
|
+
polygon: { name: "polygon" },
|
|
269
|
+
circle: { name: "circle" },
|
|
270
|
+
bit: { name: "bit" },
|
|
271
|
+
bit_varying: { name: "bit varying" },
|
|
272
|
+
money: { name: "money" },
|
|
273
|
+
interval: { name: "interval" },
|
|
274
|
+
oid: { name: "oid" },
|
|
275
|
+
}
|
|
276
|
+
*/
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
/****
|
|
280
|
+
*
|
|
281
|
+
*
|
|
282
|
+
* console.log("sdfdsfdsf", this.#tempTableName);
|
|
283
|
+
return `ALTER TABLE ${table.tableName} MODIFY COLUMN NOT NULL`
|
|
284
|
+
TODO -- We need to find a way build the alter query based on the data that is changed
|
|
285
|
+
//ALTER TABLE MyTable MODIFY COLUMN comment BIGINT NOT NULL;
|
|
286
|
+
if(table){
|
|
287
|
+
table.newName = this.#tempTableName;
|
|
288
|
+
//console.log("----------------------", table)
|
|
289
|
+
return {
|
|
290
|
+
1 : this.renameTable(table),
|
|
291
|
+
2 : this.createTable(fullTable),
|
|
292
|
+
3 : this.insertInto(table.tableName, fullTable),
|
|
293
|
+
4 : this.dropTable(this.#tempTableName)
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
else{
|
|
297
|
+
console.log("table information is null")
|
|
298
|
+
}
|
|
299
|
+
*/
|
|
@@ -39,10 +39,10 @@ module.exports = ${this.name};
|
|
|
39
39
|
|
|
40
40
|
alterColumn(type, name, parent){
|
|
41
41
|
if(type === "up"){
|
|
42
|
-
this.#up += os.EOL + ` this.alterColumn(table.${
|
|
42
|
+
this.#up += os.EOL + ` this.alterColumn(table.${name});`
|
|
43
43
|
}
|
|
44
44
|
else{
|
|
45
|
-
this.#down += os.EOL + ` this.alterColumn(table.${
|
|
45
|
+
this.#down += os.EOL + ` this.alterColumn(table.${name});`
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
createTable(type, name){
|
|
@@ -56,13 +56,13 @@ module.exports = ${this.name};
|
|
|
56
56
|
|
|
57
57
|
addColumn(type, name, parent){
|
|
58
58
|
if(type === "up"){
|
|
59
|
-
this.#up += os.EOL + ` this.addColumn(table.${
|
|
59
|
+
this.#up += os.EOL + ` this.addColumn(table.${name});`
|
|
60
60
|
}
|
|
61
61
|
else{
|
|
62
|
-
this.#down += os.EOL + ` this.addColumn(table.${
|
|
62
|
+
this.#down += os.EOL + ` this.addColumn(table.${name});`
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
-
|
|
65
|
+
//this.addColumn(table.${parent}.${name});`
|
|
66
66
|
|
|
67
67
|
dropTable(type, name){
|
|
68
68
|
if(type === "up"){
|
|
@@ -75,10 +75,10 @@ module.exports = ${this.name};
|
|
|
75
75
|
|
|
76
76
|
dropColumn(type, name, parent){
|
|
77
77
|
if(type === "up"){
|
|
78
|
-
this.#up += os.EOL + ` this.dropColumn(table.${
|
|
78
|
+
this.#up += os.EOL + ` this.dropColumn(table.${name});`
|
|
79
79
|
}
|
|
80
80
|
else{
|
|
81
|
-
this.#down += os.EOL + ` this.dropColumn(table.${
|
|
81
|
+
this.#down += os.EOL + ` this.dropColumn(table.${name});`
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
|