@opra/sqb 1.0.0-alpha.16 → 1.0.0-alpha.18

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