@opra/sqb 1.21.0 → 1.22.1

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 (34) hide show
  1. package/package.json +11 -28
  2. package/{esm/sqb-collection-service.js → sqb-collection-service.js} +4 -0
  3. package/{esm/sqb-entity-service.js → sqb-entity-service.js} +29 -2
  4. package/{esm/sqb-service-base.js → sqb-service-base.js} +4 -0
  5. package/{esm/sqb-singleton-service.js → sqb-singleton-service.js} +5 -0
  6. package/cjs/adapter-utils/prepare-filter.js +0 -108
  7. package/cjs/augmentation/datatype-factory.augmentation.js +0 -90
  8. package/cjs/augmentation/mapped-type.augmentation.js +0 -14
  9. package/cjs/augmentation/mixin-type.augmentation.js +0 -11
  10. package/cjs/index.js +0 -11
  11. package/cjs/package.json +0 -3
  12. package/cjs/sqb-adapter.js +0 -130
  13. package/cjs/sqb-collection-service.js +0 -355
  14. package/cjs/sqb-entity-service.js +0 -642
  15. package/cjs/sqb-service-base.js +0 -88
  16. package/cjs/sqb-singleton-service.js +0 -172
  17. package/esm/package.json +0 -3
  18. package/types/index.d.cts +0 -8
  19. /package/{types/adapter-utils → adapter-utils}/prepare-filter.d.ts +0 -0
  20. /package/{esm/adapter-utils → adapter-utils}/prepare-filter.js +0 -0
  21. /package/{types/augmentation → augmentation}/datatype-factory.augmentation.d.ts +0 -0
  22. /package/{esm/augmentation → augmentation}/datatype-factory.augmentation.js +0 -0
  23. /package/{types/augmentation → augmentation}/mapped-type.augmentation.d.ts +0 -0
  24. /package/{esm/augmentation → augmentation}/mapped-type.augmentation.js +0 -0
  25. /package/{types/augmentation → augmentation}/mixin-type.augmentation.d.ts +0 -0
  26. /package/{esm/augmentation → augmentation}/mixin-type.augmentation.js +0 -0
  27. /package/{types/index.d.ts → index.d.ts} +0 -0
  28. /package/{esm/index.js → index.js} +0 -0
  29. /package/{types/sqb-adapter.d.ts → sqb-adapter.d.ts} +0 -0
  30. /package/{esm/sqb-adapter.js → sqb-adapter.js} +0 -0
  31. /package/{types/sqb-collection-service.d.ts → sqb-collection-service.d.ts} +0 -0
  32. /package/{types/sqb-entity-service.d.ts → sqb-entity-service.d.ts} +0 -0
  33. /package/{types/sqb-service-base.d.ts → sqb-service-base.d.ts} +0 -0
  34. /package/{types/sqb-singleton-service.d.ts → sqb-singleton-service.d.ts} +0 -0
@@ -1,88 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SqbServiceBase = void 0;
4
- const core_1 = require("@opra/core");
5
- const connect_1 = require("@sqb/connect");
6
- const transactionKey = Symbol.for('transaction');
7
- /**
8
- * @class SqbServiceBase
9
- * @template T - The data type class type of the resource
10
- */
11
- class SqbServiceBase extends core_1.ServiceBase {
12
- /**
13
- * Constructs a new instance
14
- *
15
- * @param [options] - The options for the service.
16
- * @constructor
17
- */
18
- constructor(options) {
19
- super();
20
- this.db = options?.db;
21
- }
22
- /**
23
- * Executes the provided function within a transaction.
24
- *
25
- * @param callback - The function to be executed within the transaction.
26
- */
27
- async withTransaction(callback) {
28
- const ctx = this.context;
29
- let closeSessionOnFinish = false;
30
- let connection = ctx[transactionKey];
31
- if (!connection) {
32
- /** Determine the SqbClient or SqbConnection instance */
33
- const db = await this.getConnection();
34
- if (db instanceof connect_1.SqbConnection) {
35
- connection = db;
36
- }
37
- else {
38
- /** Acquire a connection. New connection should be at the end */
39
- connection = await db.acquire({ autoCommit: false });
40
- closeSessionOnFinish = true;
41
- }
42
- /** Store transaction connection in current context */
43
- ctx[transactionKey] = connection;
44
- }
45
- const oldInTransaction = connection.inTransaction;
46
- connection.retain();
47
- try {
48
- if (!oldInTransaction)
49
- await connection.startTransaction();
50
- const out = await callback(connection, this);
51
- if (!oldInTransaction && connection.inTransaction)
52
- await connection.commit();
53
- return out;
54
- }
55
- catch (e) {
56
- if (!oldInTransaction && connection.inTransaction)
57
- await connection.rollback();
58
- throw e;
59
- }
60
- finally {
61
- delete ctx[transactionKey];
62
- /** Release connection */
63
- if (closeSessionOnFinish) {
64
- await connection.close();
65
- }
66
- else
67
- connection.release();
68
- }
69
- }
70
- /**
71
- * Retrieves the database connection.
72
- *
73
- * @protected
74
- *
75
- * @throws {Error} If the context or database is not set.
76
- */
77
- getConnection() {
78
- const ctx = this.context;
79
- let db = ctx[transactionKey];
80
- if (db)
81
- return db;
82
- db = typeof this.db === 'function' ? this.db(this) : this.db;
83
- if (db)
84
- return db;
85
- throw new Error(`Database not set!`);
86
- }
87
- }
88
- exports.SqbServiceBase = SqbServiceBase;
@@ -1,172 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SqbSingletonService = void 0;
4
- const common_1 = require("@opra/common");
5
- const connect_1 = require("@sqb/connect");
6
- const sqb_adapter_js_1 = require("./sqb-adapter.js");
7
- const sqb_entity_service_js_1 = require("./sqb-entity-service.js");
8
- /**
9
- * @class SqbSingletonService
10
- * @template T - The data type class type of the resource
11
- */
12
- class SqbSingletonService extends sqb_entity_service_js_1.SqbEntityService {
13
- /**
14
- * Constructs a new instance
15
- *
16
- * @param {Type | string} dataType - The data type of the array elements.
17
- * @param {SqbSingletonService.Options} [options] - The options for the array service.
18
- * @constructor
19
- */
20
- constructor(dataType, options) {
21
- super(dataType, options);
22
- this.id = options?.id || 1;
23
- }
24
- /**
25
- * Asserts the existence of a resource based on the given options.
26
- *
27
- * @returns {Promise<void>} A Promise that resolves when the resource exists.
28
- * @throws {ResourceNotAvailableError} If the resource does not exist.
29
- */
30
- async assert(options) {
31
- if (!(await this.exists(options)))
32
- throw new common_1.ResourceNotAvailableError(this.getResourceName());
33
- }
34
- async create(input, options) {
35
- const command = {
36
- crud: 'create',
37
- method: 'create',
38
- byId: false,
39
- input,
40
- options,
41
- };
42
- return this._executeCommand(command, async () => {
43
- const primaryFields = connect_1.EntityMetadata.getPrimaryIndexColumns(this.entityMetadata);
44
- const data = { ...command.input };
45
- if (primaryFields.length > 1) {
46
- if (typeof primaryFields !== 'object') {
47
- throw new TypeError(`"${this.entityMetadata.name}" should has multiple primary key fields. So you should provide and object that contains key fields`);
48
- }
49
- for (const field of primaryFields) {
50
- data[field.name] = this.id[field.name];
51
- }
52
- }
53
- else
54
- data[primaryFields[0].name] = this.id;
55
- command.input = data;
56
- return await this._create(command);
57
- });
58
- }
59
- /**
60
- * Deletes the singleton record
61
- *
62
- * @param {SqbSingletonService.DeleteOptions} [options] - The options object
63
- * @return {Promise<number>} - A Promise that resolves to the number of records deleted
64
- */
65
- async delete(options) {
66
- const command = {
67
- crud: 'delete',
68
- method: 'delete',
69
- byId: true,
70
- documentId: this.id,
71
- options,
72
- };
73
- return this._executeCommand(command, async () => {
74
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([
75
- await this._getCommonFilter(command),
76
- command.options?.filter,
77
- ]);
78
- command.options = { ...command.options, filter };
79
- return this._delete(command);
80
- });
81
- }
82
- /**
83
- * Checks if the singleton record exists.
84
- *
85
- * @param {SqbSingletonService.ExistsOptions} [options] - The options for the query (optional).
86
- * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
87
- */
88
- async exists(options) {
89
- const command = {
90
- crud: 'read',
91
- method: 'exists',
92
- byId: true,
93
- documentId: this.id,
94
- options,
95
- };
96
- return this._executeCommand(command, async () => {
97
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([
98
- await this._getCommonFilter(command),
99
- command.options?.filter,
100
- ]);
101
- command.options = { ...command.options, filter };
102
- return this._exists(command);
103
- });
104
- }
105
- async find(options) {
106
- const command = {
107
- crud: 'read',
108
- method: 'findById',
109
- byId: true,
110
- documentId: this.id,
111
- options,
112
- };
113
- return this._executeCommand(command, async () => {
114
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([
115
- await this._getCommonFilter(command),
116
- command.options?.filter,
117
- ]);
118
- command.options = { ...command.options, filter };
119
- return this._findById(command);
120
- });
121
- }
122
- async get(options) {
123
- const out = await this.find(options);
124
- if (!out)
125
- throw new common_1.ResourceNotAvailableError(this.getResourceName());
126
- return out;
127
- }
128
- async update(input, options) {
129
- const command = {
130
- crud: 'update',
131
- method: 'update',
132
- documentId: this.id,
133
- byId: true,
134
- input,
135
- options,
136
- };
137
- return this._executeCommand(command, async () => {
138
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([
139
- await this._getCommonFilter(command),
140
- command.options?.filter,
141
- ]);
142
- command.options = { ...command.options, filter };
143
- return this._update(command);
144
- });
145
- }
146
- /**
147
- * Updates the singleton and returns updated record count
148
- *
149
- * @param {PatchDTO<T>} input - The partial input data to update the document with.
150
- * @param {SqbSingletonService.UpdateOptions} options - The options for updating the document.
151
- * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
152
- */
153
- async updateOnly(input, options) {
154
- const command = {
155
- crud: 'update',
156
- method: 'update',
157
- documentId: this.id,
158
- byId: true,
159
- input,
160
- options,
161
- };
162
- return this._executeCommand(command, async () => {
163
- const filter = sqb_adapter_js_1.SQBAdapter.parseFilter([
164
- await this._getCommonFilter(command),
165
- command.options?.filter,
166
- ]);
167
- command.options = { ...command.options, filter };
168
- return this._updateOnly(command);
169
- });
170
- }
171
- }
172
- exports.SqbSingletonService = SqbSingletonService;
package/esm/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
package/types/index.d.cts DELETED
@@ -1,8 +0,0 @@
1
- import './augmentation/datatype-factory.augmentation.js';
2
- import './augmentation/mapped-type.augmentation.js';
3
- import './augmentation/mixin-type.augmentation.js';
4
- export * from './sqb-adapter.js';
5
- export * from './sqb-collection-service.js';
6
- export * from './sqb-entity-service.js';
7
- export * from './sqb-service-base.js';
8
- export * from './sqb-singleton-service.js';
File without changes
File without changes
File without changes
File without changes