@opra/sqb 1.0.0-alpha.9 → 1.0.0-beta.2

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
  }
@@ -9,7 +9,7 @@ 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)
@@ -41,10 +41,10 @@ var SQBAdapter;
41
41
  const options = {
42
42
  count: context.queryParams.count,
43
43
  filter: SQBAdapter.parseFilter(context.queryParams.filter),
44
- limit: context.queryParams.limit,
44
+ projection: context.queryParams.projection || operation.compositionOptions.defaultProjection,
45
+ limit: context.queryParams.limit || operation.compositionOptions.defaultLimit,
45
46
  offset: context.queryParams.skip,
46
- projection: context.queryParams.projection,
47
- sort: context.queryParams.sort,
47
+ sort: context.queryParams.sort || operation.compositionOptions.defaultSort,
48
48
  };
49
49
  return { method: 'findMany', options };
50
50
  }
@@ -33,23 +33,33 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
33
33
  if (!(await this.exists(id, options)))
34
34
  throw new common_1.ResourceNotAvailableError(this.getResourceName(), id);
35
35
  }
36
+ async create(input, options) {
37
+ const command = {
38
+ crud: 'create',
39
+ method: 'create',
40
+ byId: false,
41
+ input,
42
+ options,
43
+ };
44
+ return this._executeCommand(command, () => this._create(command));
45
+ }
36
46
  /**
37
47
  * Creates a new resource
38
48
  *
39
49
  * @param {PartialDTO<T>} input - The input data
40
50
  * @param {SqbCollectionService.CreateOptions} [options] - The options object
41
- * @returns {Promise<PartialDTO<T>>} A promise that resolves to the created resource
51
+ * @returns {Promise<void>} A promise that resolves create operation result
42
52
  * @throws {Error} if an unknown error occurs while creating the resource
43
53
  */
44
- async create(input, options) {
45
- const info = {
54
+ async createOnly(input, options) {
55
+ const command = {
46
56
  crud: 'create',
47
- method: 'create',
57
+ method: 'createOnly',
48
58
  byId: false,
49
59
  input,
50
60
  options,
51
61
  };
52
- return this._intercept(() => this._create(input, options), info);
62
+ return this._executeCommand(command, () => this._createOnly(command));
53
63
  }
54
64
  /**
55
65
  * Returns the count of records based on the provided options
@@ -58,16 +68,17 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
58
68
  * @return {Promise<number>} - A promise that resolves to the count of records
59
69
  */
60
70
  async count(options) {
61
- const info = {
71
+ const command = {
62
72
  crud: 'read',
63
73
  method: 'count',
64
74
  byId: false,
65
75
  options,
66
76
  };
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);
77
+ return this._executeCommand(command, async () => {
78
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
79
+ command.options = { ...command.options, filter };
80
+ return this._count(command);
81
+ });
71
82
  }
72
83
  /**
73
84
  * Deletes a record from the collection.
@@ -77,17 +88,18 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
77
88
  * @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
78
89
  */
79
90
  async delete(id, options) {
80
- const info = {
91
+ const command = {
81
92
  crud: 'delete',
82
93
  method: 'delete',
83
94
  byId: true,
84
95
  documentId: id,
85
96
  options,
86
97
  };
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);
98
+ return this._executeCommand(command, async () => {
99
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
100
+ command.options = { ...command.options, filter };
101
+ return this._delete(command);
102
+ });
91
103
  }
92
104
  /**
93
105
  * Deletes multiple documents from the collection that meet the specified filter criteria.
@@ -96,16 +108,17 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
96
108
  * @return {Promise<number>} - A promise that resolves to the number of documents deleted.
97
109
  */
98
110
  async deleteMany(options) {
99
- const info = {
111
+ const command = {
100
112
  crud: 'delete',
101
113
  method: 'deleteMany',
102
114
  byId: false,
103
115
  options,
104
116
  };
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);
117
+ return this._executeCommand(command, async () => {
118
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
119
+ command.options = { ...command.options, filter };
120
+ return this._deleteMany(command);
121
+ });
109
122
  }
110
123
  /**
111
124
  * Checks if a record with the given id exists.
@@ -115,17 +128,18 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
115
128
  * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
116
129
  */
117
130
  async exists(id, options) {
118
- const info = {
131
+ const command = {
119
132
  crud: 'read',
120
133
  method: 'exists',
121
134
  byId: true,
122
135
  documentId: id,
123
136
  options,
124
137
  };
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);
138
+ return this._executeCommand(command, async () => {
139
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
140
+ command.options = { ...command.options, filter };
141
+ return this._exists(command);
142
+ });
129
143
  }
130
144
  /**
131
145
  * Checks if a record with the given arguments exists.
@@ -134,37 +148,39 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
134
148
  * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
135
149
  */
136
150
  async existsOne(options) {
137
- const info = {
151
+ const command = {
138
152
  crud: 'read',
139
153
  method: 'existsOne',
140
154
  byId: false,
141
155
  options,
142
156
  };
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);
157
+ return this._executeCommand(command, async () => {
158
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
159
+ command.options = { ...command.options, filter };
160
+ return this._existsOne(command);
161
+ });
147
162
  }
148
163
  /**
149
164
  * Finds a record by ID.
150
165
  *
151
- * @param {SQBAdapter.Id} id - The ID of the record.
166
+ * @param {SQBAdapter.IdOrIds} id - The ID of the record.
152
167
  * @param {SqbCollectionService.FindOneOptions} [options] - The options for the find query.
153
168
  * @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
154
169
  */
155
170
  async findById(id, options) {
156
- const info = {
171
+ const command = {
157
172
  crud: 'read',
158
173
  method: 'findById',
159
174
  byId: true,
160
175
  documentId: id,
161
176
  options,
162
177
  };
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);
178
+ return this._executeCommand(command, async () => {
179
+ const documentFilter = await this._getCommonFilter(command);
180
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([documentFilter, command.options?.filter]);
181
+ command.options = { ...command.options, filter };
182
+ return this._findById(command);
183
+ });
168
184
  }
169
185
  /**
170
186
  * Finds a record in the collection that matches the specified options.
@@ -173,16 +189,17 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
173
189
  * @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
174
190
  */
175
191
  async findOne(options) {
176
- const info = {
192
+ const command = {
177
193
  crud: 'read',
178
194
  method: 'findOne',
179
195
  byId: false,
180
196
  options,
181
197
  };
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);
198
+ return this._executeCommand(command, async () => {
199
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
200
+ command.options = { ...command.options, filter };
201
+ return this._findOne(command);
202
+ });
186
203
  }
187
204
  /**
188
205
  * Finds multiple records in collection.
@@ -191,16 +208,18 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
191
208
  * @return A Promise that resolves to an array of partial outputs of type T.
192
209
  */
193
210
  async findMany(options) {
194
- const info = {
211
+ const command = {
195
212
  crud: 'read',
196
213
  method: 'findMany',
197
214
  byId: false,
198
215
  options,
199
216
  };
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);
217
+ return this._executeCommand(command, async () => {
218
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
219
+ const limit = command.options?.limit || this.defaultLimit;
220
+ command.options = { ...command.options, filter, limit };
221
+ return this._findMany(command);
222
+ });
204
223
  }
205
224
  /**
206
225
  * Finds multiple records in the collection and returns both records (max limit)
@@ -216,7 +235,7 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
216
235
  /**
217
236
  * Retrieves a records from the collection by its ID. Throws error if not found.
218
237
  *
219
- * @param {SQBAdapter.Id} id - The ID of the document to retrieve.
238
+ * @param {SQBAdapter.IdOrIds} id - The ID of the document to retrieve.
220
239
  * @param {SqbCollectionService.FindOneOptions} [options] - Optional options for the findOne operation.
221
240
  * @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
222
241
  * or rejects with a ResourceNotFoundError if the document does not exist.
@@ -238,7 +257,7 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
238
257
  * undefined if the document was not found.
239
258
  */
240
259
  async update(id, input, options) {
241
- const info = {
260
+ const command = {
242
261
  crud: 'update',
243
262
  method: 'update',
244
263
  documentId: id,
@@ -246,10 +265,11 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
246
265
  input,
247
266
  options,
248
267
  };
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);
268
+ return this._executeCommand(command, async () => {
269
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
270
+ command.options = { ...command.options, filter };
271
+ return this._update(command);
272
+ });
253
273
  }
254
274
  /**
255
275
  * Updates a record in the collection with the specified ID and returns updated record count
@@ -260,7 +280,7 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
260
280
  * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
261
281
  */
262
282
  async updateOnly(id, input, options) {
263
- const info = {
283
+ const command = {
264
284
  crud: 'update',
265
285
  method: 'update',
266
286
  documentId: id,
@@ -268,10 +288,11 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
268
288
  input,
269
289
  options,
270
290
  };
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);
291
+ return this._executeCommand(command, async () => {
292
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
293
+ command.options = { ...command.options, filter };
294
+ return this._updateOnly(command);
295
+ });
275
296
  }
276
297
  /**
277
298
  * Updates multiple records in the collection based on the specified input and options.
@@ -281,17 +302,18 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
281
302
  * @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
282
303
  */
283
304
  async updateMany(input, options) {
284
- const info = {
305
+ const command = {
285
306
  crud: 'update',
286
307
  method: 'updateMany',
287
308
  byId: false,
288
309
  input,
289
310
  options,
290
311
  };
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);
312
+ return this._executeCommand(command, async () => {
313
+ const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
314
+ command.options = { ...command.options, filter };
315
+ return this._updateMany(command);
316
+ });
295
317
  }
296
318
  }
297
319
  exports.SqbCollectionService = SqbCollectionService;