nodester 0.7.16 → 0.7.18
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/lib/models/mixins.js +20 -5
- package/lib/utils/sanitizations.util.js +121 -1
- package/package.json +1 -1
package/lib/models/mixins.js
CHANGED
|
@@ -218,7 +218,13 @@ async function _updateOne(
|
|
|
218
218
|
) {
|
|
219
219
|
try {
|
|
220
220
|
const include = opts.include ?? [];
|
|
221
|
-
|
|
221
|
+
const hasValidWhere = where && Object.keys(where).length > 0 && Object.values(where).every(val => val !== undefined);
|
|
222
|
+
let instance = null;
|
|
223
|
+
|
|
224
|
+
if (hasValidWhere) {
|
|
225
|
+
instance = await this.findOne({ where, include });
|
|
226
|
+
}
|
|
227
|
+
|
|
222
228
|
let isNewRecord = false;
|
|
223
229
|
|
|
224
230
|
if (!instance) {
|
|
@@ -285,7 +291,10 @@ async function _updateOne(
|
|
|
285
291
|
}
|
|
286
292
|
return associatedModel.updateOne(
|
|
287
293
|
where,
|
|
288
|
-
|
|
294
|
+
{
|
|
295
|
+
...singleData,
|
|
296
|
+
[foreignKey]: instance.id
|
|
297
|
+
},
|
|
289
298
|
associationUpdateOpts
|
|
290
299
|
)
|
|
291
300
|
});
|
|
@@ -305,12 +314,18 @@ async function _updateOne(
|
|
|
305
314
|
}
|
|
306
315
|
else {
|
|
307
316
|
// Note: for now we are only able to work with a model with single PrimaryKey:
|
|
308
|
-
const where = {
|
|
309
|
-
|
|
317
|
+
const where = {}
|
|
318
|
+
if (includeData[pkField] !== undefined && includeData[pkField] !== null) {
|
|
319
|
+
where[pkField] = includeData[pkField];
|
|
320
|
+
} else {
|
|
321
|
+
where[foreignKey] = instance.id;
|
|
310
322
|
}
|
|
311
323
|
fullInstanceData[association] = await associatedModel.updateOne(
|
|
312
324
|
where,
|
|
313
|
-
|
|
325
|
+
{
|
|
326
|
+
...includeData,
|
|
327
|
+
[foreignKey]: instance.id
|
|
328
|
+
},
|
|
314
329
|
associationUpdateOpts
|
|
315
330
|
);
|
|
316
331
|
}
|
|
@@ -8,27 +8,59 @@
|
|
|
8
8
|
|
|
9
9
|
module.exports = {
|
|
10
10
|
UUID: _UUID,
|
|
11
|
+
UUIDV1: _UUID,
|
|
12
|
+
UUIDV4: _UUID,
|
|
11
13
|
|
|
12
14
|
CHAR: _STRING,
|
|
13
15
|
VARCHAR: _STRING,
|
|
14
16
|
STRING: _STRING,
|
|
15
17
|
TEXT: _TEXT,
|
|
18
|
+
CITEXT: _STRING,
|
|
19
|
+
TSVECTOR: _STRING,
|
|
20
|
+
|
|
21
|
+
// Network address types are stored as strings.
|
|
22
|
+
CIDR: _STRING,
|
|
23
|
+
INET: _STRING,
|
|
24
|
+
MACADDR: _STRING,
|
|
16
25
|
|
|
17
26
|
ENUM: _ENUM,
|
|
18
27
|
|
|
19
28
|
NUMBER: _NUMBER,
|
|
29
|
+
DECIMAL: _NUMBER,
|
|
20
30
|
|
|
21
31
|
INT: _INTEGER,
|
|
22
32
|
INTEGER: _INTEGER,
|
|
23
33
|
BIGINT: _INTEGER,
|
|
34
|
+
TINYINT: _INTEGER,
|
|
35
|
+
SMALLINT: _INTEGER,
|
|
36
|
+
MEDIUMINT: _INTEGER,
|
|
37
|
+
|
|
38
|
+
FLOAT: _FLOAT,
|
|
39
|
+
REAL: _FLOAT,
|
|
40
|
+
DOUBLE: _FLOAT,
|
|
24
41
|
|
|
25
42
|
BOOLEAN: _BOOLEAN,
|
|
26
43
|
|
|
27
44
|
DATE: _DATE,
|
|
28
45
|
DATETIME: _DATE,
|
|
46
|
+
NOW: _DATE,
|
|
47
|
+
DATEONLY: _DATEONLY,
|
|
48
|
+
TIME: _TIME,
|
|
29
49
|
|
|
30
50
|
JSON: _JSON,
|
|
31
|
-
JSONTYPE: _JSON
|
|
51
|
+
JSONTYPE: _JSON,
|
|
52
|
+
JSONB: _JSON,
|
|
53
|
+
// Geometry/Geography accept GeoJSON objects, HSTORE accepts key/value objects.
|
|
54
|
+
GEOMETRY: _JSON,
|
|
55
|
+
GEOGRAPHY: _JSON,
|
|
56
|
+
HSTORE: _JSON,
|
|
57
|
+
|
|
58
|
+
ARRAY: _ARRAY,
|
|
59
|
+
RANGE: _ARRAY,
|
|
60
|
+
|
|
61
|
+
BLOB: _BLOB,
|
|
62
|
+
|
|
63
|
+
VIRTUAL: _VIRTUAL
|
|
32
64
|
}
|
|
33
65
|
|
|
34
66
|
function _UUID(value=undefined, options={ fallback:undefined }) {
|
|
@@ -104,6 +136,11 @@ function _INTEGER(value=undefined, options={ fallback:undefined, min:undefined,
|
|
|
104
136
|
return num === undefined ? options?.fallback : parseInt(num);
|
|
105
137
|
}
|
|
106
138
|
|
|
139
|
+
function _FLOAT(value=undefined, options={ fallback:undefined, min:undefined, max:undefined }) {
|
|
140
|
+
const num = _NUMBER(value, { fallback:undefined, min:options?.min, max:options?.max });
|
|
141
|
+
return num === undefined ? options?.fallback : parseFloat(num);
|
|
142
|
+
}
|
|
143
|
+
|
|
107
144
|
function _BOOLEAN(value=undefined, options={ fallback:undefined }) {
|
|
108
145
|
try {
|
|
109
146
|
// If clear boolean.
|
|
@@ -152,6 +189,89 @@ function _DATE(value=undefined, options={ fallback:undefined }) {
|
|
|
152
189
|
}
|
|
153
190
|
|
|
154
191
|
|
|
192
|
+
function _DATEONLY(value=undefined, options={ fallback:undefined }) {
|
|
193
|
+
try {
|
|
194
|
+
const type = Object.prototype.toString.call(value);
|
|
195
|
+
|
|
196
|
+
switch(type) {
|
|
197
|
+
case('[object Date]'): {
|
|
198
|
+
if (isNaN(value.valueOf()))
|
|
199
|
+
throw new Error('Not a date');
|
|
200
|
+
|
|
201
|
+
// Normalize to a date-only string (YYYY-MM-DD).
|
|
202
|
+
return value.toISOString().slice(0, 10);
|
|
203
|
+
}
|
|
204
|
+
case('[object String]'): {
|
|
205
|
+
const check = new Date(value);
|
|
206
|
+
if (isNaN(check.valueOf()))
|
|
207
|
+
throw new Error('Not a date');
|
|
208
|
+
|
|
209
|
+
// Normalize to a date-only string (YYYY-MM-DD).
|
|
210
|
+
return check.toISOString().slice(0, 10);
|
|
211
|
+
}
|
|
212
|
+
default:
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
throw new Error('Not a date');
|
|
217
|
+
}
|
|
218
|
+
catch(ex) {
|
|
219
|
+
return options?.fallback;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
function _TIME(value=undefined, options={ fallback:undefined }) {
|
|
225
|
+
try {
|
|
226
|
+
if (typeof value !== 'string')
|
|
227
|
+
throw new Error(`Not a time string`);
|
|
228
|
+
|
|
229
|
+
return value;
|
|
230
|
+
}
|
|
231
|
+
catch(ex) {
|
|
232
|
+
return options?.fallback;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
function _ARRAY(value=undefined, options={ fallback:undefined }) {
|
|
238
|
+
try {
|
|
239
|
+
if (Array.isArray(value))
|
|
240
|
+
return value;
|
|
241
|
+
|
|
242
|
+
// Allow a JSON-encoded array string.
|
|
243
|
+
if (typeof value === 'string') {
|
|
244
|
+
const parsed = JSON.parse(value);
|
|
245
|
+
if (Array.isArray(parsed))
|
|
246
|
+
return parsed;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
throw new Error(`Not an Array`);
|
|
250
|
+
}
|
|
251
|
+
catch(ex) {
|
|
252
|
+
return options?.fallback;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
function _BLOB(value=undefined, options={ fallback:undefined }) {
|
|
258
|
+
try {
|
|
259
|
+
if (Buffer.isBuffer(value) || typeof value === 'string')
|
|
260
|
+
return value;
|
|
261
|
+
|
|
262
|
+
throw new Error(`Not a Buffer or String`);
|
|
263
|
+
}
|
|
264
|
+
catch(ex) {
|
|
265
|
+
return options?.fallback;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
function _VIRTUAL(value=undefined, options={ fallback:undefined }) {
|
|
271
|
+
return value === undefined ? options?.fallback : value;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
|
|
155
275
|
function _JSON(value=undefined, options={ fallback:undefined }) {
|
|
156
276
|
try {
|
|
157
277
|
if (typeof value === 'string')
|