@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.
Files changed (41) hide show
  1. package/cjs/augmentation/document-factory.augmentation.js +114 -0
  2. package/cjs/augmentation/mapped-type.augmentation.js +14 -0
  3. package/cjs/augmentation/union-type.augmentation.js +9 -0
  4. package/cjs/index.js +6 -6
  5. package/cjs/sqb-adapter.js +75 -83
  6. package/cjs/sqb-collection-resource.js +95 -0
  7. package/cjs/sqb-entity-service.js +176 -0
  8. package/cjs/sqb-singleton-resource.js +54 -0
  9. package/cjs/{convert-filter.js → transform-filter.js} +10 -13
  10. package/cjs/transform-key-values.js +14 -0
  11. package/esm/augmentation/document-factory.augmentation.js +114 -0
  12. package/esm/augmentation/mapped-type.augmentation.js +14 -0
  13. package/esm/augmentation/union-type.augmentation.js +9 -0
  14. package/esm/index.js +10 -7
  15. package/esm/sqb-adapter.js +83 -87
  16. package/esm/sqb-collection-resource.js +95 -0
  17. package/esm/sqb-entity-service.js +176 -0
  18. package/esm/sqb-singleton-resource.js +54 -0
  19. package/esm/transform-filter.js +68 -0
  20. package/esm/transform-key-values.js +14 -0
  21. package/package.json +13 -12
  22. package/types/augmentation/document-factory.augmentation.d.ts +1 -0
  23. package/types/augmentation/mapped-type.augmentation.d.ts +1 -0
  24. package/types/augmentation/union-type.augmentation.d.ts +1 -0
  25. package/types/index.d.ts +7 -0
  26. package/types/sqb-adapter.d.ts +14 -0
  27. package/types/sqb-collection-resource.d.ts +14 -0
  28. package/types/sqb-entity-service.d.ts +31 -0
  29. package/types/sqb-singleton-resource.d.ts +11 -0
  30. package/types/transform-filter.d.ts +2 -0
  31. package/types/transform-key-values.d.ts +3 -0
  32. package/cjs/base-entity-resource.js +0 -39
  33. package/cjs/base-entity-service.js +0 -130
  34. package/esm/base-entity-resource.d.ts +0 -13
  35. package/esm/base-entity-resource.js +0 -35
  36. package/esm/base-entity-service.d.ts +0 -30
  37. package/esm/base-entity-service.js +0 -126
  38. package/esm/convert-filter.d.ts +0 -2
  39. package/esm/convert-filter.js +0 -66
  40. package/esm/index.d.ts +0 -8
  41. package/esm/sqb-adapter.d.ts +0 -10
@@ -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;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SqbSingletonResource = 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 SqbSingletonResource {
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 !!(await service.deleteMany(ctx, prepared.options));
17
+ }
18
+ async get(ctx) {
19
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
20
+ const service = await this.getService(ctx);
21
+ return service.findOne(ctx, prepared.options);
22
+ }
23
+ async update(ctx) {
24
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
25
+ const service = await this.getService(ctx);
26
+ await service.updateMany(ctx, prepared.data, prepared.options);
27
+ return service.findOne(ctx, prepared.options);
28
+ }
29
+ }
30
+ tslib_1.__decorate([
31
+ common_1.Singleton.Create(),
32
+ tslib_1.__metadata("design:type", Function),
33
+ tslib_1.__metadata("design:paramtypes", [Object]),
34
+ tslib_1.__metadata("design:returntype", Promise)
35
+ ], SqbSingletonResource.prototype, "create", null);
36
+ tslib_1.__decorate([
37
+ common_1.Singleton.Delete(),
38
+ tslib_1.__metadata("design:type", Function),
39
+ tslib_1.__metadata("design:paramtypes", [Object]),
40
+ tslib_1.__metadata("design:returntype", Promise)
41
+ ], SqbSingletonResource.prototype, "delete", null);
42
+ tslib_1.__decorate([
43
+ common_1.Singleton.Get(),
44
+ tslib_1.__metadata("design:type", Function),
45
+ tslib_1.__metadata("design:paramtypes", [Object]),
46
+ tslib_1.__metadata("design:returntype", Promise)
47
+ ], SqbSingletonResource.prototype, "get", null);
48
+ tslib_1.__decorate([
49
+ common_1.Singleton.Update(),
50
+ tslib_1.__metadata("design:type", Function),
51
+ tslib_1.__metadata("design:paramtypes", [Object]),
52
+ tslib_1.__metadata("design:returntype", Promise)
53
+ ], SqbSingletonResource.prototype, "update", null);
54
+ exports.SqbSingletonResource = SqbSingletonResource;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const common_1 = require("@opra/common");
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;
8
+ if (!ast)
9
+ return;
10
+ if (ast instanceof common_1.ComparisonExpression) {
11
+ const left = ast.left instanceof common_1.QualifiedIdentifier
12
+ ? sqb.Field(ast.left.value)
13
+ : transformFilter(ast.left);
14
+ const right = transformFilter(ast.right);
15
+ switch (ast.op) {
16
+ case '=':
17
+ return sqb.Eq(left, right);
18
+ case '!=':
19
+ return sqb.Ne(left, right);
20
+ case '>':
21
+ return sqb.Gt(left, right);
22
+ case '>=':
23
+ return sqb.Gte(left, right);
24
+ case '<':
25
+ return sqb.Lt(left, right);
26
+ case '<=':
27
+ return sqb.Lte(left, right);
28
+ case 'in':
29
+ return sqb.In(left, right);
30
+ case '!in':
31
+ return sqb.Nin(left, right);
32
+ case 'like':
33
+ return sqb.Like(left, right);
34
+ case 'ilike':
35
+ return sqb.Ilike(left, right);
36
+ case '!like':
37
+ return sqb.NotLike(left, right);
38
+ case '!ilike':
39
+ return sqb.NotILike(left, right);
40
+ default:
41
+ throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
42
+ }
43
+ }
44
+ if (ast instanceof common_1.QualifiedIdentifier) {
45
+ return ast.value;
46
+ }
47
+ if (ast instanceof common_1.NumberLiteral ||
48
+ ast instanceof common_1.StringLiteral ||
49
+ ast instanceof common_1.BooleanLiteral ||
50
+ ast instanceof common_1.NullLiteral ||
51
+ ast instanceof common_1.DateLiteral ||
52
+ ast instanceof common_1.TimeLiteral) {
53
+ return ast.value;
54
+ }
55
+ if (ast instanceof common_1.ArrayExpression) {
56
+ return ast.items.map(transformFilter);
57
+ }
58
+ if (ast instanceof common_1.LogicalExpression) {
59
+ if (ast.op === 'or')
60
+ return sqb.Or(...ast.items.map(transformFilter));
61
+ return sqb.And(...ast.items.map(transformFilter));
62
+ }
63
+ if (ast instanceof common_1.ParenthesesExpression) {
64
+ return transformFilter(ast.expression);
65
+ }
66
+ throw new Error(`${ast.type} is not implemented yet`);
67
+ }
68
+ exports.default = transformFilter;
@@ -0,0 +1,14 @@
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/sqb",
3
- "version": "0.14.0",
3
+ "version": "0.16.0",
4
4
  "description": "Opra SQB adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -17,8 +17,8 @@
17
17
  "build:esm": "tsc -b tsconfig-build-esm.json",
18
18
  "postbuild": "cp README.md package.json ../../LICENSE ../../build/sqb && cp ../../package.cjs.json ../../build/sqb/cjs/package.json",
19
19
  "lint": "eslint .",
20
- "test": "NODE_OPTIONS=--experimental-vm-modules npx jest",
21
- "cover": "NODE_OPTIONS=--experimental-vm-modules npx jest --collect-coverage",
20
+ "test": "jest",
21
+ "cover": "jest --collect-coverage",
22
22
  "clean": "npm run clean:src && npm run clean:dist && npm run clean:cover",
23
23
  "clean:src": "ts-cleanup -s src --all && ts-cleanup -s test --all",
24
24
  "clean:dist": "rimraf ../../build/sqb",
@@ -26,22 +26,22 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "lodash.isnil": "^4.0.0",
29
- "lodash.omitby": "^4.6.0",
30
- "ts-gems": "^2.3.0"
29
+ "lodash.omitby": "^4.6.0"
31
30
  },
32
31
  "devDependencies": {
33
32
  "@faker-js/faker": "^7.6.0",
34
- "@sqb/builder": "^4.5.5",
35
- "@sqb/connect": "^4.5.5",
36
- "@sqb/postgres": "^4.5.5",
37
- "postgresql-client": "^2.5.2"
33
+ "@sqb/builder": "^4.8.2",
34
+ "@sqb/connect": "^4.8.2",
35
+ "@sqb/postgres": "^4.8.2",
36
+ "postgresql-client": "^2.5.5",
37
+ "ts-gems": "^2.3.0"
38
38
  },
39
39
  "peerDependencies": {
40
- "@opra/core": "^0.14.0",
41
- "@sqb/connect": ">= 4.5.4"
40
+ "@opra/core": "^0.16.0",
41
+ "@sqb/connect": ">= 4.8.2"
42
42
  },
43
43
  "type": "module",
44
- "types": "esm/index.d.ts",
44
+ "types": "types/index.d.ts",
45
45
  "exports": {
46
46
  ".": {
47
47
  "require": "./cjs/index.js",
@@ -58,6 +58,7 @@
58
58
  "bin/",
59
59
  "cjs/",
60
60
  "esm/",
61
+ "types/",
61
62
  "LICENSE",
62
63
  "README.md"
63
64
  ],
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
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';
@@ -0,0 +1,14 @@
1
+ import { Request } from '@opra/core';
2
+ import _transformFilter from './transform-filter.js';
3
+ import _transformKeyValues from './transform-key-values.js';
4
+ export declare namespace SQBAdapter {
5
+ const transformFilter: typeof _transformFilter;
6
+ const transformKeyValues: typeof _transformKeyValues;
7
+ function transformRequest(request: Request): {
8
+ method: 'create' | 'delete' | 'deleteMany' | 'find' | 'findOne' | 'findMany' | 'update' | 'updateMany';
9
+ key?: any;
10
+ data?: any;
11
+ options: any;
12
+ args: any[];
13
+ };
14
+ }
@@ -0,0 +1,14 @@
1
+ import { Maybe } from 'ts-gems';
2
+ import { PartialOutput } from '@opra/common';
3
+ import { RequestContext } from '@opra/core';
4
+ import { SqbEntityService } from './sqb-entity-service.js';
5
+ export declare abstract class SqbCollectionResource<T> {
6
+ create(ctx: RequestContext): Promise<PartialOutput<T>>;
7
+ delete(ctx: RequestContext): Promise<boolean>;
8
+ deleteMany(ctx: RequestContext): Promise<number>;
9
+ find(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
10
+ update(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
11
+ updateMany(ctx: RequestContext): Promise<number>;
12
+ search(ctx: RequestContext): Promise<PartialOutput<T>[]>;
13
+ abstract getService(ctx: RequestContext): SqbEntityService<T> | Promise<SqbEntityService<T>>;
14
+ }
@@ -0,0 +1,31 @@
1
+ import { Maybe, Type } from 'ts-gems';
2
+ import { PartialInput, PartialOutput, RequestContext } from '@opra/core';
3
+ import { EntityInput, Repository, SqbClient, SqbConnection } from '@sqb/connect';
4
+ export declare namespace SqbEntityService {
5
+ interface Options {
6
+ db?: SqbClient | SqbConnection;
7
+ defaultLimit?: number;
8
+ }
9
+ }
10
+ export declare class SqbEntityService<T, TOutput = PartialOutput<T>> {
11
+ readonly typeClass: Type<T>;
12
+ context: RequestContext;
13
+ defaultLimit: number;
14
+ db?: SqbClient | SqbConnection;
15
+ constructor(typeClass: Type<T>, options?: SqbEntityService.Options);
16
+ count(context: RequestContext, options?: Repository.CountOptions): Promise<number>;
17
+ create(context: RequestContext, data: PartialInput<T>, options?: Repository.CreateOptions): Promise<TOutput>;
18
+ delete(context: RequestContext, keyValue: any, options?: Repository.DestroyOptions): Promise<boolean>;
19
+ deleteMany(context: RequestContext, options?: Repository.DestroyOptions): Promise<number>;
20
+ find(context: RequestContext, keyValue: any, options?: Repository.FindOptions): Promise<Maybe<TOutput>>;
21
+ findOne(context: RequestContext, options?: Repository.FindOneOptions): Promise<Maybe<TOutput>>;
22
+ findAll(context: RequestContext, options?: Repository.FindAllOptions): Promise<TOutput[]>;
23
+ exists(context: RequestContext, options?: Repository.ExistsOptions): Promise<boolean>;
24
+ update(context: RequestContext, keyValue: any, data: EntityInput<T>, options?: Repository.UpdateOptions): Promise<Maybe<TOutput>>;
25
+ updateMany(context: RequestContext, data: PartialInput<T>, options?: Repository.UpdateAllOptions): Promise<number>;
26
+ with(context: RequestContext, db?: SqbClient | SqbConnection): SqbEntityService<T, TOutput>;
27
+ protected _onError(error: unknown): Promise<void>;
28
+ protected getConnection(): SqbConnection | SqbClient | Promise<SqbConnection | SqbClient>;
29
+ protected onError?(error: unknown): void | Promise<void>;
30
+ protected onTransformRow?(row: TOutput): TOutput;
31
+ }
@@ -0,0 +1,11 @@
1
+ import { Maybe } from 'ts-gems';
2
+ import { PartialOutput } from '@opra/common';
3
+ import { RequestContext } from '@opra/core';
4
+ import { SqbEntityService } from './sqb-entity-service.js';
5
+ export declare abstract class SqbSingletonResource<T> {
6
+ create(ctx: RequestContext): Promise<PartialOutput<T>>;
7
+ delete(ctx: RequestContext): Promise<boolean>;
8
+ get(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
9
+ update(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
10
+ abstract getService(req: RequestContext): SqbEntityService<T> | Promise<SqbEntityService<T>>;
11
+ }
@@ -0,0 +1,2 @@
1
+ import { Expression } from '@opra/common';
2
+ export default function transformFilter(str: string | Expression | undefined): any;
@@ -0,0 +1,3 @@
1
+ import mongodb from 'mongodb';
2
+ import { Collection } from '@opra/common';
3
+ export default function transformKeyValues(resource: Collection, keyValues: any): mongodb.Filter<any>;
@@ -1,39 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseEntityResource = void 0;
4
- const sqb_adapter_js_1 = require("./sqb-adapter.js");
5
- class BaseEntityResource {
6
- async create(ctx) {
7
- const prepared = sqb_adapter_js_1.SQBAdapter.prepare(ctx.query);
8
- return (await this.getService(ctx)).create(prepared.values, prepared.options, ctx.userContext);
9
- }
10
- async count(ctx) {
11
- const prepared = sqb_adapter_js_1.SQBAdapter.prepare(ctx.query);
12
- return (await this.getService(ctx)).count(prepared.options, ctx.userContext);
13
- }
14
- async delete(ctx) {
15
- const prepared = sqb_adapter_js_1.SQBAdapter.prepare(ctx.query);
16
- return (await this.getService(ctx)).delete(prepared.keyValue, prepared.options, ctx.userContext);
17
- }
18
- async deleteMany(ctx) {
19
- const prepared = sqb_adapter_js_1.SQBAdapter.prepare(ctx.query);
20
- return (await this.getService(ctx)).deleteMany(prepared.options, ctx.userContext);
21
- }
22
- async get(ctx) {
23
- const prepared = sqb_adapter_js_1.SQBAdapter.prepare(ctx.query);
24
- return (await this.getService(ctx)).get(prepared.keyValue, prepared.options, ctx.userContext);
25
- }
26
- async search(ctx) {
27
- const prepared = sqb_adapter_js_1.SQBAdapter.prepare(ctx.query);
28
- return (await this.getService(ctx)).search(prepared.options, ctx.userContext);
29
- }
30
- async update(ctx) {
31
- const prepared = sqb_adapter_js_1.SQBAdapter.prepare(ctx.query);
32
- return (await this.getService(ctx)).update(prepared.keyValue, prepared.values, prepared.options, ctx.userContext);
33
- }
34
- async updateMany(ctx) {
35
- const prepared = sqb_adapter_js_1.SQBAdapter.prepare(ctx.query);
36
- return (await this.getService(ctx)).updateMany(prepared.values, prepared.options, ctx.userContext);
37
- }
38
- }
39
- exports.BaseEntityResource = BaseEntityResource;
@@ -1,130 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseEntityService = void 0;
4
- const common_1 = require("@opra/common");
5
- const connect_1 = require("@sqb/connect");
6
- class BaseEntityService {
7
- constructor(resourceType) {
8
- this.resourceType = resourceType;
9
- const metadata = connect_1.EntityMetadata.get(resourceType);
10
- if (!metadata)
11
- throw new TypeError(`You must provide an SQB entity class`);
12
- this._metadata = metadata;
13
- }
14
- async create(data, options, userContext) {
15
- const conn = await this.getConnection(userContext);
16
- const repo = conn.getRepository(this.resourceType);
17
- let out;
18
- try {
19
- out = await repo.create(data, options);
20
- }
21
- catch (e) {
22
- await this._onError(e);
23
- throw new common_1.BadRequestError(e);
24
- }
25
- if (out && this.onTransformRow)
26
- out = this.onTransformRow(out, userContext, 'create');
27
- return out;
28
- }
29
- async count(options, userContext) {
30
- const conn = await this.getConnection(userContext);
31
- const repo = conn.getRepository(this.resourceType);
32
- try {
33
- return await repo.count(options);
34
- }
35
- catch (e) {
36
- await this._onError(e);
37
- throw new common_1.BadRequestError(e);
38
- }
39
- }
40
- async delete(keyValue, options, userContext) {
41
- const conn = await this.getConnection(userContext);
42
- const repo = conn.getRepository(this.resourceType);
43
- try {
44
- return await repo.destroy(keyValue, options);
45
- }
46
- catch (e) {
47
- await this._onError(e);
48
- throw new common_1.BadRequestError(e);
49
- }
50
- }
51
- async deleteMany(options, userContext) {
52
- const conn = await this.getConnection(userContext);
53
- const repo = conn.getRepository(this.resourceType);
54
- try {
55
- return await repo.destroyAll(options);
56
- }
57
- catch (e) {
58
- await this._onError(e);
59
- throw new common_1.BadRequestError(e);
60
- }
61
- }
62
- async get(keyValue, options, userContext) {
63
- const conn = await this.getConnection(userContext);
64
- const repo = conn.getRepository(this.resourceType);
65
- let out;
66
- try {
67
- out = await repo.findByPk(keyValue, options);
68
- }
69
- catch (e) {
70
- await this._onError(e);
71
- throw new common_1.BadRequestError(e);
72
- }
73
- if (out && this.onTransformRow)
74
- out = this.onTransformRow(out, userContext, 'get');
75
- return out;
76
- }
77
- async search(options, userContext) {
78
- const conn = await this.getConnection(userContext);
79
- const repo = conn.getRepository(this.resourceType);
80
- let items;
81
- try {
82
- items = await repo.findAll(options);
83
- }
84
- catch (e) {
85
- await this._onError(e);
86
- throw new common_1.BadRequestError(e);
87
- }
88
- if (items.length && this.onTransformRow) {
89
- const newItems = [];
90
- for (const item of items) {
91
- const v = this.onTransformRow(item, userContext, 'search');
92
- if (v)
93
- newItems.push(v);
94
- }
95
- return newItems;
96
- }
97
- return items;
98
- }
99
- async update(keyValue, data, options, userContext) {
100
- const conn = await this.getConnection(userContext);
101
- const repo = conn.getRepository(this.resourceType);
102
- let out;
103
- try {
104
- out = await repo.update(keyValue, data, options);
105
- }
106
- catch (e) {
107
- await this._onError(e);
108
- throw new common_1.BadRequestError(e);
109
- }
110
- if (out && this.onTransformRow)
111
- out = this.onTransformRow(out, userContext, 'update');
112
- return out;
113
- }
114
- async updateMany(data, options, userContext) {
115
- const conn = await this.getConnection(userContext);
116
- const repo = conn.getRepository(this.resourceType);
117
- try {
118
- return await repo.updateAll(data, options);
119
- }
120
- catch (e) {
121
- await this._onError(e);
122
- throw new common_1.BadRequestError(e);
123
- }
124
- }
125
- async _onError(e) {
126
- if (this.onError)
127
- await this.onError(e);
128
- }
129
- }
130
- exports.BaseEntityService = BaseEntityService;