masterrecord 0.1.0 → 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.
@@ -1,5 +1,5 @@
1
1
 
2
- // version : 0.0.6
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
- // Setter
29
- modelClass.__defineSetter__(modelField, 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
-
40
- // Getter
41
- modelClass.__defineGetter__(modelField, function(){
42
- // TODO: fix only when updating
43
- if(currentEntity[modelField]){
44
- if(!currentEntity[modelField].skipGetFunction){
45
- if(typeof currentEntity[modelField].get === "function"){
46
- return currentEntity[modelField].get(this["__proto__"]["_" + modelField]);
47
- }else{
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
- // Setter
97
- modelClass.__defineSetter__(entityField, function(value){
98
-
99
- if(typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" ){
100
- modelClass.__state = "modified";
101
- modelClass.__dirtyFields.push(entityField);
102
- modelClass.__context.__track(modelClass);
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
- else{
120
- return `Error - Entity ${parentEntity} not found. Please check your context for proper name.`
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
- var priKey = tools.getPrimaryKeyObject(ent.__entity);
130
-
131
- //var idValue = currentEntity[entityField].foreignKey;
132
- var currentValue = this.__proto__[`_${entityField}`];
133
- //var modelValue = ent.where(`r => r.${priKey} == ${ currentValue }`).single();
134
- this[entityField] = currentValue;
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
- return this["__proto__"]["_" + entityField];
138
- }
139
- }
140
- else{
141
- // user.tags = gets all tags related to user
142
- // tag.users = get all users related to tags
143
- if(currentEntity[entityField].lazyLoading){
144
- var priKey = tools.getPrimaryKeyObject(this.__entity);
145
- var entityName = currentEntity[entityField].foreignTable === undefined ? entityField : currentEntity[entityField].foreignTable;
146
- var tableName = "";
147
- if(entityName){
148
- switch(currentEntity[entityField].type){
149
- // TODO: move the SQL generation part to the SQL builder so that we can later on use many diffrent types of SQL databases.
150
- case "hasManyThrough" :
151
- try{
152
- var joiningEntity = this.__context[tools.capitalize(entityName)];
153
- var entityFieldJoinName = currentEntity[entityField].foreignTable === undefined? entityField : currentEntity[entityField].foreignTable;
154
- var thirdEntity = this.__context[tools.capitalize(entityFieldJoinName)];
155
- var firstJoiningID = joiningEntity.__entity[this.__entity.__name].foreignTable;
156
- var secondJoiningID = joiningEntity.__entity[entityField].foreignTable;
157
- if(firstJoiningID && secondJoiningID )
158
- {
159
- 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();
160
- // 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]}`;
161
- // var modelValue = ent.raw(modelQuery).toList();
162
- this[entityField] = modelValue;
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 "Joining table must declaire joining table names"
188
+ return `Error - Entity ${ent.__entity.__name} has no property named ${this.__entity.__name}`;
166
189
  }
167
- }
168
- catch(error){
169
- return error;
170
- }
171
- /*
172
- select * from User
173
- INNER JOIN Tagging ON User.id = Tagging.user_id
174
- INNER JOIN Tag ON Tag.id = Tagging.tag_id
175
- WHERE Tagging.user_id = 13
176
- */
177
- break;
178
- case "hasOne" :
179
- var entityName = tools.findForeignTable(this.__entity.__name, ent.__entity);
180
- if(entityName){
181
- tableName = entityName.foreignKey;
182
- }
183
- else{
184
- return `Error - Entity ${ent.__entity.__name} has no property named ${this.__entity.__name}`;
185
- }
186
-
187
- //var jj = ent.raw(`select * from ${entityName} where ${tableName} = ${ this[priKey] }`).single();
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 "Entity name must be defined"
214
+ return this["__proto__"]["_" + entityField];
207
215
  }
208
216
  }
209
- else{
210
- return this["__proto__"]["_" + entityField];
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/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.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": {