@opra/sqb 0.14.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 +114 -0
- package/cjs/augmentation/mapped-type.augmentation.js +14 -0
- package/cjs/augmentation/union-type.augmentation.js +9 -0
- package/cjs/index.js +6 -6
- package/cjs/sqb-adapter.js +75 -83
- package/cjs/sqb-collection-resource.js +95 -0
- package/cjs/sqb-entity-service.js +176 -0
- package/cjs/sqb-singleton-resource.js +54 -0
- package/cjs/{convert-filter.js → transform-filter.js} +10 -13
- package/cjs/transform-key-values.js +14 -0
- package/esm/augmentation/document-factory.augmentation.js +114 -0
- package/esm/augmentation/mapped-type.augmentation.js +14 -0
- package/esm/augmentation/union-type.augmentation.js +9 -0
- package/esm/index.js +10 -7
- package/esm/sqb-adapter.js +83 -87
- package/esm/sqb-collection-resource.js +95 -0
- package/esm/sqb-entity-service.js +176 -0
- package/esm/sqb-singleton-resource.js +54 -0
- package/esm/transform-filter.js +68 -0
- package/esm/transform-key-values.js +14 -0
- package/package.json +13 -12
- package/types/augmentation/document-factory.augmentation.d.ts +1 -0
- package/types/augmentation/mapped-type.augmentation.d.ts +1 -0
- package/types/augmentation/union-type.augmentation.d.ts +1 -0
- package/types/index.d.ts +7 -0
- package/types/sqb-adapter.d.ts +14 -0
- package/types/sqb-collection-resource.d.ts +14 -0
- package/types/sqb-entity-service.d.ts +31 -0
- package/types/sqb-singleton-resource.d.ts +11 -0
- package/types/transform-filter.d.ts +2 -0
- package/types/transform-key-values.d.ts +3 -0
- package/cjs/base-entity-resource.js +0 -39
- package/cjs/base-entity-service.js +0 -130
- package/esm/base-entity-resource.d.ts +0 -13
- package/esm/base-entity-resource.js +0 -35
- package/esm/base-entity-service.d.ts +0 -30
- package/esm/base-entity-service.js +0 -126
- package/esm/convert-filter.d.ts +0 -2
- package/esm/convert-filter.js +0 -66
- package/esm/index.d.ts +0 -8
- package/esm/sqb-adapter.d.ts +0 -10
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@opra/common");
|
|
4
|
+
const connect_1 = require("@sqb/connect");
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
const _extractFieldSchema = common_1.DocumentFactory.prototype.extractFieldSchema;
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
common_1.DocumentFactory.prototype.extractFieldSchema = async function (target, ctor, metadata, name) {
|
|
9
|
+
await _extractFieldSchema.call(this, target, ctor, metadata, name);
|
|
10
|
+
const sqbMeta = connect_1.EntityMetadata.get(ctor);
|
|
11
|
+
const sqbField = sqbMeta && connect_1.EntityMetadata.getField(sqbMeta, name);
|
|
12
|
+
if (!sqbField)
|
|
13
|
+
return;
|
|
14
|
+
const detectType = !metadata.type;
|
|
15
|
+
if ((0, connect_1.isAssociationField)(sqbField)) {
|
|
16
|
+
if (!sqbField.association.returnsMany())
|
|
17
|
+
delete target.isArray;
|
|
18
|
+
if (!target.type) {
|
|
19
|
+
const trg = await sqbField.association.resolveTarget();
|
|
20
|
+
if (trg) {
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
target.type = await this.importTypeClass(trg.ctor);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else if (sqbField.kind === 'column') {
|
|
27
|
+
if (typeof sqbField.enum === 'object')
|
|
28
|
+
metadata.enum = sqbField.enum;
|
|
29
|
+
if (sqbField.type && Reflect.hasMetadata(common_1.METADATA_KEY, sqbField.type)) {
|
|
30
|
+
target.type = sqbField.type;
|
|
31
|
+
}
|
|
32
|
+
switch (sqbField.dataType) {
|
|
33
|
+
case connect_1.DataType.GUID:
|
|
34
|
+
if (!target.type || (detectType && target.type === 'string'))
|
|
35
|
+
target.type = 'guid';
|
|
36
|
+
break;
|
|
37
|
+
case connect_1.DataType.JSON:
|
|
38
|
+
if (!target.type || (detectType && target.type === 'any'))
|
|
39
|
+
target.type = 'object';
|
|
40
|
+
break;
|
|
41
|
+
case connect_1.DataType.INTEGER:
|
|
42
|
+
case connect_1.DataType.SMALLINT:
|
|
43
|
+
if (!target.type || (detectType && target.type === 'number'))
|
|
44
|
+
target.type = 'integer';
|
|
45
|
+
break;
|
|
46
|
+
case connect_1.DataType.BIGINT:
|
|
47
|
+
if (!target.type || (detectType && target.type === 'number'))
|
|
48
|
+
target.type = 'bigint';
|
|
49
|
+
break;
|
|
50
|
+
case connect_1.DataType.DATE:
|
|
51
|
+
if (!target.type || (detectType && (target.type === 'timestamp' || target.type === 'string')))
|
|
52
|
+
target.type = 'date';
|
|
53
|
+
break;
|
|
54
|
+
case connect_1.DataType.TIMESTAMPTZ:
|
|
55
|
+
if (!target.type || (detectType && (target.type === 'timestamp' || target.type === 'string')))
|
|
56
|
+
target.type = 'timestamptz';
|
|
57
|
+
break;
|
|
58
|
+
case connect_1.DataType.TIME:
|
|
59
|
+
if (!target.type || (detectType && (target.type === 'timestamp' || target.type === 'string')))
|
|
60
|
+
target.type = 'time';
|
|
61
|
+
break;
|
|
62
|
+
case connect_1.DataType.BINARY:
|
|
63
|
+
if (!target.type || (detectType && target.type === 'string'))
|
|
64
|
+
target.type = 'base64';
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
if (!target.type) {
|
|
68
|
+
switch (sqbField.dataType) {
|
|
69
|
+
case connect_1.DataType.BOOL:
|
|
70
|
+
target.type = 'boolean';
|
|
71
|
+
break;
|
|
72
|
+
case connect_1.DataType.CHAR:
|
|
73
|
+
case connect_1.DataType.VARCHAR:
|
|
74
|
+
case connect_1.DataType.TEXT:
|
|
75
|
+
target.type = 'string';
|
|
76
|
+
break;
|
|
77
|
+
case connect_1.DataType.FLOAT:
|
|
78
|
+
case connect_1.DataType.DOUBLE:
|
|
79
|
+
case connect_1.DataType.NUMBER:
|
|
80
|
+
target.type = 'number';
|
|
81
|
+
break;
|
|
82
|
+
case connect_1.DataType.TIMESTAMP:
|
|
83
|
+
target.type = 'timestamp';
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (sqbField.notNull && target.required === undefined)
|
|
88
|
+
target.required = sqbField.notNull;
|
|
89
|
+
if (sqbField.exclusive && target.exclusive === undefined)
|
|
90
|
+
target.exclusive = sqbField.exclusive;
|
|
91
|
+
if (sqbField.default !== undefined && target.default === undefined)
|
|
92
|
+
target.default = sqbField.default;
|
|
93
|
+
}
|
|
94
|
+
if (sqbField.exclusive && target.exclusive === undefined)
|
|
95
|
+
target.exclusive = sqbField.exclusive;
|
|
96
|
+
};
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
const _createCollection = common_1.DocumentFactory.prototype.createCollection;
|
|
99
|
+
// @ts-ignore
|
|
100
|
+
common_1.DocumentFactory.prototype.createCollection = async function (name, schema) {
|
|
101
|
+
const { document } = this;
|
|
102
|
+
const dataType = document.getComplexType(schema.type);
|
|
103
|
+
// Determine primaryKey if not defined
|
|
104
|
+
if (!schema.primaryKey && dataType.ctor) {
|
|
105
|
+
const entityMetadata = connect_1.EntityMetadata.get(dataType.ctor);
|
|
106
|
+
if (entityMetadata?.indexes) {
|
|
107
|
+
const primaryIndex = entityMetadata.indexes.find(x => x.primary);
|
|
108
|
+
if (primaryIndex) {
|
|
109
|
+
schema.primaryKey = primaryIndex.columns;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return await _createCollection.call(this, name, schema);
|
|
114
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@opra/common");
|
|
4
|
+
const connect_1 = require("@sqb/connect");
|
|
5
|
+
const _applyMixin = common_1.MappedType._applyMixin;
|
|
6
|
+
common_1.MappedType._applyMixin = function (target, source, options) {
|
|
7
|
+
_applyMixin.call(null, target, source, options);
|
|
8
|
+
const srcMeta = connect_1.Entity.getMetadata(source);
|
|
9
|
+
if (srcMeta) {
|
|
10
|
+
const trgMeta = connect_1.EntityMetadata.define(target);
|
|
11
|
+
const { isInheritedPredicate } = options;
|
|
12
|
+
connect_1.EntityMetadata.mixin(trgMeta, srcMeta, k => isInheritedPredicate(k));
|
|
13
|
+
}
|
|
14
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@opra/common");
|
|
4
|
+
const connect_1 = require("@sqb/connect");
|
|
5
|
+
const _applyMixin = common_1.UnionType._applyMixin;
|
|
6
|
+
common_1.UnionType._applyMixin = function (target, ...sources) {
|
|
7
|
+
_applyMixin.call(null, target, ...sources);
|
|
8
|
+
connect_1.Entity.mixin(target, ...sources);
|
|
9
|
+
};
|
package/cjs/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
|
-
|
|
4
|
+
require("./augmentation/document-factory.augmentation.js");
|
|
5
|
+
require("./augmentation/mapped-type.augmentation.js");
|
|
6
|
+
require("./augmentation/union-type.augmentation.js");
|
|
5
7
|
tslib_1.__exportStar(require("./sqb-adapter.js"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./
|
|
7
|
-
tslib_1.__exportStar(require("./
|
|
8
|
-
|
|
9
|
-
globalThis[optionalsSymbol] = globalThis[optionalsSymbol] || {};
|
|
10
|
-
globalThis[optionalsSymbol].SqbConnect = _SqbConnect;
|
|
8
|
+
tslib_1.__exportStar(require("./sqb-collection-resource.js"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./sqb-entity-service.js"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./sqb-singleton-resource.js"), exports);
|
package/cjs/sqb-adapter.js
CHANGED
|
@@ -4,120 +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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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');
|
|
29
|
+
}
|
|
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') {
|
|
19
48
|
return {
|
|
20
49
|
method: 'create',
|
|
21
|
-
|
|
50
|
+
data: args.data,
|
|
22
51
|
options,
|
|
23
|
-
args: [data, options]
|
|
52
|
+
args: [args.data, options]
|
|
24
53
|
};
|
|
25
54
|
}
|
|
26
|
-
|
|
27
|
-
const options = (0, lodash_omitby_1.default)({
|
|
28
|
-
filter: (0, convert_filter_js_1.convertFilter)(query.filter)
|
|
29
|
-
}, lodash_isnil_1.default);
|
|
55
|
+
if (operation === 'deleteMany' || (operation === 'delete' && resource instanceof common_1.Singleton)) {
|
|
30
56
|
return {
|
|
31
|
-
method: '
|
|
57
|
+
method: 'deleteMany',
|
|
32
58
|
options,
|
|
33
59
|
args: [options]
|
|
34
60
|
};
|
|
35
61
|
}
|
|
36
|
-
|
|
37
|
-
if (query.kind === 'CollectionGetQuery') {
|
|
38
|
-
const options = (0, lodash_omitby_1.default)({
|
|
39
|
-
pick: query.pick?.length ? query.pick : undefined,
|
|
40
|
-
omit: query.omit?.length ? query.omit : undefined,
|
|
41
|
-
include: query.include?.length ? query.include : undefined,
|
|
42
|
-
}, lodash_isnil_1.default);
|
|
43
|
-
const keyValue = query.keyValue;
|
|
44
|
-
return {
|
|
45
|
-
method: 'findByPk',
|
|
46
|
-
keyValue,
|
|
47
|
-
options,
|
|
48
|
-
args: [keyValue, options]
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
case 'search': {
|
|
54
|
-
const options = (0, lodash_omitby_1.default)({
|
|
55
|
-
pick: query.pick?.length ? query.pick : undefined,
|
|
56
|
-
omit: query.omit?.length ? query.omit : undefined,
|
|
57
|
-
include: query.include?.length ? query.include : undefined,
|
|
58
|
-
sort: query.sort?.length ? query.sort : undefined,
|
|
59
|
-
limit: query.limit,
|
|
60
|
-
offset: query.skip,
|
|
61
|
-
distinct: query.distinct,
|
|
62
|
-
total: query.count,
|
|
63
|
-
filter: (0, convert_filter_js_1.convertFilter)(query.filter)
|
|
64
|
-
}, lodash_isnil_1.default);
|
|
62
|
+
if (operation === 'delete') {
|
|
65
63
|
return {
|
|
66
|
-
method: '
|
|
64
|
+
method: 'delete',
|
|
65
|
+
key: args.key,
|
|
67
66
|
options,
|
|
68
|
-
args: [options]
|
|
67
|
+
args: [args.key, options]
|
|
69
68
|
};
|
|
70
69
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const keyValue = query.keyValue;
|
|
70
|
+
if (operation === 'get') {
|
|
71
|
+
if (resource instanceof common_1.Singleton)
|
|
72
|
+
return {
|
|
73
|
+
method: 'findOne',
|
|
74
|
+
options,
|
|
75
|
+
args: [options]
|
|
76
|
+
};
|
|
79
77
|
return {
|
|
80
|
-
method: '
|
|
81
|
-
|
|
82
|
-
values: data,
|
|
78
|
+
method: 'find',
|
|
79
|
+
key: args.key,
|
|
83
80
|
options,
|
|
84
|
-
args: [
|
|
81
|
+
args: [args.key, options]
|
|
85
82
|
};
|
|
86
83
|
}
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
}, lodash_isnil_1.default);
|
|
91
|
-
const { data } = query;
|
|
92
|
-
return {
|
|
93
|
-
method: 'updateAll',
|
|
94
|
-
values: data,
|
|
84
|
+
if (operation === 'findMany') {
|
|
85
|
+
const out = {
|
|
86
|
+
method: 'findMany',
|
|
95
87
|
options,
|
|
96
|
-
args: [
|
|
88
|
+
args: [options]
|
|
97
89
|
};
|
|
90
|
+
if (args.count)
|
|
91
|
+
out.count = args.count;
|
|
92
|
+
return out;
|
|
98
93
|
}
|
|
99
|
-
|
|
100
|
-
const options = {};
|
|
101
|
-
const keyValue = query.keyValue;
|
|
94
|
+
if (operation === 'updateMany' || (operation === 'update' && resource instanceof common_1.Singleton)) {
|
|
102
95
|
return {
|
|
103
|
-
method: '
|
|
104
|
-
|
|
96
|
+
method: 'updateMany',
|
|
97
|
+
data: args.data,
|
|
105
98
|
options,
|
|
106
|
-
args: [
|
|
99
|
+
args: [args.data, options]
|
|
107
100
|
};
|
|
108
101
|
}
|
|
109
|
-
|
|
110
|
-
const options = (0, lodash_omitby_1.default)({
|
|
111
|
-
filter: (0, convert_filter_js_1.convertFilter)(query.filter)
|
|
112
|
-
}, lodash_isnil_1.default);
|
|
102
|
+
if (operation === 'update') {
|
|
113
103
|
return {
|
|
114
|
-
method: '
|
|
104
|
+
method: 'update',
|
|
105
|
+
key: args.key,
|
|
106
|
+
data: args.data,
|
|
115
107
|
options,
|
|
116
|
-
args: [options]
|
|
108
|
+
args: [args.key, args.data, options]
|
|
117
109
|
};
|
|
118
110
|
}
|
|
119
111
|
}
|
|
120
|
-
throw new Error(`Unimplemented
|
|
112
|
+
throw new Error(`Unimplemented request method "${request.operation}"`);
|
|
121
113
|
}
|
|
122
|
-
SQBAdapter.
|
|
114
|
+
SQBAdapter.transformRequest = transformRequest;
|
|
123
115
|
})(SQBAdapter = exports.SQBAdapter || (exports.SQBAdapter = {}));
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqbCollectionResource = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@opra/common");
|
|
6
|
+
const sqb_adapter_js_1 = require("./sqb-adapter.js");
|
|
7
|
+
class SqbCollectionResource {
|
|
8
|
+
async create(ctx) {
|
|
9
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
10
|
+
const service = await this.getService(ctx);
|
|
11
|
+
return service.create(ctx, prepared.data, prepared.options);
|
|
12
|
+
}
|
|
13
|
+
async delete(ctx) {
|
|
14
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
15
|
+
const service = await this.getService(ctx);
|
|
16
|
+
return service.delete(ctx, prepared.key, prepared.options);
|
|
17
|
+
}
|
|
18
|
+
async deleteMany(ctx) {
|
|
19
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
20
|
+
const service = await this.getService(ctx);
|
|
21
|
+
return service.deleteMany(ctx, prepared.options);
|
|
22
|
+
}
|
|
23
|
+
async find(ctx) {
|
|
24
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
25
|
+
const service = await this.getService(ctx);
|
|
26
|
+
return service.find(ctx, prepared.key, prepared.options);
|
|
27
|
+
}
|
|
28
|
+
async update(ctx) {
|
|
29
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
30
|
+
const service = await this.getService(ctx);
|
|
31
|
+
return service.update(ctx, prepared.key, prepared.data, prepared.options);
|
|
32
|
+
}
|
|
33
|
+
async updateMany(ctx) {
|
|
34
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
35
|
+
const service = await this.getService(ctx);
|
|
36
|
+
return service.updateMany(ctx, prepared.data, prepared.options);
|
|
37
|
+
}
|
|
38
|
+
async search(ctx) {
|
|
39
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
40
|
+
const service = await this.getService(ctx);
|
|
41
|
+
if (prepared.options.count) {
|
|
42
|
+
const [items, count] = await Promise.all([
|
|
43
|
+
service.findAll(prepared.options),
|
|
44
|
+
service.count(prepared.options)
|
|
45
|
+
]);
|
|
46
|
+
ctx.response.count = count;
|
|
47
|
+
return items;
|
|
48
|
+
}
|
|
49
|
+
else
|
|
50
|
+
return service.findAll(ctx, prepared.options);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
tslib_1.__decorate([
|
|
54
|
+
common_1.Collection.Create(),
|
|
55
|
+
tslib_1.__metadata("design:type", Function),
|
|
56
|
+
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
57
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
58
|
+
], SqbCollectionResource.prototype, "create", null);
|
|
59
|
+
tslib_1.__decorate([
|
|
60
|
+
common_1.Collection.Delete(),
|
|
61
|
+
tslib_1.__metadata("design:type", Function),
|
|
62
|
+
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
63
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
64
|
+
], SqbCollectionResource.prototype, "delete", null);
|
|
65
|
+
tslib_1.__decorate([
|
|
66
|
+
common_1.Collection.DeleteMany(),
|
|
67
|
+
tslib_1.__metadata("design:type", Function),
|
|
68
|
+
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
69
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
70
|
+
], SqbCollectionResource.prototype, "deleteMany", null);
|
|
71
|
+
tslib_1.__decorate([
|
|
72
|
+
common_1.Collection.Get(),
|
|
73
|
+
tslib_1.__metadata("design:type", Function),
|
|
74
|
+
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
75
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
76
|
+
], SqbCollectionResource.prototype, "find", null);
|
|
77
|
+
tslib_1.__decorate([
|
|
78
|
+
common_1.Collection.Update(),
|
|
79
|
+
tslib_1.__metadata("design:type", Function),
|
|
80
|
+
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
81
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
82
|
+
], SqbCollectionResource.prototype, "update", null);
|
|
83
|
+
tslib_1.__decorate([
|
|
84
|
+
common_1.Collection.UpdateMany(),
|
|
85
|
+
tslib_1.__metadata("design:type", Function),
|
|
86
|
+
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
87
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
88
|
+
], SqbCollectionResource.prototype, "updateMany", null);
|
|
89
|
+
tslib_1.__decorate([
|
|
90
|
+
common_1.Collection.FindMany(),
|
|
91
|
+
tslib_1.__metadata("design:type", Function),
|
|
92
|
+
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
93
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
94
|
+
], SqbCollectionResource.prototype, "search", null);
|
|
95
|
+
exports.SqbCollectionResource = SqbCollectionResource;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqbEntityService = void 0;
|
|
4
|
+
const connect_1 = require("@sqb/connect");
|
|
5
|
+
class SqbEntityService {
|
|
6
|
+
constructor(typeClass, options) {
|
|
7
|
+
this.typeClass = typeClass;
|
|
8
|
+
const metadata = connect_1.EntityMetadata.get(typeClass);
|
|
9
|
+
if (!metadata)
|
|
10
|
+
throw new TypeError(`Class ${typeClass} is not decorated with $Entity() decorator`);
|
|
11
|
+
this.db = options?.db;
|
|
12
|
+
this.defaultLimit = options?.defaultLimit || 10;
|
|
13
|
+
}
|
|
14
|
+
async count(context, options) {
|
|
15
|
+
const conn = await this.getConnection();
|
|
16
|
+
const repo = conn.getRepository(this.typeClass);
|
|
17
|
+
try {
|
|
18
|
+
return await repo.count(options);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
await this._onError(e);
|
|
22
|
+
throw e;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async create(context, data, options) {
|
|
26
|
+
const conn = await this.getConnection();
|
|
27
|
+
const repo = conn.getRepository(this.typeClass);
|
|
28
|
+
let out;
|
|
29
|
+
try {
|
|
30
|
+
out = await repo.create(data, options);
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
await this._onError(e);
|
|
34
|
+
throw e;
|
|
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;
|
|
41
|
+
}
|
|
42
|
+
async delete(context, keyValue, options) {
|
|
43
|
+
const conn = await this.getConnection();
|
|
44
|
+
const repo = conn.getRepository(this.typeClass);
|
|
45
|
+
try {
|
|
46
|
+
return await repo.delete(keyValue, options);
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
await this._onError(e);
|
|
50
|
+
throw e;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async deleteMany(context, options) {
|
|
54
|
+
const conn = await this.getConnection();
|
|
55
|
+
const repo = conn.getRepository(this.typeClass);
|
|
56
|
+
try {
|
|
57
|
+
return await repo.deleteMany(options);
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
await this._onError(e);
|
|
61
|
+
throw e;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async find(context, keyValue, options) {
|
|
65
|
+
const conn = await this.getConnection();
|
|
66
|
+
const repo = conn.getRepository(this.typeClass);
|
|
67
|
+
let out;
|
|
68
|
+
try {
|
|
69
|
+
out = await repo.find(keyValue, options);
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
await this._onError(e);
|
|
73
|
+
throw e;
|
|
74
|
+
}
|
|
75
|
+
if (out && this.onTransformRow)
|
|
76
|
+
out = this.onTransformRow(out);
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
async findOne(context, options) {
|
|
80
|
+
const conn = await this.getConnection();
|
|
81
|
+
const repo = conn.getRepository(this.typeClass);
|
|
82
|
+
let out;
|
|
83
|
+
try {
|
|
84
|
+
out = await repo.findOne(options);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
await this._onError(e);
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
90
|
+
if (out && this.onTransformRow)
|
|
91
|
+
out = this.onTransformRow(out);
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
async findAll(context, options) {
|
|
95
|
+
const conn = await this.getConnection();
|
|
96
|
+
const repo = conn.getRepository(this.typeClass);
|
|
97
|
+
let items;
|
|
98
|
+
try {
|
|
99
|
+
items = await repo.findAll(options);
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
await this._onError(e);
|
|
103
|
+
throw e;
|
|
104
|
+
}
|
|
105
|
+
if (items.length && this.onTransformRow) {
|
|
106
|
+
const newItems = [];
|
|
107
|
+
for (const item of items) {
|
|
108
|
+
const v = this.onTransformRow(item);
|
|
109
|
+
if (v)
|
|
110
|
+
newItems.push(v);
|
|
111
|
+
}
|
|
112
|
+
return newItems;
|
|
113
|
+
}
|
|
114
|
+
return items;
|
|
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
|
+
}
|
|
127
|
+
async update(context, keyValue, data, options) {
|
|
128
|
+
const conn = await this.getConnection();
|
|
129
|
+
const repo = conn.getRepository(this.typeClass);
|
|
130
|
+
let out;
|
|
131
|
+
try {
|
|
132
|
+
out = await repo.update(keyValue, data, options);
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
await this._onError(e);
|
|
136
|
+
throw e;
|
|
137
|
+
}
|
|
138
|
+
if (out && this.onTransformRow)
|
|
139
|
+
out = this.onTransformRow(out);
|
|
140
|
+
return out;
|
|
141
|
+
}
|
|
142
|
+
async updateMany(context, data, options) {
|
|
143
|
+
const conn = await this.getConnection();
|
|
144
|
+
const repo = conn.getRepository(this.typeClass);
|
|
145
|
+
try {
|
|
146
|
+
return await repo.updateMany(data, options);
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
await this._onError(e);
|
|
150
|
+
throw e;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
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) {
|
|
165
|
+
if (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;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
exports.SqbEntityService = SqbEntityService;
|