masterrecord 0.1.0 → 0.1.2
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/entityTrackerModel.js +139 -135
- package/SQLLiteEngine.js +33 -14
- package/mySQLEngine.js +15 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
// version : 0.0.
|
|
2
|
+
// version : 0.0.7
|
|
3
3
|
var tools = require('../Tools');
|
|
4
4
|
class EntityTrackerModel {
|
|
5
5
|
|
|
@@ -25,35 +25,32 @@ class EntityTrackerModel {
|
|
|
25
25
|
// current entity has a value then add
|
|
26
26
|
modelClass["__proto__"]["_" + modelField] = modelFieldValue;
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return this["__proto__"]["_" + modelField];
|
|
28
|
+
Object.defineProperty(modelClass,modelField, {
|
|
29
|
+
set: function(value) {
|
|
30
|
+
modelClass.__state = "modified";
|
|
31
|
+
modelClass.__dirtyFields.push(modelField);
|
|
32
|
+
if(typeof currentEntity[modelField].set === "function"){
|
|
33
|
+
this["__proto__"]["_" + modelField] = currentEntity[modelField].set(value);
|
|
34
|
+
}else{
|
|
35
|
+
// Then it will add name to dirty fields
|
|
36
|
+
this["__proto__"]["_" + modelField] = value;
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
get:function(){
|
|
40
|
+
// TODO: fix only when updating
|
|
41
|
+
if(currentEntity[modelField]){
|
|
42
|
+
if(!currentEntity[modelField].skipGetFunction){
|
|
43
|
+
if(typeof currentEntity[modelField].get === "function"){
|
|
44
|
+
return currentEntity[modelField].get(this["__proto__"]["_" + modelField]);
|
|
45
|
+
}else{
|
|
46
|
+
return this["__proto__"]["_" + modelField];
|
|
47
|
+
}
|
|
49
48
|
}
|
|
49
|
+
}else{
|
|
50
|
+
return this["__proto__"]["_" + modelField];
|
|
50
51
|
}
|
|
51
|
-
}else{
|
|
52
|
-
return this["__proto__"]["_" + modelField];
|
|
53
52
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
});
|
|
53
|
+
});
|
|
57
54
|
}
|
|
58
55
|
}
|
|
59
56
|
|
|
@@ -93,128 +90,135 @@ class EntityTrackerModel {
|
|
|
93
90
|
|
|
94
91
|
if($that._isRelationship(currentEntity[entityField])){
|
|
95
92
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
this["__proto__"]["_" + entityField] = value;
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
// Getter
|
|
108
|
-
modelClass.__defineGetter__(entityField, function(){
|
|
109
|
-
|
|
110
|
-
var ent = tools.findEntity(entityField, this.__context);
|
|
111
|
-
if(!ent){
|
|
112
|
-
var parentEntity = tools.findEntity(this.__name, this.__context);
|
|
113
|
-
if(parentEntity){
|
|
114
|
-
ent = tools.findEntity(parentEntity.__entity[entityField].foreignTable, this.__context);
|
|
115
|
-
if(!ent){
|
|
116
|
-
return `Error - Entity ${parentEntity.__entity[entityField].foreignTable} not found. Please check your context for proper name.`
|
|
117
|
-
}
|
|
93
|
+
|
|
94
|
+
Object.defineProperty(modelClass, entityField, {
|
|
95
|
+
set: function(value) {
|
|
96
|
+
if(typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" ){
|
|
97
|
+
modelClass.__state = "modified";
|
|
98
|
+
modelClass.__dirtyFields.push(entityField);
|
|
99
|
+
modelClass.__context.__track(modelClass);
|
|
118
100
|
}
|
|
119
|
-
|
|
120
|
-
|
|
101
|
+
this["__proto__"]["_" + entityField] = value;
|
|
102
|
+
},
|
|
103
|
+
get : function(){
|
|
104
|
+
var ent = tools.findEntity(entityField, this.__context);
|
|
105
|
+
if(!ent){
|
|
106
|
+
var parentEntity = tools.findEntity(this.__name, this.__context);
|
|
107
|
+
if(parentEntity){
|
|
108
|
+
ent = tools.findEntity(parentEntity.__entity[entityField].foreignTable, this.__context);
|
|
109
|
+
if(!ent){
|
|
110
|
+
return `Error - Entity ${parentEntity.__entity[entityField].foreignTable} not found. Please check your context for proper name.`
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else{
|
|
114
|
+
return `Error - Entity ${parentEntity} not found. Please check your context for proper name.`
|
|
115
|
+
}
|
|
121
116
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if(currentEntity[entityField].relationshipType === "belongsTo"){
|
|
126
|
-
if(currentEntity[entityField].lazyLoading){
|
|
127
|
-
// TODO: UPDATE THIS CODE TO USE SOMETHING ELSE - THIS WILL NOT WORK WHEN USING DIFFERENT DATABASES BECAUSE THIS IS USING SQLITE CODE.
|
|
117
|
+
|
|
128
118
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
119
|
+
if(currentEntity[entityField].relationshipType === "belongsTo"){
|
|
120
|
+
if(currentEntity[entityField].lazyLoading){
|
|
121
|
+
// TODO: UPDATE THIS CODE TO USE SOMETHING ELSE - THIS WILL NOT WORK WHEN USING DIFFERENT DATABASES BECAUSE THIS IS USING SQLITE CODE.
|
|
122
|
+
|
|
123
|
+
var name = currentEntity[entityField].foreignKey;
|
|
124
|
+
var priKey = tools.getPrimaryKeyObject(ent.__entity);
|
|
125
|
+
|
|
126
|
+
//var idValue = currentEntity[entityField].foreignKey;
|
|
127
|
+
var currentValue = this.__proto__[`_${name}`];
|
|
128
|
+
var val = this["__proto__"]["_"+entityField];
|
|
129
|
+
var modelValue = null;
|
|
130
|
+
if(!val){
|
|
131
|
+
modelValue = ent.where(`r => r.${priKey} == ${ currentValue }`).single();
|
|
132
|
+
|
|
133
|
+
}
|
|
134
|
+
else{
|
|
135
|
+
modelValue = val;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
this[entityField] = modelValue;
|
|
139
|
+
}
|
|
140
|
+
else{
|
|
141
|
+
return this["__proto__"]["_" + entityField];
|
|
142
|
+
}
|
|
135
143
|
}
|
|
136
144
|
else{
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
145
|
+
// user.tags = gets all tags related to user
|
|
146
|
+
// tag.users = get all users related to tags
|
|
147
|
+
if(currentEntity[entityField].lazyLoading){
|
|
148
|
+
var priKey = tools.getPrimaryKeyObject(this.__entity);
|
|
149
|
+
var entityName = currentEntity[entityField].foreignTable === undefined ? entityField : currentEntity[entityField].foreignTable;
|
|
150
|
+
var tableName = "";
|
|
151
|
+
if(entityName){
|
|
152
|
+
switch(currentEntity[entityField].type){
|
|
153
|
+
// TODO: move the SQL generation part to the SQL builder so that we can later on use many diffrent types of SQL databases.
|
|
154
|
+
case "hasManyThrough" :
|
|
155
|
+
try{
|
|
156
|
+
var joiningEntity = this.__context[tools.capitalize(entityName)];
|
|
157
|
+
var entityFieldJoinName = currentEntity[entityField].foreignTable === undefined? entityField : currentEntity[entityField].foreignTable;
|
|
158
|
+
var thirdEntity = this.__context[tools.capitalize(entityFieldJoinName)];
|
|
159
|
+
var firstJoiningID = joiningEntity.__entity[this.__entity.__name].foreignTable;
|
|
160
|
+
var secondJoiningID = joiningEntity.__entity[entityField].foreignTable;
|
|
161
|
+
if(firstJoiningID && secondJoiningID )
|
|
162
|
+
{
|
|
163
|
+
var modelValue = ent.include(`p => p.${entityFieldJoinName}.select(j => j.${joiningEntity.__entity[this.__entity.__name].foreignKey})`).include(`p =>p.${this.__entity.__name}`).where(`r =>r.${this.__entity.__name}.${priKey} = ${this[priKey]}`).toList();
|
|
164
|
+
// var modelQuery = `select ${selectParameter} from ${this.__entity.__name} INNER JOIN ${entityName} ON ${this.__entity.__name}.${priKey} = ${entityName}.${firstJoiningID} INNER JOIN ${entityField} ON ${entityField}.${joinTablePriKey} = ${entityName}.${secondJoiningID} WHERE ${this.__entity.__name}.${priKey} = ${ this[priKey]}`;
|
|
165
|
+
// var modelValue = ent.raw(modelQuery).toList();
|
|
166
|
+
this[entityField] = modelValue;
|
|
167
|
+
}
|
|
168
|
+
else{
|
|
169
|
+
return "Joining table must declaire joining table names"
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
catch(error){
|
|
173
|
+
return error;
|
|
174
|
+
}
|
|
175
|
+
/*
|
|
176
|
+
select * from User
|
|
177
|
+
INNER JOIN Tagging ON User.id = Tagging.user_id
|
|
178
|
+
INNER JOIN Tag ON Tag.id = Tagging.tag_id
|
|
179
|
+
WHERE Tagging.user_id = 13
|
|
180
|
+
*/
|
|
181
|
+
break;
|
|
182
|
+
case "hasOne" :
|
|
183
|
+
var entityName = tools.findForeignTable(this.__entity.__name, ent.__entity);
|
|
184
|
+
if(entityName){
|
|
185
|
+
tableName = entityName.foreignKey;
|
|
163
186
|
}
|
|
164
187
|
else{
|
|
165
|
-
return
|
|
188
|
+
return `Error - Entity ${ent.__entity.__name} has no property named ${this.__entity.__name}`;
|
|
166
189
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
var modelValue = ent.where(`r => r.${tableName} == ${this[priKey]}`).single();
|
|
189
|
-
this[entityField] = modelValue;
|
|
190
|
-
break;
|
|
191
|
-
case "hasMany" :
|
|
192
|
-
var entityName = tools.findForeignTable(this.__entity.__name, ent.__entity);
|
|
193
|
-
if(entityName){
|
|
194
|
-
tableName = entityName.foreignKey;
|
|
195
|
-
}
|
|
196
|
-
else{
|
|
197
|
-
return `Error - Entity ${ent.__entity.__name} has no property named ${this.__entity.__name}`;
|
|
198
|
-
}
|
|
199
|
-
//var modelValue = ent.raw(`select * from ${entityName} where ${tableName} = ${ this[priKey] }`).toList();
|
|
200
|
-
var modelValue = ent.where(`r => r.${tableName} == ${this[priKey]}`).toList();
|
|
201
|
-
this[entityField] = modelValue;
|
|
202
|
-
break;
|
|
190
|
+
|
|
191
|
+
//var jj = ent.raw(`select * from ${entityName} where ${tableName} = ${ this[priKey] }`).single();
|
|
192
|
+
var modelValue = ent.where(`r => r.${tableName} == ${this[priKey]}`).single();
|
|
193
|
+
this[entityField] = modelValue;
|
|
194
|
+
break;
|
|
195
|
+
case "hasMany" :
|
|
196
|
+
var entityName = tools.findForeignTable(this.__entity.__name, ent.__entity);
|
|
197
|
+
if(entityName){
|
|
198
|
+
tableName = entityName.foreignKey;
|
|
199
|
+
}
|
|
200
|
+
else{
|
|
201
|
+
return `Error - Entity ${ent.__entity.__name} has no property named ${this.__entity.__name}`;
|
|
202
|
+
}
|
|
203
|
+
//var modelValue = ent.raw(`select * from ${entityName} where ${tableName} = ${ this[priKey] }`).toList();
|
|
204
|
+
var modelValue = ent.where(`r => r.${tableName} == ${this[priKey]}`).toList();
|
|
205
|
+
this[entityField] = modelValue;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else{
|
|
210
|
+
return "Entity name must be defined"
|
|
203
211
|
}
|
|
204
212
|
}
|
|
205
213
|
else{
|
|
206
|
-
return
|
|
214
|
+
return this["__proto__"]["_" + entityField];
|
|
207
215
|
}
|
|
208
216
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
return this["__proto__"]["_" + entityField];
|
|
212
220
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
return this["__proto__"]["_" + entityField];
|
|
216
|
-
//return console.log("make db call to get value", entityField);
|
|
217
|
-
});
|
|
221
|
+
});
|
|
218
222
|
|
|
219
223
|
if(currentEntity[entityField].relationshipType === "belongsTo"){
|
|
220
224
|
// check if entity has a value if so then return that value
|
package/SQLLiteEngine.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Version 0.0.
|
|
1
|
+
// Version 0.0.13
|
|
2
2
|
var tools = require('masterrecord/Tools');
|
|
3
3
|
|
|
4
4
|
class SQLLiteEngine {
|
|
@@ -423,13 +423,22 @@ class SQLLiteEngine {
|
|
|
423
423
|
return "";
|
|
424
424
|
}
|
|
425
425
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
426
|
+
// return a list of entity names and skip foreign keys and underscore.
|
|
427
|
+
getEntityList(entity){
|
|
428
|
+
var entitiesList = [];
|
|
429
|
+
var $that = this;
|
|
430
|
+
for (var ent in entity) {
|
|
431
|
+
if(!ent.startsWith("_")){
|
|
432
|
+
if(!entity[ent].foreignKey){
|
|
433
|
+
if(entity[ent].relationshipTable){
|
|
434
|
+
if($that.chechUnsupportedWords(entity[ent].relationshipTable)){
|
|
435
|
+
entitiesList.push(`'${entity[ent].relationshipTable}'`);
|
|
436
|
+
}
|
|
437
|
+
else{
|
|
438
|
+
entitiesList.push(entity[ent].relationshipTable);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
else{
|
|
433
442
|
if($that.chechUnsupportedWords(ent)){
|
|
434
443
|
entitiesList.push(`'${ent}'`);
|
|
435
444
|
}
|
|
@@ -437,16 +446,26 @@ class SQLLiteEngine {
|
|
|
437
446
|
entitiesList.push(ent);
|
|
438
447
|
}
|
|
439
448
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
449
|
+
}
|
|
450
|
+
else{
|
|
451
|
+
|
|
452
|
+
if(entity[ent].relationshipType === "belongsTo"){
|
|
453
|
+
var name = entity[ent].foreignKey;
|
|
454
|
+
if($that.chechUnsupportedWords(name)){
|
|
455
|
+
entitiesList.push(`'${name}'`);
|
|
456
|
+
//entitiesList.push(`'${ent}'`);
|
|
457
|
+
}
|
|
458
|
+
else{
|
|
459
|
+
entitiesList.push(name);
|
|
460
|
+
//entitiesList.push(ent);
|
|
443
461
|
}
|
|
444
462
|
}
|
|
463
|
+
|
|
445
464
|
}
|
|
446
465
|
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
466
|
+
}
|
|
467
|
+
return entitiesList
|
|
468
|
+
}
|
|
450
469
|
chechUnsupportedWords(word){
|
|
451
470
|
for (var item in this.unsupportedWords) {
|
|
452
471
|
var text = this.unsupportedWords[item];
|
package/mySQLEngine.js
CHANGED
|
@@ -290,6 +290,21 @@ class SQLLiteEngine {
|
|
|
290
290
|
}
|
|
291
291
|
}
|
|
292
292
|
}
|
|
293
|
+
else{
|
|
294
|
+
|
|
295
|
+
if(entity[ent].relationshipType === "belongsTo"){
|
|
296
|
+
var name = entity[ent].foreignKey;
|
|
297
|
+
if($that.chechUnsupportedWords(name)){
|
|
298
|
+
entitiesList.push(`'${name}'`);
|
|
299
|
+
//entitiesList.push(`'${ent}'`);
|
|
300
|
+
}
|
|
301
|
+
else{
|
|
302
|
+
entitiesList.push(name);
|
|
303
|
+
//entitiesList.push(ent);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
}
|
|
293
308
|
}
|
|
294
309
|
}
|
|
295
310
|
return entitiesList
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"sync-mysql2" : "^1.0.4",
|
|
9
9
|
"app-root-path": "^3.1.0"
|
|
10
10
|
},
|
|
11
|
-
"version": "0.1.
|
|
11
|
+
"version": "0.1.2",
|
|
12
12
|
"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 ",
|
|
13
13
|
"homepage": "https://github.com/Tailor/MasterRecord#readme",
|
|
14
14
|
"repository": {
|