masterrecord 0.0.49 → 0.1.1
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 -0
- package/Entity/entityTrackerModel.js +139 -135
- package/Migrations/migrationMySQLQuery.js +9 -2
- package/context.js +1 -1
- package/insertManager.js +1 -1
- package/mySQLEngine.js +25 -0
- package/package.json +1 -1
package/Entity/entityModel.js
CHANGED
|
@@ -193,6 +193,7 @@ class EntityModel {
|
|
|
193
193
|
// will use table name to find forien key
|
|
194
194
|
this.obj.type = "integer";
|
|
195
195
|
this.obj.relationshipType = "belongsTo";
|
|
196
|
+
|
|
196
197
|
this.obj.foreignTable = foreignTable; // this is the table name of the current table if diffrent from the object name
|
|
197
198
|
this.obj.foreignKey = foreignKey; // this is the table name of the joining table
|
|
198
199
|
this.obj.nullable = false; // this means it cannot be null
|
|
@@ -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
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
// verison 0.0.
|
|
2
|
+
// verison 0.0.2
|
|
3
3
|
class migrationMySQLQuery {
|
|
4
4
|
|
|
5
5
|
#tempTableName = "_temp_alter_column_update"
|
|
@@ -38,7 +38,8 @@ class migrationMySQLQuery {
|
|
|
38
38
|
var type = this.typeManager(table.type);
|
|
39
39
|
var tableName = table.name;
|
|
40
40
|
var defaultValue = "";
|
|
41
|
-
if(table.default){
|
|
41
|
+
if(table.default != null){
|
|
42
|
+
|
|
42
43
|
defaultValue = ` DEFAULT ${this.boolType(table.default)}`
|
|
43
44
|
}
|
|
44
45
|
if(table.relationshipType === 'belongsTo'){
|
|
@@ -56,6 +57,12 @@ class migrationMySQLQuery {
|
|
|
56
57
|
case "false":
|
|
57
58
|
return "0"
|
|
58
59
|
break;
|
|
60
|
+
case true:
|
|
61
|
+
return "1"
|
|
62
|
+
break;
|
|
63
|
+
case false:
|
|
64
|
+
return "0"
|
|
65
|
+
break;
|
|
59
66
|
default:
|
|
60
67
|
return type;
|
|
61
68
|
}
|
package/context.js
CHANGED
package/insertManager.js
CHANGED
package/mySQLEngine.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// version : 0.0.1
|
|
1
2
|
var tools = require('masterrecord/Tools');
|
|
2
3
|
var util = require('util');
|
|
3
4
|
|
|
@@ -289,6 +290,21 @@ class SQLLiteEngine {
|
|
|
289
290
|
}
|
|
290
291
|
}
|
|
291
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
|
+
}
|
|
292
308
|
}
|
|
293
309
|
}
|
|
294
310
|
return entitiesList
|
|
@@ -324,7 +340,16 @@ class SQLLiteEngine {
|
|
|
324
340
|
for (var column in dirtyFields) {
|
|
325
341
|
// TODO Boolean value is a string with a letter
|
|
326
342
|
var type = model.__entity[dirtyFields[column]].type;
|
|
343
|
+
|
|
344
|
+
if(model.__entity[dirtyFields[column]].relationshipType === "belongsTo"){
|
|
345
|
+
type = "belongsTo";
|
|
346
|
+
}
|
|
347
|
+
|
|
327
348
|
switch(type){
|
|
349
|
+
case "belongsTo" :
|
|
350
|
+
var foreignKey = model.__entity[dirtyFields[column]].foreignKey;
|
|
351
|
+
argument = `${foreignKey} = ${model[dirtyFields[column]]},`;
|
|
352
|
+
break;
|
|
328
353
|
case "integer" :
|
|
329
354
|
argument = argument === null ? `${dirtyFields[column]} = ${model[dirtyFields[column]]},` : `${argument} ${dirtyFields[column]} = ${model[dirtyFields[column]]},`;
|
|
330
355
|
break;
|
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.
|
|
11
|
+
"version": "0.1.1",
|
|
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": {
|