multi-db-orm 2.1.25 → 2.1.27

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,252 +1,251 @@
1
- const { MultiDbORM } = require('./multidb');
1
+ const { MultiDbORM } = require("./multidb");
2
2
 
3
3
  class MySQLDB extends MultiDbORM {
4
-
5
- mysql;
6
- pool;
7
- dataMap = {
8
- "id": "VARCHAR(50) NOT NULL PRIMARY KEY",
9
- "string": "VARCHAR(4000)",
10
- "stringlarge": "TEXT",
11
- "stringsmall": "VARCHAR(255)",
12
- "number": "DOUBLE",
13
- "boolean": "BOOL",
14
- "array": "TEXT",
15
- "object": "JSON"
16
- };
17
-
18
- constructor(credentials) {
19
- super();
20
- const mysql = require('mysql');
21
- this.mysql = mysql;
22
- this.pool = mysql.createPool({
23
- connectionLimit: credentials.connectionLimit || 10,
24
- host: credentials.host,
25
- port: credentials.port,
26
- user: credentials.username,
27
- password: credentials.password,
28
- database: credentials.database,
29
- waitForConnections: true,
30
- queueLimit: 0,
31
- connectTimeout: credentials.connectTimeout || 10000,
32
- acquireTimeout: credentials.acquireTimeout || 10000,
33
- timeout: credentials.timeout || 60000
34
- });
35
- this.db = this.pool
36
- this.pool.on('connection', (connection) => {
37
- if (this.loglevel > 1)
38
- console.log('MySQLDB: New connection acquired');
39
- });
40
-
41
- this.pool.on('error', (err) => {
42
- if (this.loglevel > 0)
43
- console.error('MySQLDB: Pool error:', err);
44
- });
45
-
46
- this.dbType = 'mysql';
47
- this.reqMade = 0;
48
- }
49
-
50
- async run(query) {
51
- var that = this;
52
- this.reqMade++;
53
- return new Promise(function (resolve, reject) {
54
- that.pool.query(query, function (err, results) {
55
- if (err) {
56
- reject(err);
57
- } else {
58
- resolve(results);
59
- }
60
- if (that.loglevel > 3) {
61
- console.log("Query ", query, ' -> ', results);
62
- }
63
- });
64
- });
65
- }
66
-
67
- async get(modelname, filter, options) {
68
- this.metrics.get(modelname, filter, options);
69
- var where = '';
70
- for (var key in filter) {
71
- where = where + `${key} = '${filter[key]}' AND `;
72
- }
73
- where = where + " 1 ";
74
- var sort = "";
75
- if (options) {
76
- if (options.apply) {
77
- if (options.apply.ineq) {
78
- where = where + ` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
79
- }
80
- if (options.apply.sort) {
81
- sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`;
82
- }
83
- } else if (options.sort) {
84
- sort = `ORDER BY`;
85
- for (let i = 0; i < options.sort.length; i++) {
86
- sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
87
- if (i < options.sort.length - 1) {
88
- sort = sort + ' , ';
89
- }
90
- }
91
- }
4
+ mysql;
5
+ pool;
6
+ dataMap = {
7
+ id: "VARCHAR(50) NOT NULL PRIMARY KEY",
8
+ string: "VARCHAR(4000)",
9
+ stringlarge: "TEXT",
10
+ stringsmall: "VARCHAR(255)",
11
+ number: "DOUBLE",
12
+ boolean: "BOOL",
13
+ array: "TEXT",
14
+ object: "JSON",
15
+ };
16
+
17
+ constructor(credentials) {
18
+ super();
19
+ const mysql = require("mysql");
20
+ this.mysql = mysql;
21
+ this.pool = mysql.createPool({
22
+ connectionLimit: credentials.connectionLimit || 10,
23
+ host: credentials.host,
24
+ port: credentials.port,
25
+ user: credentials.username,
26
+ password: credentials.password,
27
+ database: credentials.database,
28
+ waitForConnections: true,
29
+ queueLimit: 0,
30
+ connectTimeout: credentials.connectTimeout || 10000,
31
+ acquireTimeout: credentials.acquireTimeout || 10000,
32
+ timeout: credentials.timeout || 60000,
33
+ });
34
+ this.db = this.pool;
35
+ this.pool.on("connection", (connection) => {
36
+ if (this.loglevel > 1) console.log("MySQLDB: New connection acquired");
37
+ });
38
+
39
+ this.pool.on("error", (err) => {
40
+ if (this.loglevel > 0) console.error("MySQLDB: Pool error:", err);
41
+ });
42
+
43
+ this.dbType = "mysql";
44
+ this.reqMade = 0;
45
+ }
46
+
47
+ async run(query) {
48
+ var that = this;
49
+ this.reqMade++;
50
+ return new Promise(function (resolve, reject) {
51
+ that.pool.query(query, function (err, results) {
52
+ if (err) {
53
+ reject(err);
54
+ } else {
55
+ resolve(results);
92
56
  }
93
- let limit = ''
94
- let offset = ''
95
- if(options?.limit){
96
- limit = `LIMIT ${options.limit}`
57
+ if (that.loglevel > 3) {
58
+ console.log("Query ", query, " -> ", results);
97
59
  }
98
- if(options?.offset){
99
- offset = `OFFSET ${options.offset}`
100
- }
101
- var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ${limit} ${offset};`;
102
- return await this.run(query);
60
+ });
61
+ });
62
+ }
63
+
64
+ async get(modelname, filter, options) {
65
+ this.metrics.get(modelname, filter, options);
66
+ var where = "";
67
+ for (var key in filter) {
68
+ where = where + `${key} = '${filter[key]}' AND `;
103
69
  }
104
-
105
- async getOne(modelname, filter) {
106
- this.metrics.getOne(modelname, filter);
107
- var where = '';
108
- for (var key in filter) {
109
- where = where + `${key} = '${filter[key]}' AND `;
70
+ where = where + " 1 ";
71
+ var sort = "";
72
+ if (options) {
73
+ if (options.apply) {
74
+ if (options.apply.ineq) {
75
+ where =
76
+ where +
77
+ ` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
110
78
  }
111
- where = where + " 1 ";
112
- var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`;
113
- var row = await this.run(query);
114
- return row[0];
115
- }
116
-
117
- async create(modelname, sampleObject) {
118
- this.sync.create(modelname, sampleObject);
119
- this.metrics.create(modelname, sampleObject);
120
-
121
- var cols = '';
122
- for (var key in sampleObject) {
123
- var type;
124
- if (this.dataMap[sampleObject[key]]) {
125
- type = this.dataMap[sampleObject[key]]
126
- } else {
127
- type = this.dataMap[typeof (sampleObject[key])] || 'TEXT';
128
- if (typeof (sampleObject[key]) == 'string') {
129
- if (sampleObject[key].length > 4000) {
130
- type = this.dataMap['stringlarge']
131
- }
132
- if (sampleObject[key].length <= 255) {
133
- type = this.dataMap['stringsmall']
134
- }
135
- }
136
- }
137
- cols = cols + `${key} ${type},`;
79
+ if (options.apply.sort) {
80
+ sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`;
138
81
  }
139
- cols = cols.substring(0, cols.length - 1);
140
- var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`;
141
- try {
142
- return await this.run(query);
143
- } catch (err) {
144
- if (this.loglevel > 0)
145
- console.log(err);
146
- throw err
82
+ } else if (options.sort) {
83
+ sort = `ORDER BY`;
84
+ for (let i = 0; i < options.sort.length; i++) {
85
+ sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
86
+ if (i < options.sort.length - 1) {
87
+ sort = sort + " , ";
88
+ }
147
89
  }
90
+ }
148
91
  }
149
-
150
- async insert(modelname, object) {
151
- this.sync.insert(modelname, object);
152
- this.metrics.insert(modelname, object);
153
- var cols = '';
154
- var vals = '';
155
- for (var key in object) {
156
- cols = cols + `${key},`;
157
- let val = object[key]
158
- if (typeof val == 'object')
159
- val = JSON.stringify(object[key])
160
- val = this.pool.escape(val)
161
- if (typeof val == "undefined")
162
- vals = vals + `Null,`;
163
- else if (typeof val == 'boolean')
164
- vals = vals + `${val},`;
165
- else
166
- vals = vals + `${val},`;
167
- }
168
- cols = cols.substring(0, cols.length - 1);
169
- vals = vals.substring(0, vals.length - 1);
170
-
171
- var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`;
172
-
173
- try {
174
- return await this.run(query);
175
- } catch (err) {
176
- if (err.code && err.code === 'ER_NO_SUCH_TABLE') {
177
- await this.create(modelname, object);
178
- return await this.run(query);
179
- } else {
180
- throw err;
181
- }
92
+ let limit = "";
93
+ let offset = "";
94
+ if (options?.limit) {
95
+ limit = `LIMIT ${options.limit}`;
96
+ }
97
+ if (options?.offset) {
98
+ offset = `OFFSET ${options.offset}`;
99
+ }
100
+ var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ${limit} ${offset};`;
101
+ return (await this.run(query)) || [];
102
+ }
103
+
104
+ async getOne(modelname, filter) {
105
+ this.metrics.getOne(modelname, filter);
106
+ var where = "";
107
+ for (var key in filter) {
108
+ where = where + `${key} = '${filter[key]}' AND `;
109
+ }
110
+ where = where + " 1 ";
111
+ var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`;
112
+ var row = await this.run(query);
113
+ return row[0];
114
+ }
115
+ async create(modelname, sampleObject) {
116
+ this.sync.create(modelname, sampleObject);
117
+ this.metrics.create(modelname, sampleObject);
118
+
119
+ var cols = "";
120
+ for (var key in sampleObject) {
121
+ var type;
122
+ if (this.dataMap[sampleObject[key]]) {
123
+ type = this.dataMap[sampleObject[key]];
124
+ } else {
125
+ type = this.dataMap[typeof sampleObject[key]] || "TEXT";
126
+ if (typeof sampleObject[key] == "string") {
127
+ if (sampleObject[key].length > 4000) {
128
+ type = this.dataMap["stringlarge"];
129
+ }
130
+ if (sampleObject[key].length <= 255) {
131
+ type = this.dataMap["stringsmall"];
132
+ }
182
133
  }
134
+ }
135
+
136
+ if (key.toLowerCase().trim() === "id") {
137
+ cols = cols + `${key} ${type} PRIMARY KEY NOT NULL UNIQUE,`;
138
+ } else {
139
+ cols = cols + `${key} ${type},`;
140
+ }
141
+ }
142
+ cols = cols.substring(0, cols.length - 1);
143
+ var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`;
144
+ try {
145
+ return await this.run(query);
146
+ } catch (err) {
147
+ if (this.loglevel > 0) console.log(err);
148
+ throw err;
149
+ }
150
+ }
151
+
152
+ async insert(modelname, object) {
153
+ this.sync.insert(modelname, object);
154
+ this.metrics.insert(modelname, object);
155
+ var cols = "";
156
+ var vals = "";
157
+ for (var key in object) {
158
+ cols = cols + `${key},`;
159
+ let val = object[key];
160
+ if (typeof val == "object") val = JSON.stringify(object[key]);
161
+ val = this.pool.escape(val);
162
+ if (typeof val == "undefined") vals = vals + `Null,`;
163
+ else if (typeof val == "boolean") vals = vals + `${val},`;
164
+ else vals = vals + `${val},`;
183
165
  }
166
+ cols = cols.substring(0, cols.length - 1);
167
+ vals = vals.substring(0, vals.length - 1);
184
168
 
185
- async update(modelname, filter, object) {
186
- this.sync.update(modelname, filter, object);
187
- this.metrics.update(modelname, filter, object);
169
+ var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`;
188
170
 
189
- var where = '';
190
- var vals = '';
191
- for (var key in filter) {
192
- where = where + `${key} = '${filter[key]}' AND `;
193
- }
194
- for (var key in object) {
195
- let val = object[key]
196
- if (typeof val == 'object' && val != undefined && val != null)
197
- val = JSON.stringify(object[key])
198
- val = this.pool.escape(val)
199
-
200
- if ( val == "undefined" || val == undefined || val == 'null'|| val == null)
201
- vals = vals + `${key} = Null,`;
202
- else if (typeof val == 'boolean')
203
- vals = vals + `${key} = ${val},`;
204
- else
205
- vals = vals + `${key} = ${val},`;
206
- }
207
- where = where + " 1 ";
208
- vals = vals.substring(0, vals.length - 1);
209
-
210
- var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
211
- try{
212
- return await this.run(query);
213
- }catch(e){
214
- if(this.loglevel > 4)
215
- console.log('Error in update',e)
216
- throw e;
217
- }
171
+ try {
172
+ return await this.run(query);
173
+ } catch (err) {
174
+ if (err.code && err.code === "ER_NO_SUCH_TABLE") {
175
+ await this.create(modelname, object);
176
+ return await this.run(query);
177
+ } else {
178
+ throw err;
179
+ }
218
180
  }
181
+ }
219
182
 
220
- async delete(modelname, filter) {
221
- this.sync.delete(modelname, filter);
222
- this.metrics.delete(modelname, filter);
183
+ async update(modelname, filter, object) {
184
+ this.sync.update(modelname, filter, object);
185
+ this.metrics.update(modelname, filter, object);
223
186
 
224
- var where = '';
225
- for (var key in filter) {
226
- where = where + `${key} = '${filter[key]}' AND `;
227
- }
228
- where = where + " 1 ";
229
- var query = `DELETE FROM ${modelname} WHERE ${where};`;
230
- return await this.run(query);
187
+ var where = "";
188
+ var vals = "";
189
+ for (var key in filter) {
190
+ where = where + `${key} = '${filter[key]}' AND `;
191
+ }
192
+ for (var key in object) {
193
+ let val = object[key];
194
+ if (typeof val == "object" && val != undefined && val != null)
195
+ val = JSON.stringify(object[key]);
196
+ val = this.pool.escape(val);
197
+
198
+ if (
199
+ val == "undefined" ||
200
+ val == undefined ||
201
+ val == "null" ||
202
+ val == null
203
+ )
204
+ vals = vals + `${key} = Null,`;
205
+ else if (typeof val == "boolean") vals = vals + `${key} = ${val},`;
206
+ else vals = vals + `${key} = ${val},`;
231
207
  }
208
+ where = where + " 1 ";
209
+ vals = vals.substring(0, vals.length - 1);
210
+
211
+ var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
212
+ try {
213
+ return await this.run(query);
214
+ } catch (e) {
215
+ if (this.loglevel > 4) console.log("Error in update", e);
216
+ throw e;
217
+ }
218
+ }
219
+
220
+ async delete(modelname, filter) {
221
+ this.sync.delete(modelname, filter);
222
+ this.metrics.delete(modelname, filter);
232
223
 
233
- closePool() {
234
- return new Promise((res, rej) => {
235
- this.pool.end((err) => {
236
- if (err) {
237
- rej(err)
238
- if (this.loglevel > 1)
239
- console.error('Error closing connection pool:', err);
240
- } else {
241
- res()
242
- if (this.loglevel > 1)
243
- console.log('MySQLDB: Connection pool closed');
244
- }
245
- });
246
- })
224
+ var where = "";
225
+ for (var key in filter) {
226
+ where = where + `${key} = '${filter[key]}' AND `;
247
227
  }
228
+ where = where + " 1 ";
229
+ var query = `DELETE FROM ${modelname} WHERE ${where};`;
230
+ return await this.run(query);
231
+ }
232
+
233
+ closePool() {
234
+ return new Promise((res, rej) => {
235
+ this.pool.end((err) => {
236
+ if (err) {
237
+ rej(err);
238
+ if (this.loglevel > 1)
239
+ console.error("Error closing connection pool:", err);
240
+ } else {
241
+ res();
242
+ if (this.loglevel > 1) console.log("MySQLDB: Connection pool closed");
243
+ }
244
+ });
245
+ });
246
+ }
248
247
  }
249
248
 
250
249
  module.exports = {
251
- MySQLDB
250
+ MySQLDB,
252
251
  };
@@ -1,174 +1,166 @@
1
1
  const { MultiDbORM } = require("./multidb");
2
- var fs = require('fs')
2
+ var fs = require("fs");
3
3
 
4
4
  class SQLiteDB extends MultiDbORM {
5
-
6
- sqlite3
7
- dataMap = {
8
- "string": "TEXT",
9
- "number": "REAL",
10
- "boolean": "BOOLEAN"
11
- }
12
- constructor(filepath) {
13
- super()
14
- var sqlite3 = require('sqlite3')
15
- this.sqlite3 = sqlite3
16
- if (filepath == undefined)
17
- filepath = ':memory:'
18
- else {
19
- var currentPath = process.cwd();
20
- if (!fs.existsSync(filepath)) {
21
- filepath = currentPath + '/' + filepath
22
- }
23
- }
24
- this.db = new sqlite3.Database(filepath);
25
- console.log("SQLite3 Initialized");
26
- this.dbType = 'sqlite3'
27
- this.reqMade = 0
5
+ sqlite3;
6
+ dataMap = {
7
+ string: "TEXT",
8
+ number: "REAL",
9
+ boolean: "BOOLEAN",
10
+ };
11
+ constructor(filepath) {
12
+ super();
13
+ var sqlite3 = require("sqlite3");
14
+ this.sqlite3 = sqlite3;
15
+ if (filepath == undefined) filepath = ":memory:";
16
+ else {
17
+ var currentPath = process.cwd();
18
+ if (!fs.existsSync(filepath)) {
19
+ filepath = currentPath + "/" + filepath;
20
+ }
28
21
  }
29
-
30
- async run(query) {
31
- var db = this.db;
32
- var that = this
33
- this.reqMade++
34
- return new Promise(function (resolve, reject) {
35
- db.all(query, function (err, resp) {
36
- if (err)
37
- reject(err)
38
- else
39
- resolve(resp);
40
- if (that.loglevel > 3)
41
- console.log("Query ", query, ' -> ', resp)
42
- });
43
- })
44
-
45
-
22
+ this.db = new sqlite3.Database(filepath);
23
+ console.log("SQLite3 Initialized");
24
+ this.dbType = "sqlite3";
25
+ this.reqMade = 0;
26
+ }
27
+
28
+ async run(query) {
29
+ var db = this.db;
30
+ var that = this;
31
+ this.reqMade++;
32
+ return new Promise(function (resolve, reject) {
33
+ db.all(query, function (err, resp) {
34
+ if (err) reject(err);
35
+ else resolve(resp);
36
+ if (that.loglevel > 3) console.log("Query ", query, " -> ", resp);
37
+ });
38
+ });
39
+ }
40
+
41
+ async get(modelname, filter, options) {
42
+ this.metrics.get(modelname, filter, options);
43
+ var where = "";
44
+ for (var key in filter) {
45
+ where = where + `${key} = '${filter[key]}' AND `;
46
46
  }
47
-
48
- async get(modelname, filter, options) {
49
- this.metrics.get(modelname, filter, options)
50
- var where = ''
51
- for (var key in filter) {
52
- where = where + `${key} = '${filter[key]}' AND `
47
+ where = where + " 1 ";
48
+ var sort = "";
49
+ if (options) {
50
+ if (options.apply) {
51
+ if (options.apply.ineq) {
52
+ where =
53
+ where +
54
+ ` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
53
55
  }
54
- where = where + " 1 ";
55
- var sort = "";
56
- if (options) {
57
- if (options.apply) {
58
- if (options.apply.ineq) {
59
- where = where + ` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
60
- }
61
- if (options.apply.sort) {
62
- sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`
63
- }
64
- }
65
- else if (options.sort) {
66
- sort = `ORDER BY`
67
- for (let i = 0; i < options.sort.length; i++) {
68
- sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
69
- if (i < options.sort.length - 1) {
70
- sort = sort + ' , ';
71
- }
72
-
73
- }
74
- }
56
+ if (options.apply.sort) {
57
+ sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`;
75
58
  }
76
- var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ;`
77
- return await this.run(query)
78
- }
79
-
80
- async getOne(modelname, filter) {
81
- this.metrics.getOne(modelname, filter)
82
- var where = ''
83
- for (var key in filter) {
84
- where = where + `${key} = '${filter[key]}' AND `
59
+ } else if (options.sort) {
60
+ sort = `ORDER BY`;
61
+ for (let i = 0; i < options.sort.length; i++) {
62
+ sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
63
+ if (i < options.sort.length - 1) {
64
+ sort = sort + " , ";
65
+ }
85
66
  }
86
- where = where + " 1 ";
87
- var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`
88
- var row = await this.run(query)
89
- return row[0];
67
+ }
90
68
  }
91
-
92
- async create(modelname, sampleObject) {
93
- this.sync.create(modelname, sampleObject)
94
- this.metrics.create(modelname, sampleObject)
95
-
96
- var cols = ''
97
- for (var key in sampleObject) {
98
- var type = this.dataMap[typeof (sampleObject[key])] || 'TEXT'
99
- cols = cols + `${key} ${type},`
100
- }
101
- cols = cols.substring(0, cols.length - 1)
102
- var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`
103
- try {
104
- return await this.run(query)
105
- } catch (err) {
106
- console.log(err)
107
- return undefined;
108
- }
69
+ var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ;`;
70
+ return (await this.run(query)) || [];
71
+ }
72
+
73
+ async getOne(modelname, filter) {
74
+ this.metrics.getOne(modelname, filter);
75
+ var where = "";
76
+ for (var key in filter) {
77
+ where = where + `${key} = '${filter[key]}' AND `;
109
78
  }
110
-
111
- async insert(modelname, object) {
112
- this.sync.insert(modelname, object)
113
- this.metrics.insert(modelname, object)
114
- var cols = ''
115
- var vals = ''
116
- for (var key in object) {
117
- cols = cols + `${key},`
118
- vals = vals + `'${object[key]}',`
119
- }
120
- cols = cols.substring(0, cols.length - 1)
121
- vals = vals.substring(0, vals.length - 1)
122
-
123
- var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`
124
-
125
- try {
126
- return await this.run(query)
127
- } catch (err) {
128
- if (err.message && err.message.indexOf('SQLITE_ERROR: no such table: ') > -1) {
129
- await this.create(modelname, object);
130
- return await this.run(query)
131
- }
132
- else
133
- throw err;
134
- }
79
+ where = where + " 1 ";
80
+ var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`;
81
+ var row = await this.run(query);
82
+ return row[0];
83
+ }
84
+
85
+ async create(modelname, sampleObject) {
86
+ this.sync.create(modelname, sampleObject);
87
+ this.metrics.create(modelname, sampleObject);
88
+
89
+ var cols = "";
90
+ for (var key in sampleObject) {
91
+ var type = this.dataMap[typeof sampleObject[key]] || "TEXT";
92
+ cols = cols + `${key} ${type},`;
135
93
  }
94
+ cols = cols.substring(0, cols.length - 1);
95
+ var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`;
96
+ try {
97
+ return await this.run(query);
98
+ } catch (err) {
99
+ console.log(err);
100
+ return undefined;
101
+ }
102
+ }
103
+
104
+ async insert(modelname, object) {
105
+ this.sync.insert(modelname, object);
106
+ this.metrics.insert(modelname, object);
107
+ var cols = "";
108
+ var vals = "";
109
+ for (var key in object) {
110
+ cols = cols + `${key},`;
111
+ vals = vals + `'${object[key]}',`;
112
+ }
113
+ cols = cols.substring(0, cols.length - 1);
114
+ vals = vals.substring(0, vals.length - 1);
115
+
116
+ var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`;
117
+
118
+ try {
119
+ return await this.run(query);
120
+ } catch (err) {
121
+ if (
122
+ err.message &&
123
+ err.message.indexOf("SQLITE_ERROR: no such table: ") > -1
124
+ ) {
125
+ await this.create(modelname, object);
126
+ return await this.run(query);
127
+ } else throw err;
128
+ }
129
+ }
136
130
 
137
- async update(modelname, filter, object) {
138
- this.sync.update(modelname, filter, object)
139
- this.metrics.update(modelname, filter, object)
131
+ async update(modelname, filter, object) {
132
+ this.sync.update(modelname, filter, object);
133
+ this.metrics.update(modelname, filter, object);
140
134
 
141
- var where = ''
142
- var vals = ''
143
- for (var key in filter) {
144
- where = where + `${key} = '${filter[key]}' AND `
145
- }
146
- for (var key in object) {
147
- vals = vals + ` ${key} = '${object[key]}',`
148
- }
149
- where = where + " 1 ";
150
- vals = vals.substring(0, vals.length - 1)
151
-
152
- var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`
153
- return await this.run(query)
135
+ var where = "";
136
+ var vals = "";
137
+ for (var key in filter) {
138
+ where = where + `${key} = '${filter[key]}' AND `;
154
139
  }
140
+ for (var key in object) {
141
+ vals = vals + ` ${key} = '${object[key]}',`;
142
+ }
143
+ where = where + " 1 ";
144
+ vals = vals.substring(0, vals.length - 1);
155
145
 
156
- async delete(modelname, filter) {
157
- this.sync.delete(modelname, filter)
158
- this.metrics.delete(modelname, filter)
146
+ var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
147
+ return await this.run(query);
148
+ }
159
149
 
160
- var where = ''
161
- for (var key in filter) {
162
- where = where + `${key} = '${filter[key]}' AND `
163
- }
164
- where = where + " 1 ";
165
- var query = `DELETE FROM ${modelname} WHERE ${where};`
166
- return await this.run(query)
150
+ async delete(modelname, filter) {
151
+ this.sync.delete(modelname, filter);
152
+ this.metrics.delete(modelname, filter);
153
+
154
+ var where = "";
155
+ for (var key in filter) {
156
+ where = where + `${key} = '${filter[key]}' AND `;
167
157
  }
158
+ where = where + " 1 ";
159
+ var query = `DELETE FROM ${modelname} WHERE ${where};`;
160
+ return await this.run(query);
161
+ }
168
162
  }
169
163
 
170
-
171
-
172
164
  module.exports = {
173
- SQLiteDB
174
- }
165
+ SQLiteDB,
166
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "multi-db-orm",
3
- "version": "2.1.25",
3
+ "version": "2.1.27",
4
4
  "description": "CRUD , Backup , Restore and Migration library for multiple databases",
5
5
  "main": "index.js",
6
6
  "dependencies": {