@ruiapp/rapid-core 0.1.71 → 0.1.73

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.
@@ -1,473 +1,512 @@
1
- import { find } 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
- } from "~/dataAccess/dataAccessTypes";
16
-
17
- const objLeftQuoteChar = '"';
18
- const objRightQuoteChar = '"';
19
-
20
- const relationalOperatorsMap = new Map<RowFilterRelationalOperators, string>([
21
- ["eq", "="],
22
- ["ne", "<>"],
23
- ["gt", ">"],
24
- ["gte", ">="],
25
- ["lt", "<"],
26
- ["lte", "<="],
27
- ]);
28
-
29
- export interface BuildQueryContext {
30
- builder: QueryBuilder;
31
- params: any[];
32
- emitTableAlias: boolean;
33
- }
34
-
35
- export interface InitQueryBuilderOptions {
36
- dbDefaultSchema: string;
37
- }
38
-
39
- export default class QueryBuilder {
40
- #dbDefaultSchema: string;
41
-
42
- constructor(options: InitQueryBuilderOptions) {
43
- this.#dbDefaultSchema = options.dbDefaultSchema;
44
- }
45
-
46
- quoteTable(options: QuoteTableOptions) {
47
- const { schema, tableName } = options;
48
- if (schema) {
49
- return `${this.quoteObject(schema)}.${this.quoteObject(tableName)}`;
50
- } else if (this.#dbDefaultSchema) {
51
- return `${this.quoteObject(this.#dbDefaultSchema)}.${this.quoteObject(tableName)}`;
52
- } else {
53
- return this.quoteObject(tableName);
54
- }
55
- }
56
-
57
- quoteObject(name: string) {
58
- return `${objLeftQuoteChar}${name}${objRightQuoteChar}`;
59
- }
60
-
61
- quoteColumn(column: ColumnSelectOptions, emitTableAlias: boolean) {
62
- if (typeof column === "string") {
63
- return `${objLeftQuoteChar}${column}${objRightQuoteChar}`;
64
- } else if (emitTableAlias && column.tableName) {
65
- return `${objLeftQuoteChar}${column.tableName}${objRightQuoteChar}.${objLeftQuoteChar}${column.name}${objRightQuoteChar}`;
66
- } else {
67
- return `${objLeftQuoteChar}${column.name}${objRightQuoteChar}`;
68
- }
69
- }
70
-
71
- select(model: RpdDataModel, options: FindRowOptions): DatabaseQuery {
72
- const ctx: BuildQueryContext = {
73
- builder: this,
74
- params: [],
75
- emitTableAlias: false,
76
- };
77
- let { fields: columns, filters, orderBy, pagination } = options;
78
- let command = "SELECT ";
79
- if (!columns || !columns.length) {
80
- command += "* FROM ";
81
- } else {
82
- command += columns.map((column) => this.quoteColumn(column, ctx.emitTableAlias)).join(", ");
83
- command += " FROM ";
84
- }
85
-
86
- command += this.quoteTable(model);
87
-
88
- if (filters && filters.length) {
89
- command += " WHERE ";
90
- command += buildFiltersQuery(ctx, filters);
91
- }
92
-
93
- if (orderBy && orderBy.length) {
94
- command += " ORDER BY ";
95
- command += orderBy
96
- .map((item) => {
97
- const quotedName = this.quoteColumn(item.field, ctx.emitTableAlias);
98
- return item.desc ? quotedName + " DESC" : quotedName;
99
- })
100
- .join(", ");
101
- }
102
-
103
- if (pagination) {
104
- command += " OFFSET ";
105
- ctx.params.push(pagination.offset);
106
- command += "$" + ctx.params.length;
107
-
108
- command += " LIMIT ";
109
- ctx.params.push(pagination.limit);
110
- command += "$" + ctx.params.length;
111
- }
112
-
113
- return {
114
- command,
115
- params: ctx.params,
116
- };
117
- }
118
-
119
- selectDerived(derivedModel: RpdDataModel, baseModel: RpdDataModel, options: FindRowOptions): DatabaseQuery {
120
- const ctx: BuildQueryContext = {
121
- builder: this,
122
- params: [],
123
- emitTableAlias: true,
124
- };
125
- let { fields: columns, filters, orderBy, pagination } = options;
126
- let command = "SELECT ";
127
- if (!columns || !columns.length) {
128
- command += `${this.quoteObject(derivedModel.tableName)}.* FROM `;
129
- } else {
130
- command += columns
131
- .map((column) => {
132
- return this.quoteColumn(column, ctx.emitTableAlias);
133
- })
134
- .join(", ");
135
- command += " FROM ";
136
- }
137
-
138
- command += `${this.quoteTable(derivedModel)} LEFT JOIN ${this.quoteTable(baseModel)} ON ${this.quoteObject(derivedModel.tableName)}.id = ${this.quoteObject(
139
- baseModel.tableName,
140
- )}.id`;
141
-
142
- if (filters && filters.length) {
143
- command += " WHERE ";
144
- command += buildFiltersQuery(ctx, filters);
145
- }
146
-
147
- if (orderBy && orderBy.length) {
148
- command += " ORDER BY ";
149
- command += orderBy
150
- .map((item) => {
151
- const quotedName = this.quoteColumn(item.field, ctx.emitTableAlias);
152
- return item.desc ? quotedName + " DESC" : quotedName;
153
- })
154
- .join(", ");
155
- }
156
-
157
- if (pagination) {
158
- command += " OFFSET ";
159
- ctx.params.push(pagination.offset);
160
- command += "$" + ctx.params.length;
161
-
162
- command += " LIMIT ";
163
- ctx.params.push(pagination.limit);
164
- command += "$" + ctx.params.length;
165
- }
166
-
167
- return {
168
- command,
169
- params: ctx.params,
170
- };
171
- }
172
-
173
- count(model: RpdDataModel, options: CountRowOptions): DatabaseQuery {
174
- const ctx: BuildQueryContext = {
175
- builder: this,
176
- params: [],
177
- emitTableAlias: false,
178
- };
179
- let { filters } = options;
180
- let command = 'SELECT COUNT(*)::int as "count" FROM ';
181
-
182
- command += this.quoteTable(model);
183
-
184
- if (filters && filters.length) {
185
- command += " WHERE ";
186
- command += buildFiltersQuery(ctx, filters);
187
- }
188
-
189
- return {
190
- command,
191
- params: ctx.params,
192
- };
193
- }
194
-
195
- countDerived(derivedModel: RpdDataModel, baseModel: RpdDataModel, options: CountRowOptions): DatabaseQuery {
196
- const ctx: BuildQueryContext = {
197
- builder: this,
198
- params: [],
199
- emitTableAlias: true,
200
- };
201
- let { filters } = options;
202
- let command = 'SELECT COUNT(*)::int as "count" FROM ';
203
-
204
- command += `${this.quoteTable(derivedModel)} LEFT JOIN ${this.quoteTable(baseModel)} ON ${this.quoteObject(derivedModel.tableName)}.id = ${this.quoteObject(
205
- baseModel.tableName,
206
- )}.id`;
207
-
208
- if (filters && filters.length) {
209
- command += " WHERE ";
210
- command += buildFiltersQuery(ctx, filters);
211
- }
212
-
213
- return {
214
- command,
215
- params: ctx.params,
216
- };
217
- }
218
-
219
- insert(model: RpdDataModel, options: CreateEntityOptions): DatabaseQuery {
220
- const params: any[] = [];
221
- const ctx: BuildQueryContext = {
222
- builder: this,
223
- params,
224
- emitTableAlias: false,
225
- };
226
- const { entity } = options;
227
- let command = "INSERT INTO ";
228
-
229
- command += this.quoteTable(model);
230
-
231
- const propertyNames: string[] = Object.keys(entity);
232
- let values = "";
233
- propertyNames.forEach((propertyName, index) => {
234
- if (index) {
235
- values += ", ";
236
- }
237
-
238
- let property: RpdDataModelProperty | null = null;
239
- if (model) {
240
- property = find(model.properties, (e: RpdDataModelProperty) => e.code === propertyName);
241
- }
242
-
243
- if (property && property.type === "json") {
244
- params.push(JSON.stringify(entity[propertyName]));
245
- values += `$${params.length}::jsonb`;
246
- } else {
247
- params.push(entity[propertyName]);
248
- values += `$${params.length}`;
249
- }
250
- });
251
-
252
- command += ` (${propertyNames.map(this.quoteObject).join(", ")})`;
253
- command += ` VALUES (${values}) RETURNING *`;
254
-
255
- return {
256
- command,
257
- params: ctx.params,
258
- };
259
- }
260
-
261
- update(model: RpdDataModel, options: UpdateRowOptions): DatabaseQuery {
262
- const params: any[] = [];
263
- const ctx: BuildQueryContext = {
264
- builder: this,
265
- params,
266
- emitTableAlias: false,
267
- };
268
- let { entity, filters } = options;
269
- let command = "UPDATE ";
270
-
271
- command += this.quoteTable(model);
272
-
273
- command += " SET ";
274
- const propertyNames: string[] = Object.keys(entity);
275
- propertyNames.forEach((propertyName, index) => {
276
- if (index) {
277
- command += ", ";
278
- }
279
-
280
- let property: RpdDataModelProperty | null = null;
281
- if (model) {
282
- property = find(model.properties, (e: RpdDataModelProperty) => (e.columnName || e.code) === propertyName);
283
- }
284
-
285
- if (property && property.type === "json") {
286
- params.push(JSON.stringify(entity[propertyName]));
287
- command += `${this.quoteObject(propertyName)}=$${params.length}::jsonb`;
288
- } else {
289
- params.push(entity[propertyName]);
290
- command += `${this.quoteObject(propertyName)}=$${params.length}`;
291
- }
292
- });
293
-
294
- if (filters && filters.length) {
295
- command += " WHERE ";
296
- command += buildFiltersQuery(ctx, filters);
297
- }
298
-
299
- command += " RETURNING *";
300
-
301
- return {
302
- command,
303
- params: ctx.params,
304
- };
305
- }
306
-
307
- delete(model: RpdDataModel, options: DeleteRowOptions): DatabaseQuery {
308
- const params: any[] = [];
309
- const ctx: BuildQueryContext = {
310
- builder: this,
311
- params,
312
- emitTableAlias: false,
313
- };
314
- let { filters } = options;
315
- let command = "DELETE FROM ";
316
-
317
- command += this.quoteTable(model);
318
-
319
- if (filters && filters.length) {
320
- command += " WHERE ";
321
- command += buildFiltersQuery(ctx, filters);
322
- }
323
-
324
- return {
325
- command,
326
- params: ctx.params,
327
- };
328
- }
329
- }
330
-
331
- export function buildFiltersQuery(ctx: BuildQueryContext, filters: RowFilterOptions[]) {
332
- return buildFilterQuery(0, ctx, {
333
- operator: "and",
334
- filters,
335
- });
336
- }
337
-
338
- function buildFilterQuery(level: number, ctx: BuildQueryContext, filter: RowFilterOptions): string {
339
- const { operator } = filter;
340
- if (operator === "eq" || operator === "ne" || operator === "gt" || operator === "gte" || operator === "lt" || operator === "lte") {
341
- return buildRelationalFilterQuery(ctx, filter);
342
- } else if (operator === "and" || operator === "or") {
343
- return buildLogicalFilterQuery(level, ctx, filter);
344
- } else if (operator === "null" || operator === "notNull") {
345
- return buildUnaryFilterQuery(ctx, filter);
346
- } else if (operator === "in" || operator === "notIn") {
347
- return buildInFilterQuery(ctx, filter);
348
- } else if (operator === "contains") {
349
- return buildContainsFilterQuery(ctx, filter);
350
- } else if (operator === "notContains") {
351
- return buildNotContainsFilterQuery(ctx, filter);
352
- } else if (operator === "startsWith") {
353
- return buildStartsWithFilterQuery(ctx, filter);
354
- } else if (operator === "notStartsWith") {
355
- return buildNotStartsWithFilterQuery(ctx, filter);
356
- } else if (operator === "endsWith") {
357
- return buildEndsWithFilterQuery(ctx, filter);
358
- } else if (operator === "notEndsWith") {
359
- return buildNotEndsWithFilterQuery(ctx, filter);
360
- } else {
361
- throw new Error(`Filter operator '${operator}' is not supported.`);
362
- }
363
- }
364
-
365
- function buildLogicalFilterQuery(level: number, ctx: BuildQueryContext, filter: FindRowLogicalFilterOptions) {
366
- let dbOperator;
367
- if (filter.operator === "and") {
368
- dbOperator = " AND ";
369
- } else {
370
- dbOperator = " OR ";
371
- }
372
-
373
- let command = filter.filters.map(buildFilterQuery.bind(null, level + 1, ctx)).join(dbOperator);
374
- if (level) {
375
- return `(${command})`;
376
- }
377
- return command;
378
- }
379
-
380
- function buildUnaryFilterQuery(ctx: BuildQueryContext, filter: FindRowUnaryFilterOptions) {
381
- let command = ctx.builder.quoteColumn(filter.field, ctx.emitTableAlias);
382
- if (filter.operator === "null") {
383
- command += " IS NULL";
384
- } else {
385
- command += " IS NOT NULL";
386
- }
387
- return command;
388
- }
389
-
390
- function buildInFilterQuery(ctx: BuildQueryContext, filter: FindRowSetFilterOptions) {
391
- let command = ctx.builder.quoteColumn(filter.field, ctx.emitTableAlias);
392
-
393
- if (filter.operator === "in") {
394
- command += " = ";
395
- } else {
396
- command += " <> ";
397
- }
398
- ctx.params.push(filter.value);
399
- command += `ANY($${ctx.params.length}::${filter.itemType || "int"}[])`;
400
-
401
- return command;
402
- }
403
-
404
- function buildContainsFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
405
- let command = ctx.builder.quoteColumn(filter.field, ctx.emitTableAlias);
406
-
407
- command += " LIKE ";
408
- ctx.params.push(`%${filter.value}%`);
409
- command += "$" + ctx.params.length;
410
-
411
- return command;
412
- }
413
-
414
- function buildNotContainsFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
415
- let command = ctx.builder.quoteColumn(filter.field, ctx.emitTableAlias);
416
-
417
- command += " NOT LIKE ";
418
- ctx.params.push(`%${filter.value}%`);
419
- command += "$" + ctx.params.length;
420
-
421
- return command;
422
- }
423
-
424
- function buildStartsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
425
- let command = ctx.builder.quoteColumn(filter.field, ctx.emitTableAlias);
426
-
427
- command += " LIKE ";
428
- ctx.params.push(`${filter.value}%`);
429
- command += "$" + ctx.params.length;
430
-
431
- return command;
432
- }
433
-
434
- function buildNotStartsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
435
- let command = ctx.builder.quoteColumn(filter.field, ctx.emitTableAlias);
436
-
437
- command += " NOT LIKE ";
438
- ctx.params.push(`${filter.value}%`);
439
- command += "$" + ctx.params.length;
440
-
441
- return command;
442
- }
443
-
444
- function buildEndsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
445
- let command = ctx.builder.quoteColumn(filter.field, ctx.emitTableAlias);
446
-
447
- command += " LIKE ";
448
- ctx.params.push(`%${filter.value}`);
449
- command += "$" + ctx.params.length;
450
-
451
- return command;
452
- }
453
-
454
- function buildNotEndsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
455
- let command = ctx.builder.quoteColumn(filter.field, ctx.emitTableAlias);
456
-
457
- command += " NOT LIKE ";
458
- ctx.params.push(`%${filter.value}`);
459
- command += "$" + ctx.params.length;
460
-
461
- return command;
462
- }
463
-
464
- function buildRelationalFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
465
- let command = ctx.builder.quoteColumn(filter.field, ctx.emitTableAlias);
466
-
467
- command += relationalOperatorsMap.get(filter.operator);
468
-
469
- ctx.params.push(filter.value);
470
- command += "$" + ctx.params.length;
471
-
472
- return command;
473
- }
1
+ import { find } 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
+ } from "~/dataAccess/dataAccessTypes";
17
+
18
+ const objLeftQuoteChar = '"';
19
+ const objRightQuoteChar = '"';
20
+
21
+ const relationalOperatorsMap = new Map<RowFilterRelationalOperators, string>([
22
+ ["eq", "="],
23
+ ["ne", "<>"],
24
+ ["gt", ">"],
25
+ ["gte", ">="],
26
+ ["lt", "<"],
27
+ ["lte", "<="],
28
+ ]);
29
+
30
+ export interface BuildQueryContext {
31
+ model: RpdDataModel;
32
+ builder: QueryBuilder;
33
+ params: any[];
34
+ emitTableAlias: boolean;
35
+ }
36
+
37
+ export interface InitQueryBuilderOptions {
38
+ dbDefaultSchema: string;
39
+ }
40
+
41
+ export default class QueryBuilder {
42
+ #dbDefaultSchema: string;
43
+
44
+ constructor(options: InitQueryBuilderOptions) {
45
+ this.#dbDefaultSchema = options.dbDefaultSchema;
46
+ }
47
+
48
+ quoteTable(options: QuoteTableOptions) {
49
+ const { schema, tableName } = options;
50
+ if (schema) {
51
+ return `${this.quoteObject(schema)}.${this.quoteObject(tableName)}`;
52
+ } else if (this.#dbDefaultSchema) {
53
+ return `${this.quoteObject(this.#dbDefaultSchema)}.${this.quoteObject(tableName)}`;
54
+ } else {
55
+ return this.quoteObject(tableName);
56
+ }
57
+ }
58
+
59
+ quoteObject(name: string) {
60
+ return `${objLeftQuoteChar}${name}${objRightQuoteChar}`;
61
+ }
62
+
63
+ quoteColumn(model: RpdDataModel, column: ColumnSelectOptions, emitTableAlias: boolean) {
64
+ if (typeof column === "string") {
65
+ if (emitTableAlias) {
66
+ return `${objLeftQuoteChar}${model.tableName}${objRightQuoteChar}.${objLeftQuoteChar}${column}${objRightQuoteChar}`;
67
+ } else {
68
+ return `${objLeftQuoteChar}${column}${objRightQuoteChar}`;
69
+ }
70
+ } else {
71
+ if (emitTableAlias && column.tableName) {
72
+ return `${objLeftQuoteChar}${column.tableName}${objRightQuoteChar}.${objLeftQuoteChar}${column.name}${objRightQuoteChar}`;
73
+ } else {
74
+ return `${objLeftQuoteChar}${column.name}${objRightQuoteChar}`;
75
+ }
76
+ }
77
+ }
78
+
79
+ select(model: RpdDataModel, options: FindRowOptions): DatabaseQuery {
80
+ const ctx: BuildQueryContext = {
81
+ model,
82
+ builder: this,
83
+ params: [],
84
+ emitTableAlias: true,
85
+ };
86
+ let { fields: columns, filters, orderBy, pagination } = options;
87
+ let command = "SELECT ";
88
+ if (!columns || !columns.length) {
89
+ command += `${this.quoteObject(model.tableName)}.* FROM `;
90
+ } else {
91
+ command += columns.map((column) => this.quoteColumn(ctx.model, column, ctx.emitTableAlias)).join(", ");
92
+ command += " FROM ";
93
+ }
94
+
95
+ command += this.quoteTable(model);
96
+
97
+ if (options.orderBy) {
98
+ options.orderBy
99
+ .filter((orderByItem) => orderByItem.relationField)
100
+ .forEach((orderByItem) => {
101
+ const { relationField } = orderByItem;
102
+ const orderField = orderByItem.field as ColumnNameWithTableName;
103
+ command += ` LEFT JOIN ${this.quoteTable({ schema: orderField.schema, tableName: orderField.tableName })} ON ${this.quoteObject(
104
+ orderField.tableName,
105
+ )}.id = ${this.quoteObject(relationField.tableName)}.${this.quoteObject(relationField.name)}`;
106
+ });
107
+ }
108
+
109
+ if (filters && filters.length) {
110
+ command += " WHERE ";
111
+ command += buildFiltersQuery(ctx, filters);
112
+ }
113
+
114
+ if (orderBy && orderBy.length) {
115
+ command += " ORDER BY ";
116
+ command += orderBy
117
+ .map((item) => {
118
+ const quotedName = this.quoteColumn(ctx.model, item.field, ctx.emitTableAlias);
119
+ return item.desc ? quotedName + " DESC" : quotedName;
120
+ })
121
+ .join(", ");
122
+ }
123
+
124
+ if (pagination) {
125
+ command += " OFFSET ";
126
+ ctx.params.push(pagination.offset);
127
+ command += "$" + ctx.params.length;
128
+
129
+ command += " LIMIT ";
130
+ ctx.params.push(pagination.limit);
131
+ command += "$" + ctx.params.length;
132
+ }
133
+
134
+ return {
135
+ command,
136
+ params: ctx.params,
137
+ };
138
+ }
139
+
140
+ selectDerived(derivedModel: RpdDataModel, baseModel: RpdDataModel, options: FindRowOptions): DatabaseQuery {
141
+ const ctx: BuildQueryContext = {
142
+ model: derivedModel,
143
+ builder: this,
144
+ params: [],
145
+ emitTableAlias: true,
146
+ };
147
+ let { fields: columns, filters, orderBy, pagination } = options;
148
+ let command = "SELECT ";
149
+ if (!columns || !columns.length) {
150
+ command += `${this.quoteObject(derivedModel.tableName)}.* FROM `;
151
+ } else {
152
+ command += columns
153
+ .map((column) => {
154
+ return this.quoteColumn(derivedModel, column, ctx.emitTableAlias);
155
+ })
156
+ .join(", ");
157
+ command += " FROM ";
158
+ }
159
+
160
+ command += `${this.quoteTable(derivedModel)} LEFT JOIN ${this.quoteTable(baseModel)} ON ${this.quoteObject(derivedModel.tableName)}.id = ${this.quoteObject(
161
+ baseModel.tableName,
162
+ )}.id`;
163
+
164
+ if (options.orderBy) {
165
+ options.orderBy
166
+ .filter((orderByItem) => orderByItem.relationField)
167
+ .forEach((orderByItem) => {
168
+ const { relationField } = orderByItem;
169
+ const orderField = orderByItem.field as ColumnNameWithTableName;
170
+ command += ` LEFT JOIN ${this.quoteTable({ schema: orderField.schema, tableName: orderField.tableName })} ON ${this.quoteObject(
171
+ orderField.tableName,
172
+ )}.id = ${this.quoteObject(relationField.tableName)}.${this.quoteObject(relationField.name)}`;
173
+ });
174
+ }
175
+
176
+ if (filters && filters.length) {
177
+ command += " WHERE ";
178
+ command += buildFiltersQuery(ctx, filters);
179
+ }
180
+
181
+ if (orderBy && orderBy.length) {
182
+ command += " ORDER BY ";
183
+ command += orderBy
184
+ .map((item) => {
185
+ const quotedName = this.quoteColumn(derivedModel, item.field, ctx.emitTableAlias);
186
+ return item.desc ? quotedName + " DESC" : quotedName;
187
+ })
188
+ .join(", ");
189
+ }
190
+
191
+ if (pagination) {
192
+ command += " OFFSET ";
193
+ ctx.params.push(pagination.offset);
194
+ command += "$" + ctx.params.length;
195
+
196
+ command += " LIMIT ";
197
+ ctx.params.push(pagination.limit);
198
+ command += "$" + ctx.params.length;
199
+ }
200
+
201
+ return {
202
+ command,
203
+ params: ctx.params,
204
+ };
205
+ }
206
+
207
+ count(model: RpdDataModel, options: CountRowOptions): DatabaseQuery {
208
+ const ctx: BuildQueryContext = {
209
+ model,
210
+ builder: this,
211
+ params: [],
212
+ emitTableAlias: false,
213
+ };
214
+ let { filters } = options;
215
+ let command = 'SELECT COUNT(*)::int as "count" FROM ';
216
+
217
+ command += this.quoteTable(model);
218
+
219
+ if (filters && filters.length) {
220
+ command += " WHERE ";
221
+ command += buildFiltersQuery(ctx, filters);
222
+ }
223
+
224
+ return {
225
+ command,
226
+ params: ctx.params,
227
+ };
228
+ }
229
+
230
+ countDerived(derivedModel: RpdDataModel, baseModel: RpdDataModel, options: CountRowOptions): DatabaseQuery {
231
+ const ctx: BuildQueryContext = {
232
+ model: derivedModel,
233
+ builder: this,
234
+ params: [],
235
+ emitTableAlias: true,
236
+ };
237
+ let { filters } = options;
238
+ let command = 'SELECT COUNT(*)::int as "count" FROM ';
239
+
240
+ command += `${this.quoteTable(derivedModel)} LEFT JOIN ${this.quoteTable(baseModel)} ON ${this.quoteObject(derivedModel.tableName)}.id = ${this.quoteObject(
241
+ baseModel.tableName,
242
+ )}.id`;
243
+
244
+ if (filters && filters.length) {
245
+ command += " WHERE ";
246
+ command += buildFiltersQuery(ctx, filters);
247
+ }
248
+
249
+ return {
250
+ command,
251
+ params: ctx.params,
252
+ };
253
+ }
254
+
255
+ insert(model: RpdDataModel, options: CreateEntityOptions): DatabaseQuery {
256
+ const params: any[] = [];
257
+ const ctx: BuildQueryContext = {
258
+ model,
259
+ builder: this,
260
+ params,
261
+ emitTableAlias: false,
262
+ };
263
+ const { entity } = options;
264
+ let command = "INSERT INTO ";
265
+
266
+ command += this.quoteTable(model);
267
+
268
+ const propertyNames: string[] = Object.keys(entity);
269
+ let values = "";
270
+ propertyNames.forEach((propertyName, index) => {
271
+ if (index) {
272
+ values += ", ";
273
+ }
274
+
275
+ let property: RpdDataModelProperty | null = null;
276
+ if (model) {
277
+ property = find(model.properties, (e: RpdDataModelProperty) => e.code === propertyName);
278
+ }
279
+
280
+ if (property && property.type === "json") {
281
+ params.push(JSON.stringify(entity[propertyName]));
282
+ values += `$${params.length}::jsonb`;
283
+ } else {
284
+ params.push(entity[propertyName]);
285
+ values += `$${params.length}`;
286
+ }
287
+ });
288
+
289
+ command += ` (${propertyNames.map(this.quoteObject).join(", ")})`;
290
+ command += ` VALUES (${values}) RETURNING *`;
291
+
292
+ return {
293
+ command,
294
+ params: ctx.params,
295
+ };
296
+ }
297
+
298
+ update(model: RpdDataModel, options: UpdateRowOptions): DatabaseQuery {
299
+ const params: any[] = [];
300
+ const ctx: BuildQueryContext = {
301
+ model,
302
+ builder: this,
303
+ params,
304
+ emitTableAlias: false,
305
+ };
306
+ let { entity, filters } = options;
307
+ let command = "UPDATE ";
308
+
309
+ command += this.quoteTable(model);
310
+
311
+ command += " SET ";
312
+ const propertyNames: string[] = Object.keys(entity);
313
+ propertyNames.forEach((propertyName, index) => {
314
+ if (index) {
315
+ command += ", ";
316
+ }
317
+
318
+ let property: RpdDataModelProperty | null = null;
319
+ if (model) {
320
+ property = find(model.properties, (e: RpdDataModelProperty) => (e.columnName || e.code) === propertyName);
321
+ }
322
+
323
+ if (property && property.type === "json") {
324
+ params.push(JSON.stringify(entity[propertyName]));
325
+ command += `${this.quoteObject(propertyName)}=$${params.length}::jsonb`;
326
+ } else {
327
+ params.push(entity[propertyName]);
328
+ command += `${this.quoteObject(propertyName)}=$${params.length}`;
329
+ }
330
+ });
331
+
332
+ if (filters && filters.length) {
333
+ command += " WHERE ";
334
+ command += buildFiltersQuery(ctx, filters);
335
+ }
336
+
337
+ command += " RETURNING *";
338
+
339
+ return {
340
+ command,
341
+ params: ctx.params,
342
+ };
343
+ }
344
+
345
+ delete(model: RpdDataModel, options: DeleteRowOptions): DatabaseQuery {
346
+ const params: any[] = [];
347
+ const ctx: BuildQueryContext = {
348
+ model,
349
+ builder: this,
350
+ params,
351
+ emitTableAlias: false,
352
+ };
353
+ let { filters } = options;
354
+ let command = "DELETE FROM ";
355
+
356
+ command += this.quoteTable(model);
357
+
358
+ if (filters && filters.length) {
359
+ command += " WHERE ";
360
+ command += buildFiltersQuery(ctx, filters);
361
+ }
362
+
363
+ return {
364
+ command,
365
+ params: ctx.params,
366
+ };
367
+ }
368
+ }
369
+
370
+ export function buildFiltersQuery(ctx: BuildQueryContext, filters: RowFilterOptions[]) {
371
+ return buildFilterQuery(0, ctx, {
372
+ operator: "and",
373
+ filters,
374
+ });
375
+ }
376
+
377
+ function buildFilterQuery(level: number, ctx: BuildQueryContext, filter: RowFilterOptions): string {
378
+ const { operator } = filter;
379
+ if (operator === "eq" || operator === "ne" || operator === "gt" || operator === "gte" || operator === "lt" || operator === "lte") {
380
+ return buildRelationalFilterQuery(ctx, filter);
381
+ } else if (operator === "and" || operator === "or") {
382
+ return buildLogicalFilterQuery(level, ctx, filter);
383
+ } else if (operator === "null" || operator === "notNull") {
384
+ return buildUnaryFilterQuery(ctx, filter);
385
+ } else if (operator === "in" || operator === "notIn") {
386
+ return buildInFilterQuery(ctx, filter);
387
+ } else if (operator === "contains") {
388
+ return buildContainsFilterQuery(ctx, filter);
389
+ } else if (operator === "notContains") {
390
+ return buildNotContainsFilterQuery(ctx, filter);
391
+ } else if (operator === "startsWith") {
392
+ return buildStartsWithFilterQuery(ctx, filter);
393
+ } else if (operator === "notStartsWith") {
394
+ return buildNotStartsWithFilterQuery(ctx, filter);
395
+ } else if (operator === "endsWith") {
396
+ return buildEndsWithFilterQuery(ctx, filter);
397
+ } else if (operator === "notEndsWith") {
398
+ return buildNotEndsWithFilterQuery(ctx, filter);
399
+ } else {
400
+ throw new Error(`Filter operator '${operator}' is not supported.`);
401
+ }
402
+ }
403
+
404
+ function buildLogicalFilterQuery(level: number, ctx: BuildQueryContext, filter: FindRowLogicalFilterOptions) {
405
+ let dbOperator;
406
+ if (filter.operator === "and") {
407
+ dbOperator = " AND ";
408
+ } else {
409
+ dbOperator = " OR ";
410
+ }
411
+
412
+ let command = filter.filters.map(buildFilterQuery.bind(null, level + 1, ctx)).join(dbOperator);
413
+ if (level) {
414
+ return `(${command})`;
415
+ }
416
+ return command;
417
+ }
418
+
419
+ function buildUnaryFilterQuery(ctx: BuildQueryContext, filter: FindRowUnaryFilterOptions) {
420
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
421
+ if (filter.operator === "null") {
422
+ command += " IS NULL";
423
+ } else {
424
+ command += " IS NOT NULL";
425
+ }
426
+ return command;
427
+ }
428
+
429
+ function buildInFilterQuery(ctx: BuildQueryContext, filter: FindRowSetFilterOptions) {
430
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
431
+
432
+ if (filter.operator === "in") {
433
+ command += " = ";
434
+ } else {
435
+ command += " <> ";
436
+ }
437
+ ctx.params.push(filter.value);
438
+ command += `ANY($${ctx.params.length}::${filter.itemType || "int"}[])`;
439
+
440
+ return command;
441
+ }
442
+
443
+ function buildContainsFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
444
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
445
+
446
+ command += " LIKE ";
447
+ ctx.params.push(`%${filter.value}%`);
448
+ command += "$" + ctx.params.length;
449
+
450
+ return command;
451
+ }
452
+
453
+ function buildNotContainsFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
454
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
455
+
456
+ command += " NOT LIKE ";
457
+ ctx.params.push(`%${filter.value}%`);
458
+ command += "$" + ctx.params.length;
459
+
460
+ return command;
461
+ }
462
+
463
+ function buildStartsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
464
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
465
+
466
+ command += " LIKE ";
467
+ ctx.params.push(`${filter.value}%`);
468
+ command += "$" + ctx.params.length;
469
+
470
+ return command;
471
+ }
472
+
473
+ function buildNotStartsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
474
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
475
+
476
+ command += " NOT LIKE ";
477
+ ctx.params.push(`${filter.value}%`);
478
+ command += "$" + ctx.params.length;
479
+
480
+ return command;
481
+ }
482
+
483
+ function buildEndsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
484
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
485
+
486
+ command += " LIKE ";
487
+ ctx.params.push(`%${filter.value}`);
488
+ command += "$" + ctx.params.length;
489
+
490
+ return command;
491
+ }
492
+
493
+ function buildNotEndsWithFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
494
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
495
+
496
+ command += " NOT LIKE ";
497
+ ctx.params.push(`%${filter.value}`);
498
+ command += "$" + ctx.params.length;
499
+
500
+ return command;
501
+ }
502
+
503
+ function buildRelationalFilterQuery(ctx: BuildQueryContext, filter: FindRowRelationalFilterOptions) {
504
+ let command = ctx.builder.quoteColumn(ctx.model, filter.field, ctx.emitTableAlias);
505
+
506
+ command += relationalOperatorsMap.get(filter.operator);
507
+
508
+ ctx.params.push(filter.value);
509
+ command += "$" + ctx.params.length;
510
+
511
+ return command;
512
+ }