@ruiapp/rapid-core 0.3.1 → 0.3.3

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