@opra/sqb 1.26.3 → 1.26.4
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/README.md +24 -1
- package/adapter-utils/prepare-filter.d.ts +3 -4
- package/adapter-utils/prepare-filter.js +3 -4
- package/augmentation/datatype-factory.augmentation.js +1 -1
- package/package.json +3 -3
- package/sqb-adapter.d.ts +41 -1
- package/sqb-adapter.js +21 -6
- package/sqb-collection-service.d.ts +141 -159
- package/sqb-collection-service.js +52 -99
- package/sqb-entity-service.d.ts +131 -153
- package/sqb-entity-service.js +123 -127
- package/sqb-service-base.d.ts +17 -13
- package/sqb-service-base.js +15 -17
- package/sqb-singleton-service.d.ts +78 -88
- package/sqb-singleton-service.js +26 -26
package/sqb-service-base.js
CHANGED
|
@@ -2,45 +2,45 @@ import { ServiceBase } from '@opra/core';
|
|
|
2
2
|
import { SqbConnection } from '@sqb/connect';
|
|
3
3
|
const transactionKey = Symbol.for('transaction');
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* @template T - The data type class type of the resource
|
|
5
|
+
* Base class for services using SQB (SQL Builder) for database operations.
|
|
7
6
|
*/
|
|
8
7
|
export class SqbServiceBase extends ServiceBase {
|
|
9
8
|
/**
|
|
10
|
-
*
|
|
9
|
+
* The SQB client or connection used by this service.
|
|
11
10
|
*/
|
|
12
11
|
db;
|
|
13
12
|
/**
|
|
14
|
-
* Constructs a new instance
|
|
13
|
+
* Constructs a new instance.
|
|
15
14
|
*
|
|
16
|
-
* @param
|
|
17
|
-
* @constructor
|
|
15
|
+
* @param options - Options for the service.
|
|
18
16
|
*/
|
|
19
17
|
constructor(options) {
|
|
20
|
-
super();
|
|
18
|
+
super(options);
|
|
21
19
|
this.db = options?.db;
|
|
22
20
|
}
|
|
23
21
|
/**
|
|
24
|
-
* Executes the provided
|
|
22
|
+
* Executes the provided callback within a database transaction.
|
|
23
|
+
* If a transaction is already active in the current context, the callback
|
|
24
|
+
* joins it rather than starting a new one.
|
|
25
25
|
*
|
|
26
|
-
* @param callback - The function to
|
|
26
|
+
* @param callback - The function to execute within the transaction.
|
|
27
27
|
*/
|
|
28
28
|
async withTransaction(callback) {
|
|
29
29
|
const ctx = this.context;
|
|
30
30
|
let closeSessionOnFinish = false;
|
|
31
31
|
let connection = ctx[transactionKey];
|
|
32
32
|
if (!connection) {
|
|
33
|
-
|
|
33
|
+
/* Determine the SqbClient or SqbConnection instance */
|
|
34
34
|
const db = await this.getConnection();
|
|
35
35
|
if (db instanceof SqbConnection) {
|
|
36
36
|
connection = db;
|
|
37
37
|
}
|
|
38
38
|
else {
|
|
39
|
-
|
|
39
|
+
/* Acquire a connection. New connection should be at the end */
|
|
40
40
|
connection = await db.acquire({ autoCommit: false });
|
|
41
41
|
closeSessionOnFinish = true;
|
|
42
42
|
}
|
|
43
|
-
|
|
43
|
+
/* Store transaction connection in current context */
|
|
44
44
|
ctx[transactionKey] = connection;
|
|
45
45
|
}
|
|
46
46
|
const oldInTransaction = connection.inTransaction;
|
|
@@ -60,7 +60,7 @@ export class SqbServiceBase extends ServiceBase {
|
|
|
60
60
|
}
|
|
61
61
|
finally {
|
|
62
62
|
delete ctx[transactionKey];
|
|
63
|
-
|
|
63
|
+
/* Release connection */
|
|
64
64
|
if (closeSessionOnFinish) {
|
|
65
65
|
await connection.close();
|
|
66
66
|
}
|
|
@@ -69,11 +69,9 @@ export class SqbServiceBase extends ServiceBase {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
|
-
*
|
|
72
|
+
* Returns the active database connection for the current context.
|
|
73
73
|
*
|
|
74
|
-
* @
|
|
75
|
-
*
|
|
76
|
-
* @throws {Error} If the context or database is not set.
|
|
74
|
+
* @throws If no database connection is configured.
|
|
77
75
|
*/
|
|
78
76
|
getConnection() {
|
|
79
77
|
const ctx = this.context;
|
|
@@ -2,138 +2,128 @@ import type { PartialDTO, PatchDTO, RequiredSome, Type } from 'ts-gems';
|
|
|
2
2
|
import { SQBAdapter } from './sqb-adapter.js';
|
|
3
3
|
import { SqbEntityService } from './sqb-entity-service.js';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Options for SqbSingletonService.
|
|
6
6
|
*/
|
|
7
7
|
export declare namespace SqbSingletonService {
|
|
8
|
-
interface Options extends SqbEntityService.Options {
|
|
9
|
-
id?: SqbSingletonService<any>['id'];
|
|
10
|
-
}
|
|
11
8
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* @interface
|
|
9
|
+
* Configuration options for SqbSingletonService.
|
|
15
10
|
*/
|
|
16
|
-
interface
|
|
11
|
+
interface Options extends SqbEntityService.Options {
|
|
12
|
+
/**
|
|
13
|
+
* The identifier used to locate the singleton record.
|
|
14
|
+
*/
|
|
15
|
+
id?: SqbSingletonService<any>['id'];
|
|
17
16
|
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Service for managing a single entity record backed by an SQB data source.
|
|
20
|
+
*
|
|
21
|
+
* @typeParam T - The entity type managed by this service
|
|
22
|
+
*/
|
|
23
|
+
export declare class SqbSingletonService<T extends object = object> extends SqbEntityService<T> {
|
|
18
24
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* @interface
|
|
25
|
+
* The identifier used to locate the singleton record.
|
|
22
26
|
*/
|
|
23
|
-
|
|
24
|
-
}
|
|
27
|
+
id: SQBAdapter.IdOrIds;
|
|
25
28
|
/**
|
|
26
|
-
*
|
|
29
|
+
* Constructs a new instance.
|
|
27
30
|
*
|
|
28
|
-
* @
|
|
31
|
+
* @param dataType - The entity class or its registered name.
|
|
32
|
+
* @param options - Options for the singleton service.
|
|
29
33
|
*/
|
|
30
|
-
|
|
31
|
-
}
|
|
34
|
+
constructor(dataType: Type<T> | string, options?: SqbSingletonService.Options);
|
|
32
35
|
/**
|
|
33
|
-
*
|
|
36
|
+
* Asserts that the singleton record exists.
|
|
37
|
+
* Throws {@link ResourceNotAvailableError} if it does not.
|
|
34
38
|
*
|
|
35
|
-
* @
|
|
39
|
+
* @param options - Optional existence check options.
|
|
40
|
+
* @throws {@link ResourceNotAvailableError} If the record does not exist.
|
|
36
41
|
*/
|
|
37
|
-
|
|
38
|
-
}
|
|
42
|
+
assert(options?: SqbEntityService.ExistsOptions): Promise<void>;
|
|
39
43
|
/**
|
|
40
|
-
*
|
|
44
|
+
* Creates the singleton record and returns it with the requested projection.
|
|
41
45
|
*
|
|
42
|
-
* @
|
|
46
|
+
* @param input - The input data for the new record.
|
|
47
|
+
* @param options - Options including a required `projection`.
|
|
48
|
+
* @returns The created record as a partial DTO.
|
|
43
49
|
*/
|
|
44
|
-
|
|
45
|
-
}
|
|
50
|
+
create(input: PartialDTO<T>, options: RequiredSome<SqbEntityService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
|
|
46
51
|
/**
|
|
47
|
-
*
|
|
52
|
+
* Creates the singleton record and returns the full DTO.
|
|
48
53
|
*
|
|
49
|
-
* @
|
|
54
|
+
* @param input - The input data for the new record.
|
|
55
|
+
* @param options - Optional create options.
|
|
56
|
+
* @returns The created record as a full DTO.
|
|
50
57
|
*/
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* @class SqbSingletonService
|
|
56
|
-
* @template T - The data type class type of the resource
|
|
57
|
-
*/
|
|
58
|
-
export declare class SqbSingletonService<T extends object = object> extends SqbEntityService<T> {
|
|
59
|
-
/**
|
|
60
|
-
* Represents a unique identifier for singleton record
|
|
61
|
-
* @property {SQBAdapter.IdOrIds}
|
|
62
|
-
*/
|
|
63
|
-
id: SQBAdapter.IdOrIds;
|
|
58
|
+
create(input: PartialDTO<T>, options?: SqbEntityService.CreateOptions): Promise<T>;
|
|
64
59
|
/**
|
|
65
|
-
*
|
|
60
|
+
* Deletes the singleton record.
|
|
66
61
|
*
|
|
67
|
-
* @param
|
|
68
|
-
* @
|
|
69
|
-
* @constructor
|
|
62
|
+
* @param options - Optional delete options.
|
|
63
|
+
* @returns The number of records deleted.
|
|
70
64
|
*/
|
|
71
|
-
|
|
65
|
+
delete(options?: SqbEntityService.DeleteOptions): Promise<number>;
|
|
72
66
|
/**
|
|
73
|
-
*
|
|
67
|
+
* Checks whether the singleton record exists.
|
|
74
68
|
*
|
|
75
|
-
* @
|
|
76
|
-
* @
|
|
69
|
+
* @param options - Optional query options.
|
|
70
|
+
* @returns `true` if the record exists, `false` otherwise.
|
|
77
71
|
*/
|
|
78
|
-
|
|
72
|
+
exists(options?: SqbEntityService.ExistsOptions): Promise<boolean>;
|
|
79
73
|
/**
|
|
80
|
-
*
|
|
74
|
+
* Finds the singleton record with the requested projection.
|
|
75
|
+
* Returns `undefined` if it does not exist.
|
|
81
76
|
*
|
|
82
|
-
* @param
|
|
83
|
-
* @
|
|
84
|
-
* @returns {Promise<PartialDTO<T>>} A promise that resolves to the created resource
|
|
85
|
-
* @throws {Error} if an unknown error occurs while creating the resource
|
|
77
|
+
* @param options - Options including a required `projection`.
|
|
78
|
+
* @returns The record as a partial DTO, or `undefined`.
|
|
86
79
|
*/
|
|
87
|
-
|
|
88
|
-
create(input: PartialDTO<T>, options?: SqbSingletonService.CreateOptions): Promise<T>;
|
|
80
|
+
find(options: RequiredSome<SqbEntityService.FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>;
|
|
89
81
|
/**
|
|
90
|
-
*
|
|
82
|
+
* Finds the singleton record. Returns `undefined` if it does not exist.
|
|
91
83
|
*
|
|
92
|
-
* @param
|
|
93
|
-
* @
|
|
84
|
+
* @param options - Optional query options.
|
|
85
|
+
* @returns The record as a full DTO, or `undefined`.
|
|
94
86
|
*/
|
|
95
|
-
|
|
87
|
+
find(options?: SqbEntityService.FindOneOptions): Promise<T | undefined>;
|
|
96
88
|
/**
|
|
97
|
-
*
|
|
89
|
+
* Retrieves the singleton record with the requested projection.
|
|
90
|
+
* Throws if the record does not exist.
|
|
98
91
|
*
|
|
99
|
-
* @param
|
|
100
|
-
* @
|
|
92
|
+
* @param options - Options including a required `projection`.
|
|
93
|
+
* @returns The record as a partial DTO.
|
|
94
|
+
* @throws {@link ResourceNotAvailableError} If the record does not exist.
|
|
101
95
|
*/
|
|
102
|
-
|
|
96
|
+
get(options: RequiredSome<SqbEntityService.FindOneOptions, 'projection'>): Promise<PartialDTO<T>>;
|
|
103
97
|
/**
|
|
104
|
-
*
|
|
98
|
+
* Retrieves the singleton record. Throws if it does not exist.
|
|
105
99
|
*
|
|
106
|
-
* @param
|
|
107
|
-
* @
|
|
100
|
+
* @param options - Optional query options.
|
|
101
|
+
* @returns The record as a full DTO.
|
|
102
|
+
* @throws {@link ResourceNotAvailableError} If the record does not exist.
|
|
108
103
|
*/
|
|
109
|
-
|
|
110
|
-
find(options?: SqbSingletonService.FindOptions): Promise<T | undefined>;
|
|
104
|
+
get(options?: SqbEntityService.FindOneOptions): Promise<T>;
|
|
111
105
|
/**
|
|
112
|
-
*
|
|
106
|
+
* Updates the singleton record and returns it with the requested projection.
|
|
113
107
|
*
|
|
114
|
-
* @param
|
|
115
|
-
* @
|
|
116
|
-
*
|
|
117
|
-
* @throws {ResourceNotAvailableError} - If the document does not exist.
|
|
108
|
+
* @param input - The fields to update.
|
|
109
|
+
* @param options - Options including a required `projection`.
|
|
110
|
+
* @returns The updated record as a partial DTO, or `undefined` if not found.
|
|
118
111
|
*/
|
|
119
|
-
|
|
120
|
-
get(options?: SqbSingletonService.FindOptions): Promise<T>;
|
|
112
|
+
update(input: PatchDTO<T>, options: RequiredSome<SqbEntityService.UpdateOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>;
|
|
121
113
|
/**
|
|
122
|
-
* Updates the singleton.
|
|
114
|
+
* Updates the singleton record and returns the full DTO.
|
|
123
115
|
*
|
|
124
|
-
* @param
|
|
125
|
-
* @param
|
|
126
|
-
* @returns
|
|
127
|
-
* undefined if the document was not found.
|
|
116
|
+
* @param input - The fields to update.
|
|
117
|
+
* @param options - Optional update options.
|
|
118
|
+
* @returns The updated record as a full DTO, or `undefined` if not found.
|
|
128
119
|
*/
|
|
129
|
-
update(input: PatchDTO<T>, options
|
|
130
|
-
update(input: PatchDTO<T>, options?: SqbSingletonService.UpdateOptions): Promise<T | undefined>;
|
|
120
|
+
update(input: PatchDTO<T>, options?: SqbEntityService.UpdateOneOptions): Promise<T | undefined>;
|
|
131
121
|
/**
|
|
132
|
-
* Updates the singleton
|
|
122
|
+
* Updates the singleton record without returning it.
|
|
133
123
|
*
|
|
134
|
-
* @param
|
|
135
|
-
* @param
|
|
136
|
-
* @returns
|
|
124
|
+
* @param input - The fields to update.
|
|
125
|
+
* @param options - Optional update options.
|
|
126
|
+
* @returns The number of records modified.
|
|
137
127
|
*/
|
|
138
|
-
updateOnly(input: PatchDTO<T>, options?:
|
|
128
|
+
updateOnly(input: PatchDTO<T>, options?: SqbEntityService.UpdateOneOptions): Promise<number>;
|
|
139
129
|
}
|
package/sqb-singleton-service.js
CHANGED
|
@@ -3,31 +3,31 @@ import { EntityMetadata } from '@sqb/connect';
|
|
|
3
3
|
import { SQBAdapter } from './sqb-adapter.js';
|
|
4
4
|
import { SqbEntityService } from './sqb-entity-service.js';
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* Service for managing a single entity record backed by an SQB data source.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam T - The entity type managed by this service
|
|
8
9
|
*/
|
|
9
10
|
export class SqbSingletonService extends SqbEntityService {
|
|
10
11
|
/**
|
|
11
|
-
*
|
|
12
|
-
* @property {SQBAdapter.IdOrIds}
|
|
12
|
+
* The identifier used to locate the singleton record.
|
|
13
13
|
*/
|
|
14
14
|
id;
|
|
15
15
|
/**
|
|
16
|
-
* Constructs a new instance
|
|
16
|
+
* Constructs a new instance.
|
|
17
17
|
*
|
|
18
|
-
* @param
|
|
19
|
-
* @param
|
|
20
|
-
* @constructor
|
|
18
|
+
* @param dataType - The entity class or its registered name.
|
|
19
|
+
* @param options - Options for the singleton service.
|
|
21
20
|
*/
|
|
22
21
|
constructor(dataType, options) {
|
|
23
22
|
super(dataType, options);
|
|
24
23
|
this.id = options?.id || 1;
|
|
25
24
|
}
|
|
26
25
|
/**
|
|
27
|
-
* Asserts the
|
|
26
|
+
* Asserts that the singleton record exists.
|
|
27
|
+
* Throws {@link ResourceNotAvailableError} if it does not.
|
|
28
28
|
*
|
|
29
|
-
* @
|
|
30
|
-
* @throws {ResourceNotAvailableError} If the
|
|
29
|
+
* @param options - Optional existence check options.
|
|
30
|
+
* @throws {@link ResourceNotAvailableError} If the record does not exist.
|
|
31
31
|
*/
|
|
32
32
|
async assert(options) {
|
|
33
33
|
if (!(await this.exists(options)))
|
|
@@ -59,10 +59,10 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
|
-
* Deletes the singleton record
|
|
62
|
+
* Deletes the singleton record.
|
|
63
63
|
*
|
|
64
|
-
* @param
|
|
65
|
-
* @
|
|
64
|
+
* @param options - Optional delete options.
|
|
65
|
+
* @returns The number of records deleted.
|
|
66
66
|
*/
|
|
67
67
|
async delete(options) {
|
|
68
68
|
const command = {
|
|
@@ -73,7 +73,7 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
73
73
|
options,
|
|
74
74
|
};
|
|
75
75
|
return this._executeCommand(command, async () => {
|
|
76
|
-
const filter = SQBAdapter.
|
|
76
|
+
const filter = SQBAdapter.prepareFilter([
|
|
77
77
|
await this._getCommonFilter(command),
|
|
78
78
|
command.options?.filter,
|
|
79
79
|
]);
|
|
@@ -82,10 +82,10 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
82
82
|
});
|
|
83
83
|
}
|
|
84
84
|
/**
|
|
85
|
-
* Checks
|
|
85
|
+
* Checks whether the singleton record exists.
|
|
86
86
|
*
|
|
87
|
-
* @param
|
|
88
|
-
* @
|
|
87
|
+
* @param options - Optional query options.
|
|
88
|
+
* @returns `true` if the record exists, `false` otherwise.
|
|
89
89
|
*/
|
|
90
90
|
async exists(options) {
|
|
91
91
|
const command = {
|
|
@@ -96,7 +96,7 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
96
96
|
options,
|
|
97
97
|
};
|
|
98
98
|
return this._executeCommand(command, async () => {
|
|
99
|
-
const filter = SQBAdapter.
|
|
99
|
+
const filter = SQBAdapter.prepareFilter([
|
|
100
100
|
await this._getCommonFilter(command),
|
|
101
101
|
command.options?.filter,
|
|
102
102
|
]);
|
|
@@ -113,7 +113,7 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
113
113
|
options,
|
|
114
114
|
};
|
|
115
115
|
return this._executeCommand(command, async () => {
|
|
116
|
-
const filter = SQBAdapter.
|
|
116
|
+
const filter = SQBAdapter.prepareFilter([
|
|
117
117
|
await this._getCommonFilter(command),
|
|
118
118
|
command.options?.filter,
|
|
119
119
|
]);
|
|
@@ -137,7 +137,7 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
137
137
|
options,
|
|
138
138
|
};
|
|
139
139
|
return this._executeCommand(command, async () => {
|
|
140
|
-
const filter = SQBAdapter.
|
|
140
|
+
const filter = SQBAdapter.prepareFilter([
|
|
141
141
|
await this._getCommonFilter(command),
|
|
142
142
|
command.options?.filter,
|
|
143
143
|
]);
|
|
@@ -146,11 +146,11 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
146
146
|
});
|
|
147
147
|
}
|
|
148
148
|
/**
|
|
149
|
-
* Updates the singleton
|
|
149
|
+
* Updates the singleton record without returning it.
|
|
150
150
|
*
|
|
151
|
-
* @param
|
|
152
|
-
* @param
|
|
153
|
-
* @returns
|
|
151
|
+
* @param input - The fields to update.
|
|
152
|
+
* @param options - Optional update options.
|
|
153
|
+
* @returns The number of records modified.
|
|
154
154
|
*/
|
|
155
155
|
async updateOnly(input, options) {
|
|
156
156
|
const command = {
|
|
@@ -162,7 +162,7 @@ export class SqbSingletonService extends SqbEntityService {
|
|
|
162
162
|
options,
|
|
163
163
|
};
|
|
164
164
|
return this._executeCommand(command, async () => {
|
|
165
|
-
const filter = SQBAdapter.
|
|
165
|
+
const filter = SQBAdapter.prepareFilter([
|
|
166
166
|
await this._getCommonFilter(command),
|
|
167
167
|
command.options?.filter,
|
|
168
168
|
]);
|