masterrecord 0.0.24 → 0.0.25

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/Masterrecord.js CHANGED
@@ -3,102 +3,123 @@
3
3
  // https://www.learnentityframeworkcore.com/dbset/deleting-data
4
4
  // version 1.0.14
5
5
 
6
- var modelBuilder = require('masterrecord/Entity/EntityModelBuilder');
7
- var query = require('masterrecord/QueryLanguage/queryBuilder');
6
+ var modelBuilder = require('./Entity/EntityModelBuilder');
7
+ var query = require('masterrecord/QueryLanguage/queryMethods');
8
8
  var tools = require('./Tools');
9
- var SQLiteEngine = require('./SQLiteEngine');
9
+ var SQLLiteEngine = require('masterrecord/SQLLiteEngine');
10
+ var MYSQLEngine = require('masterrecord/MYSQLEngine');
11
+ var insertManager = require('./InsertManager');
12
+ var deleteManager = require('./DeleteManager');
10
13
 
11
14
  class Context {
12
-
13
15
  _isModelValid = {
14
16
  isValid: true,
15
17
  errors: []
16
- }
17
- __allContexts = [];
18
+ };
19
+ __entities = [];
20
+ __builderEntities = [];
18
21
  __trackedEntities = [];
19
- __relationshipModels = []
22
+ __relationshipModels = [];
20
23
 
21
24
  constructor(){
22
- this._SQLEngine = null;
25
+ // TODO when we build the sql engine it depends on the which type.
26
+
27
+ this._SQLEngine = "";
28
+ this.__name = this.constructor.name;
23
29
  }
24
-
25
- // IMPORTANT: SQLITE has no date so use text
26
- // MUST MAP TYPES TO INTERGER, TEXT, BLOB, REAL, NUMERIC
27
- setup(rootFolderLocation, env){
28
- /*
29
- expected model {
30
- "type": "better-sqlite3",
31
- "connection" : "/db/",
32
- "password": "",
33
- "username": ""
34
- }
35
-
36
- */
37
- if(env.type !== undefined){
38
- switch(env.type) {
39
- case "better-sqlite3":
40
- // set the engine for everyone to use
41
- this._SQLEngine = new new SQLiteEngine();
42
- env.connection = rootFolderLocation + env.connection;
43
- this.db = this._SQLiteEngine.setDB(env, env.type);
44
- return this;
45
- break;
46
- }
30
+
31
+ /*
32
+ SQLite expected model
33
+ {
34
+ "type": "better-sqlite3",
35
+ "connection" : "/db/",
36
+ "password": "",
37
+ "username": ""
38
+ }
39
+ */
40
+ __SQLiteInit(env, sqlName){
41
+ try{
42
+ const sqlite3 = require(sqlName);
43
+ let DBAddress = `${env.completeConnection}${env.env}.sqlite3`;
44
+ var db = new sqlite3(DBAddress, env);
45
+ db.__name = sqlName;
46
+ this._SQLEngine = new SQLLiteEngine();
47
+ return db;
48
+ }
49
+ catch (e) {
50
+ console.log("error SQL", e);
47
51
  }
48
52
  }
49
53
 
50
- returnCopywithoutPrimaryKeyAndVirtual(currentModel){
51
- var newCurrentModel = Object.create(currentModel);
52
- for(var entity in newCurrentModel.__entity) {
53
- var currentEntity = newCurrentModel.__entity[entity];
54
- if (newCurrentModel.__entity.hasOwnProperty(entity)) {
55
- if(currentEntity.primary === true){
56
- newCurrentModel[`__primaryKey`] = newCurrentModel[entity];
57
- delete newCurrentModel[`_${entity}`];
58
- }
59
- }
60
- if(currentEntity.virtual === true){
61
- // skip it from the insert
62
- delete newCurrentModel[`_${entity}`];
63
- }
54
+ /*
55
+ mysql expected model
56
+ {
57
+ "type": "mysql",
58
+ host : 'localhost',
59
+ user : 'me',
60
+ password : 'secret',
61
+ database : 'my_db'
62
+ }
63
+ */
64
+ __mysqlInit(env, sqlName){
65
+ try{
66
+ const mysql = require(sqlName);
67
+ const connection = mysql.createConnection(env);
68
+ connection.connect();
69
+ db.__name = sqlName;
70
+ this._SQLEngine = new MYSQLEngine();
71
+ return connection;
64
72
 
65
73
  }
66
- return newCurrentModel;
74
+ catch (e) {
75
+ console.log("error SQL", e);
76
+ }
67
77
  }
68
78
 
69
- // loop through all the enitities and check if required
70
- validateEntity(currentModel){
71
- for(var entity in currentModel.__entity) {
72
- var currentEntity = currentModel.__entity[entity];
73
- if (currentModel.__entity.hasOwnProperty(entity)) {
74
- // TODO: // check if types are correct
75
- if(currentEntity.default){
76
- if(!currentModel[entity]){
77
- currentModel[entity] = currentEntity.default;
78
- }
79
- }
80
-
81
- if(currentEntity.required === true){
82
- if(!currentModel[entity]){
83
- this._isModelValid.isValid = false;
84
- this._isModelValid.errors.push( `Entity ${entity} is a required Field`);
85
- console.log(`Entity ${entity} is a required Field`);
86
- }
79
+
80
+
81
+ __clearErrorHandler(){
82
+ this._isModelValid = {
83
+ isValid: true,
84
+ errors: []
85
+ };
86
+ };
87
+
88
+ setup(options, rootFolderLocation){
89
+
90
+ if(options !== undefined){
91
+ if(options.type !== undefined){
92
+ switch(options.type) {
93
+ case "better-sqlite3":
94
+ options.completeConnection = rootFolderLocation + options.connection;
95
+ this.db = this.__SQLiteInit(options, options.type);
96
+ this._SQLEngine.setDB(this.db, "better-sqlite3");
97
+ return this;
98
+ break;
99
+ case "mysql":
100
+ this.db = this.__mysqlInit(options, options.type);
101
+ this._SQLEngine.setDB(this.db, "mysql");
102
+ return this;
103
+ break;
87
104
  }
88
105
  }
89
-
106
+ else{
107
+ console.log("database type not defined - Master Record");
108
+ }
90
109
  }
110
+ else{
111
+ console.log("database information not added - Master Record");
112
+ }
113
+
91
114
  }
92
115
 
93
- dbset(model){
94
- var validModel = modelBuilder.init(model);
95
- validModel.__name = model.name;
96
- this.__allContexts.push(validModel);
97
- this.createNewInstance(validModel);
98
- }
99
-
100
- createNewInstance(validModel){
101
- this[validModel.__name] = new query(validModel, this);
116
+ dbset(model, name){
117
+ var validModel = modelBuilder.create(model);
118
+ validModel.__name = name === undefined ? model.name : name;
119
+ this.__entities.push(validModel); // model object
120
+ var buildMod = tools.createNewInstance(validModel, query, this);
121
+ this.__builderEntities.push(buildMod); // query builder entites
122
+ this[validModel.__name] = buildMod;
102
123
  }
103
124
 
104
125
  modelState(){
@@ -106,59 +127,63 @@ class Context {
106
127
  }
107
128
 
108
129
  saveChanges(){
109
-
110
- for (var model in this.__trackedEntities) {
111
- var currentModel = this.__trackedEntities[model];
112
- // validate required fields
113
- this.validateEntity(currentModel);
114
- if(this._isModelValid.valid === false){
115
- // everything great
116
- console.log(JSON.stringify(this._isModelValid.valid.errors));
117
- }
118
-
119
- try{
120
- switch(currentModel.__state) {
121
- case "modified":
122
- if(currentModel.__dirtyFields.length <= 0){
123
- throw "Tracked entity modified with no values being changed";
124
- }
125
- var cleanCurrentModel = this.returnCopywithoutPrimaryKeyAndVirtual(currentModel);
126
- // build columns equal to value string
127
- var argu = tools.buildSQLEqualTo(cleanCurrentModel);
128
- var primaryKey = tools.getPrimaryKeyObject(cleanCurrentModel.__entity);
129
- var sqlUpdate = {tableName: cleanCurrentModel.__entity.__name, arg: argu, primaryKey : primaryKey, value : cleanCurrentModel[`__primaryKey`] };
130
- this._SQLEngine.update(sqlUpdate);
131
- // code block
132
- break;
133
- case "insert":
134
-
135
- var cleanCurrentModel = this.returnCopywithoutPrimaryKeyAndVirtual(currentModel);
136
- var insertObj = tools.getInsertObj(cleanCurrentModel);
137
- var sqlUpdate = {tableName: cleanCurrentModel.__entity.__name, columns: insertObj.columns, values: insertObj.values };
138
- this._SQLEngine.insert(sqlUpdate);
139
- break;
140
- case "delete":
141
- var primaryKey = tools.getPrimaryKeyObject(currentModel.__entity);
142
- var sqlUpdate = {tableName: currentModel.__entity.__name, primaryKey : primaryKey, value : currentModel[primaryKey] };
143
- this._SQLEngine.delete(sqlUpdate);
144
- break;
145
- }
130
+ try{
131
+ var tracked = this.__trackedEntities;
132
+ // start transaction
133
+ this._SQLEngine.startTransaction();
134
+ for (var model in tracked) {
135
+ var currentModel = tracked[model];
136
+ switch(currentModel.__state) {
137
+ case "insert":
138
+ var insert = new insertManager(this._SQLEngine, this._isModelValid, this.__entities);
139
+ insert.init(currentModel);
140
+
141
+ break;
142
+ case "modified":
143
+ if(currentModel.__dirtyFields.length > 0){
144
+ var cleanCurrentModel = tools.removePrimarykeyandVirtual(currentModel, currentModel._entity);
145
+ // build columns equal to value string
146
+ var argu = this._SQLEngine._buildSQLEqualTo(cleanCurrentModel);
147
+ var primaryKey = tools.getPrimaryKeyObject(cleanCurrentModel.__entity);
148
+ var sqlUpdate = {tableName: cleanCurrentModel.__entity.__name, arg: argu, primaryKey : primaryKey, primaryKeyValue : cleanCurrentModel[primaryKey] };
149
+ this._SQLEngine.update(sqlUpdate);
150
+ }
151
+ else{
152
+ console.log("Tracked entity modified with no values being changed");
153
+ }
154
+
155
+ // code block
156
+ break;
157
+ case "delete":
158
+ var deleteObject = new deleteManager(this._SQLEngine, this.__entities);
159
+ deleteObject.init(currentModel);
160
+
161
+ break;
162
+ }
146
163
  }
147
- catch(error){
148
- this.__trackedEntities = [];
149
- console.log("error", error);
150
- }
164
+ this.__clearErrorHandler();
165
+ this._SQLEngine.endTransaction();
151
166
  }
152
- this.__trackedEntities = [];
167
+
168
+ catch(error){
169
+ this.__clearErrorHandler();
170
+ this._SQLEngine.errorTransaction();
171
+ console.log("error", error);
172
+ this.__clearTracked();
173
+ throw error;
174
+ }
175
+
176
+ this.__clearTracked();
153
177
  return true;
154
178
  }
155
179
 
156
- __Track(model){
180
+ // TODO: WHY WE HAVE DOUBLE TRACKED OBJECTS - LOOP THROUGH ALL TRACKED OBJECTS
181
+ __track(model){
157
182
  this.__trackedEntities.push(model);
158
183
  return model;
159
184
  }
160
185
 
161
- __FindTracked(id){
186
+ __findTracked(id){
162
187
  if(id){
163
188
  for (var model in this.__trackedEntities) {
164
189
  if(this.__trackedEntities[model].__ID === id){
@@ -168,7 +193,41 @@ class Context {
168
193
  }
169
194
  return null;
170
195
  }
196
+
197
+ __clearTracked(){
198
+ this.__trackedEntities = [];
199
+ }
171
200
  }
172
201
 
173
202
 
174
- module.exports = Context;
203
+ module.exports = Context;
204
+
205
+ /*
206
+
207
+ //Create new standard
208
+ var standard = new Standard();
209
+ standard.StandardName = "Standard1";
210
+
211
+ //create three new teachers
212
+ var teacher1 = new Teacher();
213
+ teacher1.TeacherName = "New Teacher1";
214
+
215
+ var teacher2 = new Teacher();
216
+ teacher2.TeacherName = "New Teacher2";
217
+
218
+ var teacher3 = new Teacher();
219
+ teacher3.TeacherName = "New Teacher3";
220
+
221
+ //add teachers for new standard
222
+ standard.Teachers.Add(teacher1);
223
+ standard.Teachers.Add(teacher2);
224
+ standard.Teachers.Add(teacher3);
225
+
226
+ using (var dbCtx = new SchoolDBEntities())
227
+ {
228
+ //add standard entity into standards entitySet
229
+ dbCtx.Standards.Add(standard);
230
+ //Save whole entity graph to the database
231
+ dbCtx.SaveChanges();
232
+ }
233
+ */
package/Migrations/cli.js CHANGED
@@ -12,7 +12,7 @@ program
12
12
  .option('-v, --version', '0.0.1')
13
13
  .description('A ORM framework that facilitates the creation and use of business objects whose data requires persistent storage to a database');
14
14
 
15
- // Instructions : to run command you must go to folder where contaxt file is located and run command with context file name.
15
+ // Instructions : to run command you must go to folder where context file is located and run command with context file name.
16
16
  program
17
17
  .command('enable-migrations <contextFileName>')
18
18
  .alias('am')
@@ -41,6 +41,7 @@ program
41
41
  }
42
42
  });
43
43
 
44
+ // Instructions : to run command you must go to folder where migration file is located.
44
45
  program
45
46
  .command('add-migration <name>')
46
47
  .alias('am')
@@ -52,7 +53,7 @@ program
52
53
  var migration = new Migration();
53
54
  var context = require(contextSnapshot.contextLocation);
54
55
  var newEntity = migration.EDMModelDiffer(contextSnapshot.schema, context);
55
- if(newEntity !== -1){
56
+ if(newEntity.length > 0){
56
57
  var migrationDate = Date.now();
57
58
  migration.migrationCodeGenerator(name, newEntity, migrationDate);
58
59
  console.log(`migration ${name}_${migrationDate} created`);
@@ -3,36 +3,55 @@ var fs = require('fs');
3
3
  // https://blog.tekspace.io/code-first-multiple-db-context-migration/
4
4
 
5
5
  // node masterrecord add-migration josh C:\Users\rbatista\Downloads\kollege\freshmen\app\models\context
6
+ class Migrations{
6
7
 
8
+ EDMModelDiffer(snapShots, tableList){
9
+ new schemaList = []
10
+ const tableKeyList = Object.keys(tableList);
11
+ // loop through tables
12
+ for (const tableKey of tableKeyList) {
13
+ // check if table has already been migrated
14
+ var tableSnapShot = snapShots[tableKey];
15
+ if(tableSnapShot){
16
+ // check if we have the column in the sceama
17
+ const columnKeyList = Object.keys(tableKey);
18
+ // loop through tables
19
+ for (const columnKey of columnKeyList) {
20
+ var newTable = {};
21
+ if(tableSnapShot[columnKey]){
7
22
 
23
+ /*
24
+ id : {
25
+ nullabale : true,
26
+ type : string
27
+ }
8
28
 
9
- /*
10
- SQLITE CREATE MIGRATIONS
11
- CREATE TABLE "user" (
12
- "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
13
- "auth_id" INTEGER NOT NULL,
14
- "view_counter" INTEGER NOT NULL DEFAULT 0,
15
- "background_image_id" INTEGER NOT NULL,
16
- "profile_image_id" INTEGER NOT NULL,
17
- "full_name" TEXT NOT NULL,
18
- "languages" TEXT,
19
- "profile_email" TEXT,
20
- "phone_number" TEXT,
21
- "birthmonth" TEXT,
22
- "birthday" TEXT,
23
- "created_at" TEXT,
24
- "updated_at" TEXT
25
- );
26
-
27
- */
28
-
29
- class Migrations{
30
- // SQL Lite doesnt have a date so please use TEXT fields;
31
-
32
- EDMModelDiffer(snapShot, contextModel){
29
+ id : {
30
+ nullabale : true,
31
+ type : int,
32
+ unique : true
33
+ }
34
+
35
+ */
36
+ // if we then check if we have all the same types
37
+ // loop through schema list
38
+ // check if we have the same in the context
39
+ // if the same then check if if value is diffrent
40
+ // then delete from context
41
+ // after loop complete wahtever left in conext just push to object
42
+ }
43
+ else{
44
+ // if we dont have it then add it to the
45
+ newTable[columnKey] = tableKey[columnKey];
46
+ schemaList.push(newTable);
47
+ }
48
+ }
49
+ }else{
50
+ schemaList.push(contextKeys[key]);
51
+ }
33
52
 
34
- // do a diff and return only diff fields
35
- // if table doesnt exist then add a create database object.
53
+
54
+ }
36
55
  }
37
56
 
38
57
  migrationCodeGenerator(name, column, migrationDate){
@@ -0,0 +1,66 @@
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
+ */