masterrecord 0.0.23 → 0.0.25
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/DeleteManager.js +51 -0
- package/Entity/EntityModel.js +192 -120
- package/Entity/EntityModelBuilder.js +41 -63
- package/Entity/EntityTrackerModel.js +222 -42
- package/InsertManager.js +138 -0
- package/MYSQLEngine.js +409 -0
- package/Masterrecord.js +233 -179
- package/Migrations/cli.js +106 -105
- package/Migrations/migrationTemplate.js +63 -63
- package/Migrations/migrations.js +65 -22
- package/Migrations/schema.js +42 -42
- package/QueryLanguage/queryManager.js +66 -0
- package/QueryLanguage/queryMethods.js +171 -0
- package/QueryLanguage/queryScript.js +331 -0
- package/SQLLiteEngine.js +409 -0
- package/Tools.js +118 -55
- package/package.json +23 -27
- package/QueryLanguage/_Expression.js +0 -322
- package/QueryLanguage/_LogicalQuery.js +0 -23
- package/QueryLanguage/_OperatorList.js +0 -88
- package/QueryLanguage/_QueryModel.js +0 -442
- package/QueryLanguage/_Tokenization.js +0 -173
- package/QueryLanguage/__Query.js +0 -386
- package/QueryLanguage/_simpleQuery.js +0 -184
- package/QueryLanguage/queryBuilder.js +0 -52
- package/SQLEngine.js +0 -52
package/DeleteManager.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
var tools = require('./Tools');
|
|
2
|
+
|
|
3
|
+
class DeleteManager{
|
|
4
|
+
constructor(sqlEngine, entities){
|
|
5
|
+
this._SQLEngine = sqlEngine;
|
|
6
|
+
this._allEntities = entities;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
init(currentModel){
|
|
10
|
+
var $that = this;
|
|
11
|
+
try{
|
|
12
|
+
this.cascadeDelete(currentModel);
|
|
13
|
+
}
|
|
14
|
+
catch(error){
|
|
15
|
+
throw error;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
cascadeDelete(currentModel){
|
|
20
|
+
var $that = this;
|
|
21
|
+
if(!Array.isArray(currentModel)){
|
|
22
|
+
const entityKeys = Object.keys(currentModel.__entity);
|
|
23
|
+
// loop though all entity properties
|
|
24
|
+
for (const property of entityKeys) {
|
|
25
|
+
// cascade delete
|
|
26
|
+
if(currentModel.__entity[property].type === "hasOne" || currentModel.__entity[property].type === "hasMany"){
|
|
27
|
+
$that.cascadeDelete( currentModel[property]);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
this._SQLEngine.delete(currentModel);
|
|
31
|
+
}
|
|
32
|
+
else{
|
|
33
|
+
|
|
34
|
+
for(let i = 0 ; i < currentModel.length; i++) {
|
|
35
|
+
const entityKeys = Object.keys(currentModel[i].__entity);
|
|
36
|
+
// loop though all entity properties
|
|
37
|
+
for (const property of entityKeys) {
|
|
38
|
+
// cascade delete
|
|
39
|
+
if(currentModel[i].__entity[property].type === "hasOne" || currentModel[i].__entity[property].type === "hasMany"){
|
|
40
|
+
$that.cascadeDelete( currentModel[i][property]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
this._SQLEngine.delete(currentModel[i]);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = DeleteManager;
|
package/Entity/EntityModel.js
CHANGED
|
@@ -1,121 +1,193 @@
|
|
|
1
|
-
/*
|
|
2
|
-
|
|
3
|
-
:binary
|
|
4
|
-
:boolean
|
|
5
|
-
:date
|
|
6
|
-
:datetime
|
|
7
|
-
:decimal
|
|
8
|
-
:float
|
|
9
|
-
:integer
|
|
10
|
-
:bigint
|
|
11
|
-
:primary_key
|
|
12
|
-
:references
|
|
13
|
-
:string
|
|
14
|
-
:text
|
|
15
|
-
:time
|
|
16
|
-
:timestamp
|
|
17
|
-
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
foreignKey : null,
|
|
31
|
-
maxLength : null,
|
|
32
|
-
nullable :
|
|
33
|
-
unique :
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
this.obj.type = "
|
|
44
|
-
return this;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
this.obj.type = "
|
|
49
|
-
return this;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this.obj.
|
|
54
|
-
return this;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
this
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
//
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.obj.unique = true;
|
|
78
|
-
return this;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
this.obj.
|
|
84
|
-
return this;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
this.obj.
|
|
112
|
-
return this
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
this.obj.
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
:binary
|
|
4
|
+
:boolean
|
|
5
|
+
:date
|
|
6
|
+
:datetime
|
|
7
|
+
:decimal
|
|
8
|
+
:float
|
|
9
|
+
:integer
|
|
10
|
+
:bigint
|
|
11
|
+
:primary_key
|
|
12
|
+
:references
|
|
13
|
+
:string
|
|
14
|
+
:text
|
|
15
|
+
:time
|
|
16
|
+
:timestamp
|
|
17
|
+
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
// version 1.0.16
|
|
21
|
+
class EntityModel {
|
|
22
|
+
|
|
23
|
+
constructor(name){
|
|
24
|
+
this.obj = {
|
|
25
|
+
name: name,
|
|
26
|
+
type: null,
|
|
27
|
+
primary : null,
|
|
28
|
+
default : null,
|
|
29
|
+
virtual : null,
|
|
30
|
+
foreignKey : null,
|
|
31
|
+
maxLength : null,
|
|
32
|
+
nullable : true, // no
|
|
33
|
+
unique : false,
|
|
34
|
+
auto : false,
|
|
35
|
+
cascadeOnDelete : true,
|
|
36
|
+
lazyLoading : true,
|
|
37
|
+
isNavigational : false
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
string(){
|
|
43
|
+
this.obj.type = "string";
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
integer(){
|
|
48
|
+
this.obj.type = "integer";
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
time(){
|
|
53
|
+
this.obj.type = "time";
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
boolean(){
|
|
58
|
+
this.obj.type = "boolean";
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
maxLength(amount){
|
|
63
|
+
this.obj.maxLength = amount;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// will stop cascade delete which means it will stop not auto delete relationship
|
|
68
|
+
stopCascadeOnDelete(){
|
|
69
|
+
this.obj.cascadeOnDelete = false;
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// is this obj a primary key
|
|
74
|
+
primary(){
|
|
75
|
+
this.obj.primary = true;
|
|
76
|
+
this.obj.nullable = false;
|
|
77
|
+
this.obj.unique = true;
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// allows ablity to get back primaryKey on insert automaticlly return on insert
|
|
82
|
+
auto(){
|
|
83
|
+
this.obj.auto = true;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// sets the default value in the DB
|
|
88
|
+
default(value){
|
|
89
|
+
this.obj.default = value;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get(func){
|
|
94
|
+
this.obj.get = func;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
set(func){
|
|
99
|
+
this.obj.set = func;
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
unique(){
|
|
104
|
+
this.obj.unique = true; // yes
|
|
105
|
+
return this;
|
|
106
|
+
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// this means that it can be an empty field
|
|
110
|
+
nullable(){
|
|
111
|
+
this.obj.nullable = true; // yes
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
notNullable(){
|
|
116
|
+
this.obj.nullable = false; // no
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
//allows you to stop lazy loading because lazy loading is added by default
|
|
121
|
+
lazyLoadingOff(){
|
|
122
|
+
this.obj.lazyLoading = false;
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// allows you to add a virtual object that will skipped from being used as sql objects
|
|
127
|
+
virtual(){
|
|
128
|
+
this.obj.virtual = true;
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
hasMany(foreignTable, foreignKey){
|
|
133
|
+
if(foreignKey === undefined){
|
|
134
|
+
foreignKey = `${this.obj.name.toLowerCase()}_id`;
|
|
135
|
+
}
|
|
136
|
+
this.obj.type = "hasMany";
|
|
137
|
+
this.obj.foreignTable = foreignTable;
|
|
138
|
+
this.obj.foreignKey = foreignKey;
|
|
139
|
+
this.obj.isNavigational = true;
|
|
140
|
+
return this;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
hasOne(foreignTable, foreignKey){
|
|
144
|
+
if(foreignKey === undefined){
|
|
145
|
+
foreignKey = `${this.obj.name.toLowerCase()}_id`;
|
|
146
|
+
}
|
|
147
|
+
this.obj.type = "hasOne";
|
|
148
|
+
this.obj.foreignTable = foreignTable;
|
|
149
|
+
this.obj.foreignKey = foreignKey;
|
|
150
|
+
this.obj.isNavigational = true;
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// will do a inner join with foreignKey
|
|
155
|
+
//hasManyThrough("Tagging", "tag_id") ----- if foreignKey is not provided use the name of the object_id
|
|
156
|
+
hasManyThrough(foreignTable, foreignKey ){
|
|
157
|
+
if(foreignKey === undefined){
|
|
158
|
+
foreignKey = `${this.obj.name.toLowerCase()}_id`;
|
|
159
|
+
}
|
|
160
|
+
this.obj.type = "hasManyThrough";
|
|
161
|
+
this.obj.foreignTable = foreignTable;// if joinKey is undefined then use name of object.
|
|
162
|
+
this.obj.foreignKey = foreignKey; // Foreign Key table
|
|
163
|
+
this.obj.isNavigational = true;
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// will get info
|
|
168
|
+
belongsTo(foreignTable, foreignKey){
|
|
169
|
+
|
|
170
|
+
if(foreignKey === undefined){
|
|
171
|
+
foreignKey = `${foreignTable.toLowerCase()}_id`;
|
|
172
|
+
}
|
|
173
|
+
// will use table name to find forien key
|
|
174
|
+
this.obj.type = "belongsTo";
|
|
175
|
+
this.obj.foreignTable = foreignTable; // this is the table name of the current table if diffrent from the object name
|
|
176
|
+
this.obj.foreignKey = foreignKey; // this is the table name of the joining table
|
|
177
|
+
this.obj.nullable = false; // this means it cannot be null
|
|
178
|
+
return this
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
foreignKey(foreignKey){
|
|
182
|
+
this.obj.foreignKey = foreignKey;
|
|
183
|
+
this.obj.nullable = false;
|
|
184
|
+
return this
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
foreignTable(foreignTable){
|
|
188
|
+
this.obj.foreignTable = foreignTable;
|
|
189
|
+
this.obj.nullable = false;
|
|
190
|
+
return this
|
|
191
|
+
}
|
|
192
|
+
}
|
|
121
193
|
module.exports = EntityModel;
|
|
@@ -1,64 +1,42 @@
|
|
|
1
|
-
var modelDB = require('./EntityModel');
|
|
2
|
-
|
|
3
|
-
// creates new instance if entity model and calls inner functions to build out a valid entity
|
|
4
|
-
class EntityModelBuilder {
|
|
5
|
-
|
|
6
|
-
static
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
MDB.obj.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
arr += key + ",";
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
let cleaned = arr.substring(0, arr.length - 1);
|
|
46
|
-
return cleaned;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// who calls this? What does this do?
|
|
50
|
-
static callAllGets(data, model){
|
|
51
|
-
console.log("callAllGets");
|
|
52
|
-
for(var keys = Object.keys(model), i = 0, end = keys.length; i < end; i++) {
|
|
53
|
-
var key = keys[i], value = model[key];
|
|
54
|
-
if(value.get){
|
|
55
|
-
data[key] = value.get(data);
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
return data;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
1
|
+
var modelDB = require('./EntityModel');
|
|
2
|
+
|
|
3
|
+
// creates new instance if entity model and calls inner functions to build out a valid entity
|
|
4
|
+
class EntityModelBuilder {
|
|
5
|
+
|
|
6
|
+
static create(model){
|
|
7
|
+
if(model.name === undefined){
|
|
8
|
+
throw "dbset model declaired incorrectly. Check you dbset models for code errors."
|
|
9
|
+
}
|
|
10
|
+
var mod = new model(); //create new instance of Entity Model
|
|
11
|
+
var obj = {};
|
|
12
|
+
var methodNamesArray = Object.getOwnPropertyNames( mod.__proto__ );
|
|
13
|
+
var constructorIndex = methodNamesArray.indexOf("constructor");
|
|
14
|
+
// remove contructor method
|
|
15
|
+
if (constructorIndex > -1) {
|
|
16
|
+
methodNamesArray.splice(constructorIndex, 1);
|
|
17
|
+
}
|
|
18
|
+
// loop through all method names in the entity model
|
|
19
|
+
for (var i = 0; i < methodNamesArray.length; i++) {
|
|
20
|
+
let MDB = new modelDB(model.name); // create a new instance of entity Model class
|
|
21
|
+
mod[methodNamesArray[i]](MDB);
|
|
22
|
+
this.cleanNull(MDB.obj); // remove objects that are null or undefined
|
|
23
|
+
if(Object.keys(MDB.obj).length === 0){
|
|
24
|
+
MDB.obj.virtual = true;
|
|
25
|
+
}
|
|
26
|
+
MDB.obj.name = methodNamesArray[i];
|
|
27
|
+
obj[methodNamesArray[i]] = MDB.obj;
|
|
28
|
+
}
|
|
29
|
+
return obj;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static cleanNull(obj) {
|
|
33
|
+
for (var propName in obj) {
|
|
34
|
+
if (obj[propName] === null) {
|
|
35
|
+
delete obj[propName];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
64
42
|
module.exports = EntityModelBuilder;
|