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.
- package/engines/mysqldb.js +228 -229
- package/engines/sqlitedb.js +146 -154
- package/package.json +1 -1
package/engines/mysqldb.js
CHANGED
|
@@ -1,252 +1,251 @@
|
|
|
1
|
-
const { MultiDbORM } = require(
|
|
1
|
+
const { MultiDbORM } = require("./multidb");
|
|
2
2
|
|
|
3
3
|
class MySQLDB extends MultiDbORM {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
if(options?.limit){
|
|
96
|
-
limit = `LIMIT ${options.limit}`
|
|
57
|
+
if (that.loglevel > 3) {
|
|
58
|
+
console.log("Query ", query, " -> ", results);
|
|
97
59
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
112
|
-
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
221
|
-
|
|
222
|
-
|
|
183
|
+
async update(modelname, filter, object) {
|
|
184
|
+
this.sync.update(modelname, filter, object);
|
|
185
|
+
this.metrics.update(modelname, filter, object);
|
|
223
186
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
-
|
|
250
|
+
MySQLDB,
|
|
252
251
|
};
|
package/engines/sqlitedb.js
CHANGED
|
@@ -1,174 +1,166 @@
|
|
|
1
1
|
const { MultiDbORM } = require("./multidb");
|
|
2
|
-
var fs = require(
|
|
2
|
+
var fs = require("fs");
|
|
3
3
|
|
|
4
4
|
class SQLiteDB extends MultiDbORM {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
55
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
131
|
+
async update(modelname, filter, object) {
|
|
132
|
+
this.sync.update(modelname, filter, object);
|
|
133
|
+
this.metrics.update(modelname, filter, object);
|
|
140
134
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
|
|
146
|
+
var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
|
|
147
|
+
return await this.run(query);
|
|
148
|
+
}
|
|
159
149
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
|
|
174
|
-
}
|
|
165
|
+
SQLiteDB,
|
|
166
|
+
};
|