@opra/sqb 0.15.0 → 0.16.0
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/augmentation/document-factory.augmentation.js +2 -0
- package/cjs/sqb-adapter.js +102 -199
- package/cjs/sqb-collection-resource.js +20 -20
- package/cjs/sqb-entity-service.js +86 -54
- package/cjs/sqb-singleton-resource.js +10 -10
- package/cjs/{convert-filter.js → transform-filter.js} +8 -12
- package/cjs/transform-key-values.js +14 -0
- package/esm/augmentation/document-factory.augmentation.js +32 -28
- package/esm/augmentation/mapped-type.augmentation.js +9 -7
- package/esm/augmentation/union-type.augmentation.js +7 -5
- package/esm/index.js +10 -7
- package/esm/sqb-adapter.js +110 -203
- package/esm/sqb-collection-resource.js +56 -52
- package/esm/sqb-entity-service.js +92 -56
- package/esm/sqb-singleton-resource.js +34 -30
- package/esm/transform-filter.js +68 -0
- package/esm/transform-key-values.js +14 -0
- package/package.json +8 -7
- package/types/sqb-adapter.d.ts +14 -0
- package/{esm → types}/sqb-collection-resource.d.ts +1 -1
- package/types/sqb-entity-service.d.ts +31 -0
- package/types/transform-filter.d.ts +2 -0
- package/types/transform-key-values.d.ts +3 -0
- package/esm/convert-filter.d.ts +0 -2
- package/esm/convert-filter.js +0 -67
- package/esm/sqb-adapter.d.ts +0 -64
- package/esm/sqb-entity-service.d.ts +0 -21
- /package/{esm → types}/augmentation/document-factory.augmentation.d.ts +0 -0
- /package/{esm → types}/augmentation/mapped-type.augmentation.d.ts +0 -0
- /package/{esm → types}/augmentation/union-type.augmentation.d.ts +0 -0
- /package/{esm → types}/index.d.ts +0 -0
- /package/{esm → types}/sqb-singleton-resource.d.ts +0 -0
|
@@ -24,6 +24,8 @@ common_1.DocumentFactory.prototype.extractFieldSchema = async function (target,
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
else if (sqbField.kind === 'column') {
|
|
27
|
+
if (typeof sqbField.enum === 'object')
|
|
28
|
+
metadata.enum = sqbField.enum;
|
|
27
29
|
if (sqbField.type && Reflect.hasMetadata(common_1.METADATA_KEY, sqbField.type)) {
|
|
28
30
|
target.type = sqbField.type;
|
|
29
31
|
}
|
package/cjs/sqb-adapter.js
CHANGED
|
@@ -4,209 +4,112 @@ exports.SQBAdapter = void 0;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const lodash_isnil_1 = tslib_1.__importDefault(require("lodash.isnil"));
|
|
6
6
|
const lodash_omitby_1 = tslib_1.__importDefault(require("lodash.omitby"));
|
|
7
|
-
const
|
|
7
|
+
const common_1 = require("@opra/common");
|
|
8
|
+
const connect_1 = require("@sqb/connect");
|
|
9
|
+
const transform_filter_js_1 = tslib_1.__importDefault(require("./transform-filter.js"));
|
|
10
|
+
const transform_key_values_js_1 = tslib_1.__importDefault(require("./transform-key-values.js"));
|
|
8
11
|
var SQBAdapter;
|
|
9
12
|
(function (SQBAdapter) {
|
|
10
|
-
SQBAdapter.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
SQBAdapter.parseCollectionCreateRequest = parseCollectionCreateRequest;
|
|
27
|
-
function parseCollectionDeleteRequest(request) {
|
|
28
|
-
/* istanbul ignore next */
|
|
29
|
-
if (!(request.resourceKind === 'Collection' && request.operation === 'delete'))
|
|
30
|
-
throw new TypeError('"Request" is not a "CollectionDeleteRequest"');
|
|
31
|
-
const { args } = request;
|
|
32
|
-
return {
|
|
33
|
-
method: 'destroy',
|
|
34
|
-
key: args.key,
|
|
35
|
-
options: {}
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
SQBAdapter.parseCollectionDeleteRequest = parseCollectionDeleteRequest;
|
|
39
|
-
function parseCollectionDeleteManyRequest(request) {
|
|
40
|
-
/* istanbul ignore next */
|
|
41
|
-
if (!(request.resourceKind === 'Collection' && request.operation === 'deleteMany'))
|
|
42
|
-
throw new TypeError('"Request" is not a "CollectionDeleteManyRequest"');
|
|
43
|
-
const { args } = request;
|
|
44
|
-
return {
|
|
45
|
-
method: 'destroyAll',
|
|
46
|
-
options: (0, lodash_omitby_1.default)({
|
|
47
|
-
filter: SQBAdapter.parseFilter(args.filter)
|
|
48
|
-
}, lodash_isnil_1.default)
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
SQBAdapter.parseCollectionDeleteManyRequest = parseCollectionDeleteManyRequest;
|
|
52
|
-
function parseCollectionGetRequest(request) {
|
|
53
|
-
/* istanbul ignore next */
|
|
54
|
-
if (!(request.resourceKind === 'Collection' && request.operation === 'get'))
|
|
55
|
-
throw new TypeError('"Request" is not a "CollectionGetRequest"');
|
|
56
|
-
const { args } = request;
|
|
57
|
-
return {
|
|
58
|
-
method: 'findByPk',
|
|
59
|
-
key: args.key,
|
|
60
|
-
options: (0, lodash_omitby_1.default)({
|
|
61
|
-
pick: args.pick?.length ? args.pick : undefined,
|
|
62
|
-
omit: args.omit?.length ? args.omit : undefined,
|
|
63
|
-
include: args.include?.length ? args.include : undefined,
|
|
64
|
-
}, lodash_isnil_1.default)
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
SQBAdapter.parseCollectionGetRequest = parseCollectionGetRequest;
|
|
68
|
-
function parseCollectionUpdateRequest(request) {
|
|
69
|
-
/* istanbul ignore next */
|
|
70
|
-
if (!(request.resourceKind === 'Collection' && request.operation === 'update'))
|
|
71
|
-
throw new TypeError('"Request" is not a "CollectionUpdateRequest"');
|
|
72
|
-
const { args } = request;
|
|
73
|
-
return {
|
|
74
|
-
method: 'update',
|
|
75
|
-
key: args.key,
|
|
76
|
-
data: args.data,
|
|
77
|
-
options: (0, lodash_omitby_1.default)({
|
|
78
|
-
pick: args.pick?.length ? args.pick : undefined,
|
|
79
|
-
omit: args.omit?.length ? args.omit : undefined,
|
|
80
|
-
include: args.include?.length ? args.include : undefined,
|
|
81
|
-
}, lodash_isnil_1.default)
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
SQBAdapter.parseCollectionUpdateRequest = parseCollectionUpdateRequest;
|
|
85
|
-
function parseCollectionUpdateManyRequest(request) {
|
|
86
|
-
/* istanbul ignore next */
|
|
87
|
-
if (!(request.resourceKind === 'Collection' && request.operation === 'updateMany'))
|
|
88
|
-
throw new TypeError('"Request" is not a "CollectionUpdateManyRequest"');
|
|
89
|
-
const { args } = request;
|
|
90
|
-
return {
|
|
91
|
-
method: 'updateAll',
|
|
92
|
-
data: args.data,
|
|
93
|
-
options: (0, lodash_omitby_1.default)({
|
|
94
|
-
filter: SQBAdapter.parseFilter(args.filter)
|
|
95
|
-
}, lodash_isnil_1.default)
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
SQBAdapter.parseCollectionUpdateManyRequest = parseCollectionUpdateManyRequest;
|
|
99
|
-
function parseCollectionSearchRequest(request) {
|
|
100
|
-
/* istanbul ignore next */
|
|
101
|
-
if (!(request.resourceKind === 'Collection' && request.operation === 'search'))
|
|
102
|
-
throw new TypeError('"Request" is not a "CollectionSearchRequest"');
|
|
103
|
-
const { args } = request;
|
|
104
|
-
return {
|
|
105
|
-
method: 'findAll',
|
|
106
|
-
options: (0, lodash_omitby_1.default)({
|
|
107
|
-
pick: args.pick?.length ? args.pick : undefined,
|
|
108
|
-
omit: args.omit?.length ? args.omit : undefined,
|
|
109
|
-
include: args.include?.length ? args.include : undefined,
|
|
110
|
-
sort: args.sort?.length ? args.sort : undefined,
|
|
111
|
-
limit: args.limit,
|
|
112
|
-
offset: args.skip,
|
|
113
|
-
distinct: args.distinct,
|
|
114
|
-
count: args.count,
|
|
115
|
-
filter: SQBAdapter.parseFilter(args.filter)
|
|
116
|
-
}, lodash_isnil_1.default)
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
SQBAdapter.parseCollectionSearchRequest = parseCollectionSearchRequest;
|
|
120
|
-
function parseSingletonGetRequest(request) {
|
|
121
|
-
/* istanbul ignore next */
|
|
122
|
-
if (!(request.resourceKind === 'Singleton' && request.operation === 'get'))
|
|
123
|
-
throw new TypeError('"Request" is not a "SingletonGetRequest"');
|
|
124
|
-
const { args } = request;
|
|
125
|
-
return {
|
|
126
|
-
method: 'findOne',
|
|
127
|
-
options: (0, lodash_omitby_1.default)({
|
|
128
|
-
pick: args.pick?.length ? args.pick : undefined,
|
|
129
|
-
omit: args.omit?.length ? args.omit : undefined,
|
|
130
|
-
include: args.include?.length ? args.include : undefined,
|
|
131
|
-
}, lodash_isnil_1.default)
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
SQBAdapter.parseSingletonGetRequest = parseSingletonGetRequest;
|
|
135
|
-
function parseSingletonCreateRequest(request) {
|
|
136
|
-
/* istanbul ignore next */
|
|
137
|
-
if (!(request.resourceKind === 'Singleton' && request.operation === 'create'))
|
|
138
|
-
throw new TypeError('"Request" is not a "SingletonCreateRequest"');
|
|
139
|
-
const { args } = request;
|
|
140
|
-
return {
|
|
141
|
-
method: 'create',
|
|
142
|
-
data: args.data,
|
|
143
|
-
options: (0, lodash_omitby_1.default)({
|
|
144
|
-
pick: args.pick?.length ? args.pick : undefined,
|
|
145
|
-
omit: args.omit?.length ? args.omit : undefined,
|
|
146
|
-
include: args.include?.length ? args.include : undefined,
|
|
147
|
-
}, lodash_isnil_1.default)
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
SQBAdapter.parseSingletonCreateRequest = parseSingletonCreateRequest;
|
|
151
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
152
|
-
function parseSingletonDeleteRequest(request) {
|
|
153
|
-
/* istanbul ignore next */
|
|
154
|
-
if (!(request.resourceKind === 'Singleton' && request.operation === 'delete'))
|
|
155
|
-
throw new TypeError('"Request" is not a "SingletonDeleteRequest"');
|
|
156
|
-
return {
|
|
157
|
-
method: 'destroyAll',
|
|
158
|
-
options: {},
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
SQBAdapter.parseSingletonDeleteRequest = parseSingletonDeleteRequest;
|
|
162
|
-
function parseSingletonUpdateRequest(request) {
|
|
163
|
-
/* istanbul ignore next */
|
|
164
|
-
if (!(request.resourceKind === 'Singleton' && request.operation === 'update'))
|
|
165
|
-
throw new TypeError('"Request" is not a "SingletonUpdateRequest"');
|
|
166
|
-
const { args } = request;
|
|
167
|
-
return {
|
|
168
|
-
method: 'updateAll',
|
|
169
|
-
data: args.data,
|
|
170
|
-
options: (0, lodash_omitby_1.default)({
|
|
171
|
-
pick: args.pick?.length ? args.pick : undefined,
|
|
172
|
-
omit: args.omit?.length ? args.omit : undefined,
|
|
173
|
-
include: args.include?.length ? args.include : undefined,
|
|
174
|
-
}, lodash_isnil_1.default)
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
SQBAdapter.parseSingletonUpdateRequest = parseSingletonUpdateRequest;
|
|
178
|
-
function parseRequest(request) {
|
|
179
|
-
if (request.resourceKind === 'Collection') {
|
|
180
|
-
switch (request.operation) {
|
|
181
|
-
case 'create':
|
|
182
|
-
return parseCollectionCreateRequest(request);
|
|
183
|
-
case 'get':
|
|
184
|
-
return parseCollectionGetRequest(request);
|
|
185
|
-
case 'delete':
|
|
186
|
-
return parseCollectionDeleteRequest(request);
|
|
187
|
-
case 'deleteMany':
|
|
188
|
-
return parseCollectionDeleteManyRequest(request);
|
|
189
|
-
case 'update':
|
|
190
|
-
return parseCollectionUpdateRequest(request);
|
|
191
|
-
case 'updateMany':
|
|
192
|
-
return parseCollectionUpdateManyRequest(request);
|
|
193
|
-
case 'search':
|
|
194
|
-
return parseCollectionSearchRequest(request);
|
|
13
|
+
SQBAdapter.transformFilter = transform_filter_js_1.default;
|
|
14
|
+
SQBAdapter.transformKeyValues = transform_key_values_js_1.default;
|
|
15
|
+
function transformRequest(request) {
|
|
16
|
+
const { resource } = request;
|
|
17
|
+
if (resource instanceof common_1.Collection || resource instanceof common_1.Singleton) {
|
|
18
|
+
const { args, operation } = request;
|
|
19
|
+
let options = {};
|
|
20
|
+
const entityMetadata = connect_1.EntityMetadata.get(resource.type.ctor);
|
|
21
|
+
if (!entityMetadata)
|
|
22
|
+
throw new Error(`Type class "${resource.type.ctor}" is not an SQB entity`);
|
|
23
|
+
if (resource instanceof common_1.Collection) {
|
|
24
|
+
const primaryIndex = entityMetadata.indexes.find(x => x.primary);
|
|
25
|
+
// Check if resource primary keys are same with entity
|
|
26
|
+
const primaryKeys = [...(primaryIndex && primaryIndex.columns) || []];
|
|
27
|
+
if (primaryKeys.sort().join() !== [...resource.primaryKey].sort().join())
|
|
28
|
+
throw new Error('Resource primaryKey definition differs from SQB Entity primaryKey definition');
|
|
195
29
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
30
|
+
if (operation === 'create' || operation === 'update' ||
|
|
31
|
+
operation === 'get' || operation === 'findMany') {
|
|
32
|
+
options.pick = args.pick?.length ? args.pick : undefined;
|
|
33
|
+
options.omit = args.omit?.length ? args.omit : undefined;
|
|
34
|
+
options.include = args.include?.length ? args.include : undefined;
|
|
35
|
+
}
|
|
36
|
+
if (args.filter) {
|
|
37
|
+
options.filter = SQBAdapter.transformFilter(args.filter);
|
|
38
|
+
}
|
|
39
|
+
if (operation === 'findMany') {
|
|
40
|
+
options.sort = args.sort?.length ? args.sort : undefined;
|
|
41
|
+
options.limit = args.limit;
|
|
42
|
+
options.offset = args.skip;
|
|
43
|
+
options.distinct = args.distinct;
|
|
44
|
+
options.count = args.count;
|
|
45
|
+
}
|
|
46
|
+
options = (0, lodash_omitby_1.default)(options, lodash_isnil_1.default);
|
|
47
|
+
if (operation === 'create') {
|
|
48
|
+
return {
|
|
49
|
+
method: 'create',
|
|
50
|
+
data: args.data,
|
|
51
|
+
options,
|
|
52
|
+
args: [args.data, options]
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (operation === 'deleteMany' || (operation === 'delete' && resource instanceof common_1.Singleton)) {
|
|
56
|
+
return {
|
|
57
|
+
method: 'deleteMany',
|
|
58
|
+
options,
|
|
59
|
+
args: [options]
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (operation === 'delete') {
|
|
63
|
+
return {
|
|
64
|
+
method: 'delete',
|
|
65
|
+
key: args.key,
|
|
66
|
+
options,
|
|
67
|
+
args: [args.key, options]
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
if (operation === 'get') {
|
|
71
|
+
if (resource instanceof common_1.Singleton)
|
|
72
|
+
return {
|
|
73
|
+
method: 'findOne',
|
|
74
|
+
options,
|
|
75
|
+
args: [options]
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
method: 'find',
|
|
79
|
+
key: args.key,
|
|
80
|
+
options,
|
|
81
|
+
args: [args.key, options]
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
if (operation === 'findMany') {
|
|
85
|
+
const out = {
|
|
86
|
+
method: 'findMany',
|
|
87
|
+
options,
|
|
88
|
+
args: [options]
|
|
89
|
+
};
|
|
90
|
+
if (args.count)
|
|
91
|
+
out.count = args.count;
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
if (operation === 'updateMany' || (operation === 'update' && resource instanceof common_1.Singleton)) {
|
|
95
|
+
return {
|
|
96
|
+
method: 'updateMany',
|
|
97
|
+
data: args.data,
|
|
98
|
+
options,
|
|
99
|
+
args: [args.data, options]
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
if (operation === 'update') {
|
|
103
|
+
return {
|
|
104
|
+
method: 'update',
|
|
105
|
+
key: args.key,
|
|
106
|
+
data: args.data,
|
|
107
|
+
options,
|
|
108
|
+
args: [args.key, args.data, options]
|
|
109
|
+
};
|
|
207
110
|
}
|
|
208
111
|
}
|
|
209
|
-
throw new Error(`Unimplemented request method "${request.
|
|
112
|
+
throw new Error(`Unimplemented request method "${request.operation}"`);
|
|
210
113
|
}
|
|
211
|
-
SQBAdapter.
|
|
114
|
+
SQBAdapter.transformRequest = transformRequest;
|
|
212
115
|
})(SQBAdapter = exports.SQBAdapter || (exports.SQBAdapter = {}));
|
|
@@ -6,37 +6,37 @@ const common_1 = require("@opra/common");
|
|
|
6
6
|
const sqb_adapter_js_1 = require("./sqb-adapter.js");
|
|
7
7
|
class SqbCollectionResource {
|
|
8
8
|
async create(ctx) {
|
|
9
|
-
const prepared = sqb_adapter_js_1.SQBAdapter.
|
|
9
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
10
10
|
const service = await this.getService(ctx);
|
|
11
11
|
return service.create(ctx, prepared.data, prepared.options);
|
|
12
12
|
}
|
|
13
13
|
async delete(ctx) {
|
|
14
|
-
const prepared = sqb_adapter_js_1.SQBAdapter.
|
|
14
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
15
15
|
const service = await this.getService(ctx);
|
|
16
|
-
return service.
|
|
16
|
+
return service.delete(ctx, prepared.key, prepared.options);
|
|
17
17
|
}
|
|
18
18
|
async deleteMany(ctx) {
|
|
19
|
-
const prepared = sqb_adapter_js_1.SQBAdapter.
|
|
19
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
20
20
|
const service = await this.getService(ctx);
|
|
21
|
-
return service.
|
|
21
|
+
return service.deleteMany(ctx, prepared.options);
|
|
22
22
|
}
|
|
23
|
-
async
|
|
24
|
-
const prepared = sqb_adapter_js_1.SQBAdapter.
|
|
23
|
+
async find(ctx) {
|
|
24
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
25
25
|
const service = await this.getService(ctx);
|
|
26
|
-
return service.
|
|
26
|
+
return service.find(ctx, prepared.key, prepared.options);
|
|
27
27
|
}
|
|
28
28
|
async update(ctx) {
|
|
29
|
-
const prepared = sqb_adapter_js_1.SQBAdapter.
|
|
29
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
30
30
|
const service = await this.getService(ctx);
|
|
31
31
|
return service.update(ctx, prepared.key, prepared.data, prepared.options);
|
|
32
32
|
}
|
|
33
33
|
async updateMany(ctx) {
|
|
34
|
-
const prepared = sqb_adapter_js_1.SQBAdapter.
|
|
34
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
35
35
|
const service = await this.getService(ctx);
|
|
36
|
-
return service.
|
|
36
|
+
return service.updateMany(ctx, prepared.data, prepared.options);
|
|
37
37
|
}
|
|
38
38
|
async search(ctx) {
|
|
39
|
-
const prepared = sqb_adapter_js_1.SQBAdapter.
|
|
39
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
40
40
|
const service = await this.getService(ctx);
|
|
41
41
|
if (prepared.options.count) {
|
|
42
42
|
const [items, count] = await Promise.all([
|
|
@@ -51,43 +51,43 @@ class SqbCollectionResource {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
tslib_1.__decorate([
|
|
54
|
-
common_1.Collection.
|
|
54
|
+
common_1.Collection.Create(),
|
|
55
55
|
tslib_1.__metadata("design:type", Function),
|
|
56
56
|
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
57
57
|
tslib_1.__metadata("design:returntype", Promise)
|
|
58
58
|
], SqbCollectionResource.prototype, "create", null);
|
|
59
59
|
tslib_1.__decorate([
|
|
60
|
-
common_1.Collection.
|
|
60
|
+
common_1.Collection.Delete(),
|
|
61
61
|
tslib_1.__metadata("design:type", Function),
|
|
62
62
|
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
63
63
|
tslib_1.__metadata("design:returntype", Promise)
|
|
64
64
|
], SqbCollectionResource.prototype, "delete", null);
|
|
65
65
|
tslib_1.__decorate([
|
|
66
|
-
common_1.Collection.
|
|
66
|
+
common_1.Collection.DeleteMany(),
|
|
67
67
|
tslib_1.__metadata("design:type", Function),
|
|
68
68
|
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
69
69
|
tslib_1.__metadata("design:returntype", Promise)
|
|
70
70
|
], SqbCollectionResource.prototype, "deleteMany", null);
|
|
71
71
|
tslib_1.__decorate([
|
|
72
|
-
common_1.Collection.
|
|
72
|
+
common_1.Collection.Get(),
|
|
73
73
|
tslib_1.__metadata("design:type", Function),
|
|
74
74
|
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
75
75
|
tslib_1.__metadata("design:returntype", Promise)
|
|
76
|
-
], SqbCollectionResource.prototype, "
|
|
76
|
+
], SqbCollectionResource.prototype, "find", null);
|
|
77
77
|
tslib_1.__decorate([
|
|
78
|
-
common_1.Collection.
|
|
78
|
+
common_1.Collection.Update(),
|
|
79
79
|
tslib_1.__metadata("design:type", Function),
|
|
80
80
|
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
81
81
|
tslib_1.__metadata("design:returntype", Promise)
|
|
82
82
|
], SqbCollectionResource.prototype, "update", null);
|
|
83
83
|
tslib_1.__decorate([
|
|
84
|
-
common_1.Collection.
|
|
84
|
+
common_1.Collection.UpdateMany(),
|
|
85
85
|
tslib_1.__metadata("design:type", Function),
|
|
86
86
|
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
87
87
|
tslib_1.__metadata("design:returntype", Promise)
|
|
88
88
|
], SqbCollectionResource.prototype, "updateMany", null);
|
|
89
89
|
tslib_1.__decorate([
|
|
90
|
-
common_1.Collection.
|
|
90
|
+
common_1.Collection.FindMany(),
|
|
91
91
|
tslib_1.__metadata("design:type", Function),
|
|
92
92
|
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
93
93
|
tslib_1.__metadata("design:returntype", Promise)
|
|
@@ -3,106 +3,109 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SqbEntityService = void 0;
|
|
4
4
|
const connect_1 = require("@sqb/connect");
|
|
5
5
|
class SqbEntityService {
|
|
6
|
-
constructor(
|
|
7
|
-
this.
|
|
8
|
-
const metadata = connect_1.EntityMetadata.get(
|
|
6
|
+
constructor(typeClass, options) {
|
|
7
|
+
this.typeClass = typeClass;
|
|
8
|
+
const metadata = connect_1.EntityMetadata.get(typeClass);
|
|
9
9
|
if (!metadata)
|
|
10
|
-
throw new TypeError(`
|
|
11
|
-
this.
|
|
10
|
+
throw new TypeError(`Class ${typeClass} is not decorated with $Entity() decorator`);
|
|
11
|
+
this.db = options?.db;
|
|
12
|
+
this.defaultLimit = options?.defaultLimit || 10;
|
|
12
13
|
}
|
|
13
|
-
async
|
|
14
|
-
const conn = await this.getConnection(
|
|
15
|
-
const repo = conn.getRepository(this.
|
|
16
|
-
let out;
|
|
14
|
+
async count(context, options) {
|
|
15
|
+
const conn = await this.getConnection();
|
|
16
|
+
const repo = conn.getRepository(this.typeClass);
|
|
17
17
|
try {
|
|
18
|
-
|
|
18
|
+
return await repo.count(options);
|
|
19
19
|
}
|
|
20
20
|
catch (e) {
|
|
21
|
-
await this._onError(
|
|
21
|
+
await this._onError(e);
|
|
22
22
|
throw e;
|
|
23
23
|
}
|
|
24
|
-
if (out && this.onTransformRow)
|
|
25
|
-
out = this.onTransformRow(context, out, 'create');
|
|
26
|
-
return out;
|
|
27
24
|
}
|
|
28
|
-
async
|
|
29
|
-
const conn = await this.getConnection(
|
|
30
|
-
const repo = conn.getRepository(this.
|
|
25
|
+
async create(context, data, options) {
|
|
26
|
+
const conn = await this.getConnection();
|
|
27
|
+
const repo = conn.getRepository(this.typeClass);
|
|
28
|
+
let out;
|
|
31
29
|
try {
|
|
32
|
-
|
|
30
|
+
out = await repo.create(data, options);
|
|
33
31
|
}
|
|
34
32
|
catch (e) {
|
|
35
|
-
await this._onError(
|
|
33
|
+
await this._onError(e);
|
|
36
34
|
throw e;
|
|
37
35
|
}
|
|
36
|
+
if (out && this.onTransformRow)
|
|
37
|
+
out = this.onTransformRow(out);
|
|
38
|
+
if (!out)
|
|
39
|
+
throw new Error('"create" operation returned no result!');
|
|
40
|
+
return out;
|
|
38
41
|
}
|
|
39
|
-
async
|
|
40
|
-
const conn = await this.getConnection(
|
|
41
|
-
const repo = conn.getRepository(this.
|
|
42
|
+
async delete(context, keyValue, options) {
|
|
43
|
+
const conn = await this.getConnection();
|
|
44
|
+
const repo = conn.getRepository(this.typeClass);
|
|
42
45
|
try {
|
|
43
|
-
return await repo.
|
|
46
|
+
return await repo.delete(keyValue, options);
|
|
44
47
|
}
|
|
45
48
|
catch (e) {
|
|
46
|
-
await this._onError(
|
|
49
|
+
await this._onError(e);
|
|
47
50
|
throw e;
|
|
48
51
|
}
|
|
49
52
|
}
|
|
50
|
-
async
|
|
51
|
-
const conn = await this.getConnection(
|
|
52
|
-
const repo = conn.getRepository(this.
|
|
53
|
+
async deleteMany(context, options) {
|
|
54
|
+
const conn = await this.getConnection();
|
|
55
|
+
const repo = conn.getRepository(this.typeClass);
|
|
53
56
|
try {
|
|
54
|
-
return await repo.
|
|
57
|
+
return await repo.deleteMany(options);
|
|
55
58
|
}
|
|
56
59
|
catch (e) {
|
|
57
|
-
await this._onError(
|
|
60
|
+
await this._onError(e);
|
|
58
61
|
throw e;
|
|
59
62
|
}
|
|
60
63
|
}
|
|
61
|
-
async
|
|
62
|
-
const conn = await this.getConnection(
|
|
63
|
-
const repo = conn.getRepository(this.
|
|
64
|
+
async find(context, keyValue, options) {
|
|
65
|
+
const conn = await this.getConnection();
|
|
66
|
+
const repo = conn.getRepository(this.typeClass);
|
|
64
67
|
let out;
|
|
65
68
|
try {
|
|
66
|
-
out = await repo.
|
|
69
|
+
out = await repo.find(keyValue, options);
|
|
67
70
|
}
|
|
68
71
|
catch (e) {
|
|
69
|
-
await this._onError(
|
|
72
|
+
await this._onError(e);
|
|
70
73
|
throw e;
|
|
71
74
|
}
|
|
72
75
|
if (out && this.onTransformRow)
|
|
73
|
-
out = this.onTransformRow(
|
|
76
|
+
out = this.onTransformRow(out);
|
|
74
77
|
return out;
|
|
75
78
|
}
|
|
76
79
|
async findOne(context, options) {
|
|
77
|
-
const conn = await this.getConnection(
|
|
78
|
-
const repo = conn.getRepository(this.
|
|
80
|
+
const conn = await this.getConnection();
|
|
81
|
+
const repo = conn.getRepository(this.typeClass);
|
|
79
82
|
let out;
|
|
80
83
|
try {
|
|
81
84
|
out = await repo.findOne(options);
|
|
82
85
|
}
|
|
83
86
|
catch (e) {
|
|
84
|
-
await this._onError(
|
|
87
|
+
await this._onError(e);
|
|
85
88
|
throw e;
|
|
86
89
|
}
|
|
87
90
|
if (out && this.onTransformRow)
|
|
88
|
-
out = this.onTransformRow(
|
|
91
|
+
out = this.onTransformRow(out);
|
|
89
92
|
return out;
|
|
90
93
|
}
|
|
91
94
|
async findAll(context, options) {
|
|
92
|
-
const conn = await this.getConnection(
|
|
93
|
-
const repo = conn.getRepository(this.
|
|
95
|
+
const conn = await this.getConnection();
|
|
96
|
+
const repo = conn.getRepository(this.typeClass);
|
|
94
97
|
let items;
|
|
95
98
|
try {
|
|
96
99
|
items = await repo.findAll(options);
|
|
97
100
|
}
|
|
98
101
|
catch (e) {
|
|
99
|
-
await this._onError(
|
|
102
|
+
await this._onError(e);
|
|
100
103
|
throw e;
|
|
101
104
|
}
|
|
102
105
|
if (items.length && this.onTransformRow) {
|
|
103
106
|
const newItems = [];
|
|
104
107
|
for (const item of items) {
|
|
105
|
-
const v = this.onTransformRow(
|
|
108
|
+
const v = this.onTransformRow(item);
|
|
106
109
|
if (v)
|
|
107
110
|
newItems.push(v);
|
|
108
111
|
}
|
|
@@ -110,35 +113,64 @@ class SqbEntityService {
|
|
|
110
113
|
}
|
|
111
114
|
return items;
|
|
112
115
|
}
|
|
116
|
+
async exists(context, options) {
|
|
117
|
+
const conn = await this.getConnection();
|
|
118
|
+
const repo = conn.getRepository(this.typeClass);
|
|
119
|
+
try {
|
|
120
|
+
return await repo.exists(options);
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
await this._onError(e);
|
|
124
|
+
throw e;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
113
127
|
async update(context, keyValue, data, options) {
|
|
114
|
-
const conn = await this.getConnection(
|
|
115
|
-
const repo = conn.getRepository(this.
|
|
128
|
+
const conn = await this.getConnection();
|
|
129
|
+
const repo = conn.getRepository(this.typeClass);
|
|
116
130
|
let out;
|
|
117
131
|
try {
|
|
118
132
|
out = await repo.update(keyValue, data, options);
|
|
119
133
|
}
|
|
120
134
|
catch (e) {
|
|
121
|
-
await this._onError(
|
|
135
|
+
await this._onError(e);
|
|
122
136
|
throw e;
|
|
123
137
|
}
|
|
124
138
|
if (out && this.onTransformRow)
|
|
125
|
-
out = this.onTransformRow(
|
|
139
|
+
out = this.onTransformRow(out);
|
|
126
140
|
return out;
|
|
127
141
|
}
|
|
128
|
-
async
|
|
129
|
-
const conn = await this.getConnection(
|
|
130
|
-
const repo = conn.getRepository(this.
|
|
142
|
+
async updateMany(context, data, options) {
|
|
143
|
+
const conn = await this.getConnection();
|
|
144
|
+
const repo = conn.getRepository(this.typeClass);
|
|
131
145
|
try {
|
|
132
|
-
return await repo.
|
|
146
|
+
return await repo.updateMany(data, options);
|
|
133
147
|
}
|
|
134
148
|
catch (e) {
|
|
135
|
-
await this._onError(
|
|
149
|
+
await this._onError(e);
|
|
136
150
|
throw e;
|
|
137
151
|
}
|
|
138
152
|
}
|
|
139
|
-
|
|
153
|
+
with(context, db) {
|
|
154
|
+
if (this.context === context && this.db === db)
|
|
155
|
+
return this;
|
|
156
|
+
const instance = { context };
|
|
157
|
+
// Should reset session if db changed
|
|
158
|
+
if (db) {
|
|
159
|
+
instance.db = db;
|
|
160
|
+
}
|
|
161
|
+
Object.setPrototypeOf(instance, this);
|
|
162
|
+
return instance;
|
|
163
|
+
}
|
|
164
|
+
async _onError(error) {
|
|
140
165
|
if (this.onError)
|
|
141
|
-
await this.onError(
|
|
166
|
+
await this.onError(error);
|
|
167
|
+
}
|
|
168
|
+
getConnection() {
|
|
169
|
+
if (!this.context)
|
|
170
|
+
throw new Error(`Context not set!`);
|
|
171
|
+
if (!this.db)
|
|
172
|
+
throw new Error(`Database not set!`);
|
|
173
|
+
return this.db;
|
|
142
174
|
}
|
|
143
175
|
}
|
|
144
176
|
exports.SqbEntityService = SqbEntityService;
|