@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.
@@ -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
- * @class SqbServiceBase
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
- * Represents an SqbClient or SqbConnection object
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 [options] - The options for the service.
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 function within a transaction.
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 be executed within the transaction.
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
- /** Determine the SqbClient or SqbConnection instance */
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
- /** Acquire a connection. New connection should be at the end */
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
- /** Store transaction connection in current context */
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
- /** Release connection */
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
- * Retrieves the database connection.
72
+ * Returns the active database connection for the current context.
73
73
  *
74
- * @protected
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
- * @namespace SqbSingletonService
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
- * Represents options for "create" operation
13
- *
14
- * @interface
9
+ * Configuration options for SqbSingletonService.
15
10
  */
16
- interface CreateOptions extends SqbEntityService.CreateOptions {
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
- * Represents options for "delete" operation
20
- *
21
- * @interface
25
+ * The identifier used to locate the singleton record.
22
26
  */
23
- interface DeleteOptions extends SqbEntityService.DeleteOptions {
24
- }
27
+ id: SQBAdapter.IdOrIds;
25
28
  /**
26
- * Represents options for "exists" operation
29
+ * Constructs a new instance.
27
30
  *
28
- * @interface
31
+ * @param dataType - The entity class or its registered name.
32
+ * @param options - Options for the singleton service.
29
33
  */
30
- interface ExistsOptions extends SqbEntityService.ExistsOptions {
31
- }
34
+ constructor(dataType: Type<T> | string, options?: SqbSingletonService.Options);
32
35
  /**
33
- * Represents options for "find" operation
36
+ * Asserts that the singleton record exists.
37
+ * Throws {@link ResourceNotAvailableError} if it does not.
34
38
  *
35
- * @interface
39
+ * @param options - Optional existence check options.
40
+ * @throws {@link ResourceNotAvailableError} If the record does not exist.
36
41
  */
37
- interface FindOptions extends SqbEntityService.FindOneOptions {
38
- }
42
+ assert(options?: SqbEntityService.ExistsOptions): Promise<void>;
39
43
  /**
40
- * Represents options for "update" operation
44
+ * Creates the singleton record and returns it with the requested projection.
41
45
  *
42
- * @interface
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
- interface UpdateOptions extends SqbEntityService.UpdateOneOptions {
45
- }
50
+ create(input: PartialDTO<T>, options: RequiredSome<SqbEntityService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
46
51
  /**
47
- * Represents options for "updateOnly" operation
52
+ * Creates the singleton record and returns the full DTO.
48
53
  *
49
- * @interface
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
- interface UpdateOnlyOptions extends SqbEntityService.UpdateOneOptions {
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
- * Constructs a new instance
60
+ * Deletes the singleton record.
66
61
  *
67
- * @param {Type | string} dataType - The data type of the array elements.
68
- * @param {SqbSingletonService.Options} [options] - The options for the array service.
69
- * @constructor
62
+ * @param options - Optional delete options.
63
+ * @returns The number of records deleted.
70
64
  */
71
- constructor(dataType: Type<T> | string, options?: SqbSingletonService.Options);
65
+ delete(options?: SqbEntityService.DeleteOptions): Promise<number>;
72
66
  /**
73
- * Asserts the existence of a resource based on the given options.
67
+ * Checks whether the singleton record exists.
74
68
  *
75
- * @returns {Promise<void>} A Promise that resolves when the resource exists.
76
- * @throws {ResourceNotAvailableError} If the resource does not exist.
69
+ * @param options - Optional query options.
70
+ * @returns `true` if the record exists, `false` otherwise.
77
71
  */
78
- assert(options?: SqbSingletonService.ExistsOptions): Promise<void>;
72
+ exists(options?: SqbEntityService.ExistsOptions): Promise<boolean>;
79
73
  /**
80
- * Inserts a single record into the database.
74
+ * Finds the singleton record with the requested projection.
75
+ * Returns `undefined` if it does not exist.
81
76
  *
82
- * @param {PartialDTO<T>} input - The input data
83
- * @param {SqbSingletonService.CreateOptions} [options] - The options object
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
- create(input: PartialDTO<T>, options: RequiredSome<SqbSingletonService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
88
- create(input: PartialDTO<T>, options?: SqbSingletonService.CreateOptions): Promise<T>;
80
+ find(options: RequiredSome<SqbEntityService.FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>;
89
81
  /**
90
- * Deletes the singleton record
82
+ * Finds the singleton record. Returns `undefined` if it does not exist.
91
83
  *
92
- * @param {SqbSingletonService.DeleteOptions} [options] - The options object
93
- * @return {Promise<number>} - A Promise that resolves to the number of records deleted
84
+ * @param options - Optional query options.
85
+ * @returns The record as a full DTO, or `undefined`.
94
86
  */
95
- delete(options?: SqbSingletonService.DeleteOptions): Promise<number>;
87
+ find(options?: SqbEntityService.FindOneOptions): Promise<T | undefined>;
96
88
  /**
97
- * Checks if the singleton record exists.
89
+ * Retrieves the singleton record with the requested projection.
90
+ * Throws if the record does not exist.
98
91
  *
99
- * @param {SqbSingletonService.ExistsOptions} [options] - The options for the query (optional).
100
- * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
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
- exists(options?: SqbSingletonService.ExistsOptions): Promise<boolean>;
96
+ get(options: RequiredSome<SqbEntityService.FindOneOptions, 'projection'>): Promise<PartialDTO<T>>;
103
97
  /**
104
- * Finds the singleton record. Returns `undefined` if not found
98
+ * Retrieves the singleton record. Throws if it does not exist.
105
99
  *
106
- * @param {SqbSingletonService.FindOneOptions} options - The options for the query.
107
- * @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
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
- find(options: RequiredSome<SqbSingletonService.FindOptions, 'projection'>): Promise<PartialDTO<T> | undefined>;
110
- find(options?: SqbSingletonService.FindOptions): Promise<T | undefined>;
104
+ get(options?: SqbEntityService.FindOneOptions): Promise<T>;
111
105
  /**
112
- * Retrieves the singleton record. Throws error if not found.
106
+ * Updates the singleton record and returns it with the requested projection.
113
107
  *
114
- * @param {SqbSingletonService.FindOptions} [options] - Optional options for the `find` operation.
115
- * @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
116
- * or rejects with a ResourceNotFoundError if the document does not exist.
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
- get(options: RequiredSome<SqbSingletonService.FindOptions, 'projection'>): Promise<PartialDTO<T>>;
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 {PatchDTO<T>} input - The partial input object containing the fields to update.
125
- * @param {SqbSingletonService.UpdateOptions} [options] - The options for the update operation.
126
- * @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
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: RequiredSome<SqbSingletonService.UpdateOptions, 'projection'>): Promise<PartialDTO<T> | undefined>;
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 and returns updated record count
122
+ * Updates the singleton record without returning it.
133
123
  *
134
- * @param {PatchDTO<T>} input - The partial input data to update the document with.
135
- * @param {SqbSingletonService.UpdateOptions} options - The options for updating the document.
136
- * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
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?: SqbSingletonService.UpdateOnlyOptions): Promise<number>;
128
+ updateOnly(input: PatchDTO<T>, options?: SqbEntityService.UpdateOneOptions): Promise<number>;
139
129
  }
@@ -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
- * @class SqbSingletonService
7
- * @template T - The data type class type of the resource
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
- * Represents a unique identifier for singleton record
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 {Type | string} dataType - The data type of the array elements.
19
- * @param {SqbSingletonService.Options} [options] - The options for the array service.
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 existence of a resource based on the given options.
26
+ * Asserts that the singleton record exists.
27
+ * Throws {@link ResourceNotAvailableError} if it does not.
28
28
  *
29
- * @returns {Promise<void>} A Promise that resolves when the resource exists.
30
- * @throws {ResourceNotAvailableError} If the resource does not exist.
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 {SqbSingletonService.DeleteOptions} [options] - The options object
65
- * @return {Promise<number>} - A Promise that resolves to the number of records deleted
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.parseFilter([
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 if the singleton record exists.
85
+ * Checks whether the singleton record exists.
86
86
  *
87
- * @param {SqbSingletonService.ExistsOptions} [options] - The options for the query (optional).
88
- * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
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.parseFilter([
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.parseFilter([
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.parseFilter([
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 and returns updated record count
149
+ * Updates the singleton record without returning it.
150
150
  *
151
- * @param {PatchDTO<T>} input - The partial input data to update the document with.
152
- * @param {SqbSingletonService.UpdateOptions} options - The options for updating the document.
153
- * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
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.parseFilter([
165
+ const filter = SQBAdapter.prepareFilter([
166
166
  await this._getCommonFilter(command),
167
167
  command.options?.filter,
168
168
  ]);