masterrecord 0.0.33 → 0.0.35

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.2
20
+ // version 0.0.3
21
21
  class EntityModel {
22
22
 
23
23
  constructor(name){
@@ -137,9 +137,11 @@ class EntityModel {
137
137
  this.obj.foreignTable = foreignTable;
138
138
  this.obj.foreignKey = foreignKey;
139
139
  this.obj.isNavigational = true;
140
+ this.obj.nullable = false;
140
141
  return this;
141
142
  }
142
143
 
144
+ // DB must have a record or exception will be thrown unless set to nullable
143
145
  hasOne(foreignTable, foreignKey){
144
146
  if(foreignKey === undefined){
145
147
  foreignKey = `${this.obj.name.toLowerCase()}_id`;
@@ -148,6 +150,7 @@ class EntityModel {
148
150
  this.obj.foreignTable = foreignTable;
149
151
  this.obj.foreignKey = foreignKey;
150
152
  this.obj.isNavigational = true;
153
+ this.obj.nullable = false;
151
154
  return this;
152
155
  }
153
156
 
@@ -1,3 +1,5 @@
1
+
2
+ // version : 0.0.1
1
3
  var tools = require('../Tools');
2
4
  class EntityTrackerModel {
3
5
 
@@ -108,21 +110,13 @@ class EntityTrackerModel {
108
110
 
109
111
  if(currentEntity[entityField].type === "belongsTo"){
110
112
  if(currentEntity[entityField].lazyLoading){
111
- var priKey = tools.getPrimaryKeyObject(this.__entity);
112
- var currentValue = this.__proto__[`_${entityField}`];
113
- if(currentValue){
114
- // CHECK to see if you got the info already
115
- if(typeof currentValue === 'object'){
116
- currentValue = currentValue[priKey];
117
- }
118
- }
119
- else{
120
- var idValue = currentEntity[entityField].foreignTable;
121
- currentValue = this.__proto__[`_${idValue}`];
122
- }
113
+ // TODO: UPDATE THIS CODE TO USE SOMETHING ELSE - THIS WILL NOT WORK WHEN USING DIFFERENT DATABASES BECAUSE THIS IS USING SQLITE CODE.
114
+
115
+ var priKey = tools.getPrimaryKeyObject(ent.__entity);
123
116
 
124
- //var modelValue = ent.raw(`select * from ${ent.__entity.__name} where ${priKey} = ${ currentValue }`).single();
125
- var modelValue = ent.where(`r => r. ${priKey} == ${ currentValue }`)
117
+ var idValue = currentEntity[entityField].foreignKey;
118
+ var currentValue = this.__proto__[`_${idValue}`];
119
+ var modelValue = ent.where(`r => r.${priKey} == ${ currentValue }`).single();
126
120
  this[entityField] = modelValue;
127
121
  }
128
122
  else{
package/Masterrecord.js CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  // https://github.com/kriasoft/node-sqlite
3
3
  // https://www.learnentityframeworkcore.com/dbset/deleting-data
4
- // version 1.0.19
4
+ // version 1.0.20
5
5
  var context = require("./context");
6
6
  var schema = require("./Migrations/schema");
7
7
 
package/Migrations/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- // version 0.0.5
3
+ // version 0.0.7
4
4
  // https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/
5
5
  // how to add environment variables on cli call example - master=development masterrecord add-migration auth authContext
6
6
 
@@ -16,7 +16,7 @@ const [,, ...args] = process.argv
16
16
 
17
17
  program
18
18
  .version('0.0.2')
19
- .option('-v, --version', '0.0.33')
19
+ .option('-v, --version', '0.0.34')
20
20
  .description('A ORM framework that facilitates the creation and use of business objects whose data requires persistent storage to a database');
21
21
 
22
22
  // Instructions : to run command you must go to main project folder is located and run the command using the context file name.
@@ -46,7 +46,7 @@ program
46
46
  });
47
47
 
48
48
  // Instructions : to run command you must go to folder where migration file is located.
49
- program
49
+ program //// TODO ---- WHY MIGRATION IS CREATING ADD COLUMSN AS WELL AS NEW TABLE
50
50
  .command('add-migration <name> <contextFileName>')
51
51
  .alias('am')
52
52
  .action(function(name, contextFileName){
@@ -62,7 +62,7 @@ program
62
62
  var contextSnapshot = require(files[0]);
63
63
  var context = require(contextSnapshot.contextLocation);
64
64
  var contextInstance = new context();
65
- var newEntity = migration.template(name, contextSnapshot.schema, contextInstance .__entities);
65
+ var newEntity = migration.template(name, contextSnapshot.schema, contextInstance.__entities);
66
66
  var migrationDate = Date.now();
67
67
  var file = `${contextSnapshot.migrationFolder}/${migrationDate}_${name}_migration.js`
68
68
  fs.writeFile(file, newEntity, 'utf8', function (err) {
@@ -103,13 +103,12 @@ program
103
103
  });
104
104
 
105
105
  var mFile = mFiles[0];
106
- var migrationFile = require(mFile);
106
+ var migrationProjectFile = require(mFile);
107
107
  var context = require(contextSnapshot.contextLocation);
108
108
  var contextInstance = new context();
109
- var newMigrationInstance = new migrationFile(context);
110
-
111
- var tableObj = migration.up(contextSnapshot.schema, contextInstance.__entities);
112
- newMigrationInstance.up(tableObj);
109
+ var newMigrationProjectInstance = new migrationProjectFile(context);
110
+ var tableObj = migration.buildUpObject(contextSnapshot.schema, contextInstance.__entities);
111
+ newMigrationProjectInstance.up(tableObj);
113
112
 
114
113
  var snap = {
115
114
  file : contextSnapshot.contextLocation,
@@ -1,5 +1,5 @@
1
1
 
2
- // verison 0.0.3
2
+ // verison 0.0.5
3
3
  class migrationSQLiteQuery {
4
4
 
5
5
  #tempTableName = "_temp_alter_column_update"
@@ -60,12 +60,12 @@ class migrationSQLiteQuery {
60
60
 
61
61
  alterColumn(fullTable, table){
62
62
  if(table){
63
- table.newName = this.tempTableName;
63
+ table.newName = this.#tempTableName;
64
64
  return {
65
65
  1 : this.renameTable(table),
66
- 2 : this.createTable(table.tableName, fullTable),
66
+ 2 : this.createTable(fullTable),
67
67
  3 : this.insertInto(table.tableName, fullTable),
68
- 4 : this.dropTable(this.tempTableName)
68
+ 4 : this.dropTable(this.#tempTableName)
69
69
  }
70
70
  }
71
71
  else{
@@ -100,7 +100,7 @@ class migrationSQLiteQuery {
100
100
 
101
101
  insertInto(name, table){
102
102
  return `INSERT INTO ${name} (${this.#getTableColumns(table)})
103
- SELECT ${this.#getTableColumns(table)} FROM ${this.tempTableName}`;
103
+ SELECT ${this.#getTableColumns(table)} FROM ${this.#tempTableName}`;
104
104
  }
105
105
 
106
106
  createTable(table){
@@ -110,21 +110,21 @@ class migrationSQLiteQuery {
110
110
  queryVar += `${this.#columnMapping(table[key])}, `;
111
111
  }
112
112
  }
113
-
113
+
114
114
  return `CREATE TABLE ${table.__name} (${queryVar.replace(/,\s*$/, "")});`;
115
115
 
116
116
  /*
117
- INTEGER PRIMARY KEY AUTOINCREMENT
118
- all these are equal to interger
119
- INT
120
- INTEGER
121
- TINYINT
122
- SMALLINT
123
- MEDIUMINT
124
- BIGINT
125
- UNSIGNED BIG INT
126
- INT2
127
- INT8
117
+ INTEGER PRIMARY KEY AUTOINCREMENT
118
+ all these are equal to interger
119
+ INT
120
+ INTEGER
121
+ TINYINT
122
+ SMALLINT
123
+ MEDIUMINT
124
+ BIGINT
125
+ UNSIGNED BIG INT
126
+ INT2
127
+ INT8
128
128
  */
129
129
  }
130
130
 
@@ -1,4 +1,4 @@
1
- // version 0.0.5
1
+ // version 0.0.7
2
2
  // learn more about seeding info - https://www.pauric.blog/Database-Updates-and-Migrations-with-Entity-Framework/
3
3
 
4
4
  var fs = require('fs');
@@ -186,22 +186,22 @@ class Migrations{
186
186
  }
187
187
 
188
188
  //
189
- up(oldSchema, newSchema){
189
+ buildUpObject(oldSchema, newSchema){
190
190
  var tableObj = {}
191
191
  var tables = this.#buildMigrationObject(oldSchema, newSchema);
192
192
  tables.forEach(function (item, index) {
193
193
  // add new columns for table
194
194
  var columnInfo = tables[index];
195
195
 
196
+ item.newTables.forEach(function (column, ind) {
197
+ tableObj[item.name] = columnInfo.new;
198
+ });
199
+
196
200
  item.newColumns.forEach(function (column, ind) {
197
201
  columnInfo.new[column].tableName = item.name;
198
202
  tableObj[column] = columnInfo.new[column];
199
203
  });
200
204
 
201
- item.newTables.forEach(function (column, ind) {
202
- tableObj[item.name] = columnInfo.new;
203
- });
204
-
205
205
  item.deletedColumns.forEach(function (column, ind) {
206
206
  columnInfo.old[column].tableName = item.name;
207
207
  tableObj[column] = columnInfo.old[column];
@@ -218,24 +218,35 @@ class Migrations{
218
218
 
219
219
  tableObj.___table = item;
220
220
  });
221
- return tableObj;
221
+ return tableObj;
222
222
  }
223
223
 
224
224
  template(name, oldSchema, newSchema){
225
+
225
226
  var MT = new MigrationTemplate(name);
226
227
  var tables = this.#buildMigrationObject(oldSchema, newSchema);
227
228
  tables.forEach(function (item, index) {
228
- // add new columns for table
229
- item.newColumns.forEach(function (column, index) {
230
- MT.addColumn("up", column, item.name);
231
- MT.dropColumn("down", column, item.name);
232
- });
229
+ if(item.old === null){
230
+ MT.createTable("up", column, item.name);
231
+ MT.dropTable("down", column, item.name);
232
+ }
233
+
234
+ if(item.new === null){
235
+ MT.dropTable("up", column, item.name);
236
+ MT.createTable("down", column, item.name);
237
+ }
233
238
 
234
239
  item.newTables.forEach(function (column, ind) {
235
240
  MT.createTable("up", item.name);
236
241
  MT.dropTable("down", item.name);
237
242
  });
238
243
 
244
+ // add new columns for table
245
+ item.newColumns.forEach(function (column, index) {
246
+ MT.addColumn("up", column, item.name);
247
+ MT.dropColumn("down", column, item.name);
248
+ });
249
+
239
250
  item.deletedColumns.forEach(function (column, index) {
240
251
  MT.dropColumn("up", column, item.name);
241
252
  MT.addColumn("down",column, item.name);
@@ -249,16 +260,6 @@ class Migrations{
249
260
  }
250
261
  });
251
262
 
252
- if(item.old === null){
253
- MT.createTable("up", column, item.name);
254
- MT.dropTable("down", column, item.name);
255
-
256
- }
257
- if(item.new === null){
258
- MT.dropTable("up", column, item.name);
259
- MT.createTable("down", column, item.name);
260
- }
261
-
262
263
  });
263
264
 
264
265
  return MT.get();
@@ -1,6 +1,5 @@
1
- // ALL THIS SHOULD DO IS BUILD A SQL QUERY
2
- // version 1.0.222
3
- // TODO: change name of queryManager to select manager;
1
+
2
+ // version 0.0.7
4
3
  var entityTrackerModel = require('masterrecord/Entity/entityTrackerModel');
5
4
  var tools = require('masterrecord/Tools');
6
5
  var queryScript = require('masterrecord/QueryLanguage/queryScript');
@@ -45,12 +44,91 @@ class queryMethods{
45
44
  this.__queryObject.reset();
46
45
  }
47
46
 
48
- raw(query){
49
47
 
48
+ // do join on two tables = inner join
49
+ join(){
50
+
51
+ }
52
+
53
+ groupBy(){
54
+
55
+ }
56
+
57
+ contains(){
58
+ // https://entityframework.net/knowledge-base/3491721/linq-to-entities---where-in-clause-in-query
59
+ }
60
+
61
+ // do join on two tables = inner join
62
+ _____leftJoin(){
63
+
64
+ }
65
+
66
+ ______orderByCount(query, ...args){
67
+ var str = query.toString();
68
+ if(args){
69
+ for(let argument in args){
70
+ var item = args[argument];
71
+ str = str.replace("$$", item);
72
+ }
73
+ }
74
+ this.__queryObject.orderByCount(str, this.__entity.__name);
75
+ return this;
76
+ }
77
+
78
+ ______orderByCountDescending(query, ...args){
79
+ var str = query.toString();
80
+ if(args){
81
+ for(let argument in args){
82
+ var item = args[argument];
83
+ str = str.replace("$$", item);
84
+ }
85
+ }
86
+ this.__queryObject.orderByCountDesc(str, this.__entity.__name);
87
+ return this;
88
+ }
89
+
90
+ orderBy(query, ...args){
91
+ var str = query.toString();
92
+ if(args){
93
+ for(let argument in args){
94
+ var item = args[argument];
95
+ str = str.replace("$$", item);
96
+ }
97
+ }
98
+ this.__queryObject.orderBy(str, this.__entity.__name);
99
+ return this;
100
+ }
101
+
102
+ orderByDescending(query, ...args){
103
+ var str = query.toString();
104
+ if(args){
105
+ for(let argument in args){
106
+ var item = args[argument];
107
+ str = str.replace("$$", item);
108
+ }
109
+ }
110
+ this.__queryObject.orderByDesc(str, this.__entity.__name);
111
+ return this;
112
+ }
113
+
114
+ raw(query){
50
115
  this.__queryObject.raw(query);
51
116
  return this;
52
117
  }
53
118
 
119
+ /* WHERE and AND work together its a way to add to the WHERE CLAUSE DYNAMICALLY */
120
+ and(query, ...args){
121
+ var str = query.toString();
122
+ if(args){
123
+ for(let argument in args){
124
+ var item = args[argument];
125
+ str = str.replace("$$", item);
126
+ }
127
+ }
128
+ this.__queryObject.and(str, this.__entity.__name);
129
+ return this;
130
+ }
131
+
54
132
  where(query, ...args){
55
133
  var str = query.toString();
56
134
  if(args){
@@ -90,65 +168,68 @@ class queryMethods{
90
168
  return this;
91
169
  }
92
170
 
93
-
94
-
95
- // do join on two tables = inner join
96
- join(){
97
-
98
- }
99
-
100
- skip(){
101
-
102
- }
103
-
104
- limit(){
105
-
171
+ take(number){
172
+ this.__queryObject.script.take = number;
173
+ return this;
106
174
  }
107
175
 
108
- oderBy(){
109
-
176
+ skip(number){
177
+ this.__queryObject.script.skip = number;
178
+ return this;
110
179
  }
111
180
 
112
- groupBy(){
113
-
114
- }
181
+
182
+ // ------------------------------- FUNCTIONS THAT MAKE THE SQL CALL START FROM HERE ON -----------------------------------------------------
183
+ // ---------------------------------------------------------------------------------------------------------------------------------------
115
184
 
116
- contains(){
117
- // https://entityframework.net/knowledge-base/3491721/linq-to-entities---where-in-clause-in-query
118
- }
185
+ count(query, ...args){
186
+ if(query){
187
+ var str = query.toString();
188
+ if(args){
189
+ for(let argument in args){
190
+ var item = args[argument];
191
+ str = str.replace("$$", item);
192
+ }
193
+ }
194
+ this.__queryObject.count(str, this.__entity.__name);
195
+ }
119
196
 
120
- count(){
121
- // trying to match string select and relace with select Count(*);
122
- var entityValue = this.__context._SQLEngine.getCount(this.__queryObject, this.__entity, this.__context);
123
- var val = entityValue[Object.keys(entityValue)[0]];
124
- this.__reset();
125
- return val;
197
+ if(this.__context.isSQLite){
198
+ // trying to match string select and relace with select Count(*);
199
+ var entityValue = this.__context._SQLEngine.getCount(this.__queryObject, this.__entity, this.__context);
200
+ var val = entityValue[Object.keys(entityValue)[0]];
201
+ this.__reset();
202
+ return val;
203
+ }
126
204
  }
127
205
 
128
206
  single(){
129
- var entityValue = this.__context._SQLEngine.get(this.__queryObject.script, this.__entity, this.__context);
130
- var sing = this.__singleEntityBuilder(entityValue, this._queryBuilder);
131
- this.__reset();
132
- return sing;
207
+ if(this.__context.isSQLite){
208
+ var entityValue = this.__context._SQLEngine.get(this.__queryObject.script, this.__entity, this.__context);
209
+ var sing = this.__singleEntityBuilder(entityValue, this._queryBuilder);
210
+ this.__reset();
211
+ return sing;
212
+ }
133
213
  }
134
214
 
135
215
  toList(){
136
- var entityValue = this.__context._SQLEngine.all(this.__queryObject.script, this.__entity, this.__context);
137
- var toLi = this.__multipleEntityBuilder(entityValue, this._queryBuilder);
138
- this.__reset();
139
- return toLi;
140
- }
141
-
142
- asQueryable(){
143
- // returns the sql created and does not make a call to DB
216
+ if(this.__context.isSQLite){
217
+ var entityValue = this.__context._SQLEngine.all(this.__queryObject.script, this.__entity, this.__context);
218
+ var toLi = this.__multipleEntityBuilder(entityValue, this._queryBuilder);
219
+ this.__reset();
220
+ return toLi;
221
+ }
144
222
  }
145
223
 
224
+ // ------------------------------- FUNCTIONS THAT UPDATE SQL START FROM HERE -----------------------------------------------------
225
+ // ---------------------------------------------------------------------------------------------------------------------------------------
146
226
  add(entityValue){
147
227
  // This will call context API to REMOVE entity to update list
148
228
  tools.clearAllProto(entityValue);
149
229
  entityValue.__state = "insert";
150
230
  entityValue.__entity = this.__entity;
151
231
  entityValue.__context = this.__context;
232
+ entityValue.__name = this.__entity.__name;
152
233
  this.__context.__track(entityValue);
153
234
  }
154
235
 
@@ -158,6 +239,14 @@ class queryMethods{
158
239
  entityValue.__context = this.__context;
159
240
  }
160
241
 
242
+ removeRange(entityValues){
243
+ for (const property in entityValues) {
244
+ entityValues[property].__state = "delete";
245
+ entityValues[property].__entity = this.__entity;
246
+ entityValues[property].__context = this.__context;
247
+ }
248
+ }
249
+
161
250
  track(entityValue){
162
251
  entityValue.__state = "track";
163
252
  tools.clearAllProto(entityValue);
@@ -1,4 +1,4 @@
1
- // version 1.0.1
1
+ // version 0.0.2
2
2
 
3
3
  const LOG_OPERATORS_REGEX = /(\|\|)|(&&)/;
4
4
  var tools = require('../Tools');
@@ -10,10 +10,15 @@ class queryScript{
10
10
  script = {
11
11
  select : false,
12
12
  where: false,
13
+ and : [],
13
14
  include : [],
14
15
  raw: false,
15
16
  entity : "",
16
- entityMap : []
17
+ entityMap : [],
18
+ take : 0,
19
+ skip: 0,
20
+ orderBy : false,
21
+ orderByDesc : false
17
22
  };
18
23
 
19
24
 
@@ -21,10 +26,15 @@ class queryScript{
21
26
  this.script = {
22
27
  select : false,
23
28
  where: false,
29
+ and : [],
24
30
  include : [],
25
31
  raw: false,
26
32
  entity : "",
27
- entityMap : []
33
+ entityMap : [],
34
+ take : 0,
35
+ skip: 0,
36
+ orderBy : false,
37
+ orderByDesc : false
28
38
  };
29
39
  }
30
40
 
@@ -37,7 +47,23 @@ class queryScript{
37
47
  return this.script;
38
48
  }
39
49
 
50
+ orderBy(text, entityName){
51
+ this.buildScript(text, "orderBy", this.script, entityName);
52
+ return this.script;
53
+ }
54
+
55
+ orderByDesc(text, entityName){
56
+ this.buildScript(text, "orderByDesc", this.script, entityName);
57
+ return this.script;
58
+ }
59
+
60
+ and(text, entityName){
61
+ this.buildScript(text, "and", this.script, entityName);
62
+ return this.script;
63
+ }
64
+
40
65
  where(text, entityName){
66
+
41
67
  this.buildScript(text, "where", this.script, entityName);
42
68
  return this.script;
43
69
  }
@@ -52,13 +78,9 @@ class queryScript{
52
78
  return this.script;
53
79
  }
54
80
 
55
- count(queryString){
56
- var matched = queryString.match(/(?<=select)(.*?)from/gmi );
57
- matched[0] = matched[0].replace("from", "");
58
- var cleanQuery = queryString.replace(/^(.*?)from/gmi, "");
59
- var str = `Select Count(${matched[0]}) from ${cleanQuery}`
60
-
61
- return str;
81
+ count(text, entityName){
82
+ this.buildScript(text, "count", this.script, entityName);
83
+ return this.script;
62
84
  }
63
85
 
64
86
  buildScript(text, type, obj, entityName){
@@ -96,7 +118,7 @@ class queryScript{
96
118
  }
97
119
 
98
120
  this.describeExpressionParts(cachedExpr[entityName], cachedExpr.selectFields[0], obj.entityMap);
99
- if(type === "include"){
121
+ if(type === "include" || type === "and"){
100
122
  obj[type].push(cachedExpr)
101
123
  }
102
124
  else{
@@ -130,8 +152,8 @@ class queryScript{
130
152
  if (!fields.includes(field)) fields.push(field);
131
153
  });
132
154
 
133
- desc.expr = exprStr.trim()
134
- desc.selectFields = fields
155
+ desc.expr = exprStr.trim();
156
+ desc.selectFields = fields;
135
157
  return desc;
136
158
  }
137
159
  else{
@@ -0,0 +1,66 @@
1
+
2
+ // // version 0.0.2
3
+
4
+ // class selectManager{
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 = selectManager;
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
+ */