masterrecord 0.0.43 → 0.0.45

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.
@@ -17,7 +17,7 @@
17
17
 
18
18
  */
19
19
 
20
- // version 0.0.3
20
+ // version 0.0.5
21
21
  class EntityModel {
22
22
 
23
23
  constructor(name){
@@ -34,7 +34,9 @@ class EntityModel {
34
34
  auto : false,
35
35
  cascadeOnDelete : true,
36
36
  lazyLoading : true,
37
- isNavigational : false
37
+ isNavigational : false,
38
+ skipGetFunction :false,
39
+ valueConversion : true
38
40
 
39
41
  }
40
42
  }
@@ -123,6 +125,11 @@ class EntityModel {
123
125
  return this;
124
126
  }
125
127
 
128
+ valueConversion(bool){
129
+ this.obj.valueConversion = bool;
130
+ return this;
131
+ }
132
+
126
133
  // allows you to add a virtual object that will skipped from being used as sql objects
127
134
  virtual(){
128
135
  this.obj.virtual = true;
@@ -1,3 +1,5 @@
1
+ // version 0.0.3
2
+
1
3
  var modelDB = require('./entityModel');
2
4
 
3
5
  // creates new instance if entity model and calls inner functions to build out a valid entity
@@ -1,5 +1,5 @@
1
1
 
2
- // version : 0.0.1
2
+ // version : 0.0.5
3
3
  var tools = require('../Tools');
4
4
  class EntityTrackerModel {
5
5
 
@@ -39,12 +39,20 @@ class EntityTrackerModel {
39
39
 
40
40
  // Getter
41
41
  modelClass.__defineGetter__(modelField, function(){
42
- if(typeof currentEntity[modelField].get === "function"){
43
- return currentEntity[modelField].get(this["__proto__"]["_" + modelField]);
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];
49
+ }
50
+ }
44
51
  }else{
45
52
  return this["__proto__"]["_" + modelField];
46
53
  }
47
54
 
55
+
48
56
  });
49
57
  }
50
58
  }
@@ -87,6 +95,12 @@ class EntityTrackerModel {
87
95
 
88
96
  // Setter
89
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
+ }
90
104
  this["__proto__"]["_" + entityField] = value;
91
105
  });
92
106
 
@@ -97,7 +111,7 @@ class EntityTrackerModel {
97
111
  if(!ent){
98
112
  var parentEntity = tools.findEntity(this.__name, this.__context);
99
113
  if(parentEntity){
100
- ent = tools.findEntity(parentEntity.__entity[entityField].foreignKey, this.__context);
114
+ ent = tools.findEntity(parentEntity.__entity[entityField].foreignTable, this.__context);
101
115
  if(!ent){
102
116
  return `Error - Entity ${parentEntity.__entity[entityField].foreignTable} not found. Please check your context for proper name.`
103
117
  }
@@ -162,8 +176,9 @@ class EntityTrackerModel {
162
176
  */
163
177
  break;
164
178
  case "hasOne" :
165
- if(ent.__entity[this.__entity.__name]){
166
- tableName = ent.__entity[this.__entity.__name].foreignKey;
179
+ var entityName = tools.findForeignTable(this.__entity.__name, ent.__entity);
180
+ if(entityName){
181
+ tableName = entityName.foreignKey;
167
182
  }
168
183
  else{
169
184
  return `Error - Entity ${ent.__entity.__name} has no property named ${this.__entity.__name}`;
@@ -174,8 +189,9 @@ class EntityTrackerModel {
174
189
  this[entityField] = modelValue;
175
190
  break;
176
191
  case "hasMany" :
177
- if(ent.__entity[this.__entity.__name]){
178
- tableName = ent.__entity[this.__entity.__name].foreignKey;
192
+ var entityName = tools.findForeignTable(this.__entity.__name, ent.__entity);
193
+ if(entityName){
194
+ tableName = entityName.foreignKey;
179
195
  }
180
196
  else{
181
197
  return `Error - Entity ${ent.__entity.__name} has no property named ${this.__entity.__name}`;
@@ -1,5 +1,5 @@
1
1
 
2
- // version 0.0.8
2
+ // version 0.0.10
3
3
  var entityTrackerModel = require('masterrecord/Entity/entityTrackerModel');
4
4
  var tools = require('masterrecord/Tools');
5
5
  var queryScript = require('masterrecord/QueryLanguage/queryScript');
@@ -203,7 +203,7 @@ class queryMethods{
203
203
  single(){
204
204
  if(this.__context.isSQLite){
205
205
  var entityValue = this.__context._SQLEngine.get(this.__queryObject.script, this.__entity, this.__context);
206
- var sing = this.__singleEntityBuilder(entityValue, this._queryBuilder);
206
+ var sing = this.__singleEntityBuilder(entityValue);
207
207
  this.__reset();
208
208
  return sing;
209
209
  }
@@ -211,8 +211,11 @@ class queryMethods{
211
211
 
212
212
  toList(){
213
213
  if(this.__context.isSQLite){
214
+ if(this.__queryObject.script.entityMap.length === 0){
215
+ this.__queryObject.skipClause( this.__entity.__name);
216
+ }
214
217
  var entityValue = this.__context._SQLEngine.all(this.__queryObject.script, this.__entity, this.__context);
215
- var toLi = this.__multipleEntityBuilder(entityValue, this._queryBuilder);
218
+ var toLi = this.__multipleEntityBuilder(entityValue);
216
219
  this.__reset();
217
220
  return toLi;
218
221
  }
@@ -1,4 +1,4 @@
1
- // version 0.0.4
1
+ // version 0.0.5
2
2
 
3
3
  const LOG_OPERATORS_REGEX = /(\|\|)|(&&)/;
4
4
  var tools = require('../Tools');
@@ -18,7 +18,8 @@ class queryScript{
18
18
  take : 0,
19
19
  skip: 0,
20
20
  orderBy : false,
21
- orderByDesc : false
21
+ orderByDesc : false,
22
+ parentName : ""
22
23
  };
23
24
 
24
25
 
@@ -67,6 +68,17 @@ class queryScript{
67
68
  return this.script;
68
69
  }
69
70
 
71
+ // this gets called when you skip a where clause
72
+ skipClause(entityName){
73
+ //this.buildScript(text, "skipClause", this.script, entityName);
74
+ this.script.entityMap.push({
75
+ name: entityName,
76
+ entity : "ran"
77
+ });
78
+ this.script.parentName = entityName;
79
+ return this.script;
80
+ }
81
+
70
82
  include(text, entityName){
71
83
  this.buildScript(text, "include", this.script, entityName);
72
84
  return this.script;
package/SQLLiteEngine.js CHANGED
@@ -1,4 +1,4 @@
1
- // Version 0.0.8
1
+ // Version 0.0.12
2
2
  var tools = require('masterrecord/Tools');
3
3
 
4
4
  class SQLLiteEngine {
@@ -473,16 +473,39 @@ class SQLLiteEngine {
473
473
  var $that = this;
474
474
  var argument = null;
475
475
  var dirtyFields = model.__dirtyFields;
476
-
476
+
477
477
  for (var column in dirtyFields) {
478
+
478
479
  // TODO Boolean value is a string with a letter
479
480
  switch(model.__entity[dirtyFields[column]].type){
480
- case "integer" :
481
+ case "integer" :
482
+ //model.__entity[dirtyFields[column]].skipGetFunction = true;
481
483
  argument = argument === null ? `[${dirtyFields[column]}] = ${model[dirtyFields[column]]},` : `${argument} [${dirtyFields[column]}] = ${model[dirtyFields[column]]},`;
484
+ //model.__entity[dirtyFields[column]].skipGetFunction = false;
482
485
  break;
483
486
  case "string" :
484
487
  argument = argument === null ? `[${dirtyFields[column]}] = '${$that._santizeSingleQuotes(model[dirtyFields[column]])}',` : `${argument} [${dirtyFields[column]}] = '${$that._santizeSingleQuotes(model[dirtyFields[column]])}',`;
485
488
  break;
489
+ case "boolean" :
490
+ var bool = "";
491
+ if(model.__entity[dirtyFields[column]].valueConversion){
492
+ bool = tools.convertBooleanToNumber(model[dirtyFields[column]]);
493
+ }
494
+ else{
495
+ bool = model[dirtyFields[column]];
496
+ }
497
+ argument = argument === null ? `[${dirtyFields[column]}] = '${bool}',` : `${argument} [${dirtyFields[column]}] = ${bool},`;
498
+ break;
499
+ case "time" :
500
+ argument = argument === null ? `[${dirtyFields[column]}] = '${model[dirtyFields[column]]}',` : `${argument} [${dirtyFields[column]}] = ${model[dirtyFields[column]]},`;
501
+ break;
502
+ case "belongsTo" :
503
+ var fore = `_${dirtyFields[column]}`;
504
+ argument = argument === null ? `[${model.__entity[dirtyFields[column]].foreignKey}] = '${model[fore]}',` : `${argument} [${model.__entity[dirtyFields[column]].foreignKey}] = '${model[fore]}',`;
505
+ break;
506
+ case "hasMany" :
507
+ argument = argument === null ? `[${dirtyFields[column]}] = '${model[dirtyFields[column]]}',` : `${argument} [${dirtyFields[column]}] = '${model[dirtyFields[column]]}',`;
508
+ break;
486
509
  default:
487
510
  argument = argument === null ? `[${dirtyFields[column]}] = '${model[dirtyFields[column]]}',` : `${argument} [${dirtyFields[column]}] = '${model[dirtyFields[column]]}',`;
488
511
  }
@@ -555,7 +578,13 @@ class SQLLiteEngine {
555
578
 
556
579
  // will add double single quotes to allow sting to be saved.
557
580
  _santizeSingleQuotes(string){
558
- return string.replace(/'/g, "''");
581
+ if (typeof string === 'string' || string instanceof String){
582
+ return string.replace(/'/g, "''");
583
+ }
584
+ else{
585
+ console.log("warning - Field being passed is not a string");
586
+ throw "warning - Field being passed is not a string";
587
+ }
559
588
  }
560
589
 
561
590
  // converts any object into SQL parameter select string
package/Tools.js CHANGED
@@ -1,4 +1,4 @@
1
- // Version 0.0.1
1
+ // Version 0.0.3
2
2
  class Tools{
3
3
 
4
4
  static findEntity(name, entityList){
@@ -46,6 +46,19 @@ class Tools{
46
46
  }
47
47
  }
48
48
 
49
+ static findForeignTable(name, model){
50
+ for (var key in model) {
51
+ if (model.hasOwnProperty(key)) {
52
+ if(model[key].foreignTable){
53
+ if(model[key].foreignTable === name){
54
+ return model[key];
55
+ }
56
+ }
57
+ }
58
+ }
59
+ return null;
60
+ }
61
+
49
62
  static createNewInstance(validModel, type, classModel){
50
63
  return new type(validModel, classModel);
51
64
  }
@@ -134,6 +147,11 @@ class Tools{
134
147
  }
135
148
  return mainString;;
136
149
  }
150
+
151
+ static convertBooleanToNumber(num) {
152
+ num = num === 'true' ? true : (num === 'false' ? false : num);
153
+ return num ? 1 : 0;
154
+ }
137
155
  }
138
156
 
139
157
  module.exports = Tools;
package/context.js CHANGED
@@ -242,8 +242,28 @@ class context {
242
242
  }
243
243
  }
244
244
 
245
+ // __track(model){
246
+ // this.__trackedEntities.push(model);
247
+ // return model;
248
+ // }
249
+
245
250
  __track(model){
246
- this.__trackedEntities.push(model);
251
+ var add = true;
252
+ for (var mod in this.__trackedEntities) {
253
+ var id = this.__trackedEntities[mod].__ID;
254
+ if(id === model.__ID){
255
+ add = false;
256
+ }
257
+ }
258
+ if(this.__trackedEntities.length === 0){
259
+ this.__trackedEntities.push(model);
260
+ }
261
+ else{
262
+ if(add){
263
+ this.__trackedEntities.push(model);
264
+ }
265
+ }
266
+
247
267
  return model;
248
268
  }
249
269
 
package/package.json CHANGED
@@ -4,9 +4,10 @@
4
4
  "better-sqlite3": "^9.0.0",
5
5
  "commander": "^11.1.0",
6
6
  "glob" : "^10.3.10",
7
- "deep-object-diff" : "^1.1.9"
7
+ "deep-object-diff" : "^1.1.9",
8
+ "pg" : "^8.13.3"
8
9
  },
9
- "version": "0.0.43",
10
+ "version": "0.0.45",
10
11
  "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 ",
11
12
  "homepage": "https://github.com/Tailor/MasterRecord#readme",
12
13
  "repository": {
@@ -1,79 +0,0 @@
1
- // ALL THIS SHOULD DO IS BUILD A SQL QUERY
2
- // version 1.0.1
3
-
4
- class queryManager{
5
- constructor( query) {
6
-
7
- /*
8
- queryObject
9
- {
10
- "where": where user.id = 1
11
- "raw": "select * from tablename where user.id = 2" OR false,
12
- "builder": builderObject,
13
- "select" : "*",
14
- "from" : from tablename
15
- }
16
- */
17
- this._query = query;
18
- this._queryBuilder = query.builder;
19
- this._context = query.builder.__context;
20
- }
21
-
22
- single(){
23
- var entityValue = this._context._SQLEngine.get(this._query.getScript());
24
- var sing = this._queryBuilder.__execute(entityValue, this._queryBuilder);
25
- return sing;
26
- }
27
-
28
- //
29
- select(query){
30
- // this will come after the where
31
- this._query.select = "select " + query;
32
- return this;
33
- }
34
-
35
- count(){
36
- // trying to match string select and relace with select Count(*);
37
- var res = this._query.select.replace("select *", "select Count(*)");
38
- var entityValue = this._context._SQLEngine.get(res);
39
- var val = entityValue[Object.keys(entityValue)[0]];
40
- return val;
41
- }
42
-
43
- toList(){
44
- var entityValue = this._context._SQLEngine.all(this._query);
45
- var toLi = this._queryBuilder.__executeList(entityValue, this._queryBuilder);
46
- return toLi;
47
- }
48
-
49
- }
50
-
51
- module.exports = queryManager;
52
-
53
-
54
- /*
55
-
56
- LINQ Extension Methods
57
- First()
58
- FirstOrDefault()
59
- SingleOrDefault()
60
- Count()
61
- Min()
62
- Max()
63
- Last()
64
- LastOrDefault()
65
- Average()
66
- */
67
- /*
68
-
69
- LINQ Extension Methods
70
- First()
71
- FirstOrDefault()
72
- SingleOrDefault()
73
- Count()
74
- Min()
75
- Max()
76
- Last()
77
- LastOrDefault()
78
- Average()
79
- */
File without changes