@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,166 +0,0 @@
1
- import { ApiService } from '@opra/core';
2
- import { EntityMetadata } from '@sqb/connect';
3
- export class SqbEntityServiceBase extends ApiService {
4
- constructor(typeClass, options) {
5
- super();
6
- this.typeClass = typeClass;
7
- const metadata = EntityMetadata.get(typeClass);
8
- if (!metadata)
9
- throw new TypeError(`Class ${typeClass} is not decorated with $Entity() decorator`);
10
- this.db = options?.db;
11
- this.defaultLimit = options?.defaultLimit || 10;
12
- }
13
- async _count(options) {
14
- const conn = await this.getConnection();
15
- const repo = conn.getRepository(this.typeClass);
16
- try {
17
- return await repo.count(options);
18
- }
19
- catch (e) {
20
- await this._onError(e);
21
- throw e;
22
- }
23
- }
24
- async _create(data, options) {
25
- const conn = await this.getConnection();
26
- const repo = conn.getRepository(this.typeClass);
27
- let out;
28
- try {
29
- out = await repo.create(data, options);
30
- }
31
- catch (e) {
32
- await this._onError(e);
33
- throw e;
34
- }
35
- if (out && this.transformData)
36
- out = this.transformData(out);
37
- if (!out)
38
- throw new Error('"create" endpoint returned no result!');
39
- return out;
40
- }
41
- async _delete(keyValue, options) {
42
- const conn = await this.getConnection();
43
- const repo = conn.getRepository(this.typeClass);
44
- try {
45
- return await repo.delete(keyValue, options) ? 1 : 0;
46
- }
47
- catch (e) {
48
- await this._onError(e);
49
- throw e;
50
- }
51
- }
52
- async _deleteMany(options) {
53
- const conn = await this.getConnection();
54
- const repo = conn.getRepository(this.typeClass);
55
- try {
56
- return await repo.deleteMany(options);
57
- }
58
- catch (e) {
59
- await this._onError(e);
60
- throw e;
61
- }
62
- }
63
- async _find(keyValue, options) {
64
- const conn = await this.getConnection();
65
- const repo = conn.getRepository(this.typeClass);
66
- let out;
67
- try {
68
- out = await repo.find(keyValue, options);
69
- }
70
- catch (e) {
71
- await this._onError(e);
72
- throw e;
73
- }
74
- if (out && this.transformData)
75
- out = this.transformData(out);
76
- return out;
77
- }
78
- async _findOne(options) {
79
- const conn = await this.getConnection();
80
- const repo = conn.getRepository(this.typeClass);
81
- let out;
82
- try {
83
- out = await repo.findOne(options);
84
- }
85
- catch (e) {
86
- await this._onError(e);
87
- throw e;
88
- }
89
- if (out && this.transformData)
90
- out = this.transformData(out);
91
- return out;
92
- }
93
- async _findMany(options) {
94
- const conn = await this.getConnection();
95
- const repo = conn.getRepository(this.typeClass);
96
- let items;
97
- try {
98
- items = await repo.findMany(options);
99
- }
100
- catch (e) {
101
- await this._onError(e);
102
- throw e;
103
- }
104
- if (items.length && this.transformData) {
105
- const newItems = [];
106
- for (const item of items) {
107
- const v = this.transformData(item);
108
- if (v)
109
- newItems.push(v);
110
- }
111
- return newItems;
112
- }
113
- return items;
114
- }
115
- async _exists(options) {
116
- const conn = await this.getConnection();
117
- const repo = conn.getRepository(this.typeClass);
118
- try {
119
- return await repo.exists(options);
120
- }
121
- catch (e) {
122
- await this._onError(e);
123
- throw e;
124
- }
125
- }
126
- async _update(keyValue, data, options) {
127
- const conn = await this.getConnection();
128
- const repo = conn.getRepository(this.typeClass);
129
- let out;
130
- try {
131
- out = await repo.update(keyValue, data, options);
132
- }
133
- catch (e) {
134
- await this._onError(e);
135
- throw e;
136
- }
137
- if (out && this.transformData)
138
- out = this.transformData(out);
139
- return out;
140
- }
141
- async _updateMany(data, options) {
142
- const conn = await this.getConnection();
143
- const repo = conn.getRepository(this.typeClass);
144
- try {
145
- return await repo.updateMany(data, options);
146
- }
147
- catch (e) {
148
- await this._onError(e);
149
- throw e;
150
- }
151
- }
152
- for(context, overwriteProperties, overwriteContext) {
153
- return super.for(context, overwriteProperties, overwriteContext);
154
- }
155
- async _onError(error) {
156
- if (this.onError)
157
- await this.onError(error);
158
- }
159
- getConnection() {
160
- if (!this.context)
161
- throw new Error(`Context not set!`);
162
- if (!this.db)
163
- throw new Error(`Database not set!`);
164
- return this.db;
165
- }
166
- }
@@ -1,40 +0,0 @@
1
- import { SQBAdapter } from './sqb-adapter.js';
2
- export class SqbSingleton {
3
- async create(ctx) {
4
- const prepared = await this._prepare(ctx);
5
- return this._create(ctx, prepared);
6
- }
7
- async delete(ctx) {
8
- const prepared = await this._prepare(ctx);
9
- return this._delete(ctx, prepared);
10
- }
11
- async get(ctx) {
12
- const prepared = await this._prepare(ctx);
13
- return this._get(ctx, prepared);
14
- }
15
- async update(ctx) {
16
- const prepared = await this._prepare(ctx);
17
- return this._update(ctx, prepared);
18
- }
19
- async _create(ctx, prepared) {
20
- const service = await this.getService(ctx);
21
- return service.create(prepared.data, prepared.options);
22
- }
23
- async _delete(ctx, prepared) {
24
- const service = await this.getService(ctx);
25
- return await service.deleteMany(prepared.options);
26
- }
27
- async _get(ctx, prepared) {
28
- const service = await this.getService(ctx);
29
- return service.findOne(prepared.options);
30
- }
31
- async _update(ctx, prepared) {
32
- const service = await this.getService(ctx);
33
- await service.updateMany(prepared.data, prepared.options);
34
- return service.findOne(prepared.options);
35
- }
36
- async _prepare(ctx) {
37
- const prepared = SQBAdapter.transformRequest(ctx.request);
38
- return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
39
- }
40
- }
@@ -1,11 +0,0 @@
1
- export default function transformKeyValues(resource, keyValues) {
2
- const { primaryKey } = resource;
3
- if (primaryKey.length > 1) {
4
- const query = {};
5
- primaryKey.forEach((k, i) => {
6
- query[k] = typeof keyValues === 'object' ? keyValues[k] : keyValues[i];
7
- });
8
- return query;
9
- }
10
- return { [primaryKey[0]]: keyValues };
11
- }
@@ -1,31 +0,0 @@
1
- import { Maybe } from 'ts-gems';
2
- import { ICollection, PartialDTO } from '@opra/common';
3
- import { RequestContext } from '@opra/core';
4
- import { SQBAdapter } from './sqb-adapter.js';
5
- import { SqbEntityService } from './sqb-entity-service.js';
6
- export declare namespace SqbCollection {
7
- interface Options {
8
- defaultLimit?: number;
9
- }
10
- }
11
- export declare abstract class SqbCollection<T> implements ICollection<T> {
12
- defaultLimit?: number;
13
- constructor(options?: SqbCollection.Options);
14
- create?(ctx: RequestContext): Promise<PartialDTO<T>>;
15
- delete?(ctx: RequestContext): Promise<number>;
16
- deleteMany?(ctx: RequestContext): Promise<number>;
17
- get?(ctx: RequestContext): Promise<Maybe<PartialDTO<T>>>;
18
- update?(ctx: RequestContext): Promise<Maybe<PartialDTO<T>>>;
19
- updateMany?(ctx: RequestContext): Promise<number>;
20
- findMany?(ctx: RequestContext): Promise<PartialDTO<T>[]>;
21
- protected _create(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<PartialDTO<T>>;
22
- protected _delete(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<number>;
23
- protected _deleteMany(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<number>;
24
- protected _get(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<Maybe<PartialDTO<T>>>;
25
- protected _update(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<Maybe<PartialDTO<T>>>;
26
- protected _updateMany(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<number>;
27
- protected _findMany(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<PartialDTO<T>[]>;
28
- protected _prepare(ctx: RequestContext): Promise<SQBAdapter.TransformedRequest>;
29
- onPrepare?(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): SQBAdapter.TransformedRequest | Promise<SQBAdapter.TransformedRequest>;
30
- protected abstract getService(ctx: RequestContext): SqbEntityService<T> | Promise<SqbEntityService<T>>;
31
- }
@@ -1,31 +0,0 @@
1
- import { Nullish, Type } from 'ts-gems';
2
- import { DTO, PartialDTO, PatchDTO } from '@opra/common';
3
- import { ApiService, RequestContext } from '@opra/core';
4
- import { EntityInput, Repository, SqbClient, SqbConnection } from '@sqb/connect';
5
- export declare class SqbEntityServiceBase<T> extends ApiService {
6
- readonly typeClass: Type<T>;
7
- defaultLimit: number;
8
- db?: SqbClient | SqbConnection;
9
- constructor(typeClass: Type<T>, options?: SqbEntityServiceBase.Options);
10
- protected _count(options?: Repository.CountOptions): Promise<number>;
11
- protected _create(data: DTO<T>, options?: Repository.CreateOptions): Promise<PartialDTO<T>>;
12
- protected _delete(keyValue: any, options?: Repository.DeleteOptions): Promise<number>;
13
- protected _deleteMany(options?: Repository.DeleteManyOptions): Promise<number>;
14
- protected _find(keyValue: any, options?: Repository.FindOptions): Promise<PartialDTO<T> | undefined>;
15
- protected _findOne(options?: Repository.FindOneOptions): Promise<PartialDTO<T> | undefined>;
16
- protected _findMany(options?: Repository.FindManyOptions): Promise<PartialDTO<T>[]>;
17
- protected _exists(options?: Repository.ExistsOptions): Promise<boolean>;
18
- protected _update(keyValue: any, data: EntityInput<T>, options?: Repository.UpdateOptions): Promise<PartialDTO<T> | undefined>;
19
- protected _updateMany(data: PatchDTO<T>, options?: Repository.UpdateManyOptions): Promise<number>;
20
- for<C extends RequestContext, P extends Partial<this>>(context: C, overwriteProperties?: Nullish<P>, overwriteContext?: Partial<C>): this & Required<P>;
21
- protected _onError(error: unknown): Promise<void>;
22
- protected getConnection(): SqbConnection | SqbClient | Promise<SqbConnection | SqbClient>;
23
- protected onError?(error: unknown): void | Promise<void>;
24
- protected transformData?(row: PartialDTO<T>): PartialDTO<T>;
25
- }
26
- export declare namespace SqbEntityServiceBase {
27
- interface Options {
28
- db?: SqbClient | SqbConnection;
29
- defaultLimit?: number;
30
- }
31
- }
@@ -1,18 +0,0 @@
1
- import { Maybe } from 'ts-gems';
2
- import { ISingleton, PartialDTO } from '@opra/common';
3
- import { RequestContext } from '@opra/core';
4
- import { SQBAdapter } from './sqb-adapter.js';
5
- import { SqbEntityService } from './sqb-entity-service.js';
6
- export declare abstract class SqbSingleton<T> implements ISingleton<T> {
7
- create?(ctx: RequestContext): Promise<PartialDTO<T>>;
8
- delete?(ctx: RequestContext): Promise<number>;
9
- get?(ctx: RequestContext): Promise<Maybe<PartialDTO<T>>>;
10
- update?(ctx: RequestContext): Promise<Maybe<PartialDTO<T>>>;
11
- protected _create(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<PartialDTO<T>>;
12
- protected _delete(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<number>;
13
- protected _get(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<Maybe<PartialDTO<T>>>;
14
- protected _update(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): Promise<Maybe<PartialDTO<T>>>;
15
- protected _prepare(ctx: RequestContext): Promise<SQBAdapter.TransformedRequest>;
16
- onPrepare?(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): SQBAdapter.TransformedRequest | Promise<SQBAdapter.TransformedRequest>;
17
- protected abstract getService(ctx: RequestContext): SqbEntityService<T> | Promise<SqbEntityService<T>>;
18
- }
@@ -1,3 +0,0 @@
1
- import '@opra/core';
2
- import { OpraFilter } from '@opra/common';
3
- export default function transformFilter(ast: OpraFilter.Expression | undefined): any;
@@ -1,3 +0,0 @@
1
- import mongodb from 'mongodb';
2
- import { Collection } from '@opra/common';
3
- export default function transformKeyValues(resource: Collection, keyValues: any): mongodb.Filter<any>;