@ruiapp/rapid-core 0.1.82 → 0.2.0

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.
Files changed (46) hide show
  1. package/dist/bootstrapApplicationConfig.d.ts +6 -0
  2. package/dist/helpers/metaHelper.d.ts +3 -3
  3. package/dist/index.js +57 -16
  4. package/dist/plugins/webhooks/pluginConfig.d.ts +1 -0
  5. package/dist/types.d.ts +19 -1
  6. package/package.json +1 -1
  7. package/src/bootstrapApplicationConfig.ts +615 -602
  8. package/src/core/server.ts +142 -142
  9. package/src/dataAccess/columnTypeMapper.ts +22 -22
  10. package/src/dataAccess/dataAccessTypes.ts +151 -151
  11. package/src/dataAccess/entityManager.ts +1499 -1496
  12. package/src/dataAccess/entityMapper.ts +100 -100
  13. package/src/dataAccess/propertyMapper.ts +28 -28
  14. package/src/deno-std/http/cookie.ts +372 -372
  15. package/src/helpers/filterHelper.ts +47 -47
  16. package/src/helpers/metaHelper.ts +80 -76
  17. package/src/helpers/runCollectionEntityActionHandler.ts +27 -27
  18. package/src/index.ts +46 -46
  19. package/src/plugins/auth/AuthPlugin.ts +85 -85
  20. package/src/plugins/cronJob/CronJobPlugin.ts +112 -112
  21. package/src/plugins/dataManage/DataManagePlugin.ts +163 -163
  22. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntities.ts +38 -38
  23. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +22 -22
  24. package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +146 -146
  25. package/src/plugins/fileManage/FileManagePlugin.ts +52 -52
  26. package/src/plugins/fileManage/actionHandlers/downloadFile.ts +44 -44
  27. package/src/plugins/metaManage/MetaManagePlugin.ts +500 -488
  28. package/src/plugins/routeManage/RouteManagePlugin.ts +62 -62
  29. package/src/plugins/sequence/SequencePlugin.ts +136 -136
  30. package/src/plugins/sequence/SequencePluginTypes.ts +69 -69
  31. package/src/plugins/sequence/segments/autoIncrement.ts +78 -78
  32. package/src/plugins/sequence/segments/dayOfMonth.ts +17 -17
  33. package/src/plugins/sequence/segments/literal.ts +14 -14
  34. package/src/plugins/sequence/segments/month.ts +17 -17
  35. package/src/plugins/sequence/segments/parameter.ts +18 -18
  36. package/src/plugins/sequence/segments/year.ts +17 -17
  37. package/src/plugins/setting/SettingPlugin.ts +68 -68
  38. package/src/plugins/setting/SettingPluginTypes.ts +37 -37
  39. package/src/plugins/stateMachine/StateMachinePlugin.ts +186 -186
  40. package/src/plugins/stateMachine/StateMachinePluginTypes.ts +48 -48
  41. package/src/plugins/webhooks/WebhooksPlugin.ts +148 -148
  42. package/src/plugins/webhooks/pluginConfig.ts +1 -0
  43. package/src/queryBuilder/queryBuilder.ts +637 -637
  44. package/src/server.ts +463 -451
  45. package/src/types.ts +659 -637
  46. package/src/utilities/errorUtility.ts +15 -15
@@ -1,637 +1,637 @@
1
- import { find, isBoolean, isNull, isNumber, isString, isUndefined } from "lodash";
2
- import { RpdDataModel, RpdDataModelProperty, CreateEntityOptions, QuoteTableOptions, DatabaseQuery } from "../types";
3
- import {
4
- CountRowOptions,
5
- DeleteRowOptions,
6
- FindRowLogicalFilterOptions,
7
- FindRowRelationalFilterOptions,
8
- FindRowSetFilterOptions,
9
- FindRowUnaryFilterOptions,
10
- FindRowOptions,
11
- RowFilterOptions,
12
- RowFilterRelationalOperators,
13
- UpdateRowOptions,
14
- ColumnSelectOptions,
15
- ColumnNameWithTableName,
16
- DataAccessPgColumnTypes,
17
- FindRowArrayFilterOptions,
18
- } from "~/dataAccess/dataAccessTypes";
19
- import { pgPropertyTypeColumnMap } from "~/dataAccess/columnTypeMapper";
20
-
21
- const objLeftQuoteChar = '"';
22
- const objRightQuoteChar = '"';
23
-
24
- const relationalOperatorsMap = new Map<RowFilterRelationalOperators, string>([
25
- ["eq", "="],
26
- ["ne", "<>"],
27
- ["gt", ">"],
28
- ["gte", ">="],
29
- ["lt", "<"],
30
- ["lte", "<="],
31
- ]);
32
-
33
- export interface BuildQueryContext {
34
- model: RpdDataModel;
35
- builder: QueryBuilder;
36
- params: any[];
37
- emitTableAlias: boolean;
38
- /**
39
- * emit parameter value to sql literal.
40
- */
41
- paramToLiteral: boolean;
42
- }
43
-
44
- export interface InitQueryBuilderOptions {
45
- dbDefaultSchema: string;
46
- }
47
-
48
- export default class QueryBuilder {
49
- #dbDefaultSchema: string;
50
-
51
- constructor(options: InitQueryBuilderOptions) {
52
- this.#dbDefaultSchema = options.dbDefaultSchema;
53
- }
54
-
55
- quoteTable(options: QuoteTableOptions) {
56
- const { schema, tableName } = options;
57
- if (schema) {
58
- return `${this.quoteObject(schema)}.${this.quoteObject(tableName)}`;
59
- } else if (this.#dbDefaultSchema) {
60
- return `${this.quoteObject(this.#dbDefaultSchema)}.${this.quoteObject(tableName)}`;
61
- } else {
62
- return this.quoteObject(tableName);
63
- }
64
- }
65
-
66
- quoteObject(name: string) {
67
- return `${objLeftQuoteChar}${name}${objRightQuoteChar}`;
68
- }
69
-
70
- quoteColumn(model: RpdDataModel, column: ColumnSelectOptions, emitTableAlias: boolean) {
71
- if (typeof column === "string") {
72
- if (emitTableAlias) {
73
- return `${objLeftQuoteChar}${model.tableName}${objRightQuoteChar}.${objLeftQuoteChar}${column}${objRightQuoteChar}`;
74
- } else {
75
- return `${objLeftQuoteChar}${column}${objRightQuoteChar}`;
76
- }
77
- } else {
78
- if (emitTableAlias && column.tableName) {
79
- return `${objLeftQuoteChar}${column.tableName}${objRightQuoteChar}.${objLeftQuoteChar}${column.name}${objRightQuoteChar}`;
80
- } else {
81
- return `${objLeftQuoteChar}${column.name}${objRightQuoteChar}`;
82
- }
83
- }
84
- }
85
-
86
- select(model: RpdDataModel, options: FindRowOptions): DatabaseQuery {
87
- const ctx: BuildQueryContext = {
88
- model,
89
- builder: this,
90
- params: [],
91
- emitTableAlias: true,
92
- paramToLiteral: false,
93
- };
94
- let { fields: columns, filters, orderBy, pagination } = options;
95
- let command = "SELECT ";
96
- if (!columns || !columns.length) {
97
- command += `${this.quoteObject(model.tableName)}.* FROM `;
98
- } else {
99
- command += columns.map((column) => this.quoteColumn(ctx.model, column, ctx.emitTableAlias)).join(", ");
100
- command += " FROM ";
101
- }
102
-
103
- command += this.quoteTable(model);
104
-
105
- if (options.orderBy) {
106
- options.orderBy
107
- .filter((orderByItem) => orderByItem.relationField)
108
- .forEach((orderByItem) => {
109
- const { relationField } = orderByItem;
110
- const orderField = orderByItem.field as ColumnNameWithTableName;
111
- command += ` LEFT JOIN ${this.quoteTable({ schema: orderField.schema, tableName: orderField.tableName })} ON ${this.quoteObject(
112
- orderField.tableName,
113
- )}.id = ${this.quoteObject(relationField.tableName)}.${this.quoteObject(relationField.name)}`;
114
- });
115
- }
116
-
117
- if (filters && filters.length) {
118
- command += " WHERE ";
119
- command += buildFiltersQuery(ctx, filters);
120
- }
121
-
122
- if (orderBy && orderBy.length) {
123
- command += " ORDER BY ";
124
- command += orderBy
125
- .map((item) => {
126
- const quotedName = this.quoteColumn(ctx.model, item.field, ctx.emitTableAlias);
127
- return item.desc ? quotedName + " DESC" : quotedName;
128
- })
129
- .join(", ");
130
- }
131
-
132
- if (pagination) {
133
- command += " OFFSET ";
134
- ctx.params.push(pagination.offset);
135
- command += "$" + ctx.params.length;
136
-
137
- command += " LIMIT ";
138
- ctx.params.push(pagination.limit);
139
- command += "$" + ctx.params.length;
140
- }
141
-
142
- return {
143
- command,
144
- params: ctx.params,
145
- };
146
- }
147
-
148
- selectDerived(derivedModel: RpdDataModel, baseModel: RpdDataModel, options: FindRowOptions): DatabaseQuery {
149
- const ctx: BuildQueryContext = {
150
- model: derivedModel,
151
- builder: this,
152
- params: [],
153
- emitTableAlias: true,
154
- paramToLiteral: false,
155
- };
156
- let { fields: columns, filters, orderBy, pagination } = options;
157
- let command = "SELECT ";
158
- if (!columns || !columns.length) {
159
- command += `${this.quoteObject(derivedModel.tableName)}.* FROM `;
160
- } else {
161
- command += columns
162
- .map((column) => {
163
- return this.quoteColumn(derivedModel, column, ctx.emitTableAlias);
164
- })
165
- .join(", ");
166
- command += " FROM ";
167
- }
168
-
169
- command += `${this.quoteTable(derivedModel)} LEFT JOIN ${this.quoteTable(baseModel)} ON ${this.quoteObject(derivedModel.tableName)}.id = ${this.quoteObject(
170
- baseModel.tableName,
171
- )}.id`;
172
-
173
- if (options.orderBy) {
174
- options.orderBy
175
- .filter((orderByItem) => orderByItem.relationField)
176
- .forEach((orderByItem) => {
177
- const { relationField } = orderByItem;
178
- const orderField = orderByItem.field as ColumnNameWithTableName;
179
- command += ` LEFT JOIN ${this.quoteTable({ schema: orderField.schema, tableName: orderField.tableName })} ON ${this.quoteObject(
180
- orderField.tableName,
181
- )}.id = ${this.quoteObject(relationField.tableName)}.${this.quoteObject(relationField.name)}`;
182
- });
183
- }
184
-
185
- if (filters && filters.length) {
186
- command += " WHERE ";
187
- command += buildFiltersQuery(ctx, filters);
188
- }
189
-
190
- if (orderBy && orderBy.length) {
191
- command += " ORDER BY ";
192
- command += orderBy
193
- .map((item) => {
194
- const quotedName = this.quoteColumn(derivedModel, item.field, ctx.emitTableAlias);
195
- return item.desc ? quotedName + " DESC" : quotedName;
196
- })
197
- .join(", ");
198
- }
199
-
200
- if (pagination) {
201
- command += " OFFSET ";
202
- ctx.params.push(pagination.offset);
203
- command += "$" + ctx.params.length;
204
-
205
- command += " LIMIT ";
206
- ctx.params.push(pagination.limit);
207
- command += "$" + ctx.params.length;
208
- }
209
-
210
- return {
211
- command,
212
- params: ctx.params,
213
- };
214
- }
215
-
216
- count(model: RpdDataModel, options: CountRowOptions): DatabaseQuery {
217
- const ctx: BuildQueryContext = {
218
- model,
219
- builder: this,
220
- params: [],
221
- emitTableAlias: false,
222
- paramToLiteral: false,
223
- };
224
- let { filters } = options;
225
- let command = 'SELECT COUNT(*)::int as "count" FROM ';
226
-
227
- command += this.quoteTable(model);
228
-
229
- if (filters && filters.length) {
230
- command += " WHERE ";
231
- command += buildFiltersQuery(ctx, filters);
232
- }
233
-
234
- return {
235
- command,
236
- params: ctx.params,
237
- };
238
- }
239
-
240
- countDerived(derivedModel: RpdDataModel, baseModel: RpdDataModel, options: CountRowOptions): DatabaseQuery {
241
- const ctx: BuildQueryContext = {
242
- model: derivedModel,
243
- builder: this,
244
- params: [],
245
- emitTableAlias: true,
246
- paramToLiteral: false,
247
- };
248
- let { filters } = options;
249
- let command = 'SELECT COUNT(*)::int as "count" FROM ';
250
-
251
- command += `${this.quoteTable(derivedModel)} LEFT JOIN ${this.quoteTable(baseModel)} ON ${this.quoteObject(derivedModel.tableName)}.id = ${this.quoteObject(
252
- baseModel.tableName,
253
- )}.id`;
254
-
255
- if (filters && filters.length) {
256
- command += " WHERE ";
257
- command += buildFiltersQuery(ctx, filters);
258
- }
259
-
260
- return {
261
- command,
262
- params: ctx.params,
263
- };
264
- }
265
-
266
- insert(model: RpdDataModel, options: CreateEntityOptions): DatabaseQuery {
267
- const params: any[] = [];
268
- const ctx: BuildQueryContext = {
269
- model,
270
- builder: this,
271
- params,
272
- emitTableAlias: false,
273
- paramToLiteral: false,
274
- };
275
- const { entity } = options;
276
- let command = "INSERT INTO ";
277
-
278
- command += this.quoteTable(model);
279
-
280
- const columnNames: string[] = Object.keys(entity);
281
- let values = "";
282
- columnNames.forEach((columnName, index) => {
283
- if (index) {
284
- values += ", ";
285
- }
286
-
287
- let property: RpdDataModelProperty | null = null;
288
- if (model) {
289
- property = find(model.properties, (e: RpdDataModelProperty) => (e.columnName || e.code) === columnName);
290
- }
291
- const columnType: DataAccessPgColumnTypes | null = property ? pgPropertyTypeColumnMap[property.type] : null;
292
- if (columnType === "jsonb") {
293
- params.push(JSON.stringify(entity[columnName]));
294
- values += `$${params.length}::jsonb`;
295
- } else {
296
- params.push(entity[columnName]);
297
- values += `$${params.length}`;
298
- }
299
- });
300
-
301
- command += ` (${columnNames.map(this.quoteObject).join(", ")})`;
302
- command += ` VALUES (${values}) RETURNING *`;
303
-
304
- return {
305
- command,
306
- params: ctx.params,
307
- };
308
- }
309
-
310
- update(model: RpdDataModel, options: UpdateRowOptions): DatabaseQuery {
311
- const params: any[] = [];
312
- const ctx: BuildQueryContext = {
313
- model,
314
- builder: this,
315
- params,
316
- emitTableAlias: false,
317
- paramToLiteral: false,
318
- };
319
- let { entity, filters } = options;
320
- let command = "UPDATE ";
321
-
322
- command += this.quoteTable(model);
323
-
324
- command += " SET ";
325
- const columnNames: string[] = Object.keys(entity);
326
- columnNames.forEach((columnName, index) => {
327
- if (index) {
328
- command += ", ";
329
- }
330
-
331
- command += `${this.quoteObject(columnName)}=`;
332
-
333
- let property: RpdDataModelProperty | null = null;
334
- if (model) {
335
- property = find(model.properties, (e: RpdDataModelProperty) => (e.columnName || e.code) === columnName);
336
- }
337
- const columnType: DataAccessPgColumnTypes | null = property ? pgPropertyTypeColumnMap[property.type] : null;
338
- if (columnType === "jsonb") {
339
- params.push(JSON.stringify(entity[columnName]));
340
- command += `$${params.length}::jsonb`;
341
- } else {
342
- params.push(entity[columnName]);
343
- command += `$${params.length}`;
344
- }
345
- });
346
-
347
- if (filters && filters.length) {
348
- command += " WHERE ";
349
- command += buildFiltersQuery(ctx, filters);
350
- }
351
-
352
- command += " RETURNING *";
353
-
354
- return {
355
- command,
356
- params: ctx.params,
357
- };
358
- }
359
-
360
- delete(model: RpdDataModel, options: DeleteRowOptions): DatabaseQuery {
361
- const params: any[] = [];
362
- const ctx: BuildQueryContext = {
363
- model,
364
- builder: this,
365
- params,
366
- emitTableAlias: false,
367
- paramToLiteral: false,
368
- };
369
- let { filters } = options;
370
- let command = "DELETE FROM ";
371
-
372
- command += this.quoteTable(model);
373
-
374
- if (filters && filters.length) {
375
- command += " WHERE ";
376
- command += buildFiltersQuery(ctx, filters);
377
- }
378
-
379
- return {
380
- command,
381
- params: ctx.params,
382
- };
383
- }
384
-
385
- buildFiltersExpression(model: RpdDataModel, filters: RowFilterOptions[]) {
386
- const params: any[] = [];
387
- const ctx: BuildQueryContext = {
388
- model,
389
- builder: this,
390
- params,
391
- emitTableAlias: false,
392
- paramToLiteral: true,
393
- };
394
-
395
- return buildFiltersQuery(ctx, filters);
396
- }
397
- }
398
-
399
- export function buildFiltersQuery(ctx: BuildQueryContext, filters: RowFilterOptions[]) {
400
- return buildFilterQuery(0, ctx, {
401
- operator: "and",
402
- filters,
403
- });
404
- }
405
-
406
- function buildFilterQuery(level: number, ctx: BuildQueryContext, filter: RowFilterOptions): string {
407
- const { operator } = filter;
408
- if (operator === "eq" || operator === "ne" || operator === "gt" || operator === "gte" || operator === "lt" || operator === "lte") {
409
- return buildRelationalFilterQuery(ctx, filter);
410
- } else if (operator === "and" || operator === "or") {
411
- return buildLogicalFilterQuery(level, ctx, filter);
412
- } else if (operator === "null" || operator === "notNull") {
413
- return buildUnaryFilterQuery(ctx, filter);
414
- } else if (operator === "in" || operator === "notIn") {
415
- return buildInFilterQuery(ctx, filter);
416
- } else if (operator === "contains") {
417
- return buildContainsFilterQuery(ctx, filter);
418
- } else if (operator === "notContains") {
419
- return buildNotContainsFilterQuery(ctx, filter);
420
- } else if (operator === "startsWith") {
421
- return buildStartsWithFilterQuery(ctx, filter);
422
- } else if (operator === "notStartsWith") {
423
- return buildNotStartsWithFilterQuery(ctx, filter);
424
- } else if (operator === "endsWith") {
425
- return buildEndsWithFilterQuery(ctx, filter);
426
- } else if (operator === "notEndsWith") {
427
- return buildNotEndsWithFilterQuery(ctx, filter);
428
- } else if (operator === "arrayContains") {
429
- return buildArrayContainsFilterQuery(ctx, filter);
430
- } else if (operator === "arrayOverlap") {
431
- return buildArrayOverlapFilterQuery(ctx, filter);
432
- } else {
433
- throw new Error(`Filter operator '${operator}' is not supported.`);
434
- }
435
- }
436
-
437
- function buildLogicalFilterQuery(level: number, ctx: BuildQueryContext, filter: FindRowLogicalFilterOptions) {
438
- let dbOperator;
439
- if (filter.operator === "and") {
440
- dbOperator = " AND ";
441
- } else {
442
- dbOperator = " OR ";
443
- }
444
-
445
- let command = filter.filters.map(buildFilterQuery.bind(null, level + 1, ctx)).join(dbOperator);
446
- if (level) {
447
- return `(${command})`;
448
- }
449
- return command;
450
- }
451
-
452
- function buildUnaryFilterQuery(ctx: BuildQueryContext, filter: FindRowUnaryFilterOptions) {
453
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
454
- if (filter.operator === "null") {
455
- command += " IS NULL";
456
- } else {
457
- command += " IS NOT NULL";
458
- }
459
- return command;
460
- }
461
-
462
- function buildInFilterQuery(ctx: BuildQueryContext, filter: FindRowSetFilterOptions) {
463
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
464
-
465
- if (filter.operator === "in") {
466
- command += " = ";
467
- } else {
468
- command += " <> ";
469
- }
470
-
471
- if (ctx.paramToLiteral) {
472
- // TODO: implement it
473
- } else {
474
- ctx.params.push(filter.value);
475
- if (filter.operator === "in") {
476
- command += `ANY($${ctx.params.length}::${filter.itemType || "int"}[])`;
477
- } else {
478
- command += `ALL($${ctx.params.length}::${filter.itemType || "int"}[])`;
479
- }
480
- }
481
-
482
- return command;
483
- }
484
-
485
- function buildContainsFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
486
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
487
-
488
- command += " LIKE ";
489
-
490
- if (ctx.paramToLiteral) {
491
- // TODO: implement it
492
- } else {
493
- ctx.params.push(`%${filter.value}%`);
494
- command += "$" + ctx.params.length;
495
- }
496
-
497
- return command;
498
- }
499
-
500
- function buildNotContainsFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
501
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
502
-
503
- command += " NOT LIKE ";
504
- if (ctx.paramToLiteral) {
505
- // TODO: implement it
506
- } else {
507
- ctx.params.push(`%${filter.value}%`);
508
- command += "$" + ctx.params.length;
509
- }
510
-
511
- return command;
512
- }
513
-
514
- function buildStartsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
515
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
516
-
517
- command += " LIKE ";
518
-
519
- if (ctx.paramToLiteral) {
520
- // TODO: implement it
521
- } else {
522
- ctx.params.push(`${filter.value}%`);
523
- command += "$" + ctx.params.length;
524
- }
525
-
526
- return command;
527
- }
528
-
529
- function buildNotStartsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
530
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
531
-
532
- command += " NOT LIKE ";
533
-
534
- if (ctx.paramToLiteral) {
535
- // TODO: implement it
536
- } else {
537
- ctx.params.push(`${filter.value}%`);
538
- command += "$" + ctx.params.length;
539
- }
540
-
541
- return command;
542
- }
543
-
544
- function buildEndsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
545
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
546
-
547
- command += " LIKE ";
548
-
549
- if (ctx.paramToLiteral) {
550
- // TODO: implement it
551
- } else {
552
- ctx.params.push(`%${filter.value}`);
553
- command += "$" + ctx.params.length;
554
- }
555
-
556
- return command;
557
- }
558
-
559
- function buildNotEndsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
560
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
561
-
562
- command += " NOT LIKE ";
563
-
564
- if (ctx.paramToLiteral) {
565
- // TODO: implement it
566
- } else {
567
- ctx.params.push(`%${filter.value}`);
568
- command += "$" + ctx.params.length;
569
- }
570
-
571
- return command;
572
- }
573
-
574
- function buildRelationalFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
575
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
576
-
577
- command += relationalOperatorsMap.get(filter.operator);
578
-
579
- if (ctx.paramToLiteral) {
580
- command += formatValueToSqlLiteral(filter.value);
581
- } else {
582
- ctx.params.push(filter.value);
583
- command += "$" + ctx.params.length;
584
- }
585
-
586
- return command;
587
- }
588
-
589
- function buildArrayContainsFilterQuery(ctx: BuildQueryContext, filter: FindRowArrayFilterOptions) {
590
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
591
-
592
- command += " @> ";
593
-
594
- if (ctx.paramToLiteral) {
595
- // TODO: implement it
596
- } else {
597
- ctx.params.push(filter.value);
598
- command += "$" + ctx.params.length;
599
- }
600
-
601
- return command;
602
- }
603
-
604
- function buildArrayOverlapFilterQuery(ctx: BuildQueryContext, filter: FindRowArrayFilterOptions) {
605
- let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
606
-
607
- command += " && ";
608
-
609
- if (ctx.paramToLiteral) {
610
- // TODO: implement it
611
- } else {
612
- ctx.params.push(filter.value);
613
- command += "$" + ctx.params.length;
614
- }
615
-
616
- return command;
617
- }
618
-
619
- function formatValueToSqlLiteral(value: any) {
620
- if (isNull(value) || isUndefined(value)) {
621
- return "null";
622
- }
623
-
624
- if (isString(value)) {
625
- return `'${value.replaceAll("'", "''")}'`;
626
- }
627
-
628
- if (isBoolean(value)) {
629
- return value ? "true" : "false";
630
- }
631
-
632
- if (isNumber(value)) {
633
- return value.toString();
634
- }
635
-
636
- return `'${value.toString().replaceAll("'", "''")}'`;
637
- }
1
+ import { find, isBoolean, isNull, isNumber, isString, isUndefined } from "lodash";
2
+ import { RpdDataModel, RpdDataModelProperty, CreateEntityOptions, QuoteTableOptions, DatabaseQuery } from "../types";
3
+ import {
4
+ CountRowOptions,
5
+ DeleteRowOptions,
6
+ FindRowLogicalFilterOptions,
7
+ FindRowRelationalFilterOptions,
8
+ FindRowSetFilterOptions,
9
+ FindRowUnaryFilterOptions,
10
+ FindRowOptions,
11
+ RowFilterOptions,
12
+ RowFilterRelationalOperators,
13
+ UpdateRowOptions,
14
+ ColumnSelectOptions,
15
+ ColumnNameWithTableName,
16
+ DataAccessPgColumnTypes,
17
+ FindRowArrayFilterOptions,
18
+ } from "~/dataAccess/dataAccessTypes";
19
+ import { pgPropertyTypeColumnMap } from "~/dataAccess/columnTypeMapper";
20
+
21
+ const objLeftQuoteChar = '"';
22
+ const objRightQuoteChar = '"';
23
+
24
+ const relationalOperatorsMap = new Map<RowFilterRelationalOperators, string>([
25
+ ["eq", "="],
26
+ ["ne", "<>"],
27
+ ["gt", ">"],
28
+ ["gte", ">="],
29
+ ["lt", "<"],
30
+ ["lte", "<="],
31
+ ]);
32
+
33
+ export interface BuildQueryContext {
34
+ model: RpdDataModel;
35
+ builder: QueryBuilder;
36
+ params: any[];
37
+ emitTableAlias: boolean;
38
+ /**
39
+ * emit parameter value to sql literal.
40
+ */
41
+ paramToLiteral: boolean;
42
+ }
43
+
44
+ export interface InitQueryBuilderOptions {
45
+ dbDefaultSchema: string;
46
+ }
47
+
48
+ export default class QueryBuilder {
49
+ #dbDefaultSchema: string;
50
+
51
+ constructor(options: InitQueryBuilderOptions) {
52
+ this.#dbDefaultSchema = options.dbDefaultSchema;
53
+ }
54
+
55
+ quoteTable(options: QuoteTableOptions) {
56
+ const { schema, tableName } = options;
57
+ if (schema) {
58
+ return `${this.quoteObject(schema)}.${this.quoteObject(tableName)}`;
59
+ } else if (this.#dbDefaultSchema) {
60
+ return `${this.quoteObject(this.#dbDefaultSchema)}.${this.quoteObject(tableName)}`;
61
+ } else {
62
+ return this.quoteObject(tableName);
63
+ }
64
+ }
65
+
66
+ quoteObject(name: string) {
67
+ return `${objLeftQuoteChar}${name}${objRightQuoteChar}`;
68
+ }
69
+
70
+ quoteColumn(model: RpdDataModel, column: ColumnSelectOptions, emitTableAlias: boolean) {
71
+ if (typeof column === "string") {
72
+ if (emitTableAlias) {
73
+ return `${objLeftQuoteChar}${model.tableName}${objRightQuoteChar}.${objLeftQuoteChar}${column}${objRightQuoteChar}`;
74
+ } else {
75
+ return `${objLeftQuoteChar}${column}${objRightQuoteChar}`;
76
+ }
77
+ } else {
78
+ if (emitTableAlias && column.tableName) {
79
+ return `${objLeftQuoteChar}${column.tableName}${objRightQuoteChar}.${objLeftQuoteChar}${column.name}${objRightQuoteChar}`;
80
+ } else {
81
+ return `${objLeftQuoteChar}${column.name}${objRightQuoteChar}`;
82
+ }
83
+ }
84
+ }
85
+
86
+ select(model: RpdDataModel, options: FindRowOptions): DatabaseQuery {
87
+ const ctx: BuildQueryContext = {
88
+ model,
89
+ builder: this,
90
+ params: [],
91
+ emitTableAlias: true,
92
+ paramToLiteral: false,
93
+ };
94
+ let { fields: columns, filters, orderBy, pagination } = options;
95
+ let command = "SELECT ";
96
+ if (!columns || !columns.length) {
97
+ command += `${this.quoteObject(model.tableName)}.* FROM `;
98
+ } else {
99
+ command += columns.map((column) => this.quoteColumn(ctx.model, column, ctx.emitTableAlias)).join(", ");
100
+ command += " FROM ";
101
+ }
102
+
103
+ command += this.quoteTable(model);
104
+
105
+ if (options.orderBy) {
106
+ options.orderBy
107
+ .filter((orderByItem) => orderByItem.relationField)
108
+ .forEach((orderByItem) => {
109
+ const { relationField } = orderByItem;
110
+ const orderField = orderByItem.field as ColumnNameWithTableName;
111
+ command += ` LEFT JOIN ${this.quoteTable({ schema: orderField.schema, tableName: orderField.tableName })} ON ${this.quoteObject(
112
+ orderField.tableName,
113
+ )}.id = ${this.quoteObject(relationField.tableName)}.${this.quoteObject(relationField.name)}`;
114
+ });
115
+ }
116
+
117
+ if (filters && filters.length) {
118
+ command += " WHERE ";
119
+ command += buildFiltersQuery(ctx, filters);
120
+ }
121
+
122
+ if (orderBy && orderBy.length) {
123
+ command += " ORDER BY ";
124
+ command += orderBy
125
+ .map((item) => {
126
+ const quotedName = this.quoteColumn(ctx.model, item.field, ctx.emitTableAlias);
127
+ return item.desc ? quotedName + " DESC" : quotedName;
128
+ })
129
+ .join(", ");
130
+ }
131
+
132
+ if (pagination) {
133
+ command += " OFFSET ";
134
+ ctx.params.push(pagination.offset);
135
+ command += "$" + ctx.params.length;
136
+
137
+ command += " LIMIT ";
138
+ ctx.params.push(pagination.limit);
139
+ command += "$" + ctx.params.length;
140
+ }
141
+
142
+ return {
143
+ command,
144
+ params: ctx.params,
145
+ };
146
+ }
147
+
148
+ selectDerived(derivedModel: RpdDataModel, baseModel: RpdDataModel, options: FindRowOptions): DatabaseQuery {
149
+ const ctx: BuildQueryContext = {
150
+ model: derivedModel,
151
+ builder: this,
152
+ params: [],
153
+ emitTableAlias: true,
154
+ paramToLiteral: false,
155
+ };
156
+ let { fields: columns, filters, orderBy, pagination } = options;
157
+ let command = "SELECT ";
158
+ if (!columns || !columns.length) {
159
+ command += `${this.quoteObject(derivedModel.tableName)}.* FROM `;
160
+ } else {
161
+ command += columns
162
+ .map((column) => {
163
+ return this.quoteColumn(derivedModel, column, ctx.emitTableAlias);
164
+ })
165
+ .join(", ");
166
+ command += " FROM ";
167
+ }
168
+
169
+ command += `${this.quoteTable(derivedModel)} LEFT JOIN ${this.quoteTable(baseModel)} ON ${this.quoteObject(derivedModel.tableName)}.id = ${this.quoteObject(
170
+ baseModel.tableName,
171
+ )}.id`;
172
+
173
+ if (options.orderBy) {
174
+ options.orderBy
175
+ .filter((orderByItem) => orderByItem.relationField)
176
+ .forEach((orderByItem) => {
177
+ const { relationField } = orderByItem;
178
+ const orderField = orderByItem.field as ColumnNameWithTableName;
179
+ command += ` LEFT JOIN ${this.quoteTable({ schema: orderField.schema, tableName: orderField.tableName })} ON ${this.quoteObject(
180
+ orderField.tableName,
181
+ )}.id = ${this.quoteObject(relationField.tableName)}.${this.quoteObject(relationField.name)}`;
182
+ });
183
+ }
184
+
185
+ if (filters && filters.length) {
186
+ command += " WHERE ";
187
+ command += buildFiltersQuery(ctx, filters);
188
+ }
189
+
190
+ if (orderBy && orderBy.length) {
191
+ command += " ORDER BY ";
192
+ command += orderBy
193
+ .map((item) => {
194
+ const quotedName = this.quoteColumn(derivedModel, item.field, ctx.emitTableAlias);
195
+ return item.desc ? quotedName + " DESC" : quotedName;
196
+ })
197
+ .join(", ");
198
+ }
199
+
200
+ if (pagination) {
201
+ command += " OFFSET ";
202
+ ctx.params.push(pagination.offset);
203
+ command += "$" + ctx.params.length;
204
+
205
+ command += " LIMIT ";
206
+ ctx.params.push(pagination.limit);
207
+ command += "$" + ctx.params.length;
208
+ }
209
+
210
+ return {
211
+ command,
212
+ params: ctx.params,
213
+ };
214
+ }
215
+
216
+ count(model: RpdDataModel, options: CountRowOptions): DatabaseQuery {
217
+ const ctx: BuildQueryContext = {
218
+ model,
219
+ builder: this,
220
+ params: [],
221
+ emitTableAlias: false,
222
+ paramToLiteral: false,
223
+ };
224
+ let { filters } = options;
225
+ let command = 'SELECT COUNT(*)::int as "count" FROM ';
226
+
227
+ command += this.quoteTable(model);
228
+
229
+ if (filters && filters.length) {
230
+ command += " WHERE ";
231
+ command += buildFiltersQuery(ctx, filters);
232
+ }
233
+
234
+ return {
235
+ command,
236
+ params: ctx.params,
237
+ };
238
+ }
239
+
240
+ countDerived(derivedModel: RpdDataModel, baseModel: RpdDataModel, options: CountRowOptions): DatabaseQuery {
241
+ const ctx: BuildQueryContext = {
242
+ model: derivedModel,
243
+ builder: this,
244
+ params: [],
245
+ emitTableAlias: true,
246
+ paramToLiteral: false,
247
+ };
248
+ let { filters } = options;
249
+ let command = 'SELECT COUNT(*)::int as "count" FROM ';
250
+
251
+ command += `${this.quoteTable(derivedModel)} LEFT JOIN ${this.quoteTable(baseModel)} ON ${this.quoteObject(derivedModel.tableName)}.id = ${this.quoteObject(
252
+ baseModel.tableName,
253
+ )}.id`;
254
+
255
+ if (filters && filters.length) {
256
+ command += " WHERE ";
257
+ command += buildFiltersQuery(ctx, filters);
258
+ }
259
+
260
+ return {
261
+ command,
262
+ params: ctx.params,
263
+ };
264
+ }
265
+
266
+ insert(model: RpdDataModel, options: CreateEntityOptions): DatabaseQuery {
267
+ const params: any[] = [];
268
+ const ctx: BuildQueryContext = {
269
+ model,
270
+ builder: this,
271
+ params,
272
+ emitTableAlias: false,
273
+ paramToLiteral: false,
274
+ };
275
+ const { entity } = options;
276
+ let command = "INSERT INTO ";
277
+
278
+ command += this.quoteTable(model);
279
+
280
+ const columnNames: string[] = Object.keys(entity);
281
+ let values = "";
282
+ columnNames.forEach((columnName, index) => {
283
+ if (index) {
284
+ values += ", ";
285
+ }
286
+
287
+ let property: RpdDataModelProperty | null = null;
288
+ if (model) {
289
+ property = find(model.properties, (e: RpdDataModelProperty) => (e.columnName || e.code) === columnName);
290
+ }
291
+ const columnType: DataAccessPgColumnTypes | null = property ? pgPropertyTypeColumnMap[property.type] : null;
292
+ if (columnType === "jsonb") {
293
+ params.push(JSON.stringify(entity[columnName]));
294
+ values += `$${params.length}::jsonb`;
295
+ } else {
296
+ params.push(entity[columnName]);
297
+ values += `$${params.length}`;
298
+ }
299
+ });
300
+
301
+ command += ` (${columnNames.map(this.quoteObject).join(", ")})`;
302
+ command += ` VALUES (${values}) RETURNING *`;
303
+
304
+ return {
305
+ command,
306
+ params: ctx.params,
307
+ };
308
+ }
309
+
310
+ update(model: RpdDataModel, options: UpdateRowOptions): DatabaseQuery {
311
+ const params: any[] = [];
312
+ const ctx: BuildQueryContext = {
313
+ model,
314
+ builder: this,
315
+ params,
316
+ emitTableAlias: false,
317
+ paramToLiteral: false,
318
+ };
319
+ let { entity, filters } = options;
320
+ let command = "UPDATE ";
321
+
322
+ command += this.quoteTable(model);
323
+
324
+ command += " SET ";
325
+ const columnNames: string[] = Object.keys(entity);
326
+ columnNames.forEach((columnName, index) => {
327
+ if (index) {
328
+ command += ", ";
329
+ }
330
+
331
+ command += `${this.quoteObject(columnName)}=`;
332
+
333
+ let property: RpdDataModelProperty | null = null;
334
+ if (model) {
335
+ property = find(model.properties, (e: RpdDataModelProperty) => (e.columnName || e.code) === columnName);
336
+ }
337
+ const columnType: DataAccessPgColumnTypes | null = property ? pgPropertyTypeColumnMap[property.type] : null;
338
+ if (columnType === "jsonb") {
339
+ params.push(JSON.stringify(entity[columnName]));
340
+ command += `$${params.length}::jsonb`;
341
+ } else {
342
+ params.push(entity[columnName]);
343
+ command += `$${params.length}`;
344
+ }
345
+ });
346
+
347
+ if (filters && filters.length) {
348
+ command += " WHERE ";
349
+ command += buildFiltersQuery(ctx, filters);
350
+ }
351
+
352
+ command += " RETURNING *";
353
+
354
+ return {
355
+ command,
356
+ params: ctx.params,
357
+ };
358
+ }
359
+
360
+ delete(model: RpdDataModel, options: DeleteRowOptions): DatabaseQuery {
361
+ const params: any[] = [];
362
+ const ctx: BuildQueryContext = {
363
+ model,
364
+ builder: this,
365
+ params,
366
+ emitTableAlias: false,
367
+ paramToLiteral: false,
368
+ };
369
+ let { filters } = options;
370
+ let command = "DELETE FROM ";
371
+
372
+ command += this.quoteTable(model);
373
+
374
+ if (filters && filters.length) {
375
+ command += " WHERE ";
376
+ command += buildFiltersQuery(ctx, filters);
377
+ }
378
+
379
+ return {
380
+ command,
381
+ params: ctx.params,
382
+ };
383
+ }
384
+
385
+ buildFiltersExpression(model: RpdDataModel, filters: RowFilterOptions[]) {
386
+ const params: any[] = [];
387
+ const ctx: BuildQueryContext = {
388
+ model,
389
+ builder: this,
390
+ params,
391
+ emitTableAlias: false,
392
+ paramToLiteral: true,
393
+ };
394
+
395
+ return buildFiltersQuery(ctx, filters);
396
+ }
397
+ }
398
+
399
+ export function buildFiltersQuery(ctx: BuildQueryContext, filters: RowFilterOptions[]) {
400
+ return buildFilterQuery(0, ctx, {
401
+ operator: "and",
402
+ filters,
403
+ });
404
+ }
405
+
406
+ function buildFilterQuery(level: number, ctx: BuildQueryContext, filter: RowFilterOptions): string {
407
+ const { operator } = filter;
408
+ if (operator === "eq" || operator === "ne" || operator === "gt" || operator === "gte" || operator === "lt" || operator === "lte") {
409
+ return buildRelationalFilterQuery(ctx, filter);
410
+ } else if (operator === "and" || operator === "or") {
411
+ return buildLogicalFilterQuery(level, ctx, filter);
412
+ } else if (operator === "null" || operator === "notNull") {
413
+ return buildUnaryFilterQuery(ctx, filter);
414
+ } else if (operator === "in" || operator === "notIn") {
415
+ return buildInFilterQuery(ctx, filter);
416
+ } else if (operator === "contains") {
417
+ return buildContainsFilterQuery(ctx, filter);
418
+ } else if (operator === "notContains") {
419
+ return buildNotContainsFilterQuery(ctx, filter);
420
+ } else if (operator === "startsWith") {
421
+ return buildStartsWithFilterQuery(ctx, filter);
422
+ } else if (operator === "notStartsWith") {
423
+ return buildNotStartsWithFilterQuery(ctx, filter);
424
+ } else if (operator === "endsWith") {
425
+ return buildEndsWithFilterQuery(ctx, filter);
426
+ } else if (operator === "notEndsWith") {
427
+ return buildNotEndsWithFilterQuery(ctx, filter);
428
+ } else if (operator === "arrayContains") {
429
+ return buildArrayContainsFilterQuery(ctx, filter);
430
+ } else if (operator === "arrayOverlap") {
431
+ return buildArrayOverlapFilterQuery(ctx, filter);
432
+ } else {
433
+ throw new Error(`Filter operator '${operator}' is not supported.`);
434
+ }
435
+ }
436
+
437
+ function buildLogicalFilterQuery(level: number, ctx: BuildQueryContext, filter: FindRowLogicalFilterOptions) {
438
+ let dbOperator;
439
+ if (filter.operator === "and") {
440
+ dbOperator = " AND ";
441
+ } else {
442
+ dbOperator = " OR ";
443
+ }
444
+
445
+ let command = filter.filters.map(buildFilterQuery.bind(null, level + 1, ctx)).join(dbOperator);
446
+ if (level) {
447
+ return `(${command})`;
448
+ }
449
+ return command;
450
+ }
451
+
452
+ function buildUnaryFilterQuery(ctx: BuildQueryContext, filter: FindRowUnaryFilterOptions) {
453
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
454
+ if (filter.operator === "null") {
455
+ command += " IS NULL";
456
+ } else {
457
+ command += " IS NOT NULL";
458
+ }
459
+ return command;
460
+ }
461
+
462
+ function buildInFilterQuery(ctx: BuildQueryContext, filter: FindRowSetFilterOptions) {
463
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
464
+
465
+ if (filter.operator === "in") {
466
+ command += " = ";
467
+ } else {
468
+ command += " <> ";
469
+ }
470
+
471
+ if (ctx.paramToLiteral) {
472
+ // TODO: implement it
473
+ } else {
474
+ ctx.params.push(filter.value);
475
+ if (filter.operator === "in") {
476
+ command += `ANY($${ctx.params.length}::${filter.itemType || "int"}[])`;
477
+ } else {
478
+ command += `ALL($${ctx.params.length}::${filter.itemType || "int"}[])`;
479
+ }
480
+ }
481
+
482
+ return command;
483
+ }
484
+
485
+ function buildContainsFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
486
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
487
+
488
+ command += " LIKE ";
489
+
490
+ if (ctx.paramToLiteral) {
491
+ // TODO: implement it
492
+ } else {
493
+ ctx.params.push(`%${filter.value}%`);
494
+ command += "$" + ctx.params.length;
495
+ }
496
+
497
+ return command;
498
+ }
499
+
500
+ function buildNotContainsFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
501
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
502
+
503
+ command += " NOT LIKE ";
504
+ if (ctx.paramToLiteral) {
505
+ // TODO: implement it
506
+ } else {
507
+ ctx.params.push(`%${filter.value}%`);
508
+ command += "$" + ctx.params.length;
509
+ }
510
+
511
+ return command;
512
+ }
513
+
514
+ function buildStartsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
515
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
516
+
517
+ command += " LIKE ";
518
+
519
+ if (ctx.paramToLiteral) {
520
+ // TODO: implement it
521
+ } else {
522
+ ctx.params.push(`${filter.value}%`);
523
+ command += "$" + ctx.params.length;
524
+ }
525
+
526
+ return command;
527
+ }
528
+
529
+ function buildNotStartsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
530
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
531
+
532
+ command += " NOT LIKE ";
533
+
534
+ if (ctx.paramToLiteral) {
535
+ // TODO: implement it
536
+ } else {
537
+ ctx.params.push(`${filter.value}%`);
538
+ command += "$" + ctx.params.length;
539
+ }
540
+
541
+ return command;
542
+ }
543
+
544
+ function buildEndsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
545
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
546
+
547
+ command += " LIKE ";
548
+
549
+ if (ctx.paramToLiteral) {
550
+ // TODO: implement it
551
+ } else {
552
+ ctx.params.push(`%${filter.value}`);
553
+ command += "$" + ctx.params.length;
554
+ }
555
+
556
+ return command;
557
+ }
558
+
559
+ function buildNotEndsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
560
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
561
+
562
+ command += " NOT LIKE ";
563
+
564
+ if (ctx.paramToLiteral) {
565
+ // TODO: implement it
566
+ } else {
567
+ ctx.params.push(`%${filter.value}`);
568
+ command += "$" + ctx.params.length;
569
+ }
570
+
571
+ return command;
572
+ }
573
+
574
+ function buildRelationalFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
575
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
576
+
577
+ command += relationalOperatorsMap.get(filter.operator);
578
+
579
+ if (ctx.paramToLiteral) {
580
+ command += formatValueToSqlLiteral(filter.value);
581
+ } else {
582
+ ctx.params.push(filter.value);
583
+ command += "$" + ctx.params.length;
584
+ }
585
+
586
+ return command;
587
+ }
588
+
589
+ function buildArrayContainsFilterQuery(ctx: BuildQueryContext, filter: FindRowArrayFilterOptions) {
590
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
591
+
592
+ command += " @> ";
593
+
594
+ if (ctx.paramToLiteral) {
595
+ // TODO: implement it
596
+ } else {
597
+ ctx.params.push(filter.value);
598
+ command += "$" + ctx.params.length;
599
+ }
600
+
601
+ return command;
602
+ }
603
+
604
+ function buildArrayOverlapFilterQuery(ctx: BuildQueryContext, filter: FindRowArrayFilterOptions) {
605
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
606
+
607
+ command += " && ";
608
+
609
+ if (ctx.paramToLiteral) {
610
+ // TODO: implement it
611
+ } else {
612
+ ctx.params.push(filter.value);
613
+ command += "$" + ctx.params.length;
614
+ }
615
+
616
+ return command;
617
+ }
618
+
619
+ function formatValueToSqlLiteral(value: any) {
620
+ if (isNull(value) || isUndefined(value)) {
621
+ return "null";
622
+ }
623
+
624
+ if (isString(value)) {
625
+ return `'${value.replaceAll("'", "''")}'`;
626
+ }
627
+
628
+ if (isBoolean(value)) {
629
+ return value ? "true" : "false";
630
+ }
631
+
632
+ if (isNumber(value)) {
633
+ return value.toString();
634
+ }
635
+
636
+ return `'${value.toString().replaceAll("'", "''")}'`;
637
+ }