@opra/sqb 1.0.0-alpha.2 → 1.0.0-alpha.21
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.
- package/cjs/adapter-utils/parse-filter.js +5 -5
- package/cjs/augmentation/datatype-factory.augmentation.js +6 -2
- package/cjs/sqb-adapter.js +9 -4
- package/cjs/sqb-collection-service.js +71 -60
- package/cjs/sqb-entity-service.js +115 -101
- package/cjs/sqb-singleton-service.js +38 -31
- package/esm/adapter-utils/parse-filter.js +4 -4
- package/esm/augmentation/datatype-factory.augmentation.js +6 -2
- package/esm/sqb-adapter.js +9 -4
- package/esm/sqb-collection-service.js +71 -60
- package/esm/sqb-entity-service.js +115 -101
- package/esm/sqb-singleton-service.js +38 -31
- package/package.json +12 -8
- package/types/sqb-collection-service.d.ts +8 -8
- package/types/sqb-entity-service.d.ts +101 -87
- package/types/sqb-singleton-service.d.ts +5 -5
|
@@ -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 (!
|
|
69
|
+
if (!Object.prototype.hasOwnProperty.call(fieldSchema, 'exclusive'))
|
|
68
70
|
fieldSchema.exclusive = true;
|
|
69
71
|
}
|
|
70
|
-
if (!
|
|
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]);
|
package/cjs/sqb-adapter.js
CHANGED
|
@@ -14,7 +14,7 @@ var SQBAdapter;
|
|
|
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
|
|
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
|
|
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
|
};
|
|
@@ -48,7 +49,8 @@ var SQBAdapter;
|
|
|
48
49
|
return { method: 'findMany', options };
|
|
49
50
|
}
|
|
50
51
|
case 'Entity.Get': {
|
|
51
|
-
const
|
|
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
|
|
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
|
|
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.
|
|
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
|
|
61
|
+
const command = {
|
|
62
62
|
crud: 'read',
|
|
63
63
|
method: 'count',
|
|
64
64
|
byId: false,
|
|
65
65
|
options,
|
|
66
66
|
};
|
|
67
|
-
return this.
|
|
68
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
69
|
-
|
|
70
|
-
|
|
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
|
|
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.
|
|
88
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
89
|
-
|
|
90
|
-
|
|
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
|
|
101
|
+
const command = {
|
|
100
102
|
crud: 'delete',
|
|
101
103
|
method: 'deleteMany',
|
|
102
104
|
byId: false,
|
|
103
105
|
options,
|
|
104
106
|
};
|
|
105
|
-
return this.
|
|
106
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
107
|
-
|
|
108
|
-
|
|
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
|
|
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.
|
|
126
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
127
|
-
|
|
128
|
-
|
|
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
|
|
141
|
+
const command = {
|
|
138
142
|
crud: 'read',
|
|
139
143
|
method: 'existsOne',
|
|
140
144
|
byId: false,
|
|
141
145
|
options,
|
|
142
146
|
};
|
|
143
|
-
return this.
|
|
144
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
145
|
-
|
|
146
|
-
|
|
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.
|
|
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
|
|
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.
|
|
164
|
-
const documentFilter = await this._getCommonFilter(
|
|
165
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([documentFilter, options?.filter]);
|
|
166
|
-
|
|
167
|
-
|
|
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
|
|
182
|
+
const command = {
|
|
177
183
|
crud: 'read',
|
|
178
184
|
method: 'findOne',
|
|
179
185
|
byId: false,
|
|
180
186
|
options,
|
|
181
187
|
};
|
|
182
|
-
return this.
|
|
183
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
184
|
-
|
|
185
|
-
|
|
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,17 @@ 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
|
|
201
|
+
const command = {
|
|
195
202
|
crud: 'read',
|
|
196
203
|
method: 'findMany',
|
|
197
204
|
byId: false,
|
|
198
205
|
options,
|
|
199
206
|
};
|
|
200
|
-
return this.
|
|
201
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
202
|
-
|
|
203
|
-
|
|
207
|
+
return this._executeCommand(command, async () => {
|
|
208
|
+
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
209
|
+
command.options = { ...command.options, filter };
|
|
210
|
+
return this._findMany(command);
|
|
211
|
+
});
|
|
204
212
|
}
|
|
205
213
|
/**
|
|
206
214
|
* Finds multiple records in the collection and returns both records (max limit)
|
|
@@ -216,7 +224,7 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
|
|
|
216
224
|
/**
|
|
217
225
|
* Retrieves a records from the collection by its ID. Throws error if not found.
|
|
218
226
|
*
|
|
219
|
-
* @param {SQBAdapter.
|
|
227
|
+
* @param {SQBAdapter.IdOrIds} id - The ID of the document to retrieve.
|
|
220
228
|
* @param {SqbCollectionService.FindOneOptions} [options] - Optional options for the findOne operation.
|
|
221
229
|
* @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
|
|
222
230
|
* or rejects with a ResourceNotFoundError if the document does not exist.
|
|
@@ -238,7 +246,7 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
|
|
|
238
246
|
* undefined if the document was not found.
|
|
239
247
|
*/
|
|
240
248
|
async update(id, input, options) {
|
|
241
|
-
const
|
|
249
|
+
const command = {
|
|
242
250
|
crud: 'update',
|
|
243
251
|
method: 'update',
|
|
244
252
|
documentId: id,
|
|
@@ -246,10 +254,11 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
|
|
|
246
254
|
input,
|
|
247
255
|
options,
|
|
248
256
|
};
|
|
249
|
-
return this.
|
|
250
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
251
|
-
|
|
252
|
-
|
|
257
|
+
return this._executeCommand(command, async () => {
|
|
258
|
+
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
259
|
+
command.options = { ...command.options, filter };
|
|
260
|
+
return this._update(command);
|
|
261
|
+
});
|
|
253
262
|
}
|
|
254
263
|
/**
|
|
255
264
|
* Updates a record in the collection with the specified ID and returns updated record count
|
|
@@ -260,7 +269,7 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
|
|
|
260
269
|
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
261
270
|
*/
|
|
262
271
|
async updateOnly(id, input, options) {
|
|
263
|
-
const
|
|
272
|
+
const command = {
|
|
264
273
|
crud: 'update',
|
|
265
274
|
method: 'update',
|
|
266
275
|
documentId: id,
|
|
@@ -268,10 +277,11 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
|
|
|
268
277
|
input,
|
|
269
278
|
options,
|
|
270
279
|
};
|
|
271
|
-
return this.
|
|
272
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
273
|
-
|
|
274
|
-
|
|
280
|
+
return this._executeCommand(command, async () => {
|
|
281
|
+
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
282
|
+
command.options = { ...command.options, filter };
|
|
283
|
+
return this._updateOnly(command);
|
|
284
|
+
});
|
|
275
285
|
}
|
|
276
286
|
/**
|
|
277
287
|
* Updates multiple records in the collection based on the specified input and options.
|
|
@@ -281,17 +291,18 @@ class SqbCollectionService extends sqb_entity_service_js_1.SqbEntityService {
|
|
|
281
291
|
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
282
292
|
*/
|
|
283
293
|
async updateMany(input, options) {
|
|
284
|
-
const
|
|
294
|
+
const command = {
|
|
285
295
|
crud: 'update',
|
|
286
296
|
method: 'updateMany',
|
|
287
297
|
byId: false,
|
|
288
298
|
input,
|
|
289
299
|
options,
|
|
290
300
|
};
|
|
291
|
-
return this.
|
|
292
|
-
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(
|
|
293
|
-
|
|
294
|
-
|
|
301
|
+
return this._executeCommand(command, async () => {
|
|
302
|
+
const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([await this._getCommonFilter(command), command.options?.filter]);
|
|
303
|
+
command.options = { ...command.options, filter };
|
|
304
|
+
return this._updateMany(command);
|
|
305
|
+
});
|
|
295
306
|
}
|
|
296
307
|
}
|
|
297
308
|
exports.SqbCollectionService = SqbCollectionService;
|