masterrecord 1.1.5 → 1.1.6
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 +14 -0
- package/Entity/entityModelBuilder.js +7 -7
- package/Entity/entityTrackerModel.js +15 -15
- package/Migrations/cli.js +126 -121
- package/Migrations/migrationMySQLQuery.js +21 -27
- package/Migrations/migrationPostgresQuery.js +16 -16
- package/Migrations/migrationSQLiteQuery.js +14 -14
- package/Migrations/migrations.js +41 -45
- package/Migrations/schema.js +3 -3
- package/QueryLanguage/queryMethods.js +26 -26
- package/QueryLanguage/queryScript.js +28 -28
- package/SQLLiteEngine.js +116 -118
- package/Tools.js +19 -19
- package/context.js +1 -1
- package/mySQLEngine.js +3 -3
- package/package.json +9 -2
- package/postgresEngine.js +6 -6
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);
|
|
@@ -169,8 +169,8 @@ class SQLLiteEngine {
|
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
async getCount(queryObject, entity, context){
|
|
172
|
-
|
|
173
|
-
|
|
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");
|
|
@@ -243,7 +243,7 @@ class SQLLiteEngine {
|
|
|
243
243
|
}
|
|
244
244
|
|
|
245
245
|
buildCount(query, mainQuery){
|
|
246
|
-
|
|
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]})`
|
|
@@ -259,7 +259,7 @@ class SQLLiteEngine {
|
|
|
259
259
|
|
|
260
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,10 @@ class SQLLiteEngine {
|
|
|
324
324
|
|
|
325
325
|
buildOrderBy(query, entity){
|
|
326
326
|
// ORDER BY column1, column2, ... ASC|DESC;
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
327
|
+
const $that = this;
|
|
328
|
+
let orderByType = "ASC";
|
|
329
|
+
let orderByEntity = query.orderBy;
|
|
330
|
+
let strQuery = "";
|
|
331
331
|
if(orderByEntity === false){
|
|
332
332
|
orderByType = "DESC";
|
|
333
333
|
orderByEntity = query.orderByDesc;
|
|
@@ -342,7 +342,7 @@ class SQLLiteEngine {
|
|
|
342
342
|
}
|
|
343
343
|
}
|
|
344
344
|
}
|
|
345
|
-
|
|
345
|
+
const entityAlias = this.getEntity(query.parentName, query.entityMap);
|
|
346
346
|
const fieldList = [];
|
|
347
347
|
for (const item in orderByEntity.selectFields) {
|
|
348
348
|
fieldList.push(`${entityAlias}.${orderByEntity.selectFields[item]}`);
|
|
@@ -374,25 +374,25 @@ class SQLLiteEngine {
|
|
|
374
374
|
buildAnd(query, mainQuery){
|
|
375
375
|
// loop through the AND
|
|
376
376
|
// loop update ther where .expr
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
377
|
+
const andEntity = query.and;
|
|
378
|
+
const $that = this;
|
|
379
|
+
let str = "";
|
|
380
380
|
|
|
381
381
|
if(andEntity){
|
|
382
|
-
|
|
382
|
+
let entity = this.getEntity(query.parentName, query.entityMap);
|
|
383
383
|
var andList = [];
|
|
384
|
-
for (
|
|
385
|
-
|
|
386
|
-
for (
|
|
387
|
-
|
|
384
|
+
for (const entityPart in andEntity) { // loop through list of and's
|
|
385
|
+
const itemEntity = andEntity[entityPart]; // get the entityANd
|
|
386
|
+
for (const table in itemEntity[query.parentName]) { // find the main table
|
|
387
|
+
const item = itemEntity[query.parentName][table];
|
|
388
388
|
const expressions = [];
|
|
389
|
-
for (
|
|
389
|
+
for (const exp in item.expressions) {
|
|
390
390
|
// Use the field name verbatim for SQL emission; only
|
|
391
391
|
// use the capitalized form for the navigational
|
|
392
392
|
// relationship lookup (those are PascalCase keys).
|
|
393
393
|
const originalField = item.expressions[exp].field;
|
|
394
394
|
const capitalized = tools.capitalizeFirstLetter(originalField);
|
|
395
|
-
|
|
395
|
+
let field = originalField;
|
|
396
396
|
if(mainQuery[capitalized]){
|
|
397
397
|
if(mainQuery[capitalized].isNavigational){
|
|
398
398
|
entity = $that.getEntity(capitalized, query.entityMap);
|
|
@@ -411,7 +411,7 @@ class SQLLiteEngine {
|
|
|
411
411
|
expressions.push(`${entity}.${field} ${item.expressions[exp].func} ${item.expressions[exp].arg}`);
|
|
412
412
|
}else{
|
|
413
413
|
// Check if arg is a parameterized placeholder
|
|
414
|
-
|
|
414
|
+
const isPlaceholder = (item.expressions[exp].arg === '?' || /^\$\d+$/.test(item.expressions[exp].arg));
|
|
415
415
|
if(isPlaceholder){
|
|
416
416
|
expressions.push(`${entity}.${field} ${item.expressions[exp].func} ${item.expressions[exp].arg}`);
|
|
417
417
|
}else{
|
|
@@ -433,16 +433,16 @@ class SQLLiteEngine {
|
|
|
433
433
|
}
|
|
434
434
|
|
|
435
435
|
buildWhere(query, mainQuery){
|
|
436
|
-
|
|
436
|
+
const whereEntity = query.where;
|
|
437
437
|
|
|
438
|
-
|
|
438
|
+
const $that = this;
|
|
439
439
|
if(!whereEntity){
|
|
440
440
|
return "";
|
|
441
441
|
}
|
|
442
442
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
443
|
+
const entityAlias = this.getEntity(query.parentName, query.entityMap);
|
|
444
|
+
const item = whereEntity[query.parentName].query;
|
|
445
|
+
const exprs = item.expressions || [];
|
|
446
446
|
|
|
447
447
|
function exprToSql(expr){
|
|
448
448
|
// Preserve case for column emission. SQLite identifier matching
|
|
@@ -450,8 +450,8 @@ class SQLLiteEngine {
|
|
|
450
450
|
// practice, but it would have hidden a bug if SQLite were ever
|
|
451
451
|
// configured with strict case-sensitive identifiers, and it
|
|
452
452
|
// produced inconsistent SQL output across engines.
|
|
453
|
-
|
|
454
|
-
|
|
453
|
+
let field = expr.field;
|
|
454
|
+
let ent = entityAlias;
|
|
455
455
|
if(mainQuery[field]){
|
|
456
456
|
if(mainQuery[field].isNavigational){
|
|
457
457
|
ent = $that.getEntity(field, query.entityMap);
|
|
@@ -462,7 +462,7 @@ class SQLLiteEngine {
|
|
|
462
462
|
}
|
|
463
463
|
}
|
|
464
464
|
let func = expr.func;
|
|
465
|
-
|
|
465
|
+
const arg = expr.arg;
|
|
466
466
|
if((!func && typeof arg === 'undefined')){
|
|
467
467
|
return null;
|
|
468
468
|
}
|
|
@@ -481,7 +481,7 @@ class SQLLiteEngine {
|
|
|
481
481
|
return `${ent}.${field} ${func} ${arg}`;
|
|
482
482
|
}
|
|
483
483
|
// Check if arg is a parameterized placeholder (? for MySQL/SQLite, $1/$2/etc for Postgres)
|
|
484
|
-
|
|
484
|
+
const isPlaceholder = (arg === '?' || /^\$\d+$/.test(arg));
|
|
485
485
|
if(isPlaceholder){
|
|
486
486
|
// Don't quote placeholders - they must remain as bare ? or $1
|
|
487
487
|
return `${ent}.${field} ${func} ${arg}`;
|
|
@@ -518,23 +518,23 @@ class SQLLiteEngine {
|
|
|
518
518
|
|
|
519
519
|
buildInclude( query, entity, context){
|
|
520
520
|
const includeQueries = [];
|
|
521
|
-
for (
|
|
522
|
-
|
|
523
|
-
|
|
521
|
+
for (const part in query.include) {
|
|
522
|
+
const includeEntity = query.include[part];
|
|
523
|
+
const $that = this;
|
|
524
524
|
if(includeEntity){
|
|
525
|
-
|
|
526
|
-
|
|
525
|
+
const parentObj = includeEntity[query.parentName];
|
|
526
|
+
let currentContext = "";
|
|
527
527
|
if(includeEntity.selectFields){
|
|
528
528
|
currentContext = context[tools.capitalizeFirstLetter(includeEntity.selectFields[0])];
|
|
529
529
|
}
|
|
530
530
|
|
|
531
531
|
if(parentObj){
|
|
532
532
|
parentObj.entityMap = query.entityMap;
|
|
533
|
-
|
|
534
|
-
|
|
533
|
+
let foreignKey = $that.getForeignKey(entity.__name, currentContext.__entity);
|
|
534
|
+
let mainPrimaryKey = $that.getPrimarykey(entity);
|
|
535
535
|
var mainEntity = $that.getEntity(entity.__name, query.entityMap);
|
|
536
536
|
if(currentContext.__entity[entity.__name].type === "hasManyThrough"){
|
|
537
|
-
|
|
537
|
+
const foreignTable = tools.capitalizeFirstLetter(currentContext.__entity[entity.__name].foreignTable); //to uppercase letter
|
|
538
538
|
foreignKey = $that.getPrimarykey(currentContext.__entity);
|
|
539
539
|
mainPrimaryKey = context[foreignTable].__entity[currentContext.__entity.__name].foreignKey;
|
|
540
540
|
var mainEntity = $that.getEntity(foreignTable,query.entityMap);
|
|
@@ -548,7 +548,7 @@ class SQLLiteEngine {
|
|
|
548
548
|
parentObj.select.selectFields.push(foreignKey);
|
|
549
549
|
}
|
|
550
550
|
|
|
551
|
-
|
|
551
|
+
const innerQuery = $that.buildQuery(parentObj, currentContext.__entity, context);
|
|
552
552
|
|
|
553
553
|
includeQueries.push(`LEFT JOIN (${innerQuery.query}) AS ${innerQuery.entity} ON ${ mainEntity}.${mainPrimaryKey} = ${innerQuery.entity}.${foreignKey}`);
|
|
554
554
|
|
|
@@ -559,7 +559,7 @@ class SQLLiteEngine {
|
|
|
559
559
|
}
|
|
560
560
|
|
|
561
561
|
buildFrom(query, entity){
|
|
562
|
-
|
|
562
|
+
const entityName = this.getEntity(entity.__name, query.entityMap);
|
|
563
563
|
if(entityName ){
|
|
564
564
|
return `FROM ${entity.__name } AS ${entityName}`;
|
|
565
565
|
}
|
|
@@ -568,9 +568,9 @@ class SQLLiteEngine {
|
|
|
568
568
|
|
|
569
569
|
buildSelect(query, entity){
|
|
570
570
|
// this means that there is a select statement
|
|
571
|
-
|
|
571
|
+
const select = "SELECT";
|
|
572
572
|
const arr = [];
|
|
573
|
-
|
|
573
|
+
const $that = this;
|
|
574
574
|
if(query.select){
|
|
575
575
|
for (const item in query.select.selectFields) {
|
|
576
576
|
arr.push(`${$that.getEntity(entity.__name, query.entityMap)}.${query.select.selectFields[item]}`);
|
|
@@ -578,7 +578,7 @@ class SQLLiteEngine {
|
|
|
578
578
|
|
|
579
579
|
}
|
|
580
580
|
else{
|
|
581
|
-
|
|
581
|
+
const entityList = this.getEntityList(entity);
|
|
582
582
|
for (const item in entityList) {
|
|
583
583
|
arr.push(`${$that.getEntity(entity.__name, query.entityMap)}.${entityList[item]}`);
|
|
584
584
|
};
|
|
@@ -609,9 +609,9 @@ class SQLLiteEngine {
|
|
|
609
609
|
}
|
|
610
610
|
|
|
611
611
|
getInclude(name, query){
|
|
612
|
-
|
|
612
|
+
const include = query.include;
|
|
613
613
|
if(include){
|
|
614
|
-
for (
|
|
614
|
+
for (const part in include) {
|
|
615
615
|
if(tools.capitalizeFirstLetter(include[part].selectFields[0]) === name){
|
|
616
616
|
return include[part];
|
|
617
617
|
}
|
|
@@ -623,8 +623,8 @@ class SQLLiteEngine {
|
|
|
623
623
|
}
|
|
624
624
|
|
|
625
625
|
getEntity(name, maps){
|
|
626
|
-
for (
|
|
627
|
-
|
|
626
|
+
for (const item in maps) {
|
|
627
|
+
const map = maps[item];
|
|
628
628
|
if(tools.capitalizeFirstLetter(name) === tools.capitalizeFirstLetter(map.name)){
|
|
629
629
|
return map.entity
|
|
630
630
|
}
|
|
@@ -639,9 +639,9 @@ class SQLLiteEngine {
|
|
|
639
639
|
|
|
640
640
|
// return a list of entity names and skip foreign keys and underscore.
|
|
641
641
|
getEntityList(entity){
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
for (
|
|
642
|
+
const entitiesList = [];
|
|
643
|
+
const $that = this;
|
|
644
|
+
for (const ent in entity) {
|
|
645
645
|
if(!ent.startsWith("_")){
|
|
646
646
|
// Skip lifecycle hooks - they are not database columns
|
|
647
647
|
if(entity[ent].lifecycle === true){
|
|
@@ -668,7 +668,7 @@ class SQLLiteEngine {
|
|
|
668
668
|
else{
|
|
669
669
|
|
|
670
670
|
if(entity[ent].relationshipType === "belongsTo"){
|
|
671
|
-
|
|
671
|
+
const name = entity[ent].foreignKey;
|
|
672
672
|
if($that.chechUnsupportedWords(name)){
|
|
673
673
|
entitiesList.push(`'${name}'`);
|
|
674
674
|
//entitiesList.push(`'${ent}'`);
|
|
@@ -693,8 +693,8 @@ class SQLLiteEngine {
|
|
|
693
693
|
return entitiesList
|
|
694
694
|
}
|
|
695
695
|
chechUnsupportedWords(word){
|
|
696
|
-
for (
|
|
697
|
-
|
|
696
|
+
for (const item in this.unsupportedWords) {
|
|
697
|
+
const text = this.unsupportedWords[item];
|
|
698
698
|
if(text === word){
|
|
699
699
|
return true
|
|
700
700
|
}
|
|
@@ -724,15 +724,15 @@ class SQLLiteEngine {
|
|
|
724
724
|
}
|
|
725
725
|
|
|
726
726
|
_buildSQLEqualTo(model){
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
727
|
+
const $that = this;
|
|
728
|
+
let argument = null;
|
|
729
|
+
const dirtyFields = model.__dirtyFields;
|
|
730
730
|
|
|
731
|
-
for (
|
|
731
|
+
for (const column in dirtyFields) {
|
|
732
732
|
|
|
733
733
|
// Validate non-nullable constraints on updates
|
|
734
|
-
|
|
735
|
-
|
|
734
|
+
const fieldName = dirtyFields[column];
|
|
735
|
+
const entityDef = model.__entity[fieldName];
|
|
736
736
|
if(entityDef && entityDef.nullable === false && entityDef.primary !== true){
|
|
737
737
|
// Determine the value that will actually be persisted for this field
|
|
738
738
|
var persistedValue;
|
|
@@ -746,13 +746,13 @@ class SQLLiteEngine {
|
|
|
746
746
|
default:
|
|
747
747
|
persistedValue = model[fieldName];
|
|
748
748
|
}
|
|
749
|
-
|
|
749
|
+
const isEmptyString = (typeof persistedValue === 'string') && (persistedValue.trim() === '');
|
|
750
750
|
if(persistedValue === undefined || persistedValue === null || isEmptyString){
|
|
751
751
|
throw `Entity ${model.__entity.__name} column ${fieldName} is a required Field`;
|
|
752
752
|
}
|
|
753
753
|
}
|
|
754
754
|
|
|
755
|
-
|
|
755
|
+
let type = model.__entity[dirtyFields[column]].type;
|
|
756
756
|
|
|
757
757
|
if(model.__entity[dirtyFields[column]].relationshipType === "belongsTo"){
|
|
758
758
|
type = "belongsTo";
|
|
@@ -768,7 +768,9 @@ class SQLLiteEngine {
|
|
|
768
768
|
} catch(typeError) {
|
|
769
769
|
throw new Error(`UPDATE failed: ${typeError.message}`);
|
|
770
770
|
}
|
|
771
|
-
|
|
771
|
+
// Append (don't overwrite) — a belongsTo field after other
|
|
772
|
+
// dirty fields must not wipe the accumulated SET clause.
|
|
773
|
+
argument = argument === null ? `${foreignKey} = ${fkValue},` : `${argument} ${foreignKey} = ${fkValue},`;
|
|
772
774
|
break;
|
|
773
775
|
case "integer" :
|
|
774
776
|
//model.__entity[dirtyFields[column]].skipGetFunction = true;
|
|
@@ -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";
|
|
@@ -982,7 +980,7 @@ class SQLLiteEngine {
|
|
|
982
980
|
// case above. Run the toDatabase transformer here too so that
|
|
983
981
|
// fields with a serializer (e.g. JSON text columns) get their
|
|
984
982
|
// object values turned into scalars before they reach better-sqlite3.
|
|
985
|
-
|
|
983
|
+
let rawValue = model["_" + dirtyFields[column]];
|
|
986
984
|
if (rawValue === undefined) {
|
|
987
985
|
rawValue = model[dirtyFields[column]];
|
|
988
986
|
}
|
|
@@ -1010,9 +1008,9 @@ class SQLLiteEngine {
|
|
|
1010
1008
|
|
|
1011
1009
|
|
|
1012
1010
|
_buildDeleteObject(currentModel){
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1011
|
+
const primaryKey = currentModel.__Key === undefined ? tools.getPrimaryKeyObject(currentModel.__entity) : currentModel.__Key;
|
|
1012
|
+
const value = currentModel.__value === undefined ? currentModel[primaryKey] : currentModel.__value;
|
|
1013
|
+
const tableName = currentModel.__tableName === undefined ? currentModel.__entity.__name : currentModel.__tableName;
|
|
1016
1014
|
return {tableName: tableName, primaryKey : primaryKey, value : value};
|
|
1017
1015
|
}
|
|
1018
1016
|
|
|
@@ -1116,14 +1114,14 @@ class SQLLiteEngine {
|
|
|
1116
1114
|
|
|
1117
1115
|
// return columns and value strings
|
|
1118
1116
|
_buildSQLInsertObject(fields, modelEntity){
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
for (
|
|
1117
|
+
const $that = this;
|
|
1118
|
+
let columns = null;
|
|
1119
|
+
let values = null;
|
|
1120
|
+
for (let column in modelEntity) {
|
|
1123
1121
|
// column1 = value1, column2 = value2, ...
|
|
1124
1122
|
if(column.indexOf("__") === -1 ){
|
|
1125
1123
|
// call the get method if avlable
|
|
1126
|
-
|
|
1124
|
+
let fieldColumn = "";
|
|
1127
1125
|
// check if get function is avaliable if so use that
|
|
1128
1126
|
fieldColumn = fields[column];
|
|
1129
1127
|
|
|
@@ -1140,11 +1138,11 @@ class SQLLiteEngine {
|
|
|
1140
1138
|
fieldColumn = `'${$that._santizeSingleQuotes(fieldColumn, { entityName: modelEntity.__name, fieldName: column })}'`;
|
|
1141
1139
|
break;
|
|
1142
1140
|
case "time" :
|
|
1143
|
-
|
|
1141
|
+
// time values are inserted as-is (no quoting transform)
|
|
1144
1142
|
break;
|
|
1145
1143
|
}
|
|
1146
1144
|
|
|
1147
|
-
|
|
1145
|
+
const relationship = modelEntity[column].relationshipType
|
|
1148
1146
|
if(relationship === "belongsTo"){
|
|
1149
1147
|
column = modelEntity[column].foreignKey
|
|
1150
1148
|
}
|
|
@@ -1159,7 +1157,7 @@ class SQLLiteEngine {
|
|
|
1159
1157
|
case "belongsTo" :
|
|
1160
1158
|
var fieldObject = tools.findTrackedObject(fields.__context.__trackedEntities, column );
|
|
1161
1159
|
if( Object.keys(fieldObject).length > 0){
|
|
1162
|
-
|
|
1160
|
+
const primaryKey = tools.getPrimaryKeyObject(fieldObject.__entity);
|
|
1163
1161
|
fieldColumn = fieldObject[primaryKey];
|
|
1164
1162
|
column = modelEntity[column].foreignKey;
|
|
1165
1163
|
// Use bracket-quoted identifiers for SQLite column names
|
|
@@ -1185,14 +1183,14 @@ class SQLLiteEngine {
|
|
|
1185
1183
|
* This prevents SQL injection by separating SQL structure from values
|
|
1186
1184
|
*/
|
|
1187
1185
|
_buildSQLInsertObjectParameterized(fields, modelEntity){
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1186
|
+
const $that = this;
|
|
1187
|
+
const columnNames = [];
|
|
1188
|
+
const params = [];
|
|
1191
1189
|
|
|
1192
|
-
for (
|
|
1190
|
+
for (const column in modelEntity) {
|
|
1193
1191
|
// Skip internal properties
|
|
1194
1192
|
if(column.indexOf("__") === -1 ){
|
|
1195
|
-
|
|
1193
|
+
let fieldColumn = fields[column];
|
|
1196
1194
|
|
|
1197
1195
|
// 🔥 FIX: For belongsTo relationships, also check the foreignKey field name
|
|
1198
1196
|
// Users can set either orgRole.User = obj OR orgRole.user_id = 2
|
|
@@ -1225,7 +1223,7 @@ class SQLLiteEngine {
|
|
|
1225
1223
|
// Convert to database-specific format (e.g., boolean → 1/0 for SQLite)
|
|
1226
1224
|
fieldColumn = $that._convertValueForDatabase(fieldColumn, modelEntity[column].type);
|
|
1227
1225
|
|
|
1228
|
-
|
|
1226
|
+
const relationship = modelEntity[column].relationshipType;
|
|
1229
1227
|
var actualColumn = relationship === "belongsTo" ? modelEntity[column].foreignKey : column;
|
|
1230
1228
|
|
|
1231
1229
|
// Add column name and parameter
|
|
@@ -1237,7 +1235,7 @@ class SQLLiteEngine {
|
|
|
1237
1235
|
case "belongsTo":
|
|
1238
1236
|
var fieldObject = tools.findTrackedObject(fields.__context.__trackedEntities, column);
|
|
1239
1237
|
if(Object.keys(fieldObject).length > 0){
|
|
1240
|
-
|
|
1238
|
+
const primaryKey = tools.getPrimaryKeyObject(fieldObject.__entity);
|
|
1241
1239
|
fieldColumn = fieldObject[primaryKey];
|
|
1242
1240
|
var actualColumn = modelEntity[column].foreignKey;
|
|
1243
1241
|
columnNames.push(`[${actualColumn}]`);
|
|
@@ -1253,7 +1251,7 @@ class SQLLiteEngine {
|
|
|
1253
1251
|
|
|
1254
1252
|
if(columnNames.length > 0){
|
|
1255
1253
|
// Create placeholders: ?, ?, ?, ...
|
|
1256
|
-
|
|
1254
|
+
const placeholders = params.map(() => '?').join(', ');
|
|
1257
1255
|
return {
|
|
1258
1256
|
tableName: modelEntity.__name,
|
|
1259
1257
|
columns: columnNames.join(', '),
|
|
@@ -1271,15 +1269,15 @@ class SQLLiteEngine {
|
|
|
1271
1269
|
return value.replace(/'/g, "''");
|
|
1272
1270
|
}
|
|
1273
1271
|
else{
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1272
|
+
const details = context || {};
|
|
1273
|
+
const entityName = details.entityName || 'UnknownEntity';
|
|
1274
|
+
const fieldName = details.fieldName || 'UnknownField';
|
|
1275
|
+
const valueType = (value === null) ? 'null' : (value === undefined ? 'undefined' : typeof value);
|
|
1276
|
+
let preview;
|
|
1279
1277
|
try{ preview = (value === null || value === undefined) ? String(value) : JSON.stringify(value); }
|
|
1280
1278
|
catch(_){ preview = '[unserializable]'; }
|
|
1281
1279
|
if(preview && preview.length > 120){ preview = preview.substring(0, 120) + '…'; }
|
|
1282
|
-
|
|
1280
|
+
const message = `Field is not a string: entity=${entityName}, field=${fieldName}, type=${valueType}, value=${preview}`;
|
|
1283
1281
|
console.error(message);
|
|
1284
1282
|
throw new Error(message);
|
|
1285
1283
|
}
|
|
@@ -1288,7 +1286,7 @@ class SQLLiteEngine {
|
|
|
1288
1286
|
// converts any object into SQL parameter select string
|
|
1289
1287
|
_convertEntityToSelectParameterString(obj, entityName){
|
|
1290
1288
|
// todo: loop throgh object and append string with comma to
|
|
1291
|
-
|
|
1289
|
+
let mainString = "";
|
|
1292
1290
|
const entries = Object.keys(obj);
|
|
1293
1291
|
|
|
1294
1292
|
for (const [name] of entries) {
|