@opra/sqb 0.26.0 → 0.26.3
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.
- package/cjs/sqb-collection.js +11 -7
- package/cjs/sqb-entity-service.js +4 -1
- package/cjs/sqb-singleton.js +9 -5
- package/esm/sqb-collection.js +11 -7
- package/esm/sqb-entity-service.js +4 -1
- package/esm/sqb-singleton.js +9 -5
- package/package.json +4 -4
- package/types/sqb-adapter.d.ts +3 -2
- package/types/sqb-collection.d.ts +13 -10
- package/types/sqb-entity-service.d.ts +11 -10
- package/types/sqb-singleton.d.ts +10 -7
package/cjs/sqb-collection.js
CHANGED
|
@@ -10,37 +10,37 @@ class SqbCollection {
|
|
|
10
10
|
this.defaultLimit = options?.defaultLimit || 100;
|
|
11
11
|
}
|
|
12
12
|
async create(ctx) {
|
|
13
|
-
const prepared =
|
|
13
|
+
const prepared = await this._prepare(ctx);
|
|
14
14
|
const service = await this.getService(ctx);
|
|
15
15
|
return service.with(ctx).create(prepared.data, prepared.options);
|
|
16
16
|
}
|
|
17
17
|
async delete(ctx) {
|
|
18
|
-
const prepared =
|
|
18
|
+
const prepared = await this._prepare(ctx);
|
|
19
19
|
const service = await this.getService(ctx);
|
|
20
20
|
return service.with(ctx).delete(prepared.key, prepared.options);
|
|
21
21
|
}
|
|
22
22
|
async deleteMany(ctx) {
|
|
23
|
-
const prepared =
|
|
23
|
+
const prepared = await this._prepare(ctx);
|
|
24
24
|
const service = await this.getService(ctx);
|
|
25
25
|
return service.with(ctx).deleteMany(prepared.options);
|
|
26
26
|
}
|
|
27
27
|
async get(ctx) {
|
|
28
|
-
const prepared =
|
|
28
|
+
const prepared = await this._prepare(ctx);
|
|
29
29
|
const service = await this.getService(ctx);
|
|
30
30
|
return service.with(ctx).find(prepared.key, prepared.options);
|
|
31
31
|
}
|
|
32
32
|
async update(ctx) {
|
|
33
|
-
const prepared =
|
|
33
|
+
const prepared = await this._prepare(ctx);
|
|
34
34
|
const service = await this.getService(ctx);
|
|
35
35
|
return service.with(ctx).update(prepared.key, prepared.data, prepared.options);
|
|
36
36
|
}
|
|
37
37
|
async updateMany(ctx) {
|
|
38
|
-
const prepared =
|
|
38
|
+
const prepared = await this._prepare(ctx);
|
|
39
39
|
const service = await this.getService(ctx);
|
|
40
40
|
return service.with(ctx).updateMany(prepared.data, prepared.options);
|
|
41
41
|
}
|
|
42
42
|
async findMany(ctx) {
|
|
43
|
-
const prepared =
|
|
43
|
+
const prepared = await this._prepare(ctx);
|
|
44
44
|
const service = await this.getService(ctx);
|
|
45
45
|
if (prepared.options.count) {
|
|
46
46
|
const [items, count] = await Promise.all([
|
|
@@ -53,6 +53,10 @@ class SqbCollection {
|
|
|
53
53
|
else
|
|
54
54
|
return service.with(ctx).findMany(prepared.options);
|
|
55
55
|
}
|
|
56
|
+
async _prepare(ctx) {
|
|
57
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
58
|
+
return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
|
|
59
|
+
}
|
|
56
60
|
}
|
|
57
61
|
exports.SqbCollection = SqbCollection;
|
|
58
62
|
tslib_1.__decorate([
|
|
@@ -43,7 +43,7 @@ class SqbEntityService {
|
|
|
43
43
|
const conn = await this.getConnection();
|
|
44
44
|
const repo = conn.getRepository(this.typeClass);
|
|
45
45
|
try {
|
|
46
|
-
return await repo.delete(keyValue, options);
|
|
46
|
+
return await repo.delete(keyValue, options) ? 1 : 0;
|
|
47
47
|
}
|
|
48
48
|
catch (e) {
|
|
49
49
|
await this._onError(e);
|
|
@@ -151,6 +151,9 @@ class SqbEntityService {
|
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
153
|
with(context, db) {
|
|
154
|
+
return this.forContext(context, db);
|
|
155
|
+
}
|
|
156
|
+
forContext(context, db) {
|
|
154
157
|
if (this.context === context && this.db === db)
|
|
155
158
|
return this;
|
|
156
159
|
const instance = { context };
|
package/cjs/sqb-singleton.js
CHANGED
|
@@ -6,26 +6,30 @@ const common_1 = require("@opra/common");
|
|
|
6
6
|
const sqb_adapter_js_1 = require("./sqb-adapter.js");
|
|
7
7
|
class SqbSingleton {
|
|
8
8
|
async create(ctx) {
|
|
9
|
-
const prepared =
|
|
9
|
+
const prepared = await this._prepare(ctx);
|
|
10
10
|
const service = await this.getService(ctx);
|
|
11
11
|
return service.with(ctx).create(prepared.data, prepared.options);
|
|
12
12
|
}
|
|
13
13
|
async delete(ctx) {
|
|
14
|
-
const prepared =
|
|
14
|
+
const prepared = await this._prepare(ctx);
|
|
15
15
|
const service = await this.getService(ctx);
|
|
16
|
-
return
|
|
16
|
+
return await service.with(ctx).deleteMany(prepared.options);
|
|
17
17
|
}
|
|
18
18
|
async get(ctx) {
|
|
19
|
-
const prepared =
|
|
19
|
+
const prepared = await this._prepare(ctx);
|
|
20
20
|
const service = await this.getService(ctx);
|
|
21
21
|
return service.with(ctx).findOne(prepared.options);
|
|
22
22
|
}
|
|
23
23
|
async update(ctx) {
|
|
24
|
-
const prepared =
|
|
24
|
+
const prepared = await this._prepare(ctx);
|
|
25
25
|
const service = await this.getService(ctx);
|
|
26
26
|
await service.with(ctx).updateMany(prepared.data, prepared.options);
|
|
27
27
|
return service.with(ctx).findOne(prepared.options);
|
|
28
28
|
}
|
|
29
|
+
async _prepare(ctx) {
|
|
30
|
+
const prepared = sqb_adapter_js_1.SQBAdapter.transformRequest(ctx.request);
|
|
31
|
+
return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
|
|
32
|
+
}
|
|
29
33
|
}
|
|
30
34
|
exports.SqbSingleton = SqbSingleton;
|
|
31
35
|
tslib_1.__decorate([
|
package/esm/sqb-collection.js
CHANGED
|
@@ -7,37 +7,37 @@ export class SqbCollection {
|
|
|
7
7
|
this.defaultLimit = options?.defaultLimit || 100;
|
|
8
8
|
}
|
|
9
9
|
async create(ctx) {
|
|
10
|
-
const prepared =
|
|
10
|
+
const prepared = await this._prepare(ctx);
|
|
11
11
|
const service = await this.getService(ctx);
|
|
12
12
|
return service.with(ctx).create(prepared.data, prepared.options);
|
|
13
13
|
}
|
|
14
14
|
async delete(ctx) {
|
|
15
|
-
const prepared =
|
|
15
|
+
const prepared = await this._prepare(ctx);
|
|
16
16
|
const service = await this.getService(ctx);
|
|
17
17
|
return service.with(ctx).delete(prepared.key, prepared.options);
|
|
18
18
|
}
|
|
19
19
|
async deleteMany(ctx) {
|
|
20
|
-
const prepared =
|
|
20
|
+
const prepared = await this._prepare(ctx);
|
|
21
21
|
const service = await this.getService(ctx);
|
|
22
22
|
return service.with(ctx).deleteMany(prepared.options);
|
|
23
23
|
}
|
|
24
24
|
async get(ctx) {
|
|
25
|
-
const prepared =
|
|
25
|
+
const prepared = await this._prepare(ctx);
|
|
26
26
|
const service = await this.getService(ctx);
|
|
27
27
|
return service.with(ctx).find(prepared.key, prepared.options);
|
|
28
28
|
}
|
|
29
29
|
async update(ctx) {
|
|
30
|
-
const prepared =
|
|
30
|
+
const prepared = await this._prepare(ctx);
|
|
31
31
|
const service = await this.getService(ctx);
|
|
32
32
|
return service.with(ctx).update(prepared.key, prepared.data, prepared.options);
|
|
33
33
|
}
|
|
34
34
|
async updateMany(ctx) {
|
|
35
|
-
const prepared =
|
|
35
|
+
const prepared = await this._prepare(ctx);
|
|
36
36
|
const service = await this.getService(ctx);
|
|
37
37
|
return service.with(ctx).updateMany(prepared.data, prepared.options);
|
|
38
38
|
}
|
|
39
39
|
async findMany(ctx) {
|
|
40
|
-
const prepared =
|
|
40
|
+
const prepared = await this._prepare(ctx);
|
|
41
41
|
const service = await this.getService(ctx);
|
|
42
42
|
if (prepared.options.count) {
|
|
43
43
|
const [items, count] = await Promise.all([
|
|
@@ -50,6 +50,10 @@ export class SqbCollection {
|
|
|
50
50
|
else
|
|
51
51
|
return service.with(ctx).findMany(prepared.options);
|
|
52
52
|
}
|
|
53
|
+
async _prepare(ctx) {
|
|
54
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
55
|
+
return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
|
|
56
|
+
}
|
|
53
57
|
}
|
|
54
58
|
__decorate([
|
|
55
59
|
Collection.Create()
|
|
@@ -40,7 +40,7 @@ export class SqbEntityService {
|
|
|
40
40
|
const conn = await this.getConnection();
|
|
41
41
|
const repo = conn.getRepository(this.typeClass);
|
|
42
42
|
try {
|
|
43
|
-
return await repo.delete(keyValue, options);
|
|
43
|
+
return await repo.delete(keyValue, options) ? 1 : 0;
|
|
44
44
|
}
|
|
45
45
|
catch (e) {
|
|
46
46
|
await this._onError(e);
|
|
@@ -148,6 +148,9 @@ export class SqbEntityService {
|
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
150
|
with(context, db) {
|
|
151
|
+
return this.forContext(context, db);
|
|
152
|
+
}
|
|
153
|
+
forContext(context, db) {
|
|
151
154
|
if (this.context === context && this.db === db)
|
|
152
155
|
return this;
|
|
153
156
|
const instance = { context };
|
package/esm/sqb-singleton.js
CHANGED
|
@@ -3,26 +3,30 @@ import { Singleton } from '@opra/common';
|
|
|
3
3
|
import { SQBAdapter } from './sqb-adapter.js';
|
|
4
4
|
export class SqbSingleton {
|
|
5
5
|
async create(ctx) {
|
|
6
|
-
const prepared =
|
|
6
|
+
const prepared = await this._prepare(ctx);
|
|
7
7
|
const service = await this.getService(ctx);
|
|
8
8
|
return service.with(ctx).create(prepared.data, prepared.options);
|
|
9
9
|
}
|
|
10
10
|
async delete(ctx) {
|
|
11
|
-
const prepared =
|
|
11
|
+
const prepared = await this._prepare(ctx);
|
|
12
12
|
const service = await this.getService(ctx);
|
|
13
|
-
return
|
|
13
|
+
return await service.with(ctx).deleteMany(prepared.options);
|
|
14
14
|
}
|
|
15
15
|
async get(ctx) {
|
|
16
|
-
const prepared =
|
|
16
|
+
const prepared = await this._prepare(ctx);
|
|
17
17
|
const service = await this.getService(ctx);
|
|
18
18
|
return service.with(ctx).findOne(prepared.options);
|
|
19
19
|
}
|
|
20
20
|
async update(ctx) {
|
|
21
|
-
const prepared =
|
|
21
|
+
const prepared = await this._prepare(ctx);
|
|
22
22
|
const service = await this.getService(ctx);
|
|
23
23
|
await service.with(ctx).updateMany(prepared.data, prepared.options);
|
|
24
24
|
return service.with(ctx).findOne(prepared.options);
|
|
25
25
|
}
|
|
26
|
+
async _prepare(ctx) {
|
|
27
|
+
const prepared = SQBAdapter.transformRequest(ctx.request);
|
|
28
|
+
return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
|
|
29
|
+
}
|
|
26
30
|
}
|
|
27
31
|
__decorate([
|
|
28
32
|
Singleton.Create()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/sqb",
|
|
3
|
-
"version": "0.26.
|
|
3
|
+
"version": "0.26.3",
|
|
4
4
|
"description": "Opra SQB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,15 +29,15 @@
|
|
|
29
29
|
"lodash.omitby": "^4.6.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@faker-js/faker": "^8.0
|
|
32
|
+
"@faker-js/faker": "^8.1.0",
|
|
33
33
|
"@sqb/builder": "^4.9.1",
|
|
34
34
|
"@sqb/connect": "^4.9.1",
|
|
35
35
|
"@sqb/postgres": "^4.9.1",
|
|
36
|
-
"postgresql-client": "^2.
|
|
36
|
+
"postgresql-client": "^2.9.1",
|
|
37
37
|
"ts-gems": "^2.5.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@opra/core": "^0.26.
|
|
40
|
+
"@opra/core": "^0.26.3",
|
|
41
41
|
"@sqb/connect": ">= 4.9.0"
|
|
42
42
|
},
|
|
43
43
|
"type": "module",
|
package/types/sqb-adapter.d.ts
CHANGED
|
@@ -4,11 +4,12 @@ import _transformKeyValues from './transform-key-values.js';
|
|
|
4
4
|
export declare namespace SQBAdapter {
|
|
5
5
|
const transformFilter: typeof _transformFilter;
|
|
6
6
|
const transformKeyValues: typeof _transformKeyValues;
|
|
7
|
-
|
|
7
|
+
interface TransformedRequest {
|
|
8
8
|
method: 'create' | 'delete' | 'deleteMany' | 'find' | 'findOne' | 'findMany' | 'update' | 'updateMany';
|
|
9
9
|
key?: any;
|
|
10
10
|
data?: any;
|
|
11
11
|
options: any;
|
|
12
12
|
args: any[];
|
|
13
|
-
}
|
|
13
|
+
}
|
|
14
|
+
function transformRequest(request: Request): TransformedRequest;
|
|
14
15
|
}
|
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
import { Maybe } from 'ts-gems';
|
|
2
|
-
import { PartialOutput } from '@opra/common';
|
|
2
|
+
import { ICollection, PartialOutput } from '@opra/common';
|
|
3
3
|
import { RequestContext } from '@opra/core';
|
|
4
|
+
import { SQBAdapter } from './sqb-adapter.js';
|
|
4
5
|
import { SqbEntityService } from './sqb-entity-service.js';
|
|
5
6
|
export declare namespace SqbCollection {
|
|
6
7
|
interface Options {
|
|
7
8
|
defaultLimit?: number;
|
|
8
9
|
}
|
|
9
10
|
}
|
|
10
|
-
export declare abstract class SqbCollection<T
|
|
11
|
+
export declare abstract class SqbCollection<T> implements ICollection<T> {
|
|
11
12
|
defaultLimit?: number;
|
|
12
13
|
constructor(options?: SqbCollection.Options);
|
|
13
|
-
create(ctx: RequestContext): Promise<
|
|
14
|
-
delete(ctx: RequestContext): Promise<
|
|
15
|
-
deleteMany(ctx: RequestContext): Promise<number>;
|
|
16
|
-
get(ctx: RequestContext): Promise<Maybe<
|
|
17
|
-
update(ctx: RequestContext): Promise<Maybe<
|
|
18
|
-
updateMany(ctx: RequestContext): Promise<number>;
|
|
19
|
-
findMany(ctx: RequestContext): Promise<
|
|
20
|
-
|
|
14
|
+
create?(ctx: RequestContext): Promise<PartialOutput<T>>;
|
|
15
|
+
delete?(ctx: RequestContext): Promise<number>;
|
|
16
|
+
deleteMany?(ctx: RequestContext): Promise<number>;
|
|
17
|
+
get?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
18
|
+
update?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
19
|
+
updateMany?(ctx: RequestContext): Promise<number>;
|
|
20
|
+
findMany?(ctx: RequestContext): Promise<PartialOutput<T>[]>;
|
|
21
|
+
protected _prepare(ctx: RequestContext): Promise<SQBAdapter.TransformedRequest>;
|
|
22
|
+
onPrepare?(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): SQBAdapter.TransformedRequest | Promise<SQBAdapter.TransformedRequest>;
|
|
23
|
+
protected abstract getService(ctx: RequestContext): SqbEntityService<T> | Promise<SqbEntityService<T>>;
|
|
21
24
|
}
|
|
@@ -7,25 +7,26 @@ export declare namespace SqbEntityService {
|
|
|
7
7
|
defaultLimit?: number;
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
-
export declare class SqbEntityService<T
|
|
10
|
+
export declare class SqbEntityService<T> {
|
|
11
11
|
readonly typeClass: Type<T>;
|
|
12
12
|
context: RequestContext;
|
|
13
13
|
defaultLimit: number;
|
|
14
14
|
db?: SqbClient | SqbConnection;
|
|
15
15
|
constructor(typeClass: Type<T>, options?: SqbEntityService.Options);
|
|
16
16
|
count(options?: Repository.CountOptions): Promise<number>;
|
|
17
|
-
create(data: PartialInput<T>, options?: Repository.CreateOptions): Promise<
|
|
18
|
-
delete(keyValue: any, options?: Repository.
|
|
19
|
-
deleteMany(options?: Repository.
|
|
20
|
-
find(keyValue: any, options?: Repository.FindOptions): Promise<Maybe<
|
|
21
|
-
findOne(options?: Repository.FindOneOptions): Promise<Maybe<
|
|
22
|
-
findMany(options?: Repository.FindManyOptions): Promise<
|
|
17
|
+
create(data: PartialInput<T>, options?: Repository.CreateOptions): Promise<PartialOutput<T>>;
|
|
18
|
+
delete(keyValue: any, options?: Repository.DeleteOptions): Promise<number>;
|
|
19
|
+
deleteMany(options?: Repository.DeleteManyOptions): Promise<number>;
|
|
20
|
+
find(keyValue: any, options?: Repository.FindOptions): Promise<Maybe<PartialOutput<T>>>;
|
|
21
|
+
findOne(options?: Repository.FindOneOptions): Promise<Maybe<PartialOutput<T>>>;
|
|
22
|
+
findMany(options?: Repository.FindManyOptions): Promise<PartialOutput<T>[]>;
|
|
23
23
|
exists(options?: Repository.ExistsOptions): Promise<boolean>;
|
|
24
|
-
update(keyValue: any, data: EntityInput<T>, options?: Repository.UpdateOptions): Promise<Maybe<
|
|
24
|
+
update(keyValue: any, data: EntityInput<T>, options?: Repository.UpdateOptions): Promise<Maybe<PartialOutput<T>>>;
|
|
25
25
|
updateMany(data: PartialInput<T>, options?: Repository.UpdateManyOptions): Promise<number>;
|
|
26
|
-
with(context: RequestContext, db?: SqbClient | SqbConnection): SqbEntityService<T
|
|
26
|
+
with(context: RequestContext, db?: SqbClient | SqbConnection): SqbEntityService<T>;
|
|
27
|
+
forContext(context: RequestContext, db?: SqbClient | SqbConnection): SqbEntityService<T>;
|
|
27
28
|
protected _onError(error: unknown): Promise<void>;
|
|
28
29
|
protected getConnection(): SqbConnection | SqbClient | Promise<SqbConnection | SqbClient>;
|
|
29
30
|
protected onError?(error: unknown): void | Promise<void>;
|
|
30
|
-
protected transformData?(row:
|
|
31
|
+
protected transformData?(row: PartialOutput<T>): PartialOutput<T>;
|
|
31
32
|
}
|
package/types/sqb-singleton.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { Maybe } from 'ts-gems';
|
|
2
|
-
import { PartialOutput } from '@opra/common';
|
|
2
|
+
import { ISingleton, PartialOutput } from '@opra/common';
|
|
3
3
|
import { RequestContext } from '@opra/core';
|
|
4
|
+
import { SQBAdapter } from './sqb-adapter.js';
|
|
4
5
|
import { SqbEntityService } from './sqb-entity-service.js';
|
|
5
|
-
export declare abstract class SqbSingleton<T> {
|
|
6
|
-
create(ctx: RequestContext): Promise<PartialOutput<T>>;
|
|
7
|
-
delete(ctx: RequestContext): Promise<
|
|
8
|
-
get(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
9
|
-
update(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
10
|
-
|
|
6
|
+
export declare abstract class SqbSingleton<T> implements ISingleton<T> {
|
|
7
|
+
create?(ctx: RequestContext): Promise<PartialOutput<T>>;
|
|
8
|
+
delete?(ctx: RequestContext): Promise<number>;
|
|
9
|
+
get?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
10
|
+
update?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
11
|
+
protected _prepare(ctx: RequestContext): Promise<SQBAdapter.TransformedRequest>;
|
|
12
|
+
onPrepare?(ctx: RequestContext, prepared: SQBAdapter.TransformedRequest): SQBAdapter.TransformedRequest | Promise<SQBAdapter.TransformedRequest>;
|
|
13
|
+
protected abstract getService(ctx: RequestContext): SqbEntityService<T> | Promise<SqbEntityService<T>>;
|
|
11
14
|
}
|