@bee.js/node 0.0.84 → 0.0.86
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/beehive.js +5 -11
- package/lib/ORM/beeORM.js +18 -15
- package/package.json +1 -1
package/beehive.js
CHANGED
|
@@ -372,8 +372,6 @@ module.exports = function hive(req = {}, res = {}, model = null) {
|
|
|
372
372
|
},
|
|
373
373
|
|
|
374
374
|
insert: async function (_data, params = {}) {
|
|
375
|
-
//data = beeORM.forceData(data, req)
|
|
376
|
-
|
|
377
375
|
if (model.relations && params.relations)
|
|
378
376
|
await Promise.all(
|
|
379
377
|
Object.keys(model.relations).map(async (field) => {
|
|
@@ -401,10 +399,10 @@ module.exports = function hive(req = {}, res = {}, model = null) {
|
|
|
401
399
|
})
|
|
402
400
|
);
|
|
403
401
|
|
|
404
|
-
|
|
402
|
+
const onlyFields = script[0].fields.concat(
|
|
405
403
|
(req.onlyFields || {})[model.table] || []
|
|
406
404
|
);
|
|
407
|
-
|
|
405
|
+
const sql = beeORM.sqlInsertUpdate(model, _data, onlyFields, "INSERT");
|
|
408
406
|
|
|
409
407
|
_data = await dbExec(sql.SQL, sql.binds);
|
|
410
408
|
|
|
@@ -415,21 +413,17 @@ module.exports = function hive(req = {}, res = {}, model = null) {
|
|
|
415
413
|
},
|
|
416
414
|
|
|
417
415
|
update: async function (data, ids = []) {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
//data = beeORM.forceData(data, req)
|
|
421
|
-
|
|
422
|
-
let onlyFields = script[0].fields.concat(
|
|
416
|
+
const onlyFields = script[0].fields.concat(
|
|
423
417
|
(req.onlyFields || {})[model.table] || []
|
|
424
418
|
);
|
|
425
|
-
|
|
419
|
+
const sql = beeORM.sqlInsertUpdate(model, data, onlyFields, "UPDATE");
|
|
426
420
|
|
|
427
421
|
sql.SQL += beeORM.modelSqlWhere(model, ids, req.middleware);
|
|
428
422
|
sql.SQL += script[0]["where"].length
|
|
429
423
|
? (!ids.length ? " WHERE " : " AND ") + script[0]["where"].join(" AND ")
|
|
430
424
|
: "";
|
|
431
425
|
|
|
432
|
-
data = await dbExec(sql.SQL,
|
|
426
|
+
data = await dbExec(sql.SQL, sql.binds);
|
|
433
427
|
|
|
434
428
|
return { ...this, result: { ...sql.result, ...data } };
|
|
435
429
|
},
|
package/lib/ORM/beeORM.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
const mysql = require("mysql2/promise"); //https://evertpot.com/executing-a-mysql-query-in-nodejs/
|
|
2
2
|
const beeTools = require("../../tools/beeTools");
|
|
3
3
|
|
|
4
|
-
const escape = function (string) {
|
|
5
|
-
return typeof string === "string" ? string.replace(/'/g, "''") : string;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
4
|
const parseArray = function (param) {
|
|
9
5
|
switch (typeof param) {
|
|
10
6
|
case "string":
|
|
@@ -216,6 +212,15 @@ module.exports = {
|
|
|
216
212
|
|
|
217
213
|
data = data[0] ? data : [data];
|
|
218
214
|
|
|
215
|
+
const sanitizeString = (val) => {
|
|
216
|
+
return typeof val === "string" ? val.replace(/\0/g, "") : val;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const normalizeDate = (val) => {
|
|
220
|
+
if (!val || val === "" || isNaN(new Date(val).getTime())) return null;
|
|
221
|
+
return val;
|
|
222
|
+
};
|
|
223
|
+
|
|
219
224
|
Object.keys(data).map((key) => {
|
|
220
225
|
for (let field in model.schema) {
|
|
221
226
|
field = { ...model.schema[field], name: field };
|
|
@@ -224,7 +229,7 @@ module.exports = {
|
|
|
224
229
|
if (!field.ai && onlyFields.length && !onlyFields.includes(field.name))
|
|
225
230
|
continue;
|
|
226
231
|
|
|
227
|
-
let value =
|
|
232
|
+
let value = data[key][field.name];
|
|
228
233
|
let type = model.schema[field.name].type;
|
|
229
234
|
|
|
230
235
|
if (statement == "INSERT" && field.ai)
|
|
@@ -242,26 +247,24 @@ module.exports = {
|
|
|
242
247
|
switch (type) {
|
|
243
248
|
case "guid":
|
|
244
249
|
case "uuid":
|
|
245
|
-
value = `UUID_TO_BIN('${value}')`; //TODO mssql
|
|
246
|
-
break;
|
|
247
|
-
case "binary":
|
|
248
|
-
value = value;
|
|
249
|
-
break;
|
|
250
|
-
case "file":
|
|
251
250
|
binds.push(value);
|
|
252
|
-
value =
|
|
251
|
+
value = `UUID_TO_BIN(?)`;
|
|
253
252
|
break;
|
|
254
253
|
case "date":
|
|
255
254
|
case "datetime":
|
|
256
255
|
case "time":
|
|
257
256
|
case "timestamp":
|
|
258
|
-
|
|
257
|
+
binds.push(normalizeDate(value));
|
|
258
|
+
value = "?";
|
|
259
259
|
break;
|
|
260
|
+
case "binary":
|
|
261
|
+
case "file":
|
|
260
262
|
default:
|
|
261
|
-
|
|
263
|
+
binds.push(value);
|
|
264
|
+
value = "?";
|
|
262
265
|
}
|
|
263
266
|
|
|
264
|
-
if (field.onInput) value = field.onInput
|
|
267
|
+
if (field.onInput) value = `${field.onInput}(${value})`; //example md5()
|
|
265
268
|
|
|
266
269
|
if (statement === "INSERT") {
|
|
267
270
|
if (key == 0) fields.push(q(field.name));
|