masterrecord 1.1.5 → 1.1.7
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/CHANGES.md +26 -0
- package/Cache/RedisQueryCache.js +1 -1
- package/Entity/entityModelBuilder.js +8 -8
- package/Entity/entityTrackerModel.js +19 -19
- package/Migrations/cli.js +127 -123
- package/Migrations/migrationMySQLQuery.js +21 -27
- package/Migrations/migrationPostgresQuery.js +16 -16
- package/Migrations/migrationSQLiteQuery.js +14 -15
- package/Migrations/migrationTemplate.js +6 -6
- package/Migrations/migrations.js +66 -70
- package/Migrations/schema.js +3 -3
- package/QueryLanguage/queryMethods.js +27 -28
- package/QueryLanguage/queryScript.js +35 -37
- package/SQLLiteEngine.js +122 -123
- package/Tools.js +23 -23
- package/context.js +4 -5
- package/deleteManager.js +0 -1
- package/insertManager.js +1 -1
- package/mySQLEngine.js +16 -10
- package/package.json +9 -2
- package/postgresEngine.js +25 -18
- package/postgresSyncConnect.js +2 -2
package/SQLLiteEngine.js
CHANGED
|
@@ -13,31 +13,31 @@ class SQLLiteEngine {
|
|
|
13
13
|
throw new Error('UPDATE failed: Invalid parameterized query structure. Check entity definition.');
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
const sqlQuery = ` UPDATE [${query.tableName}]
|
|
17
17
|
SET ${query.arg.sql}
|
|
18
18
|
WHERE [${query.tableName}].[${query.primaryKey}] = ?`;
|
|
19
19
|
// Add primaryKeyValue to params array
|
|
20
|
-
|
|
20
|
+
const params = [...query.arg.params, query.primaryKeyValue];
|
|
21
21
|
return Promise.resolve(this._runWithParams(sqlQuery, params));
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
async delete(queryObject){
|
|
25
|
-
|
|
25
|
+
const sqlObject = this._buildDeleteObject(queryObject);
|
|
26
26
|
// Use parameterized query to prevent SQL injection
|
|
27
|
-
|
|
27
|
+
const sqlQuery = `DELETE FROM [${sqlObject.tableName}] WHERE [${sqlObject.tableName}].[${sqlObject.primaryKey}] = ?`;
|
|
28
28
|
return Promise.resolve(this._executeWithParams(sqlQuery, [sqlObject.value]));
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
async insert(queryObject){
|
|
32
32
|
// Use NEW SECURE parameterized version
|
|
33
|
-
|
|
33
|
+
const sqlObject = this._buildSQLInsertObjectParameterized(queryObject, queryObject.__entity);
|
|
34
34
|
if(sqlObject === -1){
|
|
35
35
|
throw new Error('INSERT failed: No columns to insert');
|
|
36
36
|
}
|
|
37
|
-
|
|
37
|
+
const query = `INSERT INTO [${sqlObject.tableName}] (${sqlObject.columns})
|
|
38
38
|
VALUES (${sqlObject.placeholders})`;
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
const queryObj = this._runWithParams(query, sqlObject.params);
|
|
40
|
+
const open = {
|
|
41
41
|
"id": queryObj.lastInsertRowid
|
|
42
42
|
};
|
|
43
43
|
return Promise.resolve(open);
|
|
@@ -111,7 +111,7 @@ class SQLLiteEngine {
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
async get(query, entity, context){
|
|
114
|
-
|
|
114
|
+
let queryString = {};
|
|
115
115
|
try {
|
|
116
116
|
if(query.raw){
|
|
117
117
|
queryString.query = query.raw;
|
|
@@ -131,7 +131,7 @@ class SQLLiteEngine {
|
|
|
131
131
|
console.debug("[SQL]", queryString.query);
|
|
132
132
|
console.debug("[Params]", params);
|
|
133
133
|
}
|
|
134
|
-
|
|
134
|
+
const queryReturn = this.db.prepare(queryString.query).get(...params);
|
|
135
135
|
return Promise.resolve(queryReturn);
|
|
136
136
|
}
|
|
137
137
|
return Promise.resolve(null);
|
|
@@ -168,9 +168,9 @@ class SQLLiteEngine {
|
|
|
168
168
|
}catch(_){ return Promise.resolve([]); }
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
async getCount(queryObject, entity,
|
|
172
|
-
|
|
173
|
-
|
|
171
|
+
async getCount(queryObject, entity, _context){
|
|
172
|
+
const query = queryObject.script;
|
|
173
|
+
const queryString = {};
|
|
174
174
|
try {
|
|
175
175
|
if(query.raw){
|
|
176
176
|
queryString.query = query.raw;
|
|
@@ -186,14 +186,14 @@ class SQLLiteEngine {
|
|
|
186
186
|
queryString.query = `SELECT ${this.buildCount(query, entity)} ${this.buildFrom(query, entity)} ${this.buildWhere(query, entity)} ${this.buildAnd(query, entity)}`
|
|
187
187
|
}
|
|
188
188
|
if(queryString.query){
|
|
189
|
-
|
|
189
|
+
const queryCount = queryString.query
|
|
190
190
|
// Get parameters from query script
|
|
191
191
|
const params = query.parameters ? query.parameters.getParams() : [];
|
|
192
192
|
if (process.env.LOG_SQL === 'true' || process.env.NODE_ENV !== 'production') {
|
|
193
193
|
console.debug("[SQL]", queryCount);
|
|
194
194
|
console.debug("[Params]", params);
|
|
195
195
|
}
|
|
196
|
-
|
|
196
|
+
const queryReturn = this.db.prepare(queryCount).get(...params);
|
|
197
197
|
return Promise.resolve(queryReturn);
|
|
198
198
|
}
|
|
199
199
|
return Promise.resolve(null);
|
|
@@ -204,7 +204,7 @@ class SQLLiteEngine {
|
|
|
204
204
|
}
|
|
205
205
|
|
|
206
206
|
async all(query, entity, context){
|
|
207
|
-
|
|
207
|
+
let selectQuery = {};
|
|
208
208
|
try {
|
|
209
209
|
if(query.raw){
|
|
210
210
|
selectQuery.query = query.raw;
|
|
@@ -220,7 +220,7 @@ class SQLLiteEngine {
|
|
|
220
220
|
console.debug("[SQL]", selectQuery.query);
|
|
221
221
|
console.debug("[Params]", params);
|
|
222
222
|
}
|
|
223
|
-
|
|
223
|
+
const queryReturn = this.db.prepare(selectQuery.query).all(...params);
|
|
224
224
|
return Promise.resolve(queryReturn);
|
|
225
225
|
}
|
|
226
226
|
return Promise.resolve(null);
|
|
@@ -232,7 +232,7 @@ class SQLLiteEngine {
|
|
|
232
232
|
|
|
233
233
|
changeNullQuery(query){
|
|
234
234
|
if(query.where){
|
|
235
|
-
|
|
235
|
+
let whereClaus;
|
|
236
236
|
whereClaus = query.where.expr.replace("=== null", "is null");
|
|
237
237
|
if(whereClaus === query.where.expr){
|
|
238
238
|
whereClaus = query.where.expr.replace("!= null", "is not null");
|
|
@@ -242,8 +242,8 @@ class SQLLiteEngine {
|
|
|
242
242
|
|
|
243
243
|
}
|
|
244
244
|
|
|
245
|
-
buildCount(query,
|
|
246
|
-
|
|
245
|
+
buildCount(query, _mainQuery){
|
|
246
|
+
const entity = this.getEntity(query.parentName, query.entityMap);
|
|
247
247
|
if(query.count){
|
|
248
248
|
if(query.count !== "none"){
|
|
249
249
|
return `COUNT(${entity}.${query.count.selectFields[0]})`
|
|
@@ -257,9 +257,9 @@ class SQLLiteEngine {
|
|
|
257
257
|
}
|
|
258
258
|
}
|
|
259
259
|
|
|
260
|
-
buildQuery(query, entity, context,
|
|
260
|
+
buildQuery(query, entity, context, _limit){
|
|
261
261
|
|
|
262
|
-
|
|
262
|
+
const queryObject = {};
|
|
263
263
|
queryObject.entity = this.getEntity(entity.__name, query.entityMap);
|
|
264
264
|
queryObject.select = this.buildSelect(query, entity);
|
|
265
265
|
queryObject.count = this.buildCount(query, entity);
|
|
@@ -289,7 +289,7 @@ class SQLLiteEngine {
|
|
|
289
289
|
}
|
|
290
290
|
}
|
|
291
291
|
|
|
292
|
-
|
|
292
|
+
const queryString = `${queryObject.select} ${queryObject.count} ${queryObject.from} ${queryObject.include} ${queryObject.where} ${queryObject.and} ${queryObject.orderBy} ${queryObject.take} ${queryObject.skip}`;
|
|
293
293
|
return {
|
|
294
294
|
query : queryString,
|
|
295
295
|
entity : this.getEntity(entity.__name, query.entityMap)
|
|
@@ -324,10 +324,9 @@ class SQLLiteEngine {
|
|
|
324
324
|
|
|
325
325
|
buildOrderBy(query, entity){
|
|
326
326
|
// ORDER BY column1, column2, ... ASC|DESC;
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
var strQuery = "";
|
|
327
|
+
let orderByType = "ASC";
|
|
328
|
+
let orderByEntity = query.orderBy;
|
|
329
|
+
let strQuery = "";
|
|
331
330
|
if(orderByEntity === false){
|
|
332
331
|
orderByType = "DESC";
|
|
333
332
|
orderByEntity = query.orderByDesc;
|
|
@@ -342,7 +341,7 @@ class SQLLiteEngine {
|
|
|
342
341
|
}
|
|
343
342
|
}
|
|
344
343
|
}
|
|
345
|
-
|
|
344
|
+
const entityAlias = this.getEntity(query.parentName, query.entityMap);
|
|
346
345
|
const fieldList = [];
|
|
347
346
|
for (const item in orderByEntity.selectFields) {
|
|
348
347
|
fieldList.push(`${entityAlias}.${orderByEntity.selectFields[item]}`);
|
|
@@ -374,25 +373,25 @@ class SQLLiteEngine {
|
|
|
374
373
|
buildAnd(query, mainQuery){
|
|
375
374
|
// loop through the AND
|
|
376
375
|
// loop update ther where .expr
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
376
|
+
const andEntity = query.and;
|
|
377
|
+
const $that = this;
|
|
378
|
+
let str = "";
|
|
380
379
|
|
|
381
380
|
if(andEntity){
|
|
382
|
-
|
|
381
|
+
let entity = this.getEntity(query.parentName, query.entityMap);
|
|
383
382
|
var andList = [];
|
|
384
|
-
for (
|
|
385
|
-
|
|
386
|
-
for (
|
|
387
|
-
|
|
383
|
+
for (const entityPart in andEntity) { // loop through list of and's
|
|
384
|
+
const itemEntity = andEntity[entityPart]; // get the entityANd
|
|
385
|
+
for (const table in itemEntity[query.parentName]) { // find the main table
|
|
386
|
+
const item = itemEntity[query.parentName][table];
|
|
388
387
|
const expressions = [];
|
|
389
|
-
for (
|
|
388
|
+
for (const exp in item.expressions) {
|
|
390
389
|
// Use the field name verbatim for SQL emission; only
|
|
391
390
|
// use the capitalized form for the navigational
|
|
392
391
|
// relationship lookup (those are PascalCase keys).
|
|
393
392
|
const originalField = item.expressions[exp].field;
|
|
394
393
|
const capitalized = tools.capitalizeFirstLetter(originalField);
|
|
395
|
-
|
|
394
|
+
let field = originalField;
|
|
396
395
|
if(mainQuery[capitalized]){
|
|
397
396
|
if(mainQuery[capitalized].isNavigational){
|
|
398
397
|
entity = $that.getEntity(capitalized, query.entityMap);
|
|
@@ -411,7 +410,7 @@ class SQLLiteEngine {
|
|
|
411
410
|
expressions.push(`${entity}.${field} ${item.expressions[exp].func} ${item.expressions[exp].arg}`);
|
|
412
411
|
}else{
|
|
413
412
|
// Check if arg is a parameterized placeholder
|
|
414
|
-
|
|
413
|
+
const isPlaceholder = (item.expressions[exp].arg === '?' || /^\$\d+$/.test(item.expressions[exp].arg));
|
|
415
414
|
if(isPlaceholder){
|
|
416
415
|
expressions.push(`${entity}.${field} ${item.expressions[exp].func} ${item.expressions[exp].arg}`);
|
|
417
416
|
}else{
|
|
@@ -433,16 +432,16 @@ class SQLLiteEngine {
|
|
|
433
432
|
}
|
|
434
433
|
|
|
435
434
|
buildWhere(query, mainQuery){
|
|
436
|
-
|
|
435
|
+
const whereEntity = query.where;
|
|
437
436
|
|
|
438
|
-
|
|
437
|
+
const $that = this;
|
|
439
438
|
if(!whereEntity){
|
|
440
439
|
return "";
|
|
441
440
|
}
|
|
442
441
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
442
|
+
const entityAlias = this.getEntity(query.parentName, query.entityMap);
|
|
443
|
+
const item = whereEntity[query.parentName].query;
|
|
444
|
+
const exprs = item.expressions || [];
|
|
446
445
|
|
|
447
446
|
function exprToSql(expr){
|
|
448
447
|
// Preserve case for column emission. SQLite identifier matching
|
|
@@ -450,8 +449,8 @@ class SQLLiteEngine {
|
|
|
450
449
|
// practice, but it would have hidden a bug if SQLite were ever
|
|
451
450
|
// configured with strict case-sensitive identifiers, and it
|
|
452
451
|
// produced inconsistent SQL output across engines.
|
|
453
|
-
|
|
454
|
-
|
|
452
|
+
let field = expr.field;
|
|
453
|
+
let ent = entityAlias;
|
|
455
454
|
if(mainQuery[field]){
|
|
456
455
|
if(mainQuery[field].isNavigational){
|
|
457
456
|
ent = $that.getEntity(field, query.entityMap);
|
|
@@ -462,7 +461,7 @@ class SQLLiteEngine {
|
|
|
462
461
|
}
|
|
463
462
|
}
|
|
464
463
|
let func = expr.func;
|
|
465
|
-
|
|
464
|
+
const arg = expr.arg;
|
|
466
465
|
if((!func && typeof arg === 'undefined')){
|
|
467
466
|
return null;
|
|
468
467
|
}
|
|
@@ -481,7 +480,7 @@ class SQLLiteEngine {
|
|
|
481
480
|
return `${ent}.${field} ${func} ${arg}`;
|
|
482
481
|
}
|
|
483
482
|
// Check if arg is a parameterized placeholder (? for MySQL/SQLite, $1/$2/etc for Postgres)
|
|
484
|
-
|
|
483
|
+
const isPlaceholder = (arg === '?' || /^\$\d+$/.test(arg));
|
|
485
484
|
if(isPlaceholder){
|
|
486
485
|
// Don't quote placeholders - they must remain as bare ? or $1
|
|
487
486
|
return `${ent}.${field} ${func} ${arg}`;
|
|
@@ -518,23 +517,23 @@ class SQLLiteEngine {
|
|
|
518
517
|
|
|
519
518
|
buildInclude( query, entity, context){
|
|
520
519
|
const includeQueries = [];
|
|
521
|
-
for (
|
|
522
|
-
|
|
523
|
-
|
|
520
|
+
for (const part in query.include) {
|
|
521
|
+
const includeEntity = query.include[part];
|
|
522
|
+
const $that = this;
|
|
524
523
|
if(includeEntity){
|
|
525
|
-
|
|
526
|
-
|
|
524
|
+
const parentObj = includeEntity[query.parentName];
|
|
525
|
+
let currentContext = "";
|
|
527
526
|
if(includeEntity.selectFields){
|
|
528
527
|
currentContext = context[tools.capitalizeFirstLetter(includeEntity.selectFields[0])];
|
|
529
528
|
}
|
|
530
529
|
|
|
531
530
|
if(parentObj){
|
|
532
531
|
parentObj.entityMap = query.entityMap;
|
|
533
|
-
|
|
534
|
-
|
|
532
|
+
let foreignKey = $that.getForeignKey(entity.__name, currentContext.__entity);
|
|
533
|
+
let mainPrimaryKey = $that.getPrimarykey(entity);
|
|
535
534
|
var mainEntity = $that.getEntity(entity.__name, query.entityMap);
|
|
536
535
|
if(currentContext.__entity[entity.__name].type === "hasManyThrough"){
|
|
537
|
-
|
|
536
|
+
const foreignTable = tools.capitalizeFirstLetter(currentContext.__entity[entity.__name].foreignTable); //to uppercase letter
|
|
538
537
|
foreignKey = $that.getPrimarykey(currentContext.__entity);
|
|
539
538
|
mainPrimaryKey = context[foreignTable].__entity[currentContext.__entity.__name].foreignKey;
|
|
540
539
|
var mainEntity = $that.getEntity(foreignTable,query.entityMap);
|
|
@@ -548,7 +547,7 @@ class SQLLiteEngine {
|
|
|
548
547
|
parentObj.select.selectFields.push(foreignKey);
|
|
549
548
|
}
|
|
550
549
|
|
|
551
|
-
|
|
550
|
+
const innerQuery = $that.buildQuery(parentObj, currentContext.__entity, context);
|
|
552
551
|
|
|
553
552
|
includeQueries.push(`LEFT JOIN (${innerQuery.query}) AS ${innerQuery.entity} ON ${ mainEntity}.${mainPrimaryKey} = ${innerQuery.entity}.${foreignKey}`);
|
|
554
553
|
|
|
@@ -559,7 +558,7 @@ class SQLLiteEngine {
|
|
|
559
558
|
}
|
|
560
559
|
|
|
561
560
|
buildFrom(query, entity){
|
|
562
|
-
|
|
561
|
+
const entityName = this.getEntity(entity.__name, query.entityMap);
|
|
563
562
|
if(entityName ){
|
|
564
563
|
return `FROM ${entity.__name } AS ${entityName}`;
|
|
565
564
|
}
|
|
@@ -568,9 +567,9 @@ class SQLLiteEngine {
|
|
|
568
567
|
|
|
569
568
|
buildSelect(query, entity){
|
|
570
569
|
// this means that there is a select statement
|
|
571
|
-
|
|
570
|
+
const select = "SELECT";
|
|
572
571
|
const arr = [];
|
|
573
|
-
|
|
572
|
+
const $that = this;
|
|
574
573
|
if(query.select){
|
|
575
574
|
for (const item in query.select.selectFields) {
|
|
576
575
|
arr.push(`${$that.getEntity(entity.__name, query.entityMap)}.${query.select.selectFields[item]}`);
|
|
@@ -578,7 +577,7 @@ class SQLLiteEngine {
|
|
|
578
577
|
|
|
579
578
|
}
|
|
580
579
|
else{
|
|
581
|
-
|
|
580
|
+
const entityList = this.getEntityList(entity);
|
|
582
581
|
for (const item in entityList) {
|
|
583
582
|
arr.push(`${$that.getEntity(entity.__name, query.entityMap)}.${entityList[item]}`);
|
|
584
583
|
};
|
|
@@ -609,9 +608,9 @@ class SQLLiteEngine {
|
|
|
609
608
|
}
|
|
610
609
|
|
|
611
610
|
getInclude(name, query){
|
|
612
|
-
|
|
611
|
+
const include = query.include;
|
|
613
612
|
if(include){
|
|
614
|
-
for (
|
|
613
|
+
for (const part in include) {
|
|
615
614
|
if(tools.capitalizeFirstLetter(include[part].selectFields[0]) === name){
|
|
616
615
|
return include[part];
|
|
617
616
|
}
|
|
@@ -623,8 +622,8 @@ class SQLLiteEngine {
|
|
|
623
622
|
}
|
|
624
623
|
|
|
625
624
|
getEntity(name, maps){
|
|
626
|
-
for (
|
|
627
|
-
|
|
625
|
+
for (const item in maps) {
|
|
626
|
+
const map = maps[item];
|
|
628
627
|
if(tools.capitalizeFirstLetter(name) === tools.capitalizeFirstLetter(map.name)){
|
|
629
628
|
return map.entity
|
|
630
629
|
}
|
|
@@ -639,9 +638,9 @@ class SQLLiteEngine {
|
|
|
639
638
|
|
|
640
639
|
// return a list of entity names and skip foreign keys and underscore.
|
|
641
640
|
getEntityList(entity){
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
for (
|
|
641
|
+
const entitiesList = [];
|
|
642
|
+
const $that = this;
|
|
643
|
+
for (const ent in entity) {
|
|
645
644
|
if(!ent.startsWith("_")){
|
|
646
645
|
// Skip lifecycle hooks - they are not database columns
|
|
647
646
|
if(entity[ent].lifecycle === true){
|
|
@@ -668,7 +667,7 @@ class SQLLiteEngine {
|
|
|
668
667
|
else{
|
|
669
668
|
|
|
670
669
|
if(entity[ent].relationshipType === "belongsTo"){
|
|
671
|
-
|
|
670
|
+
const name = entity[ent].foreignKey;
|
|
672
671
|
if($that.chechUnsupportedWords(name)){
|
|
673
672
|
entitiesList.push(`'${name}'`);
|
|
674
673
|
//entitiesList.push(`'${ent}'`);
|
|
@@ -693,8 +692,8 @@ class SQLLiteEngine {
|
|
|
693
692
|
return entitiesList
|
|
694
693
|
}
|
|
695
694
|
chechUnsupportedWords(word){
|
|
696
|
-
for (
|
|
697
|
-
|
|
695
|
+
for (const item in this.unsupportedWords) {
|
|
696
|
+
const text = this.unsupportedWords[item];
|
|
698
697
|
if(text === word){
|
|
699
698
|
return true
|
|
700
699
|
}
|
|
@@ -724,15 +723,15 @@ class SQLLiteEngine {
|
|
|
724
723
|
}
|
|
725
724
|
|
|
726
725
|
_buildSQLEqualTo(model){
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
726
|
+
const $that = this;
|
|
727
|
+
let argument = null;
|
|
728
|
+
const dirtyFields = model.__dirtyFields;
|
|
730
729
|
|
|
731
|
-
for (
|
|
730
|
+
for (const column in dirtyFields) {
|
|
732
731
|
|
|
733
732
|
// Validate non-nullable constraints on updates
|
|
734
|
-
|
|
735
|
-
|
|
733
|
+
const fieldName = dirtyFields[column];
|
|
734
|
+
const entityDef = model.__entity[fieldName];
|
|
736
735
|
if(entityDef && entityDef.nullable === false && entityDef.primary !== true){
|
|
737
736
|
// Determine the value that will actually be persisted for this field
|
|
738
737
|
var persistedValue;
|
|
@@ -746,20 +745,20 @@ class SQLLiteEngine {
|
|
|
746
745
|
default:
|
|
747
746
|
persistedValue = model[fieldName];
|
|
748
747
|
}
|
|
749
|
-
|
|
748
|
+
const isEmptyString = (typeof persistedValue === 'string') && (persistedValue.trim() === '');
|
|
750
749
|
if(persistedValue === undefined || persistedValue === null || isEmptyString){
|
|
751
750
|
throw `Entity ${model.__entity.__name} column ${fieldName} is a required Field`;
|
|
752
751
|
}
|
|
753
752
|
}
|
|
754
753
|
|
|
755
|
-
|
|
754
|
+
let type = model.__entity[dirtyFields[column]].type;
|
|
756
755
|
|
|
757
756
|
if(model.__entity[dirtyFields[column]].relationshipType === "belongsTo"){
|
|
758
757
|
type = "belongsTo";
|
|
759
758
|
}
|
|
760
759
|
// TODO Boolean value is a string with a letter
|
|
761
760
|
switch(type){
|
|
762
|
-
case "belongsTo" :
|
|
761
|
+
case "belongsTo" : {
|
|
763
762
|
const foreignKey = model.__entity[dirtyFields[column]].foreignKey;
|
|
764
763
|
let fkValue = model[dirtyFields[column]];
|
|
765
764
|
// 🔥 NEW: Validate foreign key type
|
|
@@ -768,8 +767,11 @@ class SQLLiteEngine {
|
|
|
768
767
|
} catch(typeError) {
|
|
769
768
|
throw new Error(`UPDATE failed: ${typeError.message}`);
|
|
770
769
|
}
|
|
771
|
-
|
|
770
|
+
// Append (don't overwrite) — a belongsTo field after other
|
|
771
|
+
// dirty fields must not wipe the accumulated SET clause.
|
|
772
|
+
argument = argument === null ? `${foreignKey} = ${fkValue},` : `${argument} ${foreignKey} = ${fkValue},`;
|
|
772
773
|
break;
|
|
774
|
+
}
|
|
773
775
|
case "integer" :
|
|
774
776
|
//model.__entity[dirtyFields[column]].skipGetFunction = true;
|
|
775
777
|
var columneValue = model[`_${dirtyFields[column]}`];
|
|
@@ -820,10 +822,6 @@ class SQLLiteEngine {
|
|
|
820
822
|
}
|
|
821
823
|
argument = argument === null ? `[${dirtyFields[column]}] = '${timeValue}',` : `${argument} [${dirtyFields[column]}] = '${timeValue}',`;
|
|
822
824
|
break;
|
|
823
|
-
case "belongsTo" :
|
|
824
|
-
var fore = `_${dirtyFields[column]}`;
|
|
825
|
-
argument = argument === null ? `[${model.__entity[dirtyFields[column]].foreignKey}] = '${model[fore]}',` : `${argument} [${model.__entity[dirtyFields[column]].foreignKey}] = '${model[fore]}',`;
|
|
826
|
-
break;
|
|
827
825
|
case "hasMany" :
|
|
828
826
|
argument = argument === null ? `[${dirtyFields[column]}] = '${model[dirtyFields[column]]}',` : `${argument} [${dirtyFields[column]}] = '${model[dirtyFields[column]]}',`;
|
|
829
827
|
break;
|
|
@@ -847,29 +845,29 @@ class SQLLiteEngine {
|
|
|
847
845
|
* This prevents SQL injection by separating SQL structure from values
|
|
848
846
|
*/
|
|
849
847
|
_buildSQLEqualToParameterized(model){
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
848
|
+
const $that = this;
|
|
849
|
+
const sqlParts = [];
|
|
850
|
+
const params = [];
|
|
851
|
+
const dirtyFields = model.__dirtyFields;
|
|
854
852
|
|
|
855
|
-
for (
|
|
853
|
+
for (const column in dirtyFields) {
|
|
856
854
|
// Validate non-nullable constraints on updates
|
|
857
|
-
|
|
858
|
-
|
|
855
|
+
const fieldName = dirtyFields[column];
|
|
856
|
+
const entityDef = model.__entity[fieldName];
|
|
859
857
|
if(entityDef && entityDef.nullable === false && entityDef.primary !== true){
|
|
860
858
|
// Read the raw backing field to get the set()-transformed value,
|
|
861
859
|
// bypassing get() which may change the type (e.g. parseFloat)
|
|
862
|
-
|
|
860
|
+
let persistedValue = model["_" + fieldName];
|
|
863
861
|
if(persistedValue === undefined){
|
|
864
862
|
persistedValue = model[fieldName];
|
|
865
863
|
}
|
|
866
|
-
|
|
864
|
+
const isEmptyString = (typeof persistedValue === 'string') && (persistedValue.trim() === '');
|
|
867
865
|
if(persistedValue === undefined || persistedValue === null || isEmptyString){
|
|
868
866
|
throw `Entity ${model.__entity.__name} column ${fieldName} is a required Field`;
|
|
869
867
|
}
|
|
870
868
|
}
|
|
871
869
|
|
|
872
|
-
|
|
870
|
+
let type = model.__entity[dirtyFields[column]].type;
|
|
873
871
|
|
|
874
872
|
if(model.__entity[dirtyFields[column]].relationshipType === "belongsTo"){
|
|
875
873
|
type = "belongsTo";
|
|
@@ -877,7 +875,7 @@ class SQLLiteEngine {
|
|
|
877
875
|
|
|
878
876
|
// Build parameterized SET clause
|
|
879
877
|
switch(type){
|
|
880
|
-
case "belongsTo":
|
|
878
|
+
case "belongsTo": {
|
|
881
879
|
const foreignKey = model.__entity[dirtyFields[column]].foreignKey;
|
|
882
880
|
let fkValue = model[dirtyFields[column]];
|
|
883
881
|
|
|
@@ -897,6 +895,7 @@ class SQLLiteEngine {
|
|
|
897
895
|
sqlParts.push(`[${foreignKey}] = ?`);
|
|
898
896
|
params.push(model[fore]);
|
|
899
897
|
break;
|
|
898
|
+
}
|
|
900
899
|
case "integer":
|
|
901
900
|
var intValue = model["_" + dirtyFields[column]];
|
|
902
901
|
|
|
@@ -982,7 +981,7 @@ class SQLLiteEngine {
|
|
|
982
981
|
// case above. Run the toDatabase transformer here too so that
|
|
983
982
|
// fields with a serializer (e.g. JSON text columns) get their
|
|
984
983
|
// object values turned into scalars before they reach better-sqlite3.
|
|
985
|
-
|
|
984
|
+
let rawValue = model["_" + dirtyFields[column]];
|
|
986
985
|
if (rawValue === undefined) {
|
|
987
986
|
rawValue = model[dirtyFields[column]];
|
|
988
987
|
}
|
|
@@ -1010,9 +1009,9 @@ class SQLLiteEngine {
|
|
|
1010
1009
|
|
|
1011
1010
|
|
|
1012
1011
|
_buildDeleteObject(currentModel){
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1012
|
+
const primaryKey = currentModel.__Key === undefined ? tools.getPrimaryKeyObject(currentModel.__entity) : currentModel.__Key;
|
|
1013
|
+
const value = currentModel.__value === undefined ? currentModel[primaryKey] : currentModel.__value;
|
|
1014
|
+
const tableName = currentModel.__tableName === undefined ? currentModel.__entity.__name : currentModel.__tableName;
|
|
1016
1015
|
return {tableName: tableName, primaryKey : primaryKey, value : value};
|
|
1017
1016
|
}
|
|
1018
1017
|
|
|
@@ -1116,14 +1115,14 @@ class SQLLiteEngine {
|
|
|
1116
1115
|
|
|
1117
1116
|
// return columns and value strings
|
|
1118
1117
|
_buildSQLInsertObject(fields, modelEntity){
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
for (
|
|
1118
|
+
const $that = this;
|
|
1119
|
+
let columns = null;
|
|
1120
|
+
let values = null;
|
|
1121
|
+
for (let column in modelEntity) {
|
|
1123
1122
|
// column1 = value1, column2 = value2, ...
|
|
1124
1123
|
if(column.indexOf("__") === -1 ){
|
|
1125
1124
|
// call the get method if avlable
|
|
1126
|
-
|
|
1125
|
+
let fieldColumn = "";
|
|
1127
1126
|
// check if get function is avaliable if so use that
|
|
1128
1127
|
fieldColumn = fields[column];
|
|
1129
1128
|
|
|
@@ -1140,11 +1139,11 @@ class SQLLiteEngine {
|
|
|
1140
1139
|
fieldColumn = `'${$that._santizeSingleQuotes(fieldColumn, { entityName: modelEntity.__name, fieldName: column })}'`;
|
|
1141
1140
|
break;
|
|
1142
1141
|
case "time" :
|
|
1143
|
-
|
|
1142
|
+
// time values are inserted as-is (no quoting transform)
|
|
1144
1143
|
break;
|
|
1145
1144
|
}
|
|
1146
1145
|
|
|
1147
|
-
|
|
1146
|
+
const relationship = modelEntity[column].relationshipType
|
|
1148
1147
|
if(relationship === "belongsTo"){
|
|
1149
1148
|
column = modelEntity[column].foreignKey
|
|
1150
1149
|
}
|
|
@@ -1159,7 +1158,7 @@ class SQLLiteEngine {
|
|
|
1159
1158
|
case "belongsTo" :
|
|
1160
1159
|
var fieldObject = tools.findTrackedObject(fields.__context.__trackedEntities, column );
|
|
1161
1160
|
if( Object.keys(fieldObject).length > 0){
|
|
1162
|
-
|
|
1161
|
+
const primaryKey = tools.getPrimaryKeyObject(fieldObject.__entity);
|
|
1163
1162
|
fieldColumn = fieldObject[primaryKey];
|
|
1164
1163
|
column = modelEntity[column].foreignKey;
|
|
1165
1164
|
// Use bracket-quoted identifiers for SQLite column names
|
|
@@ -1185,14 +1184,14 @@ class SQLLiteEngine {
|
|
|
1185
1184
|
* This prevents SQL injection by separating SQL structure from values
|
|
1186
1185
|
*/
|
|
1187
1186
|
_buildSQLInsertObjectParameterized(fields, modelEntity){
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1187
|
+
const $that = this;
|
|
1188
|
+
const columnNames = [];
|
|
1189
|
+
const params = [];
|
|
1191
1190
|
|
|
1192
|
-
for (
|
|
1191
|
+
for (const column in modelEntity) {
|
|
1193
1192
|
// Skip internal properties
|
|
1194
1193
|
if(column.indexOf("__") === -1 ){
|
|
1195
|
-
|
|
1194
|
+
let fieldColumn = fields[column];
|
|
1196
1195
|
|
|
1197
1196
|
// 🔥 FIX: For belongsTo relationships, also check the foreignKey field name
|
|
1198
1197
|
// Users can set either orgRole.User = obj OR orgRole.user_id = 2
|
|
@@ -1225,7 +1224,7 @@ class SQLLiteEngine {
|
|
|
1225
1224
|
// Convert to database-specific format (e.g., boolean → 1/0 for SQLite)
|
|
1226
1225
|
fieldColumn = $that._convertValueForDatabase(fieldColumn, modelEntity[column].type);
|
|
1227
1226
|
|
|
1228
|
-
|
|
1227
|
+
const relationship = modelEntity[column].relationshipType;
|
|
1229
1228
|
var actualColumn = relationship === "belongsTo" ? modelEntity[column].foreignKey : column;
|
|
1230
1229
|
|
|
1231
1230
|
// Add column name and parameter
|
|
@@ -1237,7 +1236,7 @@ class SQLLiteEngine {
|
|
|
1237
1236
|
case "belongsTo":
|
|
1238
1237
|
var fieldObject = tools.findTrackedObject(fields.__context.__trackedEntities, column);
|
|
1239
1238
|
if(Object.keys(fieldObject).length > 0){
|
|
1240
|
-
|
|
1239
|
+
const primaryKey = tools.getPrimaryKeyObject(fieldObject.__entity);
|
|
1241
1240
|
fieldColumn = fieldObject[primaryKey];
|
|
1242
1241
|
var actualColumn = modelEntity[column].foreignKey;
|
|
1243
1242
|
columnNames.push(`[${actualColumn}]`);
|
|
@@ -1253,7 +1252,7 @@ class SQLLiteEngine {
|
|
|
1253
1252
|
|
|
1254
1253
|
if(columnNames.length > 0){
|
|
1255
1254
|
// Create placeholders: ?, ?, ?, ...
|
|
1256
|
-
|
|
1255
|
+
const placeholders = params.map(() => '?').join(', ');
|
|
1257
1256
|
return {
|
|
1258
1257
|
tableName: modelEntity.__name,
|
|
1259
1258
|
columns: columnNames.join(', '),
|
|
@@ -1271,15 +1270,15 @@ class SQLLiteEngine {
|
|
|
1271
1270
|
return value.replace(/'/g, "''");
|
|
1272
1271
|
}
|
|
1273
1272
|
else{
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1273
|
+
const details = context || {};
|
|
1274
|
+
const entityName = details.entityName || 'UnknownEntity';
|
|
1275
|
+
const fieldName = details.fieldName || 'UnknownField';
|
|
1276
|
+
const valueType = (value === null) ? 'null' : (value === undefined ? 'undefined' : typeof value);
|
|
1277
|
+
let preview;
|
|
1279
1278
|
try{ preview = (value === null || value === undefined) ? String(value) : JSON.stringify(value); }
|
|
1280
1279
|
catch(_){ preview = '[unserializable]'; }
|
|
1281
1280
|
if(preview && preview.length > 120){ preview = preview.substring(0, 120) + '…'; }
|
|
1282
|
-
|
|
1281
|
+
const message = `Field is not a string: entity=${entityName}, field=${fieldName}, type=${valueType}, value=${preview}`;
|
|
1283
1282
|
console.error(message);
|
|
1284
1283
|
throw new Error(message);
|
|
1285
1284
|
}
|
|
@@ -1288,7 +1287,7 @@ class SQLLiteEngine {
|
|
|
1288
1287
|
// converts any object into SQL parameter select string
|
|
1289
1288
|
_convertEntityToSelectParameterString(obj, entityName){
|
|
1290
1289
|
// todo: loop throgh object and append string with comma to
|
|
1291
|
-
|
|
1290
|
+
let mainString = "";
|
|
1292
1291
|
const entries = Object.keys(obj);
|
|
1293
1292
|
|
|
1294
1293
|
for (const [name] of entries) {
|