json-rest-api 1.0.14 → 1.0.15

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 (118) hide show
  1. package/connectors_ideas/fastify.js +66 -68
  2. package/connectors_ideas/koa-plugin.js +50 -51
  3. package/eslint.config.js +11 -2
  4. package/examples/url-override-example.js +63 -63
  5. package/index.js +13 -13
  6. package/lib/enhanced-logger.js +51 -51
  7. package/lib/error-formatter.js +61 -61
  8. package/lib/rest-api-errors.js +28 -28
  9. package/package.json +1 -1
  10. package/plugins/core/bulk-operations-plugin.js +142 -145
  11. package/plugins/core/connectors/express-plugin.js +210 -217
  12. package/plugins/core/connectors/lib/busboy-detector.js +84 -84
  13. package/plugins/core/connectors/lib/formidable-detector.js +50 -50
  14. package/plugins/core/connectors/lib/request-helpers.js +74 -74
  15. package/plugins/core/file-handling-plugin.js +125 -125
  16. package/plugins/core/lib/anyapi/anyapi-registry.js +254 -254
  17. package/plugins/core/lib/anyapi/query/anyapi-query-adapter.js +287 -287
  18. package/plugins/core/lib/anyapi/schema-utils.js +101 -101
  19. package/plugins/core/lib/anyapi/utils/descriptor-helpers.js +41 -41
  20. package/plugins/core/lib/dbTablesOperations.js +134 -135
  21. package/plugins/core/lib/fractional-positioning.js +74 -76
  22. package/plugins/core/lib/querying/knex-cross-table-search.js +320 -320
  23. package/plugins/core/lib/querying/knex-json-api-transformers-querying.js +144 -144
  24. package/plugins/core/lib/querying/knex-pagination-helpers.js +164 -164
  25. package/plugins/core/lib/querying/knex-process-includes.js +50 -50
  26. package/plugins/core/lib/querying/knex-query-helpers-base.js +31 -31
  27. package/plugins/core/lib/querying/knex-query-helpers.js +451 -452
  28. package/plugins/core/lib/querying/knex-relationship-includes.js +1160 -1170
  29. package/plugins/core/lib/querying/knex-window-queries.js +89 -89
  30. package/plugins/core/lib/querying/url-helpers.js +35 -35
  31. package/plugins/core/lib/querying-writing/compile-schemas.js +97 -98
  32. package/plugins/core/lib/querying-writing/connectors-query-parser.js +31 -31
  33. package/plugins/core/lib/querying-writing/database-capabilities.js +94 -94
  34. package/plugins/core/lib/querying-writing/database-value-normalizers.js +76 -76
  35. package/plugins/core/lib/querying-writing/default-data-helpers.js +27 -27
  36. package/plugins/core/lib/querying-writing/field-utils.js +44 -42
  37. package/plugins/core/lib/querying-writing/knex-constants.js +15 -16
  38. package/plugins/core/lib/querying-writing/knex-field-helpers.js +117 -115
  39. package/plugins/core/lib/querying-writing/knex-json-api-transformers.js +41 -41
  40. package/plugins/core/lib/querying-writing/payload-validators.js +275 -275
  41. package/plugins/core/lib/querying-writing/schema-helpers.js +129 -129
  42. package/plugins/core/lib/querying-writing/scope-validations.js +143 -146
  43. package/plugins/core/lib/querying-writing/simplified-helpers.js +133 -129
  44. package/plugins/core/lib/storage/storage-adapter.js +68 -68
  45. package/plugins/core/lib/writing/knex-json-api-transformers-writing.js +31 -31
  46. package/plugins/core/lib/writing/many-to-many-manipulations.js +81 -81
  47. package/plugins/core/lib/writing/relationship-processor.js +58 -58
  48. package/plugins/core/rest-api-access.js +8 -8
  49. package/plugins/core/rest-api-anyapi-knex-plugin.js +920 -920
  50. package/plugins/core/rest-api-cors-plugin.js +170 -171
  51. package/plugins/core/rest-api-knex-plugin.js +482 -493
  52. package/plugins/core/rest-api-multihome-plugin.js +247 -251
  53. package/plugins/core/rest-api-plugin-hooks/compile-resource-schemas.js +5 -5
  54. package/plugins/core/rest-api-plugin-hooks/register-relationship-routes.js +20 -21
  55. package/plugins/core/rest-api-plugin-hooks/register-scope-routes.js +44 -44
  56. package/plugins/core/rest-api-plugin-hooks/turn-scope-init-into-vars.js +23 -23
  57. package/plugins/core/rest-api-plugin-hooks/validate-include-configurations.js +15 -15
  58. package/plugins/core/rest-api-plugin-methods/add-route.js +9 -9
  59. package/plugins/core/rest-api-plugin-methods/check-permissions.js +5 -6
  60. package/plugins/core/rest-api-plugin-methods/common.js +295 -297
  61. package/plugins/core/rest-api-plugin-methods/delete-relationship.js +35 -37
  62. package/plugins/core/rest-api-plugin-methods/delete.js +57 -59
  63. package/plugins/core/rest-api-plugin-methods/enrich-attributes.js +172 -171
  64. package/plugins/core/rest-api-plugin-methods/get-related.js +96 -96
  65. package/plugins/core/rest-api-plugin-methods/get-relationship.js +16 -18
  66. package/plugins/core/rest-api-plugin-methods/get.js +86 -86
  67. package/plugins/core/rest-api-plugin-methods/patch-relationship.js +16 -17
  68. package/plugins/core/rest-api-plugin-methods/patch.js +63 -64
  69. package/plugins/core/rest-api-plugin-methods/post-relationship.js +54 -55
  70. package/plugins/core/rest-api-plugin-methods/post.js +61 -62
  71. package/plugins/core/rest-api-plugin-methods/put.js +86 -87
  72. package/plugins/core/rest-api-plugin-methods/query.js +88 -88
  73. package/plugins/core/rest-api-plugin-methods/release.js +5 -6
  74. package/plugins/core/rest-api-plugin.js +76 -78
  75. package/plugins/core/rest-api-positioning-plugin.js +251 -252
  76. package/plugins/core/socketio-plugin.js +240 -236
  77. package/plugins/storage/local-storage.js +103 -103
  78. package/plugins/storage/s3-storage.js +41 -42
  79. package/quickTest.js +37 -37
  80. package/scripts/debug-anyapi-review-filter.mjs +18 -18
  81. package/scripts/inspect-anyapi-descriptor-country.mjs +10 -10
  82. package/scripts/inspect-anyapi-descriptor.mjs +10 -10
  83. package/scripts/run-docs.js +112 -113
  84. package/test-debug.js +28 -28
  85. package/tests/TEST_TEMPLATE.test.js +51 -51
  86. package/tests/anyapi-basic.test.js +42 -42
  87. package/tests/anyapi-cursor-pagination.test.js +48 -48
  88. package/tests/bulk-operations.test.js +208 -208
  89. package/tests/computed-fields.test.js +204 -204
  90. package/tests/cors.test.js +412 -413
  91. package/tests/curl.test.js +275 -276
  92. package/tests/custom-idproperty-relationships.test.js +266 -266
  93. package/tests/field-getters.test.js +132 -132
  94. package/tests/field-setters.test.js +110 -110
  95. package/tests/fixtures/api-configs.js +699 -699
  96. package/tests/helpers/storage-mode.js +32 -32
  97. package/tests/helpers/test-utils.js +76 -76
  98. package/tests/include-depth-validation.test.js +76 -76
  99. package/tests/includes.test.js +145 -145
  100. package/tests/multihome.test.js +175 -175
  101. package/tests/nested-includes.test.js +255 -255
  102. package/tests/pagination-cursor-multifield.test.js +164 -164
  103. package/tests/pagination-enhanced.test.js +225 -225
  104. package/tests/polymorphic-relationships.test.js +96 -96
  105. package/tests/polymorphic-simple.test.js +44 -44
  106. package/tests/positioning-advanced.test.js +226 -226
  107. package/tests/positioning-extended.test.js +399 -399
  108. package/tests/positioning-multifilter.test.js +204 -204
  109. package/tests/positioning.test.js +266 -266
  110. package/tests/queries.test.js +184 -184
  111. package/tests/query-limits.test.js +154 -155
  112. package/tests/relationship-endpoints.test.js +127 -127
  113. package/tests/relationships.test.js +178 -179
  114. package/tests/rest-api-access.test.js +86 -86
  115. package/tests/rest-api.test.js +103 -103
  116. package/tests/searchschema-merge.test.js +93 -93
  117. package/tests/socketio.test.js +326 -326
  118. package/tests/virtual-fields.test.js +164 -164
@@ -1,4 +1,4 @@
1
- import { resolveFieldInfo } from '../utils/descriptor-helpers.js';
1
+ import { resolveFieldInfo } from '../utils/descriptor-helpers.js'
2
2
 
3
3
  const JOIN_METHODS = new Set([
4
4
  'join',
@@ -9,7 +9,7 @@ const JOIN_METHODS = new Set([
9
9
  'rightOuterJoin',
10
10
  'outerJoin',
11
11
  'crossJoin',
12
- ]);
12
+ ])
13
13
 
14
14
  const JOIN_ON_METHODS = new Set([
15
15
  'on',
@@ -18,28 +18,28 @@ const JOIN_ON_METHODS = new Set([
18
18
  'onVal',
19
19
  'andOnVal',
20
20
  'orOnVal',
21
- ]);
21
+ ])
22
22
 
23
23
  const JOIN_ON_IN_METHODS = new Set([
24
24
  'onIn',
25
25
  'orOnIn',
26
26
  'onNotIn',
27
27
  'orOnNotIn',
28
- ]);
28
+ ])
29
29
 
30
30
  const JOIN_ON_NULL_METHODS = new Set([
31
31
  'onNull',
32
32
  'orOnNull',
33
33
  'onNotNull',
34
34
  'orOnNotNull',
35
- ]);
35
+ ])
36
36
 
37
37
  const JOIN_ON_BETWEEN_METHODS = new Set([
38
38
  'onBetween',
39
39
  'orOnBetween',
40
40
  'onNotBetween',
41
41
  'orOnNotBetween',
42
- ]);
42
+ ])
43
43
 
44
44
  const WHERE_METHODS = new Set([
45
45
  'where',
@@ -50,26 +50,26 @@ const WHERE_METHODS = new Set([
50
50
  'orWhereNot',
51
51
  'having',
52
52
  'orHaving',
53
- ]);
53
+ ])
54
54
 
55
55
  const WHERE_IN_METHODS = new Set([
56
56
  'whereIn',
57
57
  'orWhereIn',
58
58
  'whereNotIn',
59
59
  'orWhereNotIn',
60
- ]);
60
+ ])
61
61
 
62
- const BETWEEN_METHODS = new Set(['whereBetween', 'orWhereBetween', 'whereNotBetween', 'orWhereNotBetween']);
62
+ const BETWEEN_METHODS = new Set(['whereBetween', 'orWhereBetween', 'whereNotBetween', 'orWhereNotBetween'])
63
63
 
64
- const NULL_METHODS = new Set(['whereNull', 'orWhereNull', 'whereNotNull', 'orWhereNotNull']);
64
+ const NULL_METHODS = new Set(['whereNull', 'orWhereNull', 'whereNotNull', 'orWhereNotNull'])
65
65
 
66
- const ORDER_METHODS = new Set(['orderBy', 'orderByRaw']);
66
+ const ORDER_METHODS = new Set(['orderBy', 'orderByRaw'])
67
67
 
68
- const GROUP_METHODS = new Set(['groupBy', 'groupByRaw']);
68
+ const GROUP_METHODS = new Set(['groupBy', 'groupByRaw'])
69
69
 
70
- const SELECT_METHODS = new Set(['select', 'columns', 'distinct']);
70
+ const SELECT_METHODS = new Set(['select', 'columns', 'distinct'])
71
71
 
72
- const AGGREGATE_METHODS = new Set(['count', 'min', 'max', 'sum', 'avg']);
72
+ const AGGREGATE_METHODS = new Set(['count', 'min', 'max', 'sum', 'avg'])
73
73
 
74
74
  const CALLBACK_FIRST_METHODS = new Set([
75
75
  'where',
@@ -77,10 +77,10 @@ const CALLBACK_FIRST_METHODS = new Set([
77
77
  'orWhere',
78
78
  'having',
79
79
  'orHaving',
80
- ]);
80
+ ])
81
81
 
82
- const isFunction = (value) => typeof value === 'function';
83
- const isObject = (value) => value && typeof value === 'object' && !Array.isArray(value);
82
+ const isFunction = (value) => typeof value === 'function'
83
+ const isObject = (value) => value && typeof value === 'object' && !Array.isArray(value)
84
84
 
85
85
  const SQL_OPERATORS = new Set([
86
86
  '=',
@@ -98,111 +98,111 @@ const SQL_OPERATORS = new Set([
98
98
  'is not',
99
99
  'is null',
100
100
  'is not null',
101
- ]);
101
+ ])
102
102
 
103
103
  const parseTableExpression = (expression) => {
104
104
  if (expression && typeof expression === 'object' && !Array.isArray(expression)) {
105
- const [alias, tableName] = Object.entries(expression)[0] || [];
105
+ const [alias, tableName] = Object.entries(expression)[0] || []
106
106
  if (!alias) {
107
- return { original: expression };
107
+ return { original: expression }
108
108
  }
109
- return { alias, tableName, original: expression };
109
+ return { alias, tableName, original: expression }
110
110
  }
111
111
 
112
112
  if (typeof expression !== 'string') {
113
- return { original: expression };
113
+ return { original: expression }
114
114
  }
115
115
 
116
- const raw = expression.trim();
116
+ const raw = expression.trim()
117
117
  if (raw === '') {
118
- return { original: expression };
118
+ return { original: expression }
119
119
  }
120
120
 
121
- const asMatch = raw.split(/\s+as\s+/i);
121
+ const asMatch = raw.split(/\s+as\s+/i)
122
122
  if (asMatch.length === 2) {
123
- return { tableName: asMatch[0].trim(), alias: asMatch[1].trim(), original: expression };
123
+ return { tableName: asMatch[0].trim(), alias: asMatch[1].trim(), original: expression }
124
124
  }
125
125
 
126
- const parts = raw.split(/\s+/);
126
+ const parts = raw.split(/\s+/)
127
127
  if (parts.length === 2) {
128
- return { tableName: parts[0].trim(), alias: parts[1].trim(), original: expression };
128
+ return { tableName: parts[0].trim(), alias: parts[1].trim(), original: expression }
129
129
  }
130
130
 
131
- return { tableName: raw, alias: raw, original: expression };
132
- };
131
+ return { tableName: raw, alias: raw, original: expression }
132
+ }
133
133
 
134
134
  const translateObjectKeys = (adapter, obj) => {
135
- const result = {};
135
+ const result = {}
136
136
  for (const [key, value] of Object.entries(obj)) {
137
- result[adapter.translateColumn(key)] = value;
137
+ result[adapter.translateColumn(key)] = value
138
138
  }
139
- return result;
140
- };
139
+ return result
140
+ }
141
141
 
142
142
  const translateJoinObject = (adapter, obj) => {
143
- const result = {};
143
+ const result = {}
144
144
  for (const [lhs, rhs] of Object.entries(obj)) {
145
- const translatedKey = typeof lhs === 'string' ? adapter.translateColumn(lhs) : lhs;
146
- const translatedValue = typeof rhs === 'string' ? adapter.translateColumn(rhs) : rhs;
147
- result[translatedKey] = translatedValue;
145
+ const translatedKey = typeof lhs === 'string' ? adapter.translateColumn(lhs) : lhs
146
+ const translatedValue = typeof rhs === 'string' ? adapter.translateColumn(rhs) : rhs
147
+ result[translatedKey] = translatedValue
148
148
  }
149
- return result;
150
- };
149
+ return result
150
+ }
151
151
 
152
152
  const maybeWrapCallback = (adapter, callback) => {
153
- if (!isFunction(callback)) return callback;
154
- return function wrappedCallback(...args) {
155
- return callback.apply(adapter.proxy, args);
156
- };
157
- };
153
+ if (!isFunction(callback)) return callback
154
+ return function wrappedCallback (...args) {
155
+ return callback.apply(adapter.proxy, args)
156
+ }
157
+ }
158
158
 
159
- const isRaw = (value) => Boolean(value && typeof value === 'object' && typeof value.toSQL === 'function' && value.toSQL().method);
159
+ const isRaw = (value) => Boolean(value && typeof value === 'object' && typeof value.toSQL === 'function' && value.toSQL().method)
160
160
 
161
- const ensureArray = (value) => (Array.isArray(value) ? value : [value]);
161
+ const ensureArray = (value) => (Array.isArray(value) ? value : [value])
162
162
 
163
- export async function preloadRelatedDescriptors({ registry, descriptor }) {
164
- const visited = new Map();
165
- const queue = [descriptor.resource];
166
- visited.set(descriptor.resource, descriptor);
163
+ export async function preloadRelatedDescriptors ({ registry, descriptor }) {
164
+ const visited = new Map()
165
+ const queue = [descriptor.resource]
166
+ visited.set(descriptor.resource, descriptor)
167
167
 
168
168
  while (queue.length > 0) {
169
- const resource = queue.pop();
170
- const current = visited.get(resource);
171
- if (!current) continue;
169
+ const resource = queue.pop()
170
+ const current = visited.get(resource)
171
+ if (!current) continue
172
172
 
173
- const relatedTargets = new Set();
173
+ const relatedTargets = new Set()
174
174
 
175
175
  Object.values(current.belongsTo || {}).forEach((info) => {
176
- if (info?.target) relatedTargets.add(info.target);
177
- });
176
+ if (info?.target) relatedTargets.add(info.target)
177
+ })
178
178
 
179
179
  Object.values(current.relationships || {}).forEach((info) => {
180
- if (info?.target) relatedTargets.add(info.target);
181
- });
180
+ if (info?.target) relatedTargets.add(info.target)
181
+ })
182
182
 
183
183
  Object.values(current.manyToMany || {}).forEach((info) => {
184
- if (info?.target) relatedTargets.add(info.target);
185
- if (info?.through) relatedTargets.add(info.through);
186
- });
184
+ if (info?.target) relatedTargets.add(info.target)
185
+ if (info?.through) relatedTargets.add(info.through)
186
+ })
187
187
 
188
188
  Object.values(current.polymorphicBelongsTo || {}).forEach((info) => {
189
- ensureArray(info?.types || []).forEach((type) => relatedTargets.add(type));
190
- });
189
+ ensureArray(info?.types || []).forEach((type) => relatedTargets.add(type))
190
+ })
191
191
 
192
192
  for (const target of relatedTargets) {
193
- if (!target || visited.has(target)) continue;
194
- const nextDescriptor = await registry.getDescriptor(current.tenant, target);
195
- if (!nextDescriptor) continue;
196
- visited.set(target, nextDescriptor);
197
- queue.push(target);
193
+ if (!target || visited.has(target)) continue
194
+ const nextDescriptor = await registry.getDescriptor(current.tenant, target)
195
+ if (!nextDescriptor) continue
196
+ visited.set(target, nextDescriptor)
197
+ queue.push(target)
198
198
  }
199
199
  }
200
200
 
201
- return visited;
201
+ return visited
202
202
  }
203
203
 
204
204
  export class AnyapiQueryAdapter {
205
- constructor({
205
+ constructor ({
206
206
  descriptor,
207
207
  db,
208
208
  registry,
@@ -211,437 +211,437 @@ export class AnyapiQueryAdapter {
211
211
  tableNameToResource,
212
212
  log,
213
213
  }) {
214
- this.descriptor = descriptor;
215
- this.db = db;
216
- this.registry = registry;
217
- this.descriptorsMap = descriptorsMap;
218
- this.resourceToTableName = resourceToTableName || new Map();
219
- this.tableNameToResource = tableNameToResource || new Map();
220
- this.log = log || console;
221
-
222
- this.primaryAlias = this.resourceToTableName.get(descriptor.resource) || descriptor.resource;
223
- this.aliasDescriptors = new Map();
224
- this.aliasDescriptors.set(this.primaryAlias, descriptor);
225
-
226
- this.aliasDescriptors.set(descriptor.resource, descriptor);
227
- this.aliasActualNames = new Map();
228
- this.aliasActualNames.set(this.primaryAlias, this.primaryAlias);
229
- this.aliasActualNames.set(descriptor.resource, this.primaryAlias);
230
-
231
- const physicalTableName = this.resourceToTableName.get(descriptor.resource);
214
+ this.descriptor = descriptor
215
+ this.db = db
216
+ this.registry = registry
217
+ this.descriptorsMap = descriptorsMap
218
+ this.resourceToTableName = resourceToTableName || new Map()
219
+ this.tableNameToResource = tableNameToResource || new Map()
220
+ this.log = log || console
221
+
222
+ this.primaryAlias = this.resourceToTableName.get(descriptor.resource) || descriptor.resource
223
+ this.aliasDescriptors = new Map()
224
+ this.aliasDescriptors.set(this.primaryAlias, descriptor)
225
+
226
+ this.aliasDescriptors.set(descriptor.resource, descriptor)
227
+ this.aliasActualNames = new Map()
228
+ this.aliasActualNames.set(this.primaryAlias, this.primaryAlias)
229
+ this.aliasActualNames.set(descriptor.resource, this.primaryAlias)
230
+
231
+ const physicalTableName = this.resourceToTableName.get(descriptor.resource)
232
232
  if (physicalTableName) {
233
- this.aliasDescriptors.set(physicalTableName, descriptor);
234
- this.aliasActualNames.set(physicalTableName, this.primaryAlias);
233
+ this.aliasDescriptors.set(physicalTableName, descriptor)
234
+ this.aliasActualNames.set(physicalTableName, this.primaryAlias)
235
235
  }
236
236
 
237
- this.builders = new WeakMap();
237
+ this.builders = new WeakMap()
238
238
 
239
239
  this.builder = db({ [this.primaryAlias]: descriptor.canonical.tableName })
240
240
  .where(`${this.primaryAlias}.${descriptor.canonical.tenantColumn}`, descriptor.tenant)
241
- .where(`${this.primaryAlias}.${descriptor.canonical.resourceColumn}`, descriptor.resource);
241
+ .where(`${this.primaryAlias}.${descriptor.canonical.resourceColumn}`, descriptor.resource)
242
242
 
243
- this.proxy = this.#createProxy(this.builder);
243
+ this.proxy = this.#createProxy(this.builder)
244
244
  }
245
245
 
246
- get tableAlias() {
247
- return this.primaryAlias;
246
+ get tableAlias () {
247
+ return this.primaryAlias
248
248
  }
249
249
 
250
- get query() {
251
- return this.proxy;
250
+ get query () {
251
+ return this.proxy
252
252
  }
253
253
 
254
- translateColumn(reference) {
254
+ translateColumn (reference) {
255
255
  if (typeof reference !== 'string') {
256
- return reference;
256
+ return reference
257
257
  }
258
258
 
259
- const trimmed = reference.trim();
259
+ const trimmed = reference.trim()
260
260
  if (trimmed === '*' || trimmed === '') {
261
- return reference;
261
+ return reference
262
262
  }
263
263
 
264
- const parts = trimmed.split('.');
265
- let requestedAlias;
266
- let field;
264
+ const parts = trimmed.split('.')
265
+ let requestedAlias
266
+ let field
267
267
 
268
268
  if (parts.length === 1) {
269
- requestedAlias = this.primaryAlias;
270
- field = parts[0];
269
+ requestedAlias = this.primaryAlias
270
+ field = parts[0]
271
271
  } else {
272
- requestedAlias = parts.shift();
273
- field = parts.join('.');
272
+ requestedAlias = parts.shift()
273
+ field = parts.join('.')
274
274
  }
275
275
 
276
276
  if (!field) {
277
- return reference;
277
+ return reference
278
278
  }
279
279
 
280
- let descriptor = this.aliasDescriptors.get(requestedAlias);
280
+ let descriptor = this.aliasDescriptors.get(requestedAlias)
281
281
  if (!descriptor) {
282
- const resource = this.tableNameToResource.get(requestedAlias) || requestedAlias;
283
- descriptor = this.descriptorsMap.get(resource);
282
+ const resource = this.tableNameToResource.get(requestedAlias) || requestedAlias
283
+ descriptor = this.descriptorsMap.get(resource)
284
284
  if (descriptor) {
285
- this.aliasDescriptors.set(requestedAlias, descriptor);
286
- const actualAlias = this.resourceToTableName.get(resource) || resource;
287
- this.aliasActualNames.set(requestedAlias, actualAlias);
285
+ this.aliasDescriptors.set(requestedAlias, descriptor)
286
+ const actualAlias = this.resourceToTableName.get(resource) || resource
287
+ this.aliasActualNames.set(requestedAlias, actualAlias)
288
288
  }
289
289
  }
290
290
 
291
291
  if (!descriptor) {
292
- return reference;
292
+ return reference
293
293
  }
294
294
 
295
- const info = resolveFieldInfo(descriptor, field);
295
+ const info = resolveFieldInfo(descriptor, field)
296
296
  if (!info?.column) {
297
- return reference;
297
+ return reference
298
298
  }
299
299
 
300
- const actualAlias = this.aliasActualNames.get(requestedAlias)
301
- || this.resourceToTableName.get(descriptor.resource)
302
- || descriptor.resource;
300
+ const actualAlias = this.aliasActualNames.get(requestedAlias) ||
301
+ this.resourceToTableName.get(descriptor.resource) ||
302
+ descriptor.resource
303
303
 
304
- return `${actualAlias}.${info.column}`;
304
+ return `${actualAlias}.${info.column}`
305
305
  }
306
306
 
307
- translateColumns(columns) {
307
+ translateColumns (columns) {
308
308
  if (Array.isArray(columns)) {
309
- return columns.map((column) => this.translateColumn(column));
309
+ return columns.map((column) => this.translateColumn(column))
310
310
  }
311
- return this.translateColumn(columns);
311
+ return this.translateColumn(columns)
312
312
  }
313
313
 
314
- registerAlias(alias, descriptor) {
315
- if (!alias || !descriptor) return;
316
- this.aliasDescriptors.set(alias, descriptor);
317
- this.aliasActualNames.set(alias, alias);
314
+ registerAlias (alias, descriptor) {
315
+ if (!alias || !descriptor) return
316
+ this.aliasDescriptors.set(alias, descriptor)
317
+ this.aliasActualNames.set(alias, alias)
318
318
  }
319
319
 
320
- #translateWhereArgs(method, args) {
321
- if (args.length === 0) return args;
322
- const [first, ...rest] = args;
320
+ #translateWhereArgs (method, args) {
321
+ if (args.length === 0) return args
322
+ const [first, ...rest] = args
323
323
 
324
324
  if (isFunction(first)) {
325
- return [maybeWrapCallback(this, first), ...rest];
325
+ return [maybeWrapCallback(this, first), ...rest]
326
326
  }
327
327
 
328
328
  if (isObject(first) && !isRaw(first)) {
329
- return [translateObjectKeys(this, first), ...rest];
329
+ return [translateObjectKeys(this, first), ...rest]
330
330
  }
331
331
 
332
332
  if (Array.isArray(first)) {
333
- return [first.map((item) => (typeof item === 'string' ? this.translateColumn(item) : item)), ...rest];
333
+ return [first.map((item) => (typeof item === 'string' ? this.translateColumn(item) : item)), ...rest]
334
334
  }
335
335
 
336
- const translatedFirst = this.translateColumn(first);
336
+ const translatedFirst = this.translateColumn(first)
337
337
 
338
338
  if (rest.length > 0 && isFunction(rest[rest.length - 1])) {
339
- const newRest = [...rest];
340
- newRest[newRest.length - 1] = maybeWrapCallback(this, rest[rest.length - 1]);
341
- return [translatedFirst, ...newRest];
339
+ const newRest = [...rest]
340
+ newRest[newRest.length - 1] = maybeWrapCallback(this, rest[rest.length - 1])
341
+ return [translatedFirst, ...newRest]
342
342
  }
343
343
 
344
- return [translatedFirst, ...rest];
344
+ return [translatedFirst, ...rest]
345
345
  }
346
346
 
347
- #translateWhereInArgs(args) {
348
- if (args.length === 0) return args;
349
- const [column, values, ...rest] = args;
350
- const translatedColumn = this.translateColumn(column);
347
+ #translateWhereInArgs (args) {
348
+ if (args.length === 0) return args
349
+ const [column, values, ...rest] = args
350
+ const translatedColumn = this.translateColumn(column)
351
351
 
352
352
  if (isFunction(values)) {
353
- return [translatedColumn, maybeWrapCallback(this, values), ...rest];
353
+ return [translatedColumn, maybeWrapCallback(this, values), ...rest]
354
354
  }
355
355
 
356
- return [translatedColumn, values, ...rest];
356
+ return [translatedColumn, values, ...rest]
357
357
  }
358
358
 
359
- #translateBetweenArgs(args) {
360
- if (args.length === 0) return args;
361
- const [column, range, ...rest] = args;
362
- return [this.translateColumn(column), range, ...rest];
359
+ #translateBetweenArgs (args) {
360
+ if (args.length === 0) return args
361
+ const [column, range, ...rest] = args
362
+ return [this.translateColumn(column), range, ...rest]
363
363
  }
364
364
 
365
- #translateNullArgs(args) {
366
- if (args.length === 0) return args;
367
- const [column, ...rest] = args;
368
- return [this.translateColumn(column), ...rest];
365
+ #translateNullArgs (args) {
366
+ if (args.length === 0) return args
367
+ const [column, ...rest] = args
368
+ return [this.translateColumn(column), ...rest]
369
369
  }
370
370
 
371
- #translateOrderArgs(method, args) {
372
- if (args.length === 0) return args;
371
+ #translateOrderArgs (method, args) {
372
+ if (args.length === 0) return args
373
373
 
374
374
  if (method === 'orderByRaw') {
375
- return args;
375
+ return args
376
376
  }
377
377
 
378
- const [column, direction, ...rest] = args;
378
+ const [column, direction, ...rest] = args
379
379
 
380
380
  if (Array.isArray(column)) {
381
381
  return [column.map((entry) => {
382
382
  if (typeof entry === 'string') {
383
- return this.translateColumn(entry);
383
+ return this.translateColumn(entry)
384
384
  }
385
385
  if (isObject(entry)) {
386
- const [col, dir] = Object.entries(entry)[0] || [];
387
- if (!col) return entry;
388
- return { [this.translateColumn(col)]: dir };
386
+ const [col, dir] = Object.entries(entry)[0] || []
387
+ if (!col) return entry
388
+ return { [this.translateColumn(col)]: dir }
389
389
  }
390
- return entry;
391
- }), direction, ...rest];
390
+ return entry
391
+ }), direction, ...rest]
392
392
  }
393
393
 
394
394
  if (isObject(column) && column.column) {
395
- return [{ ...column, column: this.translateColumn(column.column) }, direction, ...rest];
395
+ return [{ ...column, column: this.translateColumn(column.column) }, direction, ...rest]
396
396
  }
397
397
 
398
- return [this.translateColumn(column), direction, ...rest];
398
+ return [this.translateColumn(column), direction, ...rest]
399
399
  }
400
400
 
401
- #translateGroupArgs(args) {
402
- if (args.length === 0) return args;
403
- const [first, ...rest] = args;
401
+ #translateGroupArgs (args) {
402
+ if (args.length === 0) return args
403
+ const [first, ...rest] = args
404
404
  if (Array.isArray(first)) {
405
- return [first.map((entry) => (typeof entry === 'string' ? this.translateColumn(entry) : entry)), ...rest];
405
+ return [first.map((entry) => (typeof entry === 'string' ? this.translateColumn(entry) : entry)), ...rest]
406
406
  }
407
- return [this.translateColumn(first), ...rest];
407
+ return [this.translateColumn(first), ...rest]
408
408
  }
409
409
 
410
- #translateSelectArgs(args) {
411
- if (args.length === 0) return args;
412
- const [first, ...rest] = args;
410
+ #translateSelectArgs (args) {
411
+ if (args.length === 0) return args
412
+ const [first, ...rest] = args
413
413
 
414
414
  if (Array.isArray(first)) {
415
- return [first.map((entry) => (typeof entry === 'string' ? this.translateColumn(entry) : entry)), ...rest];
415
+ return [first.map((entry) => (typeof entry === 'string' ? this.translateColumn(entry) : entry)), ...rest]
416
416
  }
417
417
 
418
418
  if (typeof first === 'string') {
419
- return [this.translateColumn(first), ...rest];
419
+ return [this.translateColumn(first), ...rest]
420
420
  }
421
421
 
422
- return [first, ...rest];
422
+ return [first, ...rest]
423
423
  }
424
424
 
425
- #translateAggregateArgs(args) {
426
- if (args.length === 0) return args;
427
- const [column, options] = args;
425
+ #translateAggregateArgs (args) {
426
+ if (args.length === 0) return args
427
+ const [column, options] = args
428
428
  if (Array.isArray(column)) {
429
- return [column.map((entry) => (typeof entry === 'string' ? this.translateColumn(entry) : entry)), options];
429
+ return [column.map((entry) => (typeof entry === 'string' ? this.translateColumn(entry) : entry)), options]
430
430
  }
431
431
  if (typeof column === 'string') {
432
- return [this.translateColumn(column), options];
432
+ return [this.translateColumn(column), options]
433
433
  }
434
- return args;
434
+ return args
435
435
  }
436
436
 
437
- #translateOnArgs(args) {
438
- if (args.length === 0) return args;
439
- const [first, ...rest] = args;
437
+ #translateOnArgs (args) {
438
+ if (args.length === 0) return args
439
+ const [first, ...rest] = args
440
440
 
441
441
  if (isFunction(first)) {
442
- return [maybeWrapCallback(this, first), ...rest];
442
+ return [maybeWrapCallback(this, first), ...rest]
443
443
  }
444
444
 
445
445
  if (isObject(first) && !isRaw(first)) {
446
- return [translateJoinObject(this, first), ...rest];
446
+ return [translateJoinObject(this, first), ...rest]
447
447
  }
448
448
 
449
- const translatedFirst = typeof first === 'string' ? this.translateColumn(first) : first;
449
+ const translatedFirst = typeof first === 'string' ? this.translateColumn(first) : first
450
450
 
451
451
  if (rest.length === 0) {
452
- return [translatedFirst];
452
+ return [translatedFirst]
453
453
  }
454
454
 
455
455
  const translatedRest = rest.map((value, index) => {
456
456
  if (isFunction(value)) {
457
- return maybeWrapCallback(this, value);
457
+ return maybeWrapCallback(this, value)
458
458
  }
459
459
  if (typeof value !== 'string') {
460
- return value;
460
+ return value
461
461
  }
462
- const lower = value.toLowerCase();
462
+ const lower = value.toLowerCase()
463
463
  if (SQL_OPERATORS.has(lower)) {
464
- return value;
464
+ return value
465
465
  }
466
- return this.translateColumn(value);
467
- });
466
+ return this.translateColumn(value)
467
+ })
468
468
 
469
- return [translatedFirst, ...translatedRest];
469
+ return [translatedFirst, ...translatedRest]
470
470
  }
471
471
 
472
- #translateOnInArgs(args) {
473
- if (args.length === 0) return args;
474
- const [column, values, ...rest] = args;
475
- const translatedColumn = typeof column === 'string' ? this.translateColumn(column) : column;
472
+ #translateOnInArgs (args) {
473
+ if (args.length === 0) return args
474
+ const [column, values, ...rest] = args
475
+ const translatedColumn = typeof column === 'string' ? this.translateColumn(column) : column
476
476
 
477
477
  if (isFunction(values)) {
478
- return [translatedColumn, maybeWrapCallback(this, values), ...rest];
478
+ return [translatedColumn, maybeWrapCallback(this, values), ...rest]
479
479
  }
480
480
 
481
- return [translatedColumn, values, ...rest];
481
+ return [translatedColumn, values, ...rest]
482
482
  }
483
483
 
484
- #translateOnBetweenArgs(args) {
485
- if (args.length === 0) return args;
486
- const [column, range, ...rest] = args;
487
- return [typeof column === 'string' ? this.translateColumn(column) : column, range, ...rest];
484
+ #translateOnBetweenArgs (args) {
485
+ if (args.length === 0) return args
486
+ const [column, range, ...rest] = args
487
+ return [typeof column === 'string' ? this.translateColumn(column) : column, range, ...rest]
488
488
  }
489
489
 
490
- #translateOnNullArgs(args) {
491
- if (args.length === 0) return args;
492
- const [column, ...rest] = args;
493
- return [typeof column === 'string' ? this.translateColumn(column) : column, ...rest];
490
+ #translateOnNullArgs (args) {
491
+ if (args.length === 0) return args
492
+ const [column, ...rest] = args
493
+ return [typeof column === 'string' ? this.translateColumn(column) : column, ...rest]
494
494
  }
495
495
 
496
- #addResourceConstraints(joinClause, alias, descriptor) {
497
- joinClause.on(`${alias}.${descriptor.canonical.tenantColumn}`, this.db.raw('?', [descriptor.tenant]));
498
- joinClause.andOn(`${alias}.${descriptor.canonical.resourceColumn}`, this.db.raw('?', [descriptor.resource]));
496
+ #addResourceConstraints (joinClause, alias, descriptor) {
497
+ joinClause.on(`${alias}.${descriptor.canonical.tenantColumn}`, this.db.raw('?', [descriptor.tenant]))
498
+ joinClause.andOn(`${alias}.${descriptor.canonical.resourceColumn}`, this.db.raw('?', [descriptor.resource]))
499
499
  }
500
500
 
501
- #createJoinProxy(joinClause) {
502
- const adapter = this;
501
+ #createJoinProxy (joinClause) {
502
+ const adapter = this
503
503
  const handler = {
504
- get(target, prop, receiver) {
505
- const original = target[prop];
504
+ get (target, prop, receiver) {
505
+ const original = target[prop]
506
506
  if (typeof original !== 'function') {
507
- return Reflect.get(target, prop, receiver);
507
+ return Reflect.get(target, prop, receiver)
508
508
  }
509
509
  return (...args) => {
510
- let translatedArgs = args;
510
+ let translatedArgs = args
511
511
 
512
512
  if (WHERE_METHODS.has(prop)) {
513
- translatedArgs = adapter.#translateWhereArgs(prop, args);
513
+ translatedArgs = adapter.#translateWhereArgs(prop, args)
514
514
  } else if (WHERE_IN_METHODS.has(prop)) {
515
- translatedArgs = adapter.#translateWhereInArgs(args);
515
+ translatedArgs = adapter.#translateWhereInArgs(args)
516
516
  } else if (BETWEEN_METHODS.has(prop)) {
517
- translatedArgs = adapter.#translateBetweenArgs(args);
517
+ translatedArgs = adapter.#translateBetweenArgs(args)
518
518
  } else if (NULL_METHODS.has(prop)) {
519
- translatedArgs = adapter.#translateNullArgs(args);
519
+ translatedArgs = adapter.#translateNullArgs(args)
520
520
  } else if (JOIN_ON_METHODS.has(prop)) {
521
- translatedArgs = adapter.#translateOnArgs(args);
521
+ translatedArgs = adapter.#translateOnArgs(args)
522
522
  } else if (JOIN_ON_IN_METHODS.has(prop)) {
523
- translatedArgs = adapter.#translateOnInArgs(args);
523
+ translatedArgs = adapter.#translateOnInArgs(args)
524
524
  } else if (JOIN_ON_BETWEEN_METHODS.has(prop)) {
525
- translatedArgs = adapter.#translateOnBetweenArgs(args);
525
+ translatedArgs = adapter.#translateOnBetweenArgs(args)
526
526
  } else if (JOIN_ON_NULL_METHODS.has(prop)) {
527
- translatedArgs = adapter.#translateOnNullArgs(args);
527
+ translatedArgs = adapter.#translateOnNullArgs(args)
528
528
  }
529
529
 
530
- const result = original.apply(target, translatedArgs);
531
- return result === target ? receiver : result;
532
- };
530
+ const result = original.apply(target, translatedArgs)
531
+ return result === target ? receiver : result
532
+ }
533
533
  },
534
- };
534
+ }
535
535
 
536
- return new Proxy(joinClause, handler);
536
+ return new Proxy(joinClause, handler)
537
537
  }
538
538
 
539
- #handleJoin(method, args) {
540
- if (args.length === 0) return args;
541
- const [tableExpression, ...rest] = args;
542
- const parsed = parseTableExpression(tableExpression);
539
+ #handleJoin (method, args) {
540
+ if (args.length === 0) return args
541
+ const [tableExpression, ...rest] = args
542
+ const parsed = parseTableExpression(tableExpression)
543
543
 
544
544
  if (!parsed.tableName || !parsed.alias) {
545
- return args;
545
+ return args
546
546
  }
547
547
 
548
- const resource = this.tableNameToResource.get(parsed.tableName) || parsed.tableName;
549
- const descriptor = this.descriptorsMap.get(resource);
548
+ const resource = this.tableNameToResource.get(parsed.tableName) || parsed.tableName
549
+ const descriptor = this.descriptorsMap.get(resource)
550
550
 
551
551
  if (!descriptor) {
552
- return args;
552
+ return args
553
553
  }
554
554
 
555
- this.registerAlias(parsed.alias, descriptor);
555
+ this.registerAlias(parsed.alias, descriptor)
556
556
 
557
- const joinTarget = { [parsed.alias]: descriptor.canonical.tableName };
557
+ const joinTarget = { [parsed.alias]: descriptor.canonical.tableName }
558
558
 
559
559
  if (rest.length === 0) {
560
- return [joinTarget];
560
+ return [joinTarget]
561
561
  }
562
562
 
563
- const [first, ...remaining] = rest;
563
+ const [first, ...remaining] = rest
564
564
 
565
565
  if (isFunction(first)) {
566
- const joinCallback = first;
566
+ const joinCallback = first
567
567
  const wrappedCallback = (joinClause) => {
568
- this.#addResourceConstraints(joinClause, parsed.alias, descriptor);
569
- const proxyClause = this.#createJoinProxy(joinClause);
570
- return joinCallback.call(proxyClause, proxyClause);
571
- };
572
- return [joinTarget, wrappedCallback, ...remaining];
568
+ this.#addResourceConstraints(joinClause, parsed.alias, descriptor)
569
+ const proxyClause = this.#createJoinProxy(joinClause)
570
+ return joinCallback.call(proxyClause, proxyClause)
571
+ }
572
+ return [joinTarget, wrappedCallback, ...remaining]
573
573
  }
574
574
 
575
575
  const translatedArgs = rest.map((value, index) => {
576
576
  if (index === 0 || index === 1) {
577
- return this.translateColumn(value);
577
+ return this.translateColumn(value)
578
578
  }
579
- return value;
580
- });
579
+ return value
580
+ })
581
581
 
582
- return [joinTarget, ...translatedArgs];
582
+ return [joinTarget, ...translatedArgs]
583
583
  }
584
584
 
585
- #createProxy(builder) {
586
- const adapter = this;
585
+ #createProxy (builder) {
586
+ const adapter = this
587
587
  const handler = {
588
- get(target, prop, receiver) {
588
+ get (target, prop, receiver) {
589
589
  if (prop === '__adapter__') {
590
- return adapter;
590
+ return adapter
591
591
  }
592
592
 
593
- const value = target[prop];
593
+ const value = target[prop]
594
594
  if (typeof value !== 'function') {
595
- return Reflect.get(target, prop, receiver);
595
+ return Reflect.get(target, prop, receiver)
596
596
  }
597
597
 
598
598
  if (JOIN_METHODS.has(prop)) {
599
599
  return (...args) => {
600
- const translatedArgs = adapter.#handleJoin(prop, args);
601
- const result = value.apply(target, translatedArgs);
602
- return result === target ? receiver : result;
603
- };
600
+ const translatedArgs = adapter.#handleJoin(prop, args)
601
+ const result = value.apply(target, translatedArgs)
602
+ return result === target ? receiver : result
603
+ }
604
604
  }
605
605
 
606
606
  return (...args) => {
607
- let translatedArgs = args;
607
+ let translatedArgs = args
608
608
 
609
609
  if (WHERE_METHODS.has(prop)) {
610
- translatedArgs = adapter.#translateWhereArgs(prop, args);
610
+ translatedArgs = adapter.#translateWhereArgs(prop, args)
611
611
  } else if (WHERE_IN_METHODS.has(prop)) {
612
- translatedArgs = adapter.#translateWhereInArgs(args);
612
+ translatedArgs = adapter.#translateWhereInArgs(args)
613
613
  } else if (BETWEEN_METHODS.has(prop)) {
614
- translatedArgs = adapter.#translateBetweenArgs(args);
614
+ translatedArgs = adapter.#translateBetweenArgs(args)
615
615
  } else if (NULL_METHODS.has(prop)) {
616
- translatedArgs = adapter.#translateNullArgs(args);
616
+ translatedArgs = adapter.#translateNullArgs(args)
617
617
  } else if (JOIN_ON_METHODS.has(prop)) {
618
- translatedArgs = adapter.#translateOnArgs(args);
618
+ translatedArgs = adapter.#translateOnArgs(args)
619
619
  } else if (JOIN_ON_IN_METHODS.has(prop)) {
620
- translatedArgs = adapter.#translateOnInArgs(args);
620
+ translatedArgs = adapter.#translateOnInArgs(args)
621
621
  } else if (JOIN_ON_BETWEEN_METHODS.has(prop)) {
622
- translatedArgs = adapter.#translateOnBetweenArgs(args);
622
+ translatedArgs = adapter.#translateOnBetweenArgs(args)
623
623
  } else if (JOIN_ON_NULL_METHODS.has(prop)) {
624
- translatedArgs = adapter.#translateOnNullArgs(args);
624
+ translatedArgs = adapter.#translateOnNullArgs(args)
625
625
  } else if (ORDER_METHODS.has(prop)) {
626
- translatedArgs = adapter.#translateOrderArgs(prop, args);
626
+ translatedArgs = adapter.#translateOrderArgs(prop, args)
627
627
  } else if (GROUP_METHODS.has(prop)) {
628
- translatedArgs = adapter.#translateGroupArgs(args);
628
+ translatedArgs = adapter.#translateGroupArgs(args)
629
629
  } else if (SELECT_METHODS.has(prop)) {
630
- translatedArgs = adapter.#translateSelectArgs(args);
630
+ translatedArgs = adapter.#translateSelectArgs(args)
631
631
  } else if (AGGREGATE_METHODS.has(prop)) {
632
- translatedArgs = adapter.#translateAggregateArgs(args);
632
+ translatedArgs = adapter.#translateAggregateArgs(args)
633
633
  } else if (CALLBACK_FIRST_METHODS.has(prop) && args.length && isFunction(args[0])) {
634
- translatedArgs = [maybeWrapCallback(adapter, args[0]), ...args.slice(1)];
634
+ translatedArgs = [maybeWrapCallback(adapter, args[0]), ...args.slice(1)]
635
635
  }
636
636
 
637
- const result = value.apply(target, translatedArgs);
638
- return result === target ? receiver : result;
639
- };
637
+ const result = value.apply(target, translatedArgs)
638
+ return result === target ? receiver : result
639
+ }
640
640
  },
641
- };
641
+ }
642
642
 
643
- const proxy = new Proxy(builder, handler);
644
- this.builders.set(proxy, builder);
645
- return proxy;
643
+ const proxy = new Proxy(builder, handler)
644
+ this.builders.set(proxy, builder)
645
+ return proxy
646
646
  }
647
647
  }