@opra/sqb 0.16.2 → 0.17.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/sqb-adapter.js +2 -2
- package/cjs/sqb-collection-resource.js +2 -2
- package/cjs/transform-filter.js +10 -9
- package/esm/augmentation/document-factory.augmentation.js +28 -30
- package/esm/augmentation/mapped-type.augmentation.js +7 -9
- package/esm/augmentation/union-type.augmentation.js +5 -7
- package/esm/index.js +7 -10
- package/esm/sqb-adapter.js +19 -23
- package/esm/sqb-collection-resource.js +48 -52
- package/esm/sqb-entity-service.js +3 -7
- package/esm/sqb-singleton-resource.js +28 -32
- package/esm/transform-filter.js +24 -27
- package/esm/transform-key-values.js +1 -4
- package/package.json +2 -2
- package/types/sqb-collection-resource.d.ts +1 -1
- package/types/transform-filter.d.ts +2 -2
package/cjs/sqb-adapter.js
CHANGED
|
@@ -33,8 +33,8 @@ var SQBAdapter;
|
|
|
33
33
|
options.omit = args.omit?.length ? args.omit : undefined;
|
|
34
34
|
options.include = args.include?.length ? args.include : undefined;
|
|
35
35
|
}
|
|
36
|
-
if (args.filter) {
|
|
37
|
-
options.filter =
|
|
36
|
+
if (resource instanceof common_1.Collection && args.filter) {
|
|
37
|
+
options.filter = (0, transform_filter_js_1.default)(resource, args.filter);
|
|
38
38
|
}
|
|
39
39
|
if (operation === 'findMany') {
|
|
40
40
|
options.sort = args.sort?.length ? args.sort : undefined;
|
|
@@ -35,7 +35,7 @@ class SqbCollectionResource {
|
|
|
35
35
|
const service = await this.getService(ctx);
|
|
36
36
|
return service.with(ctx).updateMany(prepared.data, prepared.options);
|
|
37
37
|
}
|
|
38
|
-
async
|
|
38
|
+
async findMany(ctx) {
|
|
39
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) {
|
|
@@ -91,5 +91,5 @@ tslib_1.__decorate([
|
|
|
91
91
|
tslib_1.__metadata("design:type", Function),
|
|
92
92
|
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
93
93
|
tslib_1.__metadata("design:returntype", Promise)
|
|
94
|
-
], SqbCollectionResource.prototype, "
|
|
94
|
+
], SqbCollectionResource.prototype, "findMany", null);
|
|
95
95
|
exports.SqbCollectionResource = SqbCollectionResource;
|
package/cjs/transform-filter.js
CHANGED
|
@@ -3,15 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
const common_1 = require("@opra/common");
|
|
5
5
|
const sqb = tslib_1.__importStar(require("@sqb/builder"));
|
|
6
|
-
function transformFilter(str) {
|
|
6
|
+
function transformFilter(resource, str) {
|
|
7
7
|
const ast = typeof str === 'string' ? (0, common_1.parseFilter)(str) : str;
|
|
8
8
|
if (!ast)
|
|
9
9
|
return;
|
|
10
|
+
resource.normalizeFilter(ast);
|
|
10
11
|
if (ast instanceof common_1.ComparisonExpression) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const right = transformFilter(ast.right);
|
|
12
|
+
if (!(ast.left instanceof common_1.QualifiedIdentifier && ast.left.field))
|
|
13
|
+
throw new TypeError(`Unsupported filter query. Left side should be a data field.`);
|
|
14
|
+
const left = sqb.Field(ast.left.value);
|
|
15
|
+
const right = transformFilter(resource, ast.right);
|
|
15
16
|
switch (ast.op) {
|
|
16
17
|
case '=':
|
|
17
18
|
return sqb.Eq(left, right);
|
|
@@ -53,15 +54,15 @@ function transformFilter(str) {
|
|
|
53
54
|
return ast.value;
|
|
54
55
|
}
|
|
55
56
|
if (ast instanceof common_1.ArrayExpression) {
|
|
56
|
-
return ast.items.map(transformFilter);
|
|
57
|
+
return ast.items.map(i => transformFilter(resource, i));
|
|
57
58
|
}
|
|
58
59
|
if (ast instanceof common_1.LogicalExpression) {
|
|
59
60
|
if (ast.op === 'or')
|
|
60
|
-
return sqb.Or(...ast.items.map(transformFilter));
|
|
61
|
-
return sqb.And(...ast.items.map(transformFilter));
|
|
61
|
+
return sqb.Or(...ast.items.map((i => transformFilter(resource, i))));
|
|
62
|
+
return sqb.And(...ast.items.map((i => transformFilter(resource, i))));
|
|
62
63
|
}
|
|
63
64
|
if (ast instanceof common_1.ParenthesesExpression) {
|
|
64
|
-
return transformFilter(ast.expression);
|
|
65
|
+
return transformFilter(resource, ast.expression);
|
|
65
66
|
}
|
|
66
67
|
throw new Error(`${ast.type} is not implemented yet`);
|
|
67
68
|
}
|
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const common_1 = require("@opra/common");
|
|
4
|
-
const connect_1 = require("@sqb/connect");
|
|
1
|
+
import { DocumentFactory, METADATA_KEY } from "@opra/common";
|
|
2
|
+
import { DataType as SqbDataType, EntityMetadata, isAssociationField } from '@sqb/connect';
|
|
5
3
|
// @ts-ignore
|
|
6
|
-
const _extractFieldSchema =
|
|
4
|
+
const _extractFieldSchema = DocumentFactory.prototype.extractFieldSchema;
|
|
7
5
|
// @ts-ignore
|
|
8
|
-
|
|
6
|
+
DocumentFactory.prototype.extractFieldSchema = async function (target, ctor, metadata, name) {
|
|
9
7
|
await _extractFieldSchema.call(this, target, ctor, metadata, name);
|
|
10
|
-
const sqbMeta =
|
|
11
|
-
const sqbField = sqbMeta &&
|
|
8
|
+
const sqbMeta = EntityMetadata.get(ctor);
|
|
9
|
+
const sqbField = sqbMeta && EntityMetadata.getField(sqbMeta, name);
|
|
12
10
|
if (!sqbField)
|
|
13
11
|
return;
|
|
14
12
|
const detectType = !metadata.type;
|
|
15
|
-
if (
|
|
13
|
+
if (isAssociationField(sqbField)) {
|
|
16
14
|
if (!sqbField.association.returnsMany())
|
|
17
15
|
delete target.isArray;
|
|
18
16
|
if (!target.type) {
|
|
@@ -26,60 +24,60 @@ common_1.DocumentFactory.prototype.extractFieldSchema = async function (target,
|
|
|
26
24
|
else if (sqbField.kind === 'column') {
|
|
27
25
|
if (typeof sqbField.enum === 'object')
|
|
28
26
|
metadata.enum = sqbField.enum;
|
|
29
|
-
if (sqbField.type && Reflect.hasMetadata(
|
|
27
|
+
if (sqbField.type && Reflect.hasMetadata(METADATA_KEY, sqbField.type)) {
|
|
30
28
|
target.type = sqbField.type;
|
|
31
29
|
}
|
|
32
30
|
switch (sqbField.dataType) {
|
|
33
|
-
case
|
|
31
|
+
case SqbDataType.GUID:
|
|
34
32
|
if (!target.type || (detectType && target.type === 'string'))
|
|
35
33
|
target.type = 'guid';
|
|
36
34
|
break;
|
|
37
|
-
case
|
|
35
|
+
case SqbDataType.JSON:
|
|
38
36
|
if (!target.type || (detectType && target.type === 'any'))
|
|
39
37
|
target.type = 'object';
|
|
40
38
|
break;
|
|
41
|
-
case
|
|
42
|
-
case
|
|
39
|
+
case SqbDataType.INTEGER:
|
|
40
|
+
case SqbDataType.SMALLINT:
|
|
43
41
|
if (!target.type || (detectType && target.type === 'number'))
|
|
44
42
|
target.type = 'integer';
|
|
45
43
|
break;
|
|
46
|
-
case
|
|
44
|
+
case SqbDataType.BIGINT:
|
|
47
45
|
if (!target.type || (detectType && target.type === 'number'))
|
|
48
46
|
target.type = 'bigint';
|
|
49
47
|
break;
|
|
50
|
-
case
|
|
48
|
+
case SqbDataType.DATE:
|
|
51
49
|
if (!target.type || (detectType && (target.type === 'timestamp' || target.type === 'string')))
|
|
52
50
|
target.type = 'date';
|
|
53
51
|
break;
|
|
54
|
-
case
|
|
52
|
+
case SqbDataType.TIMESTAMPTZ:
|
|
55
53
|
if (!target.type || (detectType && (target.type === 'timestamp' || target.type === 'string')))
|
|
56
54
|
target.type = 'timestamptz';
|
|
57
55
|
break;
|
|
58
|
-
case
|
|
56
|
+
case SqbDataType.TIME:
|
|
59
57
|
if (!target.type || (detectType && (target.type === 'timestamp' || target.type === 'string')))
|
|
60
58
|
target.type = 'time';
|
|
61
59
|
break;
|
|
62
|
-
case
|
|
60
|
+
case SqbDataType.BINARY:
|
|
63
61
|
if (!target.type || (detectType && target.type === 'string'))
|
|
64
62
|
target.type = 'base64';
|
|
65
63
|
break;
|
|
66
64
|
}
|
|
67
65
|
if (!target.type) {
|
|
68
66
|
switch (sqbField.dataType) {
|
|
69
|
-
case
|
|
67
|
+
case SqbDataType.BOOL:
|
|
70
68
|
target.type = 'boolean';
|
|
71
69
|
break;
|
|
72
|
-
case
|
|
73
|
-
case
|
|
74
|
-
case
|
|
70
|
+
case SqbDataType.CHAR:
|
|
71
|
+
case SqbDataType.VARCHAR:
|
|
72
|
+
case SqbDataType.TEXT:
|
|
75
73
|
target.type = 'string';
|
|
76
74
|
break;
|
|
77
|
-
case
|
|
78
|
-
case
|
|
79
|
-
case
|
|
75
|
+
case SqbDataType.FLOAT:
|
|
76
|
+
case SqbDataType.DOUBLE:
|
|
77
|
+
case SqbDataType.NUMBER:
|
|
80
78
|
target.type = 'number';
|
|
81
79
|
break;
|
|
82
|
-
case
|
|
80
|
+
case SqbDataType.TIMESTAMP:
|
|
83
81
|
target.type = 'timestamp';
|
|
84
82
|
break;
|
|
85
83
|
}
|
|
@@ -95,14 +93,14 @@ common_1.DocumentFactory.prototype.extractFieldSchema = async function (target,
|
|
|
95
93
|
target.exclusive = sqbField.exclusive;
|
|
96
94
|
};
|
|
97
95
|
// @ts-ignore
|
|
98
|
-
const _createCollection =
|
|
96
|
+
const _createCollection = DocumentFactory.prototype.createCollection;
|
|
99
97
|
// @ts-ignore
|
|
100
|
-
|
|
98
|
+
DocumentFactory.prototype.createCollection = async function (name, schema) {
|
|
101
99
|
const { document } = this;
|
|
102
100
|
const dataType = document.getComplexType(schema.type);
|
|
103
101
|
// Determine primaryKey if not defined
|
|
104
102
|
if (!schema.primaryKey && dataType.ctor) {
|
|
105
|
-
const entityMetadata =
|
|
103
|
+
const entityMetadata = EntityMetadata.get(dataType.ctor);
|
|
106
104
|
if (entityMetadata?.indexes) {
|
|
107
105
|
const primaryIndex = entityMetadata.indexes.find(x => x.primary);
|
|
108
106
|
if (primaryIndex) {
|
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
const _applyMixin = common_1.MappedType._applyMixin;
|
|
6
|
-
common_1.MappedType._applyMixin = function (target, source, options) {
|
|
1
|
+
import { MappedType } from "@opra/common";
|
|
2
|
+
import { Entity, EntityMetadata } from '@sqb/connect';
|
|
3
|
+
const _applyMixin = MappedType._applyMixin;
|
|
4
|
+
MappedType._applyMixin = function (target, source, options) {
|
|
7
5
|
_applyMixin.call(null, target, source, options);
|
|
8
|
-
const srcMeta =
|
|
6
|
+
const srcMeta = Entity.getMetadata(source);
|
|
9
7
|
if (srcMeta) {
|
|
10
|
-
const trgMeta =
|
|
8
|
+
const trgMeta = EntityMetadata.define(target);
|
|
11
9
|
const { isInheritedPredicate } = options;
|
|
12
|
-
|
|
10
|
+
EntityMetadata.mixin(trgMeta, srcMeta, k => isInheritedPredicate(k));
|
|
13
11
|
}
|
|
14
12
|
};
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
const _applyMixin = common_1.UnionType._applyMixin;
|
|
6
|
-
common_1.UnionType._applyMixin = function (target, ...sources) {
|
|
1
|
+
import { UnionType } from "@opra/common";
|
|
2
|
+
import { Entity } from '@sqb/connect';
|
|
3
|
+
const _applyMixin = UnionType._applyMixin;
|
|
4
|
+
UnionType._applyMixin = function (target, ...sources) {
|
|
7
5
|
_applyMixin.call(null, target, ...sources);
|
|
8
|
-
|
|
6
|
+
Entity.mixin(target, ...sources);
|
|
9
7
|
};
|
package/esm/index.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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);
|
|
1
|
+
import './augmentation/document-factory.augmentation.js';
|
|
2
|
+
import './augmentation/mapped-type.augmentation.js';
|
|
3
|
+
import './augmentation/union-type.augmentation.js';
|
|
4
|
+
export * from './sqb-adapter.js';
|
|
5
|
+
export * from './sqb-collection-resource.js';
|
|
6
|
+
export * from './sqb-entity-service.js';
|
|
7
|
+
export * from './sqb-singleton-resource.js';
|
package/esm/sqb-adapter.js
CHANGED
|
@@ -1,26 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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"));
|
|
11
|
-
var SQBAdapter;
|
|
1
|
+
import isNil from 'lodash.isnil';
|
|
2
|
+
import omitBy from 'lodash.omitby';
|
|
3
|
+
import { Collection, Singleton } from '@opra/common';
|
|
4
|
+
import { EntityMetadata } from '@sqb/connect';
|
|
5
|
+
import _transformFilter from './transform-filter.js';
|
|
6
|
+
import _transformKeyValues from './transform-key-values.js';
|
|
7
|
+
export var SQBAdapter;
|
|
12
8
|
(function (SQBAdapter) {
|
|
13
|
-
SQBAdapter.transformFilter =
|
|
14
|
-
SQBAdapter.transformKeyValues =
|
|
9
|
+
SQBAdapter.transformFilter = _transformFilter;
|
|
10
|
+
SQBAdapter.transformKeyValues = _transformKeyValues;
|
|
15
11
|
function transformRequest(request) {
|
|
16
12
|
const { resource } = request;
|
|
17
|
-
if (resource instanceof
|
|
13
|
+
if (resource instanceof Collection || resource instanceof Singleton) {
|
|
18
14
|
const { args, operation } = request;
|
|
19
15
|
let options = {};
|
|
20
|
-
const entityMetadata =
|
|
16
|
+
const entityMetadata = EntityMetadata.get(resource.type.ctor);
|
|
21
17
|
if (!entityMetadata)
|
|
22
18
|
throw new Error(`Type class "${resource.type.ctor}" is not an SQB entity`);
|
|
23
|
-
if (resource instanceof
|
|
19
|
+
if (resource instanceof Collection) {
|
|
24
20
|
const primaryIndex = entityMetadata.indexes.find(x => x.primary);
|
|
25
21
|
// Check if resource primary keys are same with entity
|
|
26
22
|
const primaryKeys = [...(primaryIndex && primaryIndex.columns) || []];
|
|
@@ -33,8 +29,8 @@ var SQBAdapter;
|
|
|
33
29
|
options.omit = args.omit?.length ? args.omit : undefined;
|
|
34
30
|
options.include = args.include?.length ? args.include : undefined;
|
|
35
31
|
}
|
|
36
|
-
if (args.filter) {
|
|
37
|
-
options.filter =
|
|
32
|
+
if (resource instanceof Collection && args.filter) {
|
|
33
|
+
options.filter = _transformFilter(resource, args.filter);
|
|
38
34
|
}
|
|
39
35
|
if (operation === 'findMany') {
|
|
40
36
|
options.sort = args.sort?.length ? args.sort : undefined;
|
|
@@ -43,7 +39,7 @@ var SQBAdapter;
|
|
|
43
39
|
options.distinct = args.distinct;
|
|
44
40
|
options.count = args.count;
|
|
45
41
|
}
|
|
46
|
-
options = (
|
|
42
|
+
options = omitBy(options, isNil);
|
|
47
43
|
if (operation === 'create') {
|
|
48
44
|
return {
|
|
49
45
|
method: 'create',
|
|
@@ -52,7 +48,7 @@ var SQBAdapter;
|
|
|
52
48
|
args: [args.data, options]
|
|
53
49
|
};
|
|
54
50
|
}
|
|
55
|
-
if (operation === 'deleteMany' || (operation === 'delete' && resource instanceof
|
|
51
|
+
if (operation === 'deleteMany' || (operation === 'delete' && resource instanceof Singleton)) {
|
|
56
52
|
return {
|
|
57
53
|
method: 'deleteMany',
|
|
58
54
|
options,
|
|
@@ -68,7 +64,7 @@ var SQBAdapter;
|
|
|
68
64
|
};
|
|
69
65
|
}
|
|
70
66
|
if (operation === 'get') {
|
|
71
|
-
if (resource instanceof
|
|
67
|
+
if (resource instanceof Singleton)
|
|
72
68
|
return {
|
|
73
69
|
method: 'findOne',
|
|
74
70
|
options,
|
|
@@ -91,7 +87,7 @@ var SQBAdapter;
|
|
|
91
87
|
out.count = args.count;
|
|
92
88
|
return out;
|
|
93
89
|
}
|
|
94
|
-
if (operation === 'updateMany' || (operation === 'update' && resource instanceof
|
|
90
|
+
if (operation === 'updateMany' || (operation === 'update' && resource instanceof Singleton)) {
|
|
95
91
|
return {
|
|
96
92
|
method: 'updateMany',
|
|
97
93
|
data: args.data,
|
|
@@ -112,4 +108,4 @@ var SQBAdapter;
|
|
|
112
108
|
throw new Error(`Unimplemented request method "${request.operation}"`);
|
|
113
109
|
}
|
|
114
110
|
SQBAdapter.transformRequest = transformRequest;
|
|
115
|
-
})(SQBAdapter
|
|
111
|
+
})(SQBAdapter || (SQBAdapter = {}));
|
|
@@ -1,42 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const common_1 = require("@opra/common");
|
|
6
|
-
const sqb_adapter_js_1 = require("./sqb-adapter.js");
|
|
7
|
-
class SqbCollectionResource {
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { Collection } from '@opra/common';
|
|
3
|
+
import { SQBAdapter } from './sqb-adapter.js';
|
|
4
|
+
export class SqbCollectionResource {
|
|
8
5
|
async create(ctx) {
|
|
9
|
-
const prepared =
|
|
6
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
10
7
|
const service = await this.getService(ctx);
|
|
11
8
|
return service.with(ctx).create(prepared.data, prepared.options);
|
|
12
9
|
}
|
|
13
10
|
async delete(ctx) {
|
|
14
|
-
const prepared =
|
|
11
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
15
12
|
const service = await this.getService(ctx);
|
|
16
13
|
return service.with(ctx).delete(prepared.key, prepared.options);
|
|
17
14
|
}
|
|
18
15
|
async deleteMany(ctx) {
|
|
19
|
-
const prepared =
|
|
16
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
20
17
|
const service = await this.getService(ctx);
|
|
21
18
|
return service.with(ctx).deleteMany(prepared.options);
|
|
22
19
|
}
|
|
23
20
|
async find(ctx) {
|
|
24
|
-
const prepared =
|
|
21
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
25
22
|
const service = await this.getService(ctx);
|
|
26
23
|
return service.with(ctx).find(prepared.key, prepared.options);
|
|
27
24
|
}
|
|
28
25
|
async update(ctx) {
|
|
29
|
-
const prepared =
|
|
26
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
30
27
|
const service = await this.getService(ctx);
|
|
31
28
|
return service.with(ctx).update(prepared.key, prepared.data, prepared.options);
|
|
32
29
|
}
|
|
33
30
|
async updateMany(ctx) {
|
|
34
|
-
const prepared =
|
|
31
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
35
32
|
const service = await this.getService(ctx);
|
|
36
33
|
return service.with(ctx).updateMany(prepared.data, prepared.options);
|
|
37
34
|
}
|
|
38
|
-
async
|
|
39
|
-
const prepared =
|
|
35
|
+
async findMany(ctx) {
|
|
36
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
40
37
|
const service = await this.getService(ctx);
|
|
41
38
|
if (prepared.options.count) {
|
|
42
39
|
const [items, count] = await Promise.all([
|
|
@@ -50,46 +47,45 @@ class SqbCollectionResource {
|
|
|
50
47
|
return service.with(ctx).findAll(prepared.options);
|
|
51
48
|
}
|
|
52
49
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
__decorate([
|
|
51
|
+
Collection.Create(),
|
|
52
|
+
__metadata("design:type", Function),
|
|
53
|
+
__metadata("design:paramtypes", [Object]),
|
|
54
|
+
__metadata("design:returntype", Promise)
|
|
58
55
|
], SqbCollectionResource.prototype, "create", null);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
__decorate([
|
|
57
|
+
Collection.Delete(),
|
|
58
|
+
__metadata("design:type", Function),
|
|
59
|
+
__metadata("design:paramtypes", [Object]),
|
|
60
|
+
__metadata("design:returntype", Promise)
|
|
64
61
|
], SqbCollectionResource.prototype, "delete", null);
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
62
|
+
__decorate([
|
|
63
|
+
Collection.DeleteMany(),
|
|
64
|
+
__metadata("design:type", Function),
|
|
65
|
+
__metadata("design:paramtypes", [Object]),
|
|
66
|
+
__metadata("design:returntype", Promise)
|
|
70
67
|
], SqbCollectionResource.prototype, "deleteMany", null);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
68
|
+
__decorate([
|
|
69
|
+
Collection.Get(),
|
|
70
|
+
__metadata("design:type", Function),
|
|
71
|
+
__metadata("design:paramtypes", [Object]),
|
|
72
|
+
__metadata("design:returntype", Promise)
|
|
76
73
|
], SqbCollectionResource.prototype, "find", null);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
74
|
+
__decorate([
|
|
75
|
+
Collection.Update(),
|
|
76
|
+
__metadata("design:type", Function),
|
|
77
|
+
__metadata("design:paramtypes", [Object]),
|
|
78
|
+
__metadata("design:returntype", Promise)
|
|
82
79
|
], SqbCollectionResource.prototype, "update", null);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
80
|
+
__decorate([
|
|
81
|
+
Collection.UpdateMany(),
|
|
82
|
+
__metadata("design:type", Function),
|
|
83
|
+
__metadata("design:paramtypes", [Object]),
|
|
84
|
+
__metadata("design:returntype", Promise)
|
|
88
85
|
], SqbCollectionResource.prototype, "updateMany", null);
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
], SqbCollectionResource.prototype, "
|
|
95
|
-
exports.SqbCollectionResource = SqbCollectionResource;
|
|
86
|
+
__decorate([
|
|
87
|
+
Collection.FindMany(),
|
|
88
|
+
__metadata("design:type", Function),
|
|
89
|
+
__metadata("design:paramtypes", [Object]),
|
|
90
|
+
__metadata("design:returntype", Promise)
|
|
91
|
+
], SqbCollectionResource.prototype, "findMany", null);
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.SqbEntityService = void 0;
|
|
4
|
-
const connect_1 = require("@sqb/connect");
|
|
5
|
-
class SqbEntityService {
|
|
1
|
+
import { EntityMetadata } from '@sqb/connect';
|
|
2
|
+
export class SqbEntityService {
|
|
6
3
|
constructor(typeClass, options) {
|
|
7
4
|
this.typeClass = typeClass;
|
|
8
|
-
const metadata =
|
|
5
|
+
const metadata = EntityMetadata.get(typeClass);
|
|
9
6
|
if (!metadata)
|
|
10
7
|
throw new TypeError(`Class ${typeClass} is not decorated with $Entity() decorator`);
|
|
11
8
|
this.db = options?.db;
|
|
@@ -173,4 +170,3 @@ class SqbEntityService {
|
|
|
173
170
|
return this.db;
|
|
174
171
|
}
|
|
175
172
|
}
|
|
176
|
-
exports.SqbEntityService = SqbEntityService;
|
|
@@ -1,54 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const common_1 = require("@opra/common");
|
|
6
|
-
const sqb_adapter_js_1 = require("./sqb-adapter.js");
|
|
7
|
-
class SqbSingletonResource {
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { Singleton } from '@opra/common';
|
|
3
|
+
import { SQBAdapter } from './sqb-adapter.js';
|
|
4
|
+
export class SqbSingletonResource {
|
|
8
5
|
async create(ctx) {
|
|
9
|
-
const prepared =
|
|
6
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
10
7
|
const service = await this.getService(ctx);
|
|
11
8
|
return service.with(ctx).create(prepared.data, prepared.options);
|
|
12
9
|
}
|
|
13
10
|
async delete(ctx) {
|
|
14
|
-
const prepared =
|
|
11
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
15
12
|
const service = await this.getService(ctx);
|
|
16
13
|
return !!(await service.with(ctx).deleteMany(prepared.options));
|
|
17
14
|
}
|
|
18
15
|
async get(ctx) {
|
|
19
|
-
const prepared =
|
|
16
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
20
17
|
const service = await this.getService(ctx);
|
|
21
18
|
return service.with(ctx).findOne(prepared.options);
|
|
22
19
|
}
|
|
23
20
|
async update(ctx) {
|
|
24
|
-
const prepared =
|
|
21
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
25
22
|
const service = await this.getService(ctx);
|
|
26
23
|
await service.with(ctx).updateMany(prepared.data, prepared.options);
|
|
27
24
|
return service.with(ctx).findOne(prepared.options);
|
|
28
25
|
}
|
|
29
26
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
__decorate([
|
|
28
|
+
Singleton.Create(),
|
|
29
|
+
__metadata("design:type", Function),
|
|
30
|
+
__metadata("design:paramtypes", [Object]),
|
|
31
|
+
__metadata("design:returntype", Promise)
|
|
35
32
|
], SqbSingletonResource.prototype, "create", null);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
__decorate([
|
|
34
|
+
Singleton.Delete(),
|
|
35
|
+
__metadata("design:type", Function),
|
|
36
|
+
__metadata("design:paramtypes", [Object]),
|
|
37
|
+
__metadata("design:returntype", Promise)
|
|
41
38
|
], SqbSingletonResource.prototype, "delete", null);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
__decorate([
|
|
40
|
+
Singleton.Get(),
|
|
41
|
+
__metadata("design:type", Function),
|
|
42
|
+
__metadata("design:paramtypes", [Object]),
|
|
43
|
+
__metadata("design:returntype", Promise)
|
|
47
44
|
], SqbSingletonResource.prototype, "get", null);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
__decorate([
|
|
46
|
+
Singleton.Update(),
|
|
47
|
+
__metadata("design:type", Function),
|
|
48
|
+
__metadata("design:paramtypes", [Object]),
|
|
49
|
+
__metadata("design:returntype", Promise)
|
|
53
50
|
], SqbSingletonResource.prototype, "update", null);
|
|
54
|
-
exports.SqbSingletonResource = SqbSingletonResource;
|
package/esm/transform-filter.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const sqb = tslib_1.__importStar(require("@sqb/builder"));
|
|
6
|
-
function transformFilter(str) {
|
|
7
|
-
const ast = typeof str === 'string' ? (0, common_1.parseFilter)(str) : str;
|
|
1
|
+
import { ArrayExpression, BooleanLiteral, ComparisonExpression, DateLiteral, LogicalExpression, NullLiteral, NumberLiteral, ParenthesesExpression, parseFilter, QualifiedIdentifier, StringLiteral, TimeLiteral } from '@opra/common';
|
|
2
|
+
import * as sqb from '@sqb/builder';
|
|
3
|
+
export default function transformFilter(resource, str) {
|
|
4
|
+
const ast = typeof str === 'string' ? parseFilter(str) : str;
|
|
8
5
|
if (!ast)
|
|
9
6
|
return;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
7
|
+
resource.normalizeFilter(ast);
|
|
8
|
+
if (ast instanceof ComparisonExpression) {
|
|
9
|
+
if (!(ast.left instanceof QualifiedIdentifier && ast.left.field))
|
|
10
|
+
throw new TypeError(`Unsupported filter query. Left side should be a data field.`);
|
|
11
|
+
const left = sqb.Field(ast.left.value);
|
|
12
|
+
const right = transformFilter(resource, ast.right);
|
|
15
13
|
switch (ast.op) {
|
|
16
14
|
case '=':
|
|
17
15
|
return sqb.Eq(left, right);
|
|
@@ -41,28 +39,27 @@ function transformFilter(str) {
|
|
|
41
39
|
throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
|
|
42
40
|
}
|
|
43
41
|
}
|
|
44
|
-
if (ast instanceof
|
|
42
|
+
if (ast instanceof QualifiedIdentifier) {
|
|
45
43
|
return ast.value;
|
|
46
44
|
}
|
|
47
|
-
if (ast instanceof
|
|
48
|
-
ast instanceof
|
|
49
|
-
ast instanceof
|
|
50
|
-
ast instanceof
|
|
51
|
-
ast instanceof
|
|
52
|
-
ast instanceof
|
|
45
|
+
if (ast instanceof NumberLiteral ||
|
|
46
|
+
ast instanceof StringLiteral ||
|
|
47
|
+
ast instanceof BooleanLiteral ||
|
|
48
|
+
ast instanceof NullLiteral ||
|
|
49
|
+
ast instanceof DateLiteral ||
|
|
50
|
+
ast instanceof TimeLiteral) {
|
|
53
51
|
return ast.value;
|
|
54
52
|
}
|
|
55
|
-
if (ast instanceof
|
|
56
|
-
return ast.items.map(transformFilter);
|
|
53
|
+
if (ast instanceof ArrayExpression) {
|
|
54
|
+
return ast.items.map(i => transformFilter(resource, i));
|
|
57
55
|
}
|
|
58
|
-
if (ast instanceof
|
|
56
|
+
if (ast instanceof LogicalExpression) {
|
|
59
57
|
if (ast.op === 'or')
|
|
60
|
-
return sqb.Or(...ast.items.map(transformFilter));
|
|
61
|
-
return sqb.And(...ast.items.map(transformFilter));
|
|
58
|
+
return sqb.Or(...ast.items.map((i => transformFilter(resource, i))));
|
|
59
|
+
return sqb.And(...ast.items.map((i => transformFilter(resource, i))));
|
|
62
60
|
}
|
|
63
|
-
if (ast instanceof
|
|
64
|
-
return transformFilter(ast.expression);
|
|
61
|
+
if (ast instanceof ParenthesesExpression) {
|
|
62
|
+
return transformFilter(resource, ast.expression);
|
|
65
63
|
}
|
|
66
64
|
throw new Error(`${ast.type} is not implemented yet`);
|
|
67
65
|
}
|
|
68
|
-
exports.default = transformFilter;
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
function transformKeyValues(resource, keyValues) {
|
|
1
|
+
export default function transformKeyValues(resource, keyValues) {
|
|
4
2
|
const { primaryKey } = resource;
|
|
5
3
|
if (primaryKey.length > 1) {
|
|
6
4
|
const query = {};
|
|
@@ -11,4 +9,3 @@ function transformKeyValues(resource, keyValues) {
|
|
|
11
9
|
}
|
|
12
10
|
return { [primaryKey[0]]: keyValues };
|
|
13
11
|
}
|
|
14
|
-
exports.default = transformKeyValues;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/sqb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "Opra SQB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"ts-gems": "^2.3.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@opra/core": "^0.
|
|
40
|
+
"@opra/core": "^0.17.0",
|
|
41
41
|
"@sqb/connect": ">= 4.8.2"
|
|
42
42
|
},
|
|
43
43
|
"type": "module",
|
|
@@ -9,6 +9,6 @@ export declare abstract class SqbCollectionResource<T> {
|
|
|
9
9
|
find(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
10
10
|
update(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
11
11
|
updateMany(ctx: RequestContext): Promise<number>;
|
|
12
|
-
|
|
12
|
+
findMany(ctx: RequestContext): Promise<PartialOutput<T>[]>;
|
|
13
13
|
abstract getService(ctx: RequestContext): SqbEntityService<T> | Promise<SqbEntityService<T>>;
|
|
14
14
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Expression } from '@opra/common';
|
|
2
|
-
export default function transformFilter(str: string | Expression | undefined): any;
|
|
1
|
+
import { Collection, Expression } from '@opra/common';
|
|
2
|
+
export default function transformFilter(resource: Collection, str: string | Expression | undefined): any;
|