@opra/sqb 0.15.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 (32) hide show
  1. package/cjs/augmentation/document-factory.augmentation.js +2 -0
  2. package/cjs/sqb-adapter.js +102 -199
  3. package/cjs/sqb-collection-resource.js +20 -20
  4. package/cjs/sqb-entity-service.js +86 -54
  5. package/cjs/sqb-singleton-resource.js +10 -10
  6. package/cjs/{convert-filter.js → transform-filter.js} +8 -12
  7. package/cjs/transform-key-values.js +14 -0
  8. package/esm/augmentation/document-factory.augmentation.js +32 -28
  9. package/esm/augmentation/mapped-type.augmentation.js +9 -7
  10. package/esm/augmentation/union-type.augmentation.js +7 -5
  11. package/esm/index.js +10 -7
  12. package/esm/sqb-adapter.js +110 -203
  13. package/esm/sqb-collection-resource.js +56 -52
  14. package/esm/sqb-entity-service.js +92 -56
  15. package/esm/sqb-singleton-resource.js +34 -30
  16. package/esm/transform-filter.js +68 -0
  17. package/esm/transform-key-values.js +14 -0
  18. package/package.json +8 -7
  19. package/types/sqb-adapter.d.ts +14 -0
  20. package/{esm → types}/sqb-collection-resource.d.ts +1 -1
  21. package/types/sqb-entity-service.d.ts +31 -0
  22. package/types/transform-filter.d.ts +2 -0
  23. package/types/transform-key-values.d.ts +3 -0
  24. package/esm/convert-filter.d.ts +0 -2
  25. package/esm/convert-filter.js +0 -67
  26. package/esm/sqb-adapter.d.ts +0 -64
  27. package/esm/sqb-entity-service.d.ts +0 -21
  28. /package/{esm → types}/augmentation/document-factory.augmentation.d.ts +0 -0
  29. /package/{esm → types}/augmentation/mapped-type.augmentation.d.ts +0 -0
  30. /package/{esm → types}/augmentation/union-type.augmentation.d.ts +0 -0
  31. /package/{esm → types}/index.d.ts +0 -0
  32. /package/{esm → types}/sqb-singleton-resource.d.ts +0 -0
@@ -1,39 +1,42 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Collection } from '@opra/common';
3
- import { SQBAdapter } from './sqb-adapter.js';
4
- export class SqbCollectionResource {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SqbCollectionResource = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@opra/common");
6
+ const sqb_adapter_js_1 = require("./sqb-adapter.js");
7
+ class SqbCollectionResource {
5
8
  async create(ctx) {
6
- const prepared = SQBAdapter.parseCollectionCreateRequest(ctx.request);
9
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
7
10
  const service = await this.getService(ctx);
8
11
  return service.create(ctx, prepared.data, prepared.options);
9
12
  }
10
13
  async delete(ctx) {
11
- const prepared = SQBAdapter.parseCollectionDeleteRequest(ctx.request);
14
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
12
15
  const service = await this.getService(ctx);
13
- return service.destroy(ctx, prepared.key, prepared.options);
16
+ return service.delete(ctx, prepared.key, prepared.options);
14
17
  }
15
18
  async deleteMany(ctx) {
16
- const prepared = SQBAdapter.parseCollectionDeleteManyRequest(ctx.request);
19
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
17
20
  const service = await this.getService(ctx);
18
- return service.destroyAll(ctx, prepared.options);
21
+ return service.deleteMany(ctx, prepared.options);
19
22
  }
20
- async get(ctx) {
21
- const prepared = SQBAdapter.parseCollectionGetRequest(ctx.request);
23
+ async find(ctx) {
24
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
22
25
  const service = await this.getService(ctx);
23
- return service.findByPk(ctx, prepared.key, prepared.options);
26
+ return service.find(ctx, prepared.key, prepared.options);
24
27
  }
25
28
  async update(ctx) {
26
- const prepared = SQBAdapter.parseCollectionUpdateRequest(ctx.request);
29
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
27
30
  const service = await this.getService(ctx);
28
31
  return service.update(ctx, prepared.key, prepared.data, prepared.options);
29
32
  }
30
33
  async updateMany(ctx) {
31
- const prepared = SQBAdapter.parseCollectionUpdateManyRequest(ctx.request);
34
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
32
35
  const service = await this.getService(ctx);
33
- return service.updateAll(ctx, prepared.data, prepared.options);
36
+ return service.updateMany(ctx, prepared.data, prepared.options);
34
37
  }
35
38
  async search(ctx) {
36
- const prepared = SQBAdapter.parseCollectionSearchRequest(ctx.request);
39
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
37
40
  const service = await this.getService(ctx);
38
41
  if (prepared.options.count) {
39
42
  const [items, count] = await Promise.all([
@@ -47,45 +50,46 @@ export class SqbCollectionResource {
47
50
  return service.findAll(ctx, prepared.options);
48
51
  }
49
52
  }
50
- __decorate([
51
- Collection.CreateOperation(),
52
- __metadata("design:type", Function),
53
- __metadata("design:paramtypes", [Object]),
54
- __metadata("design:returntype", Promise)
53
+ tslib_1.__decorate([
54
+ common_1.Collection.Create(),
55
+ tslib_1.__metadata("design:type", Function),
56
+ tslib_1.__metadata("design:paramtypes", [Object]),
57
+ tslib_1.__metadata("design:returntype", Promise)
55
58
  ], SqbCollectionResource.prototype, "create", null);
56
- __decorate([
57
- Collection.DeleteOperation(),
58
- __metadata("design:type", Function),
59
- __metadata("design:paramtypes", [Object]),
60
- __metadata("design:returntype", Promise)
59
+ tslib_1.__decorate([
60
+ common_1.Collection.Delete(),
61
+ tslib_1.__metadata("design:type", Function),
62
+ tslib_1.__metadata("design:paramtypes", [Object]),
63
+ tslib_1.__metadata("design:returntype", Promise)
61
64
  ], SqbCollectionResource.prototype, "delete", null);
62
- __decorate([
63
- Collection.DeleteManyOperation(),
64
- __metadata("design:type", Function),
65
- __metadata("design:paramtypes", [Object]),
66
- __metadata("design:returntype", Promise)
65
+ tslib_1.__decorate([
66
+ common_1.Collection.DeleteMany(),
67
+ tslib_1.__metadata("design:type", Function),
68
+ tslib_1.__metadata("design:paramtypes", [Object]),
69
+ tslib_1.__metadata("design:returntype", Promise)
67
70
  ], SqbCollectionResource.prototype, "deleteMany", null);
68
- __decorate([
69
- Collection.GetOperation(),
70
- __metadata("design:type", Function),
71
- __metadata("design:paramtypes", [Object]),
72
- __metadata("design:returntype", Promise)
73
- ], SqbCollectionResource.prototype, "get", null);
74
- __decorate([
75
- Collection.UpdateOperation(),
76
- __metadata("design:type", Function),
77
- __metadata("design:paramtypes", [Object]),
78
- __metadata("design:returntype", Promise)
71
+ tslib_1.__decorate([
72
+ common_1.Collection.Get(),
73
+ tslib_1.__metadata("design:type", Function),
74
+ tslib_1.__metadata("design:paramtypes", [Object]),
75
+ tslib_1.__metadata("design:returntype", Promise)
76
+ ], SqbCollectionResource.prototype, "find", null);
77
+ tslib_1.__decorate([
78
+ common_1.Collection.Update(),
79
+ tslib_1.__metadata("design:type", Function),
80
+ tslib_1.__metadata("design:paramtypes", [Object]),
81
+ tslib_1.__metadata("design:returntype", Promise)
79
82
  ], SqbCollectionResource.prototype, "update", null);
80
- __decorate([
81
- Collection.UpdateManyOperation(),
82
- __metadata("design:type", Function),
83
- __metadata("design:paramtypes", [Object]),
84
- __metadata("design:returntype", Promise)
83
+ tslib_1.__decorate([
84
+ common_1.Collection.UpdateMany(),
85
+ tslib_1.__metadata("design:type", Function),
86
+ tslib_1.__metadata("design:paramtypes", [Object]),
87
+ tslib_1.__metadata("design:returntype", Promise)
85
88
  ], SqbCollectionResource.prototype, "updateMany", null);
86
- __decorate([
87
- Collection.SearchOperation(),
88
- __metadata("design:type", Function),
89
- __metadata("design:paramtypes", [Object]),
90
- __metadata("design:returntype", Promise)
89
+ tslib_1.__decorate([
90
+ common_1.Collection.FindMany(),
91
+ tslib_1.__metadata("design:type", Function),
92
+ tslib_1.__metadata("design:paramtypes", [Object]),
93
+ tslib_1.__metadata("design:returntype", Promise)
91
94
  ], SqbCollectionResource.prototype, "search", null);
95
+ exports.SqbCollectionResource = SqbCollectionResource;
@@ -1,105 +1,111 @@
1
- import { EntityMetadata } from '@sqb/connect';
2
- export class SqbEntityService {
3
- constructor(resourceType) {
4
- this.resourceType = resourceType;
5
- const metadata = EntityMetadata.get(resourceType);
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);
6
9
  if (!metadata)
7
- throw new TypeError(`You must provide an SQB entity class`);
8
- this._entityMetadata = metadata;
10
+ throw new TypeError(`Class ${typeClass} is not decorated with $Entity() decorator`);
11
+ this.db = options?.db;
12
+ this.defaultLimit = options?.defaultLimit || 10;
9
13
  }
10
- async create(context, data, options) {
11
- const conn = await this.getConnection(context);
12
- const repo = conn.getRepository(this.resourceType);
13
- let out;
14
+ async count(context, options) {
15
+ const conn = await this.getConnection();
16
+ const repo = conn.getRepository(this.typeClass);
14
17
  try {
15
- out = await repo.create(data, options);
18
+ return await repo.count(options);
16
19
  }
17
20
  catch (e) {
18
- await this._onError(context, e);
21
+ await this._onError(e);
19
22
  throw e;
20
23
  }
21
- if (out && this.onTransformRow)
22
- out = this.onTransformRow(context, out, 'create');
23
- return out;
24
24
  }
25
- async count(context, options) {
26
- const conn = await this.getConnection(context);
27
- const repo = conn.getRepository(this.resourceType);
25
+ async create(context, data, options) {
26
+ const conn = await this.getConnection();
27
+ const repo = conn.getRepository(this.typeClass);
28
+ let out;
28
29
  try {
29
- return await repo.count(options);
30
+ out = await repo.create(data, options);
30
31
  }
31
32
  catch (e) {
32
- await this._onError(context, e);
33
+ await this._onError(e);
33
34
  throw e;
34
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;
35
41
  }
36
- async destroy(context, keyValue, options) {
37
- const conn = await this.getConnection(context);
38
- const repo = conn.getRepository(this.resourceType);
42
+ async delete(context, keyValue, options) {
43
+ const conn = await this.getConnection();
44
+ const repo = conn.getRepository(this.typeClass);
39
45
  try {
40
- return await repo.destroy(keyValue, options);
46
+ return await repo.delete(keyValue, options);
41
47
  }
42
48
  catch (e) {
43
- await this._onError(context, e);
49
+ await this._onError(e);
44
50
  throw e;
45
51
  }
46
52
  }
47
- async destroyAll(context, options) {
48
- const conn = await this.getConnection(context);
49
- const repo = conn.getRepository(this.resourceType);
53
+ async deleteMany(context, options) {
54
+ const conn = await this.getConnection();
55
+ const repo = conn.getRepository(this.typeClass);
50
56
  try {
51
- return await repo.destroyAll(options);
57
+ return await repo.deleteMany(options);
52
58
  }
53
59
  catch (e) {
54
- await this._onError(context, e);
60
+ await this._onError(e);
55
61
  throw e;
56
62
  }
57
63
  }
58
- async findByPk(context, keyValue, options) {
59
- const conn = await this.getConnection(context);
60
- const repo = conn.getRepository(this.resourceType);
64
+ async find(context, keyValue, options) {
65
+ const conn = await this.getConnection();
66
+ const repo = conn.getRepository(this.typeClass);
61
67
  let out;
62
68
  try {
63
- out = await repo.findByPk(keyValue, options);
69
+ out = await repo.find(keyValue, options);
64
70
  }
65
71
  catch (e) {
66
- await this._onError(context, e);
72
+ await this._onError(e);
67
73
  throw e;
68
74
  }
69
75
  if (out && this.onTransformRow)
70
- out = this.onTransformRow(context, out, 'read');
76
+ out = this.onTransformRow(out);
71
77
  return out;
72
78
  }
73
79
  async findOne(context, options) {
74
- const conn = await this.getConnection(context);
75
- const repo = conn.getRepository(this.resourceType);
80
+ const conn = await this.getConnection();
81
+ const repo = conn.getRepository(this.typeClass);
76
82
  let out;
77
83
  try {
78
84
  out = await repo.findOne(options);
79
85
  }
80
86
  catch (e) {
81
- await this._onError(context, e);
87
+ await this._onError(e);
82
88
  throw e;
83
89
  }
84
90
  if (out && this.onTransformRow)
85
- out = this.onTransformRow(context, out, 'read');
91
+ out = this.onTransformRow(out);
86
92
  return out;
87
93
  }
88
94
  async findAll(context, options) {
89
- const conn = await this.getConnection(context);
90
- const repo = conn.getRepository(this.resourceType);
95
+ const conn = await this.getConnection();
96
+ const repo = conn.getRepository(this.typeClass);
91
97
  let items;
92
98
  try {
93
99
  items = await repo.findAll(options);
94
100
  }
95
101
  catch (e) {
96
- await this._onError(context, e);
102
+ await this._onError(e);
97
103
  throw e;
98
104
  }
99
105
  if (items.length && this.onTransformRow) {
100
106
  const newItems = [];
101
107
  for (const item of items) {
102
- const v = this.onTransformRow(context, item, 'read');
108
+ const v = this.onTransformRow(item);
103
109
  if (v)
104
110
  newItems.push(v);
105
111
  }
@@ -107,34 +113,64 @@ export class SqbEntityService {
107
113
  }
108
114
  return items;
109
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
+ }
110
127
  async update(context, keyValue, data, options) {
111
- const conn = await this.getConnection(context);
112
- const repo = conn.getRepository(this.resourceType);
128
+ const conn = await this.getConnection();
129
+ const repo = conn.getRepository(this.typeClass);
113
130
  let out;
114
131
  try {
115
132
  out = await repo.update(keyValue, data, options);
116
133
  }
117
134
  catch (e) {
118
- await this._onError(context, e);
135
+ await this._onError(e);
119
136
  throw e;
120
137
  }
121
138
  if (out && this.onTransformRow)
122
- out = this.onTransformRow(context, out, 'update');
139
+ out = this.onTransformRow(out);
123
140
  return out;
124
141
  }
125
- async updateAll(context, data, options) {
126
- const conn = await this.getConnection(context);
127
- const repo = conn.getRepository(this.resourceType);
142
+ async updateMany(context, data, options) {
143
+ const conn = await this.getConnection();
144
+ const repo = conn.getRepository(this.typeClass);
128
145
  try {
129
- return await repo.updateAll(data, options);
146
+ return await repo.updateMany(data, options);
130
147
  }
131
148
  catch (e) {
132
- await this._onError(context, e);
149
+ await this._onError(e);
133
150
  throw e;
134
151
  }
135
152
  }
136
- async _onError(context, error) {
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) {
137
165
  if (this.onError)
138
- await this.onError(context, error);
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;
139
174
  }
140
175
  }
176
+ exports.SqbEntityService = SqbEntityService;
@@ -1,50 +1,54 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Singleton } from '@opra/common';
3
- import { SQBAdapter } from './sqb-adapter.js';
4
- export class SqbSingletonResource {
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 {
5
8
  async create(ctx) {
6
- const prepared = SQBAdapter.parseSingletonCreateRequest(ctx.request);
9
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
7
10
  const service = await this.getService(ctx);
8
11
  return service.create(ctx, prepared.data, prepared.options);
9
12
  }
10
13
  async delete(ctx) {
11
- const prepared = SQBAdapter.parseSingletonDeleteRequest(ctx.request);
14
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
12
15
  const service = await this.getService(ctx);
13
- return !!(await service.destroyAll(ctx, prepared.options));
16
+ return !!(await service.deleteMany(ctx, prepared.options));
14
17
  }
15
18
  async get(ctx) {
16
- const prepared = SQBAdapter.parseSingletonGetRequest(ctx.request);
19
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
17
20
  const service = await this.getService(ctx);
18
21
  return service.findOne(ctx, prepared.options);
19
22
  }
20
23
  async update(ctx) {
21
- const prepared = SQBAdapter.parseSingletonUpdateRequest(ctx.request);
24
+ const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
22
25
  const service = await this.getService(ctx);
23
- await service.updateAll(ctx, prepared.data, prepared.options);
26
+ await service.updateMany(ctx, prepared.data, prepared.options);
24
27
  return service.findOne(ctx, prepared.options);
25
28
  }
26
29
  }
27
- __decorate([
28
- Singleton.CreateOperation(),
29
- __metadata("design:type", Function),
30
- __metadata("design:paramtypes", [Object]),
31
- __metadata("design:returntype", Promise)
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)
32
35
  ], SqbSingletonResource.prototype, "create", null);
33
- __decorate([
34
- Singleton.DeleteOperation(),
35
- __metadata("design:type", Function),
36
- __metadata("design:paramtypes", [Object]),
37
- __metadata("design:returntype", Promise)
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)
38
41
  ], SqbSingletonResource.prototype, "delete", null);
39
- __decorate([
40
- Singleton.GetOperation(),
41
- __metadata("design:type", Function),
42
- __metadata("design:paramtypes", [Object]),
43
- __metadata("design:returntype", Promise)
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)
44
47
  ], SqbSingletonResource.prototype, "get", null);
45
- __decorate([
46
- Singleton.UpdateOperation(),
47
- __metadata("design:type", Function),
48
- __metadata("design:paramtypes", [Object]),
49
- __metadata("design:returntype", Promise)
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)
50
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.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Opra SQB adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -30,18 +30,18 @@
30
30
  },
31
31
  "devDependencies": {
32
32
  "@faker-js/faker": "^7.6.0",
33
- "@sqb/builder": "^4.6.3",
34
- "@sqb/connect": "^4.6.3",
35
- "@sqb/postgres": "^4.6.3",
33
+ "@sqb/builder": "^4.8.2",
34
+ "@sqb/connect": "^4.8.2",
35
+ "@sqb/postgres": "^4.8.2",
36
36
  "postgresql-client": "^2.5.5",
37
37
  "ts-gems": "^2.3.0"
38
38
  },
39
39
  "peerDependencies": {
40
- "@opra/core": "^0.15.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,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
+ }
@@ -6,7 +6,7 @@ export declare abstract class SqbCollectionResource<T> {
6
6
  create(ctx: RequestContext): Promise<PartialOutput<T>>;
7
7
  delete(ctx: RequestContext): Promise<boolean>;
8
8
  deleteMany(ctx: RequestContext): Promise<number>;
9
- get(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
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
  search(ctx: RequestContext): Promise<PartialOutput<T>[]>;
@@ -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,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,2 +0,0 @@
1
- import { Expression } from '@opra/common';
2
- export declare function convertFilter(str: string | Expression | undefined): any;