@opra/sqb 0.13.0 → 0.15.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 (31) hide show
  1. package/cjs/augmentation/document-factory.augmentation.js +112 -0
  2. package/cjs/augmentation/mapped-type.augmentation.js +14 -0
  3. package/cjs/augmentation/union-type.augmentation.js +9 -0
  4. package/cjs/convert-filter.js +3 -2
  5. package/cjs/index.js +6 -6
  6. package/cjs/sqb-adapter.js +198 -109
  7. package/cjs/sqb-collection-resource.js +95 -0
  8. package/cjs/{base-entity-service.js → sqb-entity-service.js} +57 -43
  9. package/cjs/sqb-singleton-resource.js +54 -0
  10. package/esm/augmentation/document-factory.augmentation.d.ts +1 -0
  11. package/esm/augmentation/document-factory.augmentation.js +110 -0
  12. package/esm/augmentation/mapped-type.augmentation.d.ts +1 -0
  13. package/esm/augmentation/mapped-type.augmentation.js +12 -0
  14. package/esm/augmentation/union-type.augmentation.d.ts +1 -0
  15. package/esm/augmentation/union-type.augmentation.js +7 -0
  16. package/esm/convert-filter.js +3 -2
  17. package/esm/index.d.ts +6 -7
  18. package/esm/index.js +6 -6
  19. package/esm/sqb-adapter.d.ts +60 -6
  20. package/esm/sqb-adapter.js +198 -109
  21. package/esm/sqb-collection-resource.d.ts +14 -0
  22. package/esm/sqb-collection-resource.js +91 -0
  23. package/esm/sqb-entity-service.d.ts +21 -0
  24. package/esm/{base-entity-service.js → sqb-entity-service.js} +55 -41
  25. package/esm/sqb-singleton-resource.d.ts +11 -0
  26. package/esm/sqb-singleton-resource.js +50 -0
  27. package/package.json +10 -10
  28. package/cjs/base-entity-resource.js +0 -39
  29. package/esm/base-entity-resource.d.ts +0 -13
  30. package/esm/base-entity-resource.js +0 -35
  31. package/esm/base-entity-service.d.ts +0 -30
@@ -1,94 +1,108 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseEntityService = void 0;
4
- const common_1 = require("@opra/common");
3
+ exports.SqbEntityService = void 0;
5
4
  const connect_1 = require("@sqb/connect");
6
- class BaseEntityService {
5
+ class SqbEntityService {
7
6
  constructor(resourceType) {
8
7
  this.resourceType = resourceType;
9
8
  const metadata = connect_1.EntityMetadata.get(resourceType);
10
9
  if (!metadata)
11
10
  throw new TypeError(`You must provide an SQB entity class`);
12
- this._metadata = metadata;
11
+ this._entityMetadata = metadata;
13
12
  }
14
- async create(data, options, userContext) {
15
- const conn = await this.getConnection(userContext);
13
+ async create(context, data, options) {
14
+ const conn = await this.getConnection(context);
16
15
  const repo = conn.getRepository(this.resourceType);
17
16
  let out;
18
17
  try {
19
18
  out = await repo.create(data, options);
20
19
  }
21
20
  catch (e) {
22
- await this._onError(e);
23
- throw new common_1.BadRequestError(e);
21
+ await this._onError(context, e);
22
+ throw e;
24
23
  }
25
24
  if (out && this.onTransformRow)
26
- out = this.onTransformRow(out, userContext, 'create');
25
+ out = this.onTransformRow(context, out, 'create');
27
26
  return out;
28
27
  }
29
- async count(options, userContext) {
30
- const conn = await this.getConnection(userContext);
28
+ async count(context, options) {
29
+ const conn = await this.getConnection(context);
31
30
  const repo = conn.getRepository(this.resourceType);
32
31
  try {
33
32
  return await repo.count(options);
34
33
  }
35
34
  catch (e) {
36
- await this._onError(e);
37
- throw new common_1.BadRequestError(e);
35
+ await this._onError(context, e);
36
+ throw e;
38
37
  }
39
38
  }
40
- async delete(keyValue, options, userContext) {
41
- const conn = await this.getConnection(userContext);
39
+ async destroy(context, keyValue, options) {
40
+ const conn = await this.getConnection(context);
42
41
  const repo = conn.getRepository(this.resourceType);
43
42
  try {
44
43
  return await repo.destroy(keyValue, options);
45
44
  }
46
45
  catch (e) {
47
- await this._onError(e);
48
- throw new common_1.BadRequestError(e);
46
+ await this._onError(context, e);
47
+ throw e;
49
48
  }
50
49
  }
51
- async deleteMany(options, userContext) {
52
- const conn = await this.getConnection(userContext);
50
+ async destroyAll(context, options) {
51
+ const conn = await this.getConnection(context);
53
52
  const repo = conn.getRepository(this.resourceType);
54
53
  try {
55
54
  return await repo.destroyAll(options);
56
55
  }
57
56
  catch (e) {
58
- await this._onError(e);
59
- throw new common_1.BadRequestError(e);
57
+ await this._onError(context, e);
58
+ throw e;
60
59
  }
61
60
  }
62
- async get(keyValue, options, userContext) {
63
- const conn = await this.getConnection(userContext);
61
+ async findByPk(context, keyValue, options) {
62
+ const conn = await this.getConnection(context);
64
63
  const repo = conn.getRepository(this.resourceType);
65
64
  let out;
66
65
  try {
67
66
  out = await repo.findByPk(keyValue, options);
68
67
  }
69
68
  catch (e) {
70
- await this._onError(e);
71
- throw new common_1.BadRequestError(e);
69
+ await this._onError(context, e);
70
+ throw e;
72
71
  }
73
72
  if (out && this.onTransformRow)
74
- out = this.onTransformRow(out, userContext, 'get');
73
+ out = this.onTransformRow(context, out, 'read');
75
74
  return out;
76
75
  }
77
- async search(options, userContext) {
78
- const conn = await this.getConnection(userContext);
76
+ async findOne(context, options) {
77
+ const conn = await this.getConnection(context);
78
+ const repo = conn.getRepository(this.resourceType);
79
+ let out;
80
+ try {
81
+ out = await repo.findOne(options);
82
+ }
83
+ catch (e) {
84
+ await this._onError(context, e);
85
+ throw e;
86
+ }
87
+ if (out && this.onTransformRow)
88
+ out = this.onTransformRow(context, out, 'read');
89
+ return out;
90
+ }
91
+ async findAll(context, options) {
92
+ const conn = await this.getConnection(context);
79
93
  const repo = conn.getRepository(this.resourceType);
80
94
  let items;
81
95
  try {
82
96
  items = await repo.findAll(options);
83
97
  }
84
98
  catch (e) {
85
- await this._onError(e);
86
- throw new common_1.BadRequestError(e);
99
+ await this._onError(context, e);
100
+ throw e;
87
101
  }
88
102
  if (items.length && this.onTransformRow) {
89
103
  const newItems = [];
90
104
  for (const item of items) {
91
- const v = this.onTransformRow(item, userContext, 'search');
105
+ const v = this.onTransformRow(context, item, 'read');
92
106
  if (v)
93
107
  newItems.push(v);
94
108
  }
@@ -96,35 +110,35 @@ class BaseEntityService {
96
110
  }
97
111
  return items;
98
112
  }
99
- async update(keyValue, data, options, userContext) {
100
- const conn = await this.getConnection(userContext);
113
+ async update(context, keyValue, data, options) {
114
+ const conn = await this.getConnection(context);
101
115
  const repo = conn.getRepository(this.resourceType);
102
116
  let out;
103
117
  try {
104
118
  out = await repo.update(keyValue, data, options);
105
119
  }
106
120
  catch (e) {
107
- await this._onError(e);
108
- throw new common_1.BadRequestError(e);
121
+ await this._onError(context, e);
122
+ throw e;
109
123
  }
110
124
  if (out && this.onTransformRow)
111
- out = this.onTransformRow(out, userContext, 'update');
125
+ out = this.onTransformRow(context, out, 'update');
112
126
  return out;
113
127
  }
114
- async updateMany(data, options, userContext) {
115
- const conn = await this.getConnection(userContext);
128
+ async updateAll(context, data, options) {
129
+ const conn = await this.getConnection(context);
116
130
  const repo = conn.getRepository(this.resourceType);
117
131
  try {
118
132
  return await repo.updateAll(data, options);
119
133
  }
120
134
  catch (e) {
121
- await this._onError(e);
122
- throw new common_1.BadRequestError(e);
135
+ await this._onError(context, e);
136
+ throw e;
123
137
  }
124
138
  }
125
- async _onError(e) {
139
+ async _onError(context, error) {
126
140
  if (this.onError)
127
- await this.onError(e);
141
+ await this.onError(context, error);
128
142
  }
129
143
  }
130
- exports.BaseEntityService = BaseEntityService;
144
+ 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.parseSingletonCreateRequest(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.parseSingletonDeleteRequest(ctx.request);
15
+ const service = await this.getService(ctx);
16
+ return !!(await service.destroyAll(ctx, prepared.options));
17
+ }
18
+ async get(ctx) {
19
+ const prepared = sqb_adapter_js_1.SQBAdapter.parseSingletonGetRequest(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.parseSingletonUpdateRequest(ctx.request);
25
+ const service = await this.getService(ctx);
26
+ await service.updateAll(ctx, prepared.data, prepared.options);
27
+ return service.findOne(ctx, prepared.options);
28
+ }
29
+ }
30
+ tslib_1.__decorate([
31
+ common_1.Singleton.CreateOperation(),
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.DeleteOperation(),
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.GetOperation(),
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.UpdateOperation(),
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,110 @@
1
+ import { DocumentFactory, METADATA_KEY } from "@opra/common";
2
+ import { DataType as SqbDataType, EntityMetadata, isAssociationField } from '@sqb/connect';
3
+ // @ts-ignore
4
+ const _extractFieldSchema = DocumentFactory.prototype.extractFieldSchema;
5
+ // @ts-ignore
6
+ DocumentFactory.prototype.extractFieldSchema = async function (target, ctor, metadata, name) {
7
+ await _extractFieldSchema.call(this, target, ctor, metadata, name);
8
+ const sqbMeta = EntityMetadata.get(ctor);
9
+ const sqbField = sqbMeta && EntityMetadata.getField(sqbMeta, name);
10
+ if (!sqbField)
11
+ return;
12
+ const detectType = !metadata.type;
13
+ if (isAssociationField(sqbField)) {
14
+ if (!sqbField.association.returnsMany())
15
+ delete target.isArray;
16
+ if (!target.type) {
17
+ const trg = await sqbField.association.resolveTarget();
18
+ if (trg) {
19
+ // @ts-ignore
20
+ target.type = await this.importTypeClass(trg.ctor);
21
+ }
22
+ }
23
+ }
24
+ else if (sqbField.kind === 'column') {
25
+ if (sqbField.type && Reflect.hasMetadata(METADATA_KEY, sqbField.type)) {
26
+ target.type = sqbField.type;
27
+ }
28
+ switch (sqbField.dataType) {
29
+ case SqbDataType.GUID:
30
+ if (!target.type || (detectType && target.type === 'string'))
31
+ target.type = 'guid';
32
+ break;
33
+ case SqbDataType.JSON:
34
+ if (!target.type || (detectType && target.type === 'any'))
35
+ target.type = 'object';
36
+ break;
37
+ case SqbDataType.INTEGER:
38
+ case SqbDataType.SMALLINT:
39
+ if (!target.type || (detectType && target.type === 'number'))
40
+ target.type = 'integer';
41
+ break;
42
+ case SqbDataType.BIGINT:
43
+ if (!target.type || (detectType && target.type === 'number'))
44
+ target.type = 'bigint';
45
+ break;
46
+ case SqbDataType.DATE:
47
+ if (!target.type || (detectType && (target.type === 'timestamp' || target.type === 'string')))
48
+ target.type = 'date';
49
+ break;
50
+ case SqbDataType.TIMESTAMPTZ:
51
+ if (!target.type || (detectType && (target.type === 'timestamp' || target.type === 'string')))
52
+ target.type = 'timestamptz';
53
+ break;
54
+ case SqbDataType.TIME:
55
+ if (!target.type || (detectType && (target.type === 'timestamp' || target.type === 'string')))
56
+ target.type = 'time';
57
+ break;
58
+ case SqbDataType.BINARY:
59
+ if (!target.type || (detectType && target.type === 'string'))
60
+ target.type = 'base64';
61
+ break;
62
+ }
63
+ if (!target.type) {
64
+ switch (sqbField.dataType) {
65
+ case SqbDataType.BOOL:
66
+ target.type = 'boolean';
67
+ break;
68
+ case SqbDataType.CHAR:
69
+ case SqbDataType.VARCHAR:
70
+ case SqbDataType.TEXT:
71
+ target.type = 'string';
72
+ break;
73
+ case SqbDataType.FLOAT:
74
+ case SqbDataType.DOUBLE:
75
+ case SqbDataType.NUMBER:
76
+ target.type = 'number';
77
+ break;
78
+ case SqbDataType.TIMESTAMP:
79
+ target.type = 'timestamp';
80
+ break;
81
+ }
82
+ }
83
+ if (sqbField.notNull && target.required === undefined)
84
+ target.required = sqbField.notNull;
85
+ if (sqbField.exclusive && target.exclusive === undefined)
86
+ target.exclusive = sqbField.exclusive;
87
+ if (sqbField.default !== undefined && target.default === undefined)
88
+ target.default = sqbField.default;
89
+ }
90
+ if (sqbField.exclusive && target.exclusive === undefined)
91
+ target.exclusive = sqbField.exclusive;
92
+ };
93
+ // @ts-ignore
94
+ const _createCollection = DocumentFactory.prototype.createCollection;
95
+ // @ts-ignore
96
+ DocumentFactory.prototype.createCollection = async function (name, schema) {
97
+ const { document } = this;
98
+ const dataType = document.getComplexType(schema.type);
99
+ // Determine primaryKey if not defined
100
+ if (!schema.primaryKey && dataType.ctor) {
101
+ const entityMetadata = EntityMetadata.get(dataType.ctor);
102
+ if (entityMetadata?.indexes) {
103
+ const primaryIndex = entityMetadata.indexes.find(x => x.primary);
104
+ if (primaryIndex) {
105
+ schema.primaryKey = primaryIndex.columns;
106
+ }
107
+ }
108
+ }
109
+ return await _createCollection.call(this, name, schema);
110
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
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) {
5
+ _applyMixin.call(null, target, source, options);
6
+ const srcMeta = Entity.getMetadata(source);
7
+ if (srcMeta) {
8
+ const trgMeta = EntityMetadata.define(target);
9
+ const { isInheritedPredicate } = options;
10
+ EntityMetadata.mixin(trgMeta, srcMeta, k => isInheritedPredicate(k));
11
+ }
12
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import { UnionType } from "@opra/common";
2
+ import { Entity } from '@sqb/connect';
3
+ const _applyMixin = UnionType._applyMixin;
4
+ UnionType._applyMixin = function (target, ...sources) {
5
+ _applyMixin.call(null, target, ...sources);
6
+ Entity.mixin(target, ...sources);
7
+ };
@@ -6,7 +6,8 @@ export function convertFilter(str) {
6
6
  return;
7
7
  if (ast instanceof ComparisonExpression) {
8
8
  const left = ast.left instanceof QualifiedIdentifier
9
- ? ast.left.value : convertFilter(ast.left);
9
+ ? sqb.Field(ast.left.value)
10
+ : convertFilter(ast.left);
10
11
  const right = convertFilter(ast.right);
11
12
  switch (ast.op) {
12
13
  case '=':
@@ -38,7 +39,7 @@ export function convertFilter(str) {
38
39
  }
39
40
  }
40
41
  if (ast instanceof QualifiedIdentifier) {
41
- return sqb.Field(ast.value);
42
+ return ast.value;
42
43
  }
43
44
  if (ast instanceof NumberLiteral ||
44
45
  ast instanceof StringLiteral ||
package/esm/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- import * as _SqbConnect from '@sqb/connect';
1
+ import './augmentation/document-factory.augmentation.js';
2
+ import './augmentation/mapped-type.augmentation.js';
3
+ import './augmentation/union-type.augmentation.js';
2
4
  export * from './sqb-adapter.js';
3
- export * from './base-entity-resource.js';
4
- export * from './base-entity-service.js';
5
- export type { _SqbConnect as SqbConnect };
6
- declare global {
7
- export type SqbConnect = typeof _SqbConnect;
8
- }
5
+ export * from './sqb-collection-resource.js';
6
+ export * from './sqb-entity-service.js';
7
+ export * from './sqb-singleton-resource.js';
package/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import * as _SqbConnect from '@sqb/connect';
1
+ import './augmentation/document-factory.augmentation.js';
2
+ import './augmentation/mapped-type.augmentation.js';
3
+ import './augmentation/union-type.augmentation.js';
2
4
  export * from './sqb-adapter.js';
3
- export * from './base-entity-resource.js';
4
- export * from './base-entity-service.js';
5
- const optionalsSymbol = Symbol.for('opra.optional-lib.sqb-connect');
6
- globalThis[optionalsSymbol] = globalThis[optionalsSymbol] || {};
7
- globalThis[optionalsSymbol].SqbConnect = _SqbConnect;
5
+ export * from './sqb-collection-resource.js';
6
+ export * from './sqb-entity-service.js';
7
+ export * from './sqb-singleton-resource.js';
@@ -1,10 +1,64 @@
1
- import { OpraQuery } from '@opra/common';
1
+ import { Request } from '@opra/core';
2
+ import { convertFilter } from './convert-filter.js';
2
3
  export declare namespace SQBAdapter {
3
- function prepare(query: OpraQuery): {
4
- method: 'create' | 'count' | 'findByPk' | 'findAll' | 'update' | 'updateAll' | 'destroy' | 'destroyAll';
4
+ const parseFilter: typeof convertFilter;
5
+ type parseFilter = typeof convertFilter;
6
+ function parseCollectionCreateRequest(request: Request): {
7
+ method: string;
8
+ data: any;
9
+ options: any;
10
+ };
11
+ function parseCollectionDeleteRequest(request: Request): {
12
+ method: string;
13
+ key: any;
14
+ options: {};
15
+ };
16
+ function parseCollectionDeleteManyRequest(request: Request): {
17
+ method: string;
18
+ options: any;
19
+ };
20
+ function parseCollectionGetRequest(request: Request): {
21
+ method: string;
22
+ key: any;
23
+ options: any;
24
+ };
25
+ function parseCollectionUpdateRequest(request: Request): {
26
+ method: string;
27
+ key: any;
28
+ data: any;
29
+ options: any;
30
+ };
31
+ function parseCollectionUpdateManyRequest(request: Request): {
32
+ method: string;
33
+ data: any;
34
+ options: any;
35
+ };
36
+ function parseCollectionSearchRequest(request: Request): {
37
+ method: string;
38
+ options: any;
39
+ };
40
+ function parseSingletonGetRequest(request: Request): {
41
+ method: string;
42
+ options: any;
43
+ };
44
+ function parseSingletonCreateRequest(request: Request): {
45
+ method: string;
46
+ data: any;
47
+ options: any;
48
+ };
49
+ function parseSingletonDeleteRequest(request: Request): {
50
+ method: string;
51
+ options: {};
52
+ };
53
+ function parseSingletonUpdateRequest(request: Request): {
54
+ method: string;
55
+ data: any;
56
+ options: any;
57
+ };
58
+ function parseRequest(request: Request): {
59
+ method: string;
60
+ key?: any;
61
+ data?: any;
5
62
  options: any;
6
- keyValue?: any;
7
- values?: any;
8
- args: any[];
9
63
  };
10
64
  }