masterrecord 0.0.23 → 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.
@@ -0,0 +1,51 @@
1
+ var tools = require('./Tools');
2
+
3
+ class DeleteManager{
4
+ constructor(sqlEngine, entities){
5
+ this._SQLEngine = sqlEngine;
6
+ this._allEntities = entities;
7
+ }
8
+
9
+ init(currentModel){
10
+ var $that = this;
11
+ try{
12
+ this.cascadeDelete(currentModel);
13
+ }
14
+ catch(error){
15
+ throw error;
16
+ }
17
+ }
18
+
19
+ cascadeDelete(currentModel){
20
+ var $that = this;
21
+ if(!Array.isArray(currentModel)){
22
+ const entityKeys = Object.keys(currentModel.__entity);
23
+ // loop though all entity properties
24
+ for (const property of entityKeys) {
25
+ // cascade delete
26
+ if(currentModel.__entity[property].type === "hasOne" || currentModel.__entity[property].type === "hasMany"){
27
+ $that.cascadeDelete( currentModel[property]);
28
+ }
29
+ }
30
+ this._SQLEngine.delete(currentModel);
31
+ }
32
+ else{
33
+
34
+ for(let i = 0 ; i < currentModel.length; i++) {
35
+ const entityKeys = Object.keys(currentModel[i].__entity);
36
+ // loop though all entity properties
37
+ for (const property of entityKeys) {
38
+ // cascade delete
39
+ if(currentModel[i].__entity[property].type === "hasOne" || currentModel[i].__entity[property].type === "hasMany"){
40
+ $that.cascadeDelete( currentModel[i][property]);
41
+ }
42
+ }
43
+ this._SQLEngine.delete(currentModel[i]);
44
+ }
45
+ }
46
+
47
+
48
+ }
49
+ }
50
+
51
+ module.exports = DeleteManager;
@@ -1,121 +1,193 @@
1
- /*
2
-
3
- :binary
4
- :boolean
5
- :date
6
- :datetime
7
- :decimal
8
- :float
9
- :integer
10
- :bigint
11
- :primary_key
12
- :references
13
- :string
14
- :text
15
- :time
16
- :timestamp
17
-
18
- */
19
-
20
- class EntityModel {
21
-
22
- constructor(){
23
- this.obj = {
24
- type: null,
25
- primary : null,
26
- default : null,
27
- virtual : null,
28
- belongsTo : null,
29
- get : null,
30
- foreignKey : null,
31
- maxLength : null,
32
- nullable : false, // no
33
- unique : null
34
- }
35
- }
36
-
37
- string(){
38
- this.obj.type = "string";
39
- return this;
40
- }
41
-
42
- integer(){
43
- this.obj.type = "integer";
44
- return this;
45
- }
46
-
47
- time(){
48
- this.obj.type = "time";
49
- return this;
50
- }
51
-
52
- maxLength(amount){
53
- this.obj.maxLength = amount;
54
- return this;
55
- }
56
-
57
- // is this obj a primary key
58
- primary(){
59
- this.obj.primary = true;
60
- this.obj.nullable = false;
61
- this.obj.unique = true;
62
- return this;
63
- }
64
-
65
- // sets the default value in the DB
66
- default(value){
67
- this.obj.default = value;
68
- return this;
69
- }
70
-
71
- // get(func){
72
- // this.obj.get = func;
73
- // return this;
74
- // }
75
-
76
- unique(){
77
- this.obj.unique = true; // yes
78
- return this;
79
-
80
- }
81
-
82
- nullable(){
83
- this.obj.nullable = true; // yes
84
- return this;
85
- }
86
-
87
- notNullable(){
88
- this.obj.nullable = false; // no
89
- return this;
90
- }
91
-
92
- virtual(tableName){
93
- this.obj.virtual = true;
94
- if(tableName){
95
- this.obj.hasOne = tableName;
96
- }
97
- return this;
98
- }
99
-
100
- hasMany(name){
101
- this.obj.hasMany = name;
102
- return this;
103
- }
104
-
105
- hasOne(tableName){
106
- this.obj.hasOne = tableName;
107
- return this;
108
- }
109
-
110
- belongsTo(tableName){
111
- this.obj.foreignKey = tableName;
112
- return this
113
- }
114
-
115
- hasForeignKey(tableName){
116
- this.obj.foreignKey = tableName;
117
- return this;
118
- }
119
-
120
- }
1
+ /*
2
+
3
+ :binary
4
+ :boolean
5
+ :date
6
+ :datetime
7
+ :decimal
8
+ :float
9
+ :integer
10
+ :bigint
11
+ :primary_key
12
+ :references
13
+ :string
14
+ :text
15
+ :time
16
+ :timestamp
17
+
18
+ */
19
+
20
+ // version 1.0.16
21
+ class EntityModel {
22
+
23
+ constructor(name){
24
+ this.obj = {
25
+ name: name,
26
+ type: null,
27
+ primary : null,
28
+ default : null,
29
+ virtual : null,
30
+ foreignKey : null,
31
+ maxLength : null,
32
+ nullable : true, // no
33
+ unique : false,
34
+ auto : false,
35
+ cascadeOnDelete : true,
36
+ lazyLoading : true,
37
+ isNavigational : false
38
+
39
+ }
40
+ }
41
+
42
+ string(){
43
+ this.obj.type = "string";
44
+ return this;
45
+ }
46
+
47
+ integer(){
48
+ this.obj.type = "integer";
49
+ return this;
50
+ }
51
+
52
+ time(){
53
+ this.obj.type = "time";
54
+ return this;
55
+ }
56
+
57
+ boolean(){
58
+ this.obj.type = "boolean";
59
+ return this;
60
+ }
61
+
62
+ maxLength(amount){
63
+ this.obj.maxLength = amount;
64
+ return this;
65
+ }
66
+
67
+ // will stop cascade delete which means it will stop not auto delete relationship
68
+ stopCascadeOnDelete(){
69
+ this.obj.cascadeOnDelete = false;
70
+ return this;
71
+ }
72
+
73
+ // is this obj a primary key
74
+ primary(){
75
+ this.obj.primary = true;
76
+ this.obj.nullable = false;
77
+ this.obj.unique = true;
78
+ return this;
79
+ }
80
+
81
+ // allows ablity to get back primaryKey on insert automaticlly return on insert
82
+ auto(){
83
+ this.obj.auto = true;
84
+ return this;
85
+ }
86
+
87
+ // sets the default value in the DB
88
+ default(value){
89
+ this.obj.default = value;
90
+ return this;
91
+ }
92
+
93
+ get(func){
94
+ this.obj.get = func;
95
+ return this;
96
+ }
97
+
98
+ set(func){
99
+ this.obj.set = func;
100
+ return this;
101
+ }
102
+
103
+ unique(){
104
+ this.obj.unique = true; // yes
105
+ return this;
106
+
107
+ }
108
+
109
+ // this means that it can be an empty field
110
+ nullable(){
111
+ this.obj.nullable = true; // yes
112
+ return this;
113
+ }
114
+
115
+ notNullable(){
116
+ this.obj.nullable = false; // no
117
+ return this;
118
+ }
119
+
120
+ //allows you to stop lazy loading because lazy loading is added by default
121
+ lazyLoadingOff(){
122
+ this.obj.lazyLoading = false;
123
+ return this;
124
+ }
125
+
126
+ // allows you to add a virtual object that will skipped from being used as sql objects
127
+ virtual(){
128
+ this.obj.virtual = true;
129
+ return this;
130
+ }
131
+
132
+ hasMany(foreignTable, foreignKey){
133
+ if(foreignKey === undefined){
134
+ foreignKey = `${this.obj.name.toLowerCase()}_id`;
135
+ }
136
+ this.obj.type = "hasMany";
137
+ this.obj.foreignTable = foreignTable;
138
+ this.obj.foreignKey = foreignKey;
139
+ this.obj.isNavigational = true;
140
+ return this;
141
+ }
142
+
143
+ hasOne(foreignTable, foreignKey){
144
+ if(foreignKey === undefined){
145
+ foreignKey = `${this.obj.name.toLowerCase()}_id`;
146
+ }
147
+ this.obj.type = "hasOne";
148
+ this.obj.foreignTable = foreignTable;
149
+ this.obj.foreignKey = foreignKey;
150
+ this.obj.isNavigational = true;
151
+ return this;
152
+ }
153
+
154
+ // will do a inner join with foreignKey
155
+ //hasManyThrough("Tagging", "tag_id") ----- if foreignKey is not provided use the name of the object_id
156
+ hasManyThrough(foreignTable, foreignKey ){
157
+ if(foreignKey === undefined){
158
+ foreignKey = `${this.obj.name.toLowerCase()}_id`;
159
+ }
160
+ this.obj.type = "hasManyThrough";
161
+ this.obj.foreignTable = foreignTable;// if joinKey is undefined then use name of object.
162
+ this.obj.foreignKey = foreignKey; // Foreign Key table
163
+ this.obj.isNavigational = true;
164
+ return this;
165
+ }
166
+
167
+ // will get info
168
+ belongsTo(foreignTable, foreignKey){
169
+
170
+ if(foreignKey === undefined){
171
+ foreignKey = `${foreignTable.toLowerCase()}_id`;
172
+ }
173
+ // will use table name to find forien key
174
+ this.obj.type = "belongsTo";
175
+ this.obj.foreignTable = foreignTable; // this is the table name of the current table if diffrent from the object name
176
+ this.obj.foreignKey = foreignKey; // this is the table name of the joining table
177
+ this.obj.nullable = false; // this means it cannot be null
178
+ return this
179
+ }
180
+
181
+ foreignKey(foreignKey){
182
+ this.obj.foreignKey = foreignKey;
183
+ this.obj.nullable = false;
184
+ return this
185
+ }
186
+
187
+ foreignTable(foreignTable){
188
+ this.obj.foreignTable = foreignTable;
189
+ this.obj.nullable = false;
190
+ return this
191
+ }
192
+ }
121
193
  module.exports = EntityModel;
@@ -1,64 +1,42 @@
1
- var modelDB = require('./EntityModel');
2
-
3
- // creates new instance if entity model and calls inner functions to build out a valid entity
4
- class EntityModelBuilder {
5
-
6
- static init(model){
7
- var mod = new model(); //create new instance of Entity Model
8
- var obj = {};
9
- var methodNamesArray = Object.getOwnPropertyNames( mod.__proto__ );
10
- var constructorIndex = methodNamesArray.indexOf("constructor");
11
- // remove contructor method
12
- if (constructorIndex > -1) {
13
- methodNamesArray.splice(constructorIndex, 1);
14
- }
15
- // loop through all method names in the entity model
16
- for (var i = 0; i < methodNamesArray.length; i++) {
17
- let MDB = new modelDB(); // create a new instance of entity Model class
18
- mod[methodNamesArray[i]](MDB);
19
- this.cleanNull(MDB.obj); // remove objects that are null or undefined
20
- if(Object.keys(MDB.obj).length === 0){
21
- MDB.obj.virtual = true;
22
- }
23
- MDB.obj.name = methodNamesArray[i];
24
- obj[methodNamesArray[i]] = MDB.obj;
25
- }
26
- return obj;
27
- }
28
-
29
- static cleanNull(obj) {
30
- for (var propName in obj) {
31
- if (obj[propName]) {
32
- delete obj[propName];
33
- }
34
- }
35
- }
36
-
37
- static selectColumnQuery(objectLiteral){
38
- var arr = "";
39
- for(var keys = Object.keys(model), i = 0, end = keys.length; i < end; i++) {
40
- var key = keys[i], value = model[key];
41
- if(value.virtual === undefined){
42
- arr += key + ",";
43
- }
44
- };
45
- let cleaned = arr.substring(0, arr.length - 1);
46
- return cleaned;
47
- }
48
-
49
- // who calls this? What does this do?
50
- static callAllGets(data, model){
51
- console.log("callAllGets");
52
- for(var keys = Object.keys(model), i = 0, end = keys.length; i < end; i++) {
53
- var key = keys[i], value = model[key];
54
- if(value.get){
55
- data[key] = value.get(data);
56
- }
57
- };
58
-
59
- return data;
60
- }
61
-
62
- }
63
-
1
+ var modelDB = require('./EntityModel');
2
+
3
+ // creates new instance if entity model and calls inner functions to build out a valid entity
4
+ class EntityModelBuilder {
5
+
6
+ static create(model){
7
+ if(model.name === undefined){
8
+ throw "dbset model declaired incorrectly. Check you dbset models for code errors."
9
+ }
10
+ var mod = new model(); //create new instance of Entity Model
11
+ var obj = {};
12
+ var methodNamesArray = Object.getOwnPropertyNames( mod.__proto__ );
13
+ var constructorIndex = methodNamesArray.indexOf("constructor");
14
+ // remove contructor method
15
+ if (constructorIndex > -1) {
16
+ methodNamesArray.splice(constructorIndex, 1);
17
+ }
18
+ // loop through all method names in the entity model
19
+ for (var i = 0; i < methodNamesArray.length; i++) {
20
+ let MDB = new modelDB(model.name); // create a new instance of entity Model class
21
+ mod[methodNamesArray[i]](MDB);
22
+ this.cleanNull(MDB.obj); // remove objects that are null or undefined
23
+ if(Object.keys(MDB.obj).length === 0){
24
+ MDB.obj.virtual = true;
25
+ }
26
+ MDB.obj.name = methodNamesArray[i];
27
+ obj[methodNamesArray[i]] = MDB.obj;
28
+ }
29
+ return obj;
30
+ }
31
+
32
+ static cleanNull(obj) {
33
+ for (var propName in obj) {
34
+ if (obj[propName] === null) {
35
+ delete obj[propName];
36
+ }
37
+ }
38
+ }
39
+
40
+ }
41
+
64
42
  module.exports = EntityModelBuilder;