@opra/sqb 1.0.0-alpha.3 → 1.0.0-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = parseFilter;
3
4
  const tslib_1 = require("tslib");
4
5
  require("@opra/core");
5
6
  const common_1 = require("@opra/common");
@@ -31,7 +32,6 @@ function parseFilter(filters) {
31
32
  }
32
33
  return arr.length > 1 ? sqb.And(...arr) : arr[0];
33
34
  }
34
- exports.default = parseFilter;
35
35
  function prepareFilterAst(ast) {
36
36
  if (!ast)
37
37
  return;
@@ -79,13 +79,13 @@ function prepareFilterAst(ast) {
79
79
  case '!in':
80
80
  return sqb.Nin(left, right);
81
81
  case 'like':
82
- return sqb.Like(left, String(right).replace(/\*/, '%'));
82
+ return sqb.Like(left, String(right).replace(/\*/g, '%'));
83
83
  case 'ilike':
84
- return sqb.Ilike(left, String(right).replace(/\*/, '%'));
84
+ return sqb.Ilike(left, String(right).replace(/\*/g, '%'));
85
85
  case '!like':
86
- return sqb.NotLike(left, String(right).replace(/\*/, '%'));
86
+ return sqb.NotLike(left, String(right).replace(/\*/g, '%'));
87
87
  case '!ilike':
88
- return sqb.NotILike(left, String(right).replace(/\*/, '%'));
88
+ return sqb.NotILike(left, String(right).replace(/\*/g, '%'));
89
89
  default:
90
90
  throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
91
91
  }
@@ -59,16 +59,20 @@ DataTypeFactory._prepareComplexTypeArgs = async function (context, owner, initAr
59
59
  if (hasNoType || fieldSchema.type === String)
60
60
  fieldSchema.type = 'time';
61
61
  break;
62
+ default:
63
+ break;
62
64
  }
63
65
  }
64
66
  if ((0, connect_1.isAssociationField)(sqbField)) {
65
67
  if (sqbField.association.returnsMany())
66
68
  fieldSchema.isArray = true;
67
- if (!fieldSchema.hasOwnProperty('exclusive'))
69
+ if (!Object.prototype.hasOwnProperty.call(fieldSchema, 'exclusive'))
68
70
  fieldSchema.exclusive = true;
69
71
  }
70
- if (!fieldSchema.hasOwnProperty('exclusive') && sqbField.hasOwnProperty('exclusive'))
72
+ if (!Object.prototype.hasOwnProperty.call(fieldSchema, 'exclusive') &&
73
+ Object.prototype.hasOwnProperty.call(sqbField, 'exclusive')) {
71
74
  fieldSchema.exclusive = sqbField.exclusive;
75
+ }
72
76
  }
73
77
  }
74
78
  return _prepareComplexTypeArgs.apply(DataTypeFactory, [context, owner, initArgs, metadata]);
@@ -9,12 +9,12 @@ var SQBAdapter;
9
9
  SQBAdapter.parseFilter = parse_filter_js_1.default;
10
10
  async function parseRequest(context) {
11
11
  const { operation } = context;
12
- if (operation.composition?.startsWith('Entity.') && operation.compositionOptions?.type) {
12
+ if (operation?.composition?.startsWith('Entity.') && operation.compositionOptions?.type) {
13
13
  const dataType = context.document.node.getComplexType(operation.compositionOptions?.type);
14
14
  const entityMetadata = connect_1.EntityMetadata.get(dataType.ctor);
15
15
  if (!entityMetadata)
16
16
  throw new Error(`Type class "${dataType.ctor}" is not an SQB entity`);
17
- const { compositionOptions } = operation;
17
+ const controller = operation.owner;
18
18
  switch (operation.composition) {
19
19
  case 'Entity.Create': {
20
20
  const data = await context.getBody();
@@ -24,7 +24,8 @@ var SQBAdapter;
24
24
  return { method: 'create', data, options };
25
25
  }
26
26
  case 'Entity.Delete': {
27
- const key = context.pathParams[compositionOptions.keyParameter];
27
+ const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
28
+ const key = keyParam && context.pathParams[String(keyParam.name)];
28
29
  const options = {
29
30
  filter: SQBAdapter.parseFilter(context.queryParams.filter),
30
31
  };
@@ -40,15 +41,16 @@ var SQBAdapter;
40
41
  const options = {
41
42
  count: context.queryParams.count,
42
43
  filter: SQBAdapter.parseFilter(context.queryParams.filter),
43
- limit: context.queryParams.limit,
44
+ projection: context.queryParams.projection || operation.compositionOptions.defaultProjection,
45
+ limit: context.queryParams.limit || operation.compositionOptions.defaultLimit,
44
46
  offset: context.queryParams.skip,
45
- projection: context.queryParams.projection,
46
- sort: context.queryParams.sort,
47
+ sort: context.queryParams.sort || operation.compositionOptions.defaultSort,
47
48
  };
48
49
  return { method: 'findMany', options };
49
50
  }
50
51
  case 'Entity.Get': {
51
- const key = context.pathParams[compositionOptions.keyParameter];
52
+ const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
53
+ const key = keyParam && context.pathParams[String(keyParam.name)];
52
54
  const options = {
53
55
  projection: context.queryParams.projection,
54
56
  filter: SQBAdapter.parseFilter(context.queryParams.filter),
@@ -57,7 +59,8 @@ var SQBAdapter;
57
59
  }
58
60
  case 'Entity.Update': {
59
61
  const data = await context.getBody();
60
- const key = context.pathParams[compositionOptions.keyParameter];
62
+ const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
63
+ const key = keyParam && context.pathParams[String(keyParam.name)];
61
64
  const options = {
62
65
  projection: context.queryParams.projection,
63
66
  filter: SQBAdapter.parseFilter(context.queryParams.filter),
@@ -71,6 +74,8 @@ var SQBAdapter;
71
74
  };
72
75
  return { method: 'updateMany', data, options };
73
76
  }
77
+ default:
78
+ break;
74
79
  }
75
80
  }
76
81
  throw new Error(`This operation is not compatible to SQB Adapter`);
@@ -42,14 +42,14 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
42
42
  * @throws {Error} if an unknown error occurs while creating the resource
43
43
  */
44
44
  async create(input, options) {
45
- const info = {
45
+ const command = {
46
46
  crud: 'create',
47
47
  method: 'create',
48
48
  byId: false,
49
49
  input,
50
50
  options,
51
51
  };
52
- return this._intercept(() => this._create(input, options), info);
52
+ return this._executeCommand(command, () => this._create(command));
53
53
  }
54
54
  /**
55
55
  * Returns the count of records based on the provided options
@@ -58,16 +58,17 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
58
58
  * @return {Promise<number>} - A promise that resolves to the count of records
59
59
  */
60
60
  async count(options) {
61
- const info = {
61
+ const command = {
62
62
  crud: 'read',
63
63
  method: 'count',
64
64
  byId: false,
65
65
  options,
66
66
  };
67
- return this._intercept(async () => {
68
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
69
- return this._count({ ...options, filter });
70
- }, info);
67
+ return this._executeCommand(command, async () => {
68
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
69
+ command.options = { ...command.options, filter };
70
+ return this._count(command);
71
+ });
71
72
  }
72
73
  /**
73
74
  * Deletes a record from the collection.
@@ -77,17 +78,18 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
77
78
  * @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
78
79
  */
79
80
  async delete(id, options) {
80
- const info = {
81
+ const command = {
81
82
  crud: 'delete',
82
83
  method: 'delete',
83
84
  byId: true,
84
85
  documentId: id,
85
86
  options,
86
87
  };
87
- return this._intercept(async () => {
88
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
89
- return this._delete(id, { ...options, filter });
90
- }, info);
88
+ return this._executeCommand(command, async () => {
89
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
90
+ command.options = { ...command.options, filter };
91
+ return this._delete(command);
92
+ });
91
93
  }
92
94
  /**
93
95
  * Deletes multiple documents from the collection that meet the specified filter criteria.
@@ -96,16 +98,17 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
96
98
  * @return {Promise<number>} - A promise that resolves to the number of documents deleted.
97
99
  */
98
100
  async deleteMany(options) {
99
- const info = {
101
+ const command = {
100
102
  crud: 'delete',
101
103
  method: 'deleteMany',
102
104
  byId: false,
103
105
  options,
104
106
  };
105
- return this._intercept(async () => {
106
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
107
- return this._deleteMany({ ...options, filter });
108
- }, info);
107
+ return this._executeCommand(command, async () => {
108
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
109
+ command.options = { ...command.options, filter };
110
+ return this._deleteMany(command);
111
+ });
109
112
  }
110
113
  /**
111
114
  * Checks if a record with the given id exists.
@@ -115,17 +118,18 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
115
118
  * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
116
119
  */
117
120
  async exists(id, options) {
118
- const info = {
121
+ const command = {
119
122
  crud: 'read',
120
123
  method: 'exists',
121
124
  byId: true,
122
125
  documentId: id,
123
126
  options,
124
127
  };
125
- return this._intercept(async () => {
126
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
127
- return this._exists(id, { ...options, filter });
128
- }, info);
128
+ return this._executeCommand(command, async () => {
129
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
130
+ command.options = { ...command.options, filter };
131
+ return this._exists(command);
132
+ });
129
133
  }
130
134
  /**
131
135
  * Checks if a record with the given arguments exists.
@@ -134,37 +138,39 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
134
138
  * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
135
139
  */
136
140
  async existsOne(options) {
137
- const info = {
141
+ const command = {
138
142
  crud: 'read',
139
143
  method: 'existsOne',
140
144
  byId: false,
141
145
  options,
142
146
  };
143
- return this._intercept(async () => {
144
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
145
- return this._existsOne({ ...options, filter });
146
- }, info);
147
+ return this._executeCommand(command, async () => {
148
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
149
+ command.options = { ...command.options, filter };
150
+ return this._existsOne(command);
151
+ });
147
152
  }
148
153
  /**
149
154
  * Finds a record by ID.
150
155
  *
151
- * @param {SQBAdapter.Id} id - The ID of the record.
156
+ * @param {SQBAdapter.IdOrIds} id - The ID of the record.
152
157
  * @param {SqbCollectionService.FindOneOptions} [options] - The options for the find query.
153
158
  * @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
154
159
  */
155
160
  async findById(id, options) {
156
- const info = {
161
+ const command = {
157
162
  crud: 'read',
158
163
  method: 'findById',
159
164
  byId: true,
160
165
  documentId: id,
161
166
  options,
162
167
  };
163
- return this._intercept(async () => {
164
- const documentFilter = await this._getCommonFilter(info);
165
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([documentFilter, options?.filter]);
166
- return this._findById(id, { ...options, filter });
167
- }, info);
168
+ return this._executeCommand(command, async () => {
169
+ const documentFilter = await this._getCommonFilter(command);
170
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([documentFilter, command.options?.filter]);
171
+ command.options = { ...command.options, filter };
172
+ return this._findById(command);
173
+ });
168
174
  }
169
175
  /**
170
176
  * Finds a record in the collection that matches the specified options.
@@ -173,16 +179,17 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
173
179
  * @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
174
180
  */
175
181
  async findOne(options) {
176
- const info = {
182
+ const command = {
177
183
  crud: 'read',
178
184
  method: 'findOne',
179
185
  byId: false,
180
186
  options,
181
187
  };
182
- return this._intercept(async () => {
183
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
184
- return this._findOne({ ...options, filter });
185
- }, info);
188
+ return this._executeCommand(command, async () => {
189
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
190
+ command.options = { ...command.options, filter };
191
+ return this._findOne(command);
192
+ });
186
193
  }
187
194
  /**
188
195
  * Finds multiple records in collection.
@@ -191,16 +198,18 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
191
198
  * @return A Promise that resolves to an array of partial outputs of type T.
192
199
  */
193
200
  async findMany(options) {
194
- const info = {
201
+ const command = {
195
202
  crud: 'read',
196
203
  method: 'findMany',
197
204
  byId: false,
198
205
  options,
199
206
  };
200
- return this._intercept(async () => {
201
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
202
- return this._findMany({ ...options, filter });
203
- }, info);
207
+ return this._executeCommand(command, async () => {
208
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
209
+ const limit = command.options?.limit || this.defaultLimit;
210
+ command.options = { ...command.options, filter, limit };
211
+ return this._findMany(command);
212
+ });
204
213
  }
205
214
  /**
206
215
  * Finds multiple records in the collection and returns both records (max limit)
@@ -216,7 +225,7 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
216
225
  /**
217
226
  * Retrieves a records from the collection by its ID. Throws error if not found.
218
227
  *
219
- * @param {SQBAdapter.Id} id - The ID of the document to retrieve.
228
+ * @param {SQBAdapter.IdOrIds} id - The ID of the document to retrieve.
220
229
  * @param {SqbCollectionService.FindOneOptions} [options] - Optional options for the findOne operation.
221
230
  * @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
222
231
  * or rejects with a ResourceNotFoundError if the document does not exist.
@@ -238,7 +247,7 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
238
247
  * undefined if the document was not found.
239
248
  */
240
249
  async update(id, input, options) {
241
- const info = {
250
+ const command = {
242
251
  crud: 'update',
243
252
  method: 'update',
244
253
  documentId: id,
@@ -246,10 +255,11 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
246
255
  input,
247
256
  options,
248
257
  };
249
- return this._intercept(async () => {
250
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
251
- return this._update(id, input, { ...options, filter });
252
- }, info);
258
+ return this._executeCommand(command, async () => {
259
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
260
+ command.options = { ...command.options, filter };
261
+ return this._update(command);
262
+ });
253
263
  }
254
264
  /**
255
265
  * Updates a record in the collection with the specified ID and returns updated record count
@@ -260,7 +270,7 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
260
270
  * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
261
271
  */
262
272
  async updateOnly(id, input, options) {
263
- const info = {
273
+ const command = {
264
274
  crud: 'update',
265
275
  method: 'update',
266
276
  documentId: id,
@@ -268,10 +278,11 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
268
278
  input,
269
279
  options,
270
280
  };
271
- return this._intercept(async () => {
272
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
273
- return this._updateOnly(id, input, { ...options, filter });
274
- }, info);
281
+ return this._executeCommand(command, async () => {
282
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
283
+ command.options = { ...command.options, filter };
284
+ return this._updateOnly(command);
285
+ });
275
286
  }
276
287
  /**
277
288
  * Updates multiple records in the collection based on the specified input and options.
@@ -281,17 +292,18 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
281
292
  * @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
282
293
  */
283
294
  async updateMany(input, options) {
284
- const info = {
295
+ const command = {
285
296
  crud: 'update',
286
297
  method: 'updateMany',
287
298
  byId: false,
288
299
  input,
289
300
  options,
290
301
  };
291
- return this._intercept(async () => {
292
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(info), options?.filter]);
293
- return this._updateMany(input, { ...options, filter });
294
- }, info);
302
+ return this._executeCommand(command, async () => {
303
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
304
+ command.options = { ...command.options, filter };
305
+ return this._updateMany(command);
306
+ });
295
307
  }
296
308
  }
297
309
  exports.SqbCollectionService = SqbCollectionService;