@opra/sqb 0.33.13 → 1.0.0-alpha.2

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.
Files changed (43) hide show
  1. package/cjs/{transform-filter.js → adapter-utils/parse-filter.js} +37 -12
  2. package/cjs/augmentation/datatype-factory.augmentation.js +75 -0
  3. package/cjs/augmentation/mixin-type.augmentation.js +5 -3
  4. package/cjs/index.js +3 -4
  5. package/cjs/sqb-adapter.js +66 -100
  6. package/cjs/sqb-collection-service.js +297 -0
  7. package/cjs/sqb-entity-service.js +444 -25
  8. package/cjs/sqb-singleton-service.js +180 -0
  9. package/esm/{transform-filter.js → adapter-utils/parse-filter.js} +36 -11
  10. package/esm/augmentation/datatype-factory.augmentation.js +73 -0
  11. package/esm/augmentation/mapped-type.augmentation.js +1 -1
  12. package/esm/augmentation/mixin-type.augmentation.js +6 -4
  13. package/esm/index.js +3 -4
  14. package/esm/sqb-adapter.js +66 -100
  15. package/esm/sqb-collection-service.js +293 -0
  16. package/esm/sqb-entity-service.js +444 -25
  17. package/esm/sqb-singleton-service.js +176 -0
  18. package/package.json +10 -9
  19. package/types/adapter-utils/parse-filter.d.ts +10 -0
  20. package/types/index.d.ts +3 -4
  21. package/types/sqb-adapter.d.ts +10 -8
  22. package/types/sqb-collection-service.d.ts +233 -0
  23. package/types/sqb-entity-service.d.ts +418 -18
  24. package/types/sqb-singleton-service.d.ts +137 -0
  25. package/cjs/augmentation/api-document-factory.augmentation.js +0 -20
  26. package/cjs/augmentation/type-document-factory.augmentation.js +0 -99
  27. package/cjs/sqb-collection.js +0 -80
  28. package/cjs/sqb-entity-service-base.js +0 -170
  29. package/cjs/sqb-singleton.js +0 -44
  30. package/cjs/transform-key-values.js +0 -14
  31. package/esm/augmentation/api-document-factory.augmentation.js +0 -18
  32. package/esm/augmentation/type-document-factory.augmentation.js +0 -97
  33. package/esm/sqb-collection.js +0 -76
  34. package/esm/sqb-entity-service-base.js +0 -166
  35. package/esm/sqb-singleton.js +0 -40
  36. package/esm/transform-key-values.js +0 -11
  37. package/types/augmentation/type-document-factory.augmentation.d.ts +0 -1
  38. package/types/sqb-collection.d.ts +0 -31
  39. package/types/sqb-entity-service-base.d.ts +0 -31
  40. package/types/sqb-singleton.d.ts +0 -18
  41. package/types/transform-filter.d.ts +0 -3
  42. package/types/transform-key-values.d.ts +0 -3
  43. /package/types/augmentation/{api-document-factory.augmentation.d.ts → datatype-factory.augmentation.d.ts} +0 -0
@@ -1,99 +0,0 @@
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 _prepareDataTypeInitArguments = common_1.TypeDocumentFactory.prototype.prepareDataTypeInitArguments;
7
- // @ts-ignore
8
- common_1.TypeDocumentFactory.prototype.prepareDataTypeInitArguments = async function (schema, ctor) {
9
- if (ctor && schema.kind === 'ComplexType' && schema.fields) {
10
- const sqbMeta = connect_1.EntityMetadata.get(ctor);
11
- for (const [fieldName, fieldSchema] of Object.entries(schema.fields)) {
12
- const sqbField = sqbMeta && connect_1.EntityMetadata.getField(sqbMeta, fieldName);
13
- if (!sqbField)
14
- continue;
15
- const detectType = !fieldSchema.type;
16
- if ((0, connect_1.isAssociationField)(sqbField)) {
17
- if (!fieldSchema.type) {
18
- const trg = await sqbField.association.resolveTarget();
19
- if (trg) {
20
- fieldSchema.type = await this.importDataType(trg.ctor);
21
- }
22
- }
23
- }
24
- else if (sqbField.kind === 'column') {
25
- if (typeof sqbField.enum === 'object')
26
- fieldSchema.enum = sqbField.enum;
27
- if (fieldSchema.required == null && sqbField.notNull)
28
- fieldSchema.required = true;
29
- if (sqbField.type && Reflect.hasMetadata(common_1.DATATYPE_METADATA, sqbField.type)) {
30
- fieldSchema.type = sqbField.type;
31
- }
32
- switch (sqbField.dataType) {
33
- case connect_1.DataType.GUID:
34
- if (!fieldSchema.type || (detectType && fieldSchema.type === 'string'))
35
- fieldSchema.type = 'uuid';
36
- break;
37
- case connect_1.DataType.JSON:
38
- if (!fieldSchema.type || (detectType && fieldSchema.type === 'any'))
39
- fieldSchema.type = 'object';
40
- break;
41
- case connect_1.DataType.INTEGER:
42
- case connect_1.DataType.SMALLINT:
43
- if (!fieldSchema.type || (detectType && fieldSchema.type === 'number'))
44
- fieldSchema.type = 'integer';
45
- break;
46
- case connect_1.DataType.BIGINT:
47
- if (!fieldSchema.type || (detectType && fieldSchema.type === 'number'))
48
- fieldSchema.type = 'bigint';
49
- break;
50
- case connect_1.DataType.DATE:
51
- if (!fieldSchema.type || (detectType && (fieldSchema.type === 'datetime' || fieldSchema.type === 'string')))
52
- fieldSchema.type = 'date';
53
- break;
54
- case connect_1.DataType.TIMESTAMPTZ:
55
- if (!fieldSchema.type || (detectType && (fieldSchema.type === 'datetime' || fieldSchema.type === 'string')))
56
- fieldSchema.type = 'timestamptz';
57
- break;
58
- case connect_1.DataType.TIME:
59
- if (!fieldSchema.type || (detectType && (fieldSchema.type === 'datetime' || fieldSchema.type === 'string')))
60
- fieldSchema.type = 'time';
61
- break;
62
- case connect_1.DataType.BINARY:
63
- if (!fieldSchema.type || (detectType && fieldSchema.type === 'string'))
64
- fieldSchema.type = 'base64';
65
- break;
66
- }
67
- if (!fieldSchema.type) {
68
- switch (sqbField.dataType) {
69
- case connect_1.DataType.BOOL:
70
- fieldSchema.type = 'boolean';
71
- break;
72
- case connect_1.DataType.CHAR:
73
- case connect_1.DataType.VARCHAR:
74
- case connect_1.DataType.TEXT:
75
- fieldSchema.type = 'string';
76
- break;
77
- case connect_1.DataType.FLOAT:
78
- case connect_1.DataType.DOUBLE:
79
- case connect_1.DataType.NUMBER:
80
- fieldSchema.type = 'number';
81
- break;
82
- case connect_1.DataType.TIMESTAMP:
83
- fieldSchema.type = 'datetime';
84
- break;
85
- }
86
- }
87
- if (sqbField.notNull && fieldSchema.required === undefined)
88
- fieldSchema.required = sqbField.notNull;
89
- if (sqbField.exclusive && fieldSchema.exclusive === undefined)
90
- fieldSchema.exclusive = sqbField.exclusive;
91
- if (sqbField.default !== undefined && fieldSchema.default === undefined)
92
- fieldSchema.default = sqbField.default;
93
- }
94
- if (sqbField.exclusive && fieldSchema.exclusive === undefined)
95
- fieldSchema.exclusive = sqbField.exclusive;
96
- }
97
- }
98
- return await _prepareDataTypeInitArguments.call(this, schema, ctor);
99
- };
@@ -1,80 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SqbCollection = void 0;
4
- const sqb_adapter_js_1 = require("./sqb-adapter.js");
5
- // noinspection TypeScriptAbstractClassConstructorCanBeMadeProtected
6
- class SqbCollection {
7
- constructor(options) {
8
- this.defaultLimit = options?.defaultLimit || 100;
9
- }
10
- async create(ctx) {
11
- const prepared = await this._prepare(ctx);
12
- return this._create(ctx, prepared);
13
- }
14
- async delete(ctx) {
15
- const prepared = await this._prepare(ctx);
16
- return this._delete(ctx, prepared);
17
- }
18
- async deleteMany(ctx) {
19
- const prepared = await this._prepare(ctx);
20
- return this._deleteMany(ctx, prepared);
21
- }
22
- async get(ctx) {
23
- const prepared = await this._prepare(ctx);
24
- return this._get(ctx, prepared);
25
- }
26
- async update(ctx) {
27
- const prepared = await this._prepare(ctx);
28
- return this._update(ctx, prepared);
29
- }
30
- async updateMany(ctx) {
31
- const prepared = await this._prepare(ctx);
32
- return this._updateMany(ctx, prepared);
33
- }
34
- async findMany(ctx) {
35
- const prepared = await this._prepare(ctx);
36
- return this._findMany(ctx, prepared);
37
- }
38
- async _create(ctx, prepared) {
39
- const service = await this.getService(ctx);
40
- return service.create(prepared.data, prepared.options);
41
- }
42
- async _delete(ctx, prepared) {
43
- const service = await this.getService(ctx);
44
- return service.delete(prepared.key, prepared.options);
45
- }
46
- async _deleteMany(ctx, prepared) {
47
- const service = await this.getService(ctx);
48
- return service.deleteMany(prepared.options);
49
- }
50
- async _get(ctx, prepared) {
51
- const service = await this.getService(ctx);
52
- return service.find(prepared.key, prepared.options);
53
- }
54
- async _update(ctx, prepared) {
55
- const service = await this.getService(ctx);
56
- return service.update(prepared.key, prepared.data, prepared.options);
57
- }
58
- async _updateMany(ctx, prepared) {
59
- const service = await this.getService(ctx);
60
- return service.updateMany(prepared.data, prepared.options);
61
- }
62
- async _findMany(ctx, prepared) {
63
- const service = await this.getService(ctx);
64
- if (prepared.options.count) {
65
- const [items, count] = await Promise.all([
66
- service.findMany(prepared.options),
67
- service.count(prepared.options)
68
- ]);
69
- ctx.response.totalMatches = count;
70
- return items;
71
- }
72
- else
73
- return service.findMany(prepared.options);
74
- }
75
- async _prepare(ctx) {
76
- const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
77
- return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
78
- }
79
- }
80
- exports.SqbCollection = SqbCollection;
@@ -1,170 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SqbEntityServiceBase = void 0;
4
- const core_1 = require("@opra/core");
5
- const connect_1 = require("@sqb/connect");
6
- class SqbEntityServiceBase extends core_1.ApiService {
7
- constructor(typeClass, options) {
8
- super();
9
- this.typeClass = typeClass;
10
- const metadata = connect_1.EntityMetadata.get(typeClass);
11
- if (!metadata)
12
- throw new TypeError(`Class ${typeClass} is not decorated with $Entity() decorator`);
13
- this.db = options?.db;
14
- this.defaultLimit = options?.defaultLimit || 10;
15
- }
16
- async _count(options) {
17
- const conn = await this.getConnection();
18
- const repo = conn.getRepository(this.typeClass);
19
- try {
20
- return await repo.count(options);
21
- }
22
- catch (e) {
23
- await this._onError(e);
24
- throw e;
25
- }
26
- }
27
- async _create(data, options) {
28
- const conn = await this.getConnection();
29
- const repo = conn.getRepository(this.typeClass);
30
- let out;
31
- try {
32
- out = await repo.create(data, options);
33
- }
34
- catch (e) {
35
- await this._onError(e);
36
- throw e;
37
- }
38
- if (out && this.transformData)
39
- out = this.transformData(out);
40
- if (!out)
41
- throw new Error('"create" endpoint returned no result!');
42
- return out;
43
- }
44
- async _delete(keyValue, options) {
45
- const conn = await this.getConnection();
46
- const repo = conn.getRepository(this.typeClass);
47
- try {
48
- return await repo.delete(keyValue, options) ? 1 : 0;
49
- }
50
- catch (e) {
51
- await this._onError(e);
52
- throw e;
53
- }
54
- }
55
- async _deleteMany(options) {
56
- const conn = await this.getConnection();
57
- const repo = conn.getRepository(this.typeClass);
58
- try {
59
- return await repo.deleteMany(options);
60
- }
61
- catch (e) {
62
- await this._onError(e);
63
- throw e;
64
- }
65
- }
66
- async _find(keyValue, options) {
67
- const conn = await this.getConnection();
68
- const repo = conn.getRepository(this.typeClass);
69
- let out;
70
- try {
71
- out = await repo.find(keyValue, options);
72
- }
73
- catch (e) {
74
- await this._onError(e);
75
- throw e;
76
- }
77
- if (out && this.transformData)
78
- out = this.transformData(out);
79
- return out;
80
- }
81
- async _findOne(options) {
82
- const conn = await this.getConnection();
83
- const repo = conn.getRepository(this.typeClass);
84
- let out;
85
- try {
86
- out = await repo.findOne(options);
87
- }
88
- catch (e) {
89
- await this._onError(e);
90
- throw e;
91
- }
92
- if (out && this.transformData)
93
- out = this.transformData(out);
94
- return out;
95
- }
96
- async _findMany(options) {
97
- const conn = await this.getConnection();
98
- const repo = conn.getRepository(this.typeClass);
99
- let items;
100
- try {
101
- items = await repo.findMany(options);
102
- }
103
- catch (e) {
104
- await this._onError(e);
105
- throw e;
106
- }
107
- if (items.length && this.transformData) {
108
- const newItems = [];
109
- for (const item of items) {
110
- const v = this.transformData(item);
111
- if (v)
112
- newItems.push(v);
113
- }
114
- return newItems;
115
- }
116
- return items;
117
- }
118
- async _exists(options) {
119
- const conn = await this.getConnection();
120
- const repo = conn.getRepository(this.typeClass);
121
- try {
122
- return await repo.exists(options);
123
- }
124
- catch (e) {
125
- await this._onError(e);
126
- throw e;
127
- }
128
- }
129
- async _update(keyValue, data, options) {
130
- const conn = await this.getConnection();
131
- const repo = conn.getRepository(this.typeClass);
132
- let out;
133
- try {
134
- out = await repo.update(keyValue, data, options);
135
- }
136
- catch (e) {
137
- await this._onError(e);
138
- throw e;
139
- }
140
- if (out && this.transformData)
141
- out = this.transformData(out);
142
- return out;
143
- }
144
- async _updateMany(data, options) {
145
- const conn = await this.getConnection();
146
- const repo = conn.getRepository(this.typeClass);
147
- try {
148
- return await repo.updateMany(data, options);
149
- }
150
- catch (e) {
151
- await this._onError(e);
152
- throw e;
153
- }
154
- }
155
- for(context, overwriteProperties, overwriteContext) {
156
- return super.for(context, overwriteProperties, overwriteContext);
157
- }
158
- async _onError(error) {
159
- if (this.onError)
160
- await this.onError(error);
161
- }
162
- getConnection() {
163
- if (!this.context)
164
- throw new Error(`Context not set!`);
165
- if (!this.db)
166
- throw new Error(`Database not set!`);
167
- return this.db;
168
- }
169
- }
170
- exports.SqbEntityServiceBase = SqbEntityServiceBase;
@@ -1,44 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SqbSingleton = void 0;
4
- const sqb_adapter_js_1 = require("./sqb-adapter.js");
5
- class SqbSingleton {
6
- async create(ctx) {
7
- const prepared = await this._prepare(ctx);
8
- return this._create(ctx, prepared);
9
- }
10
- async delete(ctx) {
11
- const prepared = await this._prepare(ctx);
12
- return this._delete(ctx, prepared);
13
- }
14
- async get(ctx) {
15
- const prepared = await this._prepare(ctx);
16
- return this._get(ctx, prepared);
17
- }
18
- async update(ctx) {
19
- const prepared = await this._prepare(ctx);
20
- return this._update(ctx, prepared);
21
- }
22
- async _create(ctx, prepared) {
23
- const service = await this.getService(ctx);
24
- return service.create(prepared.data, prepared.options);
25
- }
26
- async _delete(ctx, prepared) {
27
- const service = await this.getService(ctx);
28
- return await service.deleteMany(prepared.options);
29
- }
30
- async _get(ctx, prepared) {
31
- const service = await this.getService(ctx);
32
- return service.findOne(prepared.options);
33
- }
34
- async _update(ctx, prepared) {
35
- const service = await this.getService(ctx);
36
- await service.updateMany(prepared.data, prepared.options);
37
- return service.findOne(prepared.options);
38
- }
39
- async _prepare(ctx) {
40
- const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
41
- return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
42
- }
43
- }
44
- exports.SqbSingleton = SqbSingleton;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- function transformKeyValues(resource, keyValues) {
4
- const { primaryKey } = resource;
5
- if (primaryKey.length > 1) {
6
- const query = {};
7
- primaryKey.forEach((k, i) => {
8
- query[k] = typeof keyValues === 'object' ? keyValues[k] : keyValues[i];
9
- });
10
- return query;
11
- }
12
- return { [primaryKey[0]]: keyValues };
13
- }
14
- exports.default = transformKeyValues;
@@ -1,18 +0,0 @@
1
- import { ApiDocumentFactory } from '@opra/common';
2
- import { EntityMetadata } from '@sqb/connect';
3
- // @ts-ignore
4
- const _createResource = ApiDocumentFactory.prototype.createResource;
5
- // @ts-ignore
6
- ApiDocumentFactory.prototype.createResource = function (container, initArguments) {
7
- // Determine primaryKey if not defined
8
- if (initArguments.kind === 'Collection' && !initArguments.primaryKey && initArguments.type.ctor) {
9
- const entityMetadata = EntityMetadata.get(initArguments.type.ctor);
10
- if (entityMetadata?.indexes) {
11
- const primaryIndex = entityMetadata.indexes.find(x => x.primary);
12
- if (primaryIndex) {
13
- initArguments.primaryKey = primaryIndex.columns;
14
- }
15
- }
16
- }
17
- return _createResource.call(this, container, initArguments);
18
- };
@@ -1,97 +0,0 @@
1
- import { DATATYPE_METADATA, TypeDocumentFactory } from "@opra/common";
2
- import { DataType as SqbDataType, EntityMetadata, isAssociationField } from '@sqb/connect';
3
- // @ts-ignore
4
- const _prepareDataTypeInitArguments = TypeDocumentFactory.prototype.prepareDataTypeInitArguments;
5
- // @ts-ignore
6
- TypeDocumentFactory.prototype.prepareDataTypeInitArguments = async function (schema, ctor) {
7
- if (ctor && schema.kind === 'ComplexType' && schema.fields) {
8
- const sqbMeta = EntityMetadata.get(ctor);
9
- for (const [fieldName, fieldSchema] of Object.entries(schema.fields)) {
10
- const sqbField = sqbMeta && EntityMetadata.getField(sqbMeta, fieldName);
11
- if (!sqbField)
12
- continue;
13
- const detectType = !fieldSchema.type;
14
- if (isAssociationField(sqbField)) {
15
- if (!fieldSchema.type) {
16
- const trg = await sqbField.association.resolveTarget();
17
- if (trg) {
18
- fieldSchema.type = await this.importDataType(trg.ctor);
19
- }
20
- }
21
- }
22
- else if (sqbField.kind === 'column') {
23
- if (typeof sqbField.enum === 'object')
24
- fieldSchema.enum = sqbField.enum;
25
- if (fieldSchema.required == null && sqbField.notNull)
26
- fieldSchema.required = true;
27
- if (sqbField.type && Reflect.hasMetadata(DATATYPE_METADATA, sqbField.type)) {
28
- fieldSchema.type = sqbField.type;
29
- }
30
- switch (sqbField.dataType) {
31
- case SqbDataType.GUID:
32
- if (!fieldSchema.type || (detectType && fieldSchema.type === 'string'))
33
- fieldSchema.type = 'uuid';
34
- break;
35
- case SqbDataType.JSON:
36
- if (!fieldSchema.type || (detectType && fieldSchema.type === 'any'))
37
- fieldSchema.type = 'object';
38
- break;
39
- case SqbDataType.INTEGER:
40
- case SqbDataType.SMALLINT:
41
- if (!fieldSchema.type || (detectType && fieldSchema.type === 'number'))
42
- fieldSchema.type = 'integer';
43
- break;
44
- case SqbDataType.BIGINT:
45
- if (!fieldSchema.type || (detectType && fieldSchema.type === 'number'))
46
- fieldSchema.type = 'bigint';
47
- break;
48
- case SqbDataType.DATE:
49
- if (!fieldSchema.type || (detectType && (fieldSchema.type === 'datetime' || fieldSchema.type === 'string')))
50
- fieldSchema.type = 'date';
51
- break;
52
- case SqbDataType.TIMESTAMPTZ:
53
- if (!fieldSchema.type || (detectType && (fieldSchema.type === 'datetime' || fieldSchema.type === 'string')))
54
- fieldSchema.type = 'timestamptz';
55
- break;
56
- case SqbDataType.TIME:
57
- if (!fieldSchema.type || (detectType && (fieldSchema.type === 'datetime' || fieldSchema.type === 'string')))
58
- fieldSchema.type = 'time';
59
- break;
60
- case SqbDataType.BINARY:
61
- if (!fieldSchema.type || (detectType && fieldSchema.type === 'string'))
62
- fieldSchema.type = 'base64';
63
- break;
64
- }
65
- if (!fieldSchema.type) {
66
- switch (sqbField.dataType) {
67
- case SqbDataType.BOOL:
68
- fieldSchema.type = 'boolean';
69
- break;
70
- case SqbDataType.CHAR:
71
- case SqbDataType.VARCHAR:
72
- case SqbDataType.TEXT:
73
- fieldSchema.type = 'string';
74
- break;
75
- case SqbDataType.FLOAT:
76
- case SqbDataType.DOUBLE:
77
- case SqbDataType.NUMBER:
78
- fieldSchema.type = 'number';
79
- break;
80
- case SqbDataType.TIMESTAMP:
81
- fieldSchema.type = 'datetime';
82
- break;
83
- }
84
- }
85
- if (sqbField.notNull && fieldSchema.required === undefined)
86
- fieldSchema.required = sqbField.notNull;
87
- if (sqbField.exclusive && fieldSchema.exclusive === undefined)
88
- fieldSchema.exclusive = sqbField.exclusive;
89
- if (sqbField.default !== undefined && fieldSchema.default === undefined)
90
- fieldSchema.default = sqbField.default;
91
- }
92
- if (sqbField.exclusive && fieldSchema.exclusive === undefined)
93
- fieldSchema.exclusive = sqbField.exclusive;
94
- }
95
- }
96
- return await _prepareDataTypeInitArguments.call(this, schema, ctor);
97
- };
@@ -1,76 +0,0 @@
1
- import { SQBAdapter } from './sqb-adapter.js';
2
- // noinspection TypeScriptAbstractClassConstructorCanBeMadeProtected
3
- export class SqbCollection {
4
- constructor(options) {
5
- this.defaultLimit = options?.defaultLimit || 100;
6
- }
7
- async create(ctx) {
8
- const prepared = await this._prepare(ctx);
9
- return this._create(ctx, prepared);
10
- }
11
- async delete(ctx) {
12
- const prepared = await this._prepare(ctx);
13
- return this._delete(ctx, prepared);
14
- }
15
- async deleteMany(ctx) {
16
- const prepared = await this._prepare(ctx);
17
- return this._deleteMany(ctx, prepared);
18
- }
19
- async get(ctx) {
20
- const prepared = await this._prepare(ctx);
21
- return this._get(ctx, prepared);
22
- }
23
- async update(ctx) {
24
- const prepared = await this._prepare(ctx);
25
- return this._update(ctx, prepared);
26
- }
27
- async updateMany(ctx) {
28
- const prepared = await this._prepare(ctx);
29
- return this._updateMany(ctx, prepared);
30
- }
31
- async findMany(ctx) {
32
- const prepared = await this._prepare(ctx);
33
- return this._findMany(ctx, prepared);
34
- }
35
- async _create(ctx, prepared) {
36
- const service = await this.getService(ctx);
37
- return service.create(prepared.data, prepared.options);
38
- }
39
- async _delete(ctx, prepared) {
40
- const service = await this.getService(ctx);
41
- return service.delete(prepared.key, prepared.options);
42
- }
43
- async _deleteMany(ctx, prepared) {
44
- const service = await this.getService(ctx);
45
- return service.deleteMany(prepared.options);
46
- }
47
- async _get(ctx, prepared) {
48
- const service = await this.getService(ctx);
49
- return service.find(prepared.key, prepared.options);
50
- }
51
- async _update(ctx, prepared) {
52
- const service = await this.getService(ctx);
53
- return service.update(prepared.key, prepared.data, prepared.options);
54
- }
55
- async _updateMany(ctx, prepared) {
56
- const service = await this.getService(ctx);
57
- return service.updateMany(prepared.data, prepared.options);
58
- }
59
- async _findMany(ctx, prepared) {
60
- const service = await this.getService(ctx);
61
- if (prepared.options.count) {
62
- const [items, count] = await Promise.all([
63
- service.findMany(prepared.options),
64
- service.count(prepared.options)
65
- ]);
66
- ctx.response.totalMatches = count;
67
- return items;
68
- }
69
- else
70
- return service.findMany(prepared.options);
71
- }
72
- async _prepare(ctx) {
73
- const prepared = SQBAdapter.transformRequest(ctx.request);
74
- return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
75
- }
76
- }