@sqb/connect 4.14.0 → 4.15.0
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/client/sqb-connection.js +1 -1
- package/cjs/orm/repository.class.js +77 -19
- package/esm/client/adapter.js +2 -1
- package/esm/client/cursor-stream.js +9 -4
- package/esm/client/cursor.js +22 -17
- package/esm/client/extensions.js +5 -1
- package/esm/client/field-info-map.js +5 -1
- package/esm/client/helpers.js +19 -12
- package/esm/client/sqb-client.js +32 -27
- package/esm/client/sqb-connection.js +42 -37
- package/esm/client/types.js +5 -1
- package/esm/index.js +40 -32
- package/esm/orm/backward.js +19 -12
- package/esm/orm/base-entity.js +10 -6
- package/esm/orm/commands/command.helper.js +34 -28
- package/esm/orm/commands/count.command.js +10 -6
- package/esm/orm/commands/create.command.js +18 -14
- package/esm/orm/commands/delete.command.js +10 -6
- package/esm/orm/commands/find.command.js +34 -30
- package/esm/orm/commands/row-converter.js +7 -3
- package/esm/orm/commands/update.command.js +18 -14
- package/esm/orm/decorators/column.decorator.js +10 -7
- package/esm/orm/decorators/embedded.decorator.js +7 -4
- package/esm/orm/decorators/entity.decorator.js +56 -53
- package/esm/orm/decorators/events.decorator.js +27 -19
- package/esm/orm/decorators/foreignkey.decorator.js +7 -4
- package/esm/orm/decorators/index.decorator.js +9 -6
- package/esm/orm/decorators/link.decorator.js +11 -8
- package/esm/orm/decorators/primarykey.decorator.js +11 -8
- package/esm/orm/decorators/transform.decorator.js +11 -7
- package/esm/orm/model/association-field-metadata.js +8 -4
- package/esm/orm/model/association-node.js +6 -2
- package/esm/orm/model/association.js +20 -16
- package/esm/orm/model/column-field-metadata.js +8 -4
- package/esm/orm/model/embedded-field-metadata.js +7 -4
- package/esm/orm/model/entity-metadata.js +50 -47
- package/esm/orm/model/field-metadata.js +2 -1
- package/esm/orm/model/index-metadata.js +2 -1
- package/esm/orm/model/link-chain.js +8 -4
- package/esm/orm/orm.const.js +6 -3
- package/esm/orm/orm.type.js +2 -1
- package/esm/orm/repository.class.js +103 -41
- package/esm/orm/util/apply-mixins.js +4 -1
- package/esm/orm/util/extract-keyvalues.js +9 -6
- package/esm/orm/util/orm.helper.js +20 -10
- package/esm/orm/util/parse-fields-projection.js +11 -6
- package/esm/orm/util/serialize-field.js +13 -10
- package/package.json +30 -24
- package/types/orm/repository.class.d.ts +110 -11
- package/cjs/package.json +0 -3
|
@@ -78,7 +78,7 @@ class SqbConnection extends (0, strict_typed_events_1.TypedEventEmitterClass)(st
|
|
|
78
78
|
await this.emitAsyncSerial('close');
|
|
79
79
|
const intlcon = this._intlcon;
|
|
80
80
|
this._intlcon = undefined;
|
|
81
|
-
this.client.pool.release(intlcon, e => {
|
|
81
|
+
this.client.pool.release(intlcon, (e) => {
|
|
82
82
|
if (e)
|
|
83
83
|
this.client.emit('error', e);
|
|
84
84
|
});
|
|
@@ -9,6 +9,10 @@ const delete_command_js_1 = require("./commands/delete.command.js");
|
|
|
9
9
|
const find_command_js_1 = require("./commands/find.command.js");
|
|
10
10
|
const update_command_js_1 = require("./commands/update.command.js");
|
|
11
11
|
const extract_keyvalues_js_1 = require("./util/extract-keyvalues.js");
|
|
12
|
+
/**
|
|
13
|
+
* @class Repository
|
|
14
|
+
* @template T - The data type class type of the record
|
|
15
|
+
*/
|
|
12
16
|
class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(strict_typed_events_1.AsyncEventEmitter) {
|
|
13
17
|
constructor(entityDef, executor, schema) {
|
|
14
18
|
super();
|
|
@@ -22,29 +26,74 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
22
26
|
get type() {
|
|
23
27
|
return this._entity.ctor;
|
|
24
28
|
}
|
|
25
|
-
create(
|
|
29
|
+
create(input, options) {
|
|
26
30
|
return this._execute(async (connection) => {
|
|
27
|
-
const keyValue = await this._create(
|
|
31
|
+
const keyValue = await this._create(input, { ...options, connection, returning: true });
|
|
28
32
|
const result = keyValue && (await this._find(keyValue, { ...options, connection }));
|
|
29
33
|
if (!result)
|
|
30
34
|
throw new Error('Unable to insert new row');
|
|
31
35
|
return result;
|
|
32
36
|
}, options);
|
|
33
37
|
}
|
|
34
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new resource but returns nothing
|
|
40
|
+
*
|
|
41
|
+
* @param {PartialDTO<T>} input - The input data
|
|
42
|
+
* @param {Repository.CreateOptions} [options] - The options object
|
|
43
|
+
* @throws {Error} if an unknown error occurs while creating the resource
|
|
44
|
+
*/
|
|
45
|
+
createOnly(input, options) {
|
|
35
46
|
return this._execute(async (connection) => {
|
|
36
|
-
await this._create(
|
|
47
|
+
await this._create(input, { ...options, connection, returning: false });
|
|
37
48
|
}, options);
|
|
38
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns the count of records based on the provided options
|
|
52
|
+
*
|
|
53
|
+
* @param {Repository.CountOptions} options - The options for the count operation.
|
|
54
|
+
* @return {Promise<number>} - A promise that resolves to the count of records
|
|
55
|
+
*/
|
|
56
|
+
count(options) {
|
|
57
|
+
return this._execute(async (connection) => this._count({ ...options, connection }), options);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Deletes a record from the collection.
|
|
61
|
+
*
|
|
62
|
+
* @param {any} keyValue - The ID of the resource to delete.
|
|
63
|
+
* @param {Repository.DeleteOptions} [options] - Optional delete options.
|
|
64
|
+
* @return {Promise<boolean>} - A Promise that resolves true or false. True when resource deleted.
|
|
65
|
+
*/
|
|
66
|
+
delete(keyValue, options) {
|
|
67
|
+
return this._execute(async (connection) => this._delete(keyValue, { ...options, connection }), options);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
71
|
+
*
|
|
72
|
+
* @param {Repository.DeleteManyOptions} options - The options for the delete operation.
|
|
73
|
+
* @return {Promise<number>} - A promise that resolves to the number of resources deleted.
|
|
74
|
+
*/
|
|
75
|
+
deleteMany(options) {
|
|
76
|
+
return this._execute(async (connection) => this._deleteMany({ ...options, connection }), options);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Checks if a record with the given id exists.
|
|
80
|
+
*
|
|
81
|
+
* @param {any} keyValue - The id of the object to check.
|
|
82
|
+
* @param {Repository.ExistsOptions} [options] - The options for the query (optional).
|
|
83
|
+
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
|
|
84
|
+
*/
|
|
39
85
|
exists(keyValue, options) {
|
|
40
86
|
return this._execute(async (connection) => this._exists(keyValue, { ...options, connection }), options);
|
|
41
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Checks if a record with the given arguments exists.
|
|
90
|
+
*
|
|
91
|
+
* @param {Repository.ExistsOptions} [options] - The options for the query (optional).
|
|
92
|
+
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not.
|
|
93
|
+
*/
|
|
42
94
|
existsOne(options) {
|
|
43
95
|
return this._execute(async (connection) => this._existsOne({ ...options, connection }), options);
|
|
44
96
|
}
|
|
45
|
-
count(options) {
|
|
46
|
-
return this._execute(async (connection) => this._count({ ...options, connection }), options);
|
|
47
|
-
}
|
|
48
97
|
findById(keyValue, options) {
|
|
49
98
|
return this._execute(async (connection) => this._find(keyValue, { ...options, connection }), options);
|
|
50
99
|
}
|
|
@@ -57,25 +106,34 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
57
106
|
findMany(options) {
|
|
58
107
|
return this._execute(async (connection) => this._findMany({ ...options, connection }), options);
|
|
59
108
|
}
|
|
60
|
-
|
|
61
|
-
return this._execute(async (connection) => this._delete(keyValue, { ...options, connection }), options);
|
|
62
|
-
}
|
|
63
|
-
deleteMany(options) {
|
|
64
|
-
return this._execute(async (connection) => this._deleteMany({ ...options, connection }), options);
|
|
65
|
-
}
|
|
66
|
-
update(keyValue, values, options) {
|
|
109
|
+
update(keyValue, input, options) {
|
|
67
110
|
return this._execute(async (connection) => {
|
|
68
111
|
const opts = { ...options, connection };
|
|
69
|
-
const keyValues = await this._update(keyValue,
|
|
112
|
+
const keyValues = await this._update(keyValue, input, opts);
|
|
70
113
|
if (keyValues)
|
|
71
114
|
return this._find(keyValues, opts);
|
|
72
115
|
}, options);
|
|
73
116
|
}
|
|
74
|
-
|
|
75
|
-
|
|
117
|
+
/**
|
|
118
|
+
* Updates a record in the collection with the specified ID and returns updated record count
|
|
119
|
+
*
|
|
120
|
+
* @param {any} keyValue - The ID of the document to update.
|
|
121
|
+
* @param {PatchDTO<T>} input - The partial input data to update the document with.
|
|
122
|
+
* @param {Repository.UpdateOptions} options - The options for updating the document.
|
|
123
|
+
* @returns {Promise<number>} - A Promise that resolves true or false. True when resource updated.
|
|
124
|
+
*/
|
|
125
|
+
updateOnly(keyValue, input, options) {
|
|
126
|
+
return this._execute(async (connection) => !!(await this._update(keyValue, input, { ...options, connection })), options);
|
|
76
127
|
}
|
|
77
|
-
|
|
78
|
-
|
|
128
|
+
/**
|
|
129
|
+
* Updates multiple records in the collection based on the specified input and options.
|
|
130
|
+
*
|
|
131
|
+
* @param {PatchDTO<T>} input - The partial input to update the documents with.
|
|
132
|
+
* @param {Repository.UpdateManyOptions} options - The options for updating the documents.
|
|
133
|
+
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
134
|
+
*/
|
|
135
|
+
updateMany(input, options) {
|
|
136
|
+
return this._execute(async (connection) => this._updateMany(input, { ...options, connection }));
|
|
79
137
|
}
|
|
80
138
|
async _execute(fn, opts) {
|
|
81
139
|
let connection = opts?.connection;
|
package/esm/client/adapter.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CursorStream = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
6
|
+
const stream_1 = require("stream");
|
|
3
7
|
const inspect = Symbol.for('nodejs.util.inspect.custom');
|
|
4
|
-
const debug =
|
|
5
|
-
|
|
8
|
+
const debug = (0, debug_1.default)('sqb:cursorstream');
|
|
9
|
+
class CursorStream extends stream_1.Readable {
|
|
6
10
|
constructor(cursor, options) {
|
|
7
11
|
super(options);
|
|
8
12
|
this._rowNum = -1;
|
|
@@ -94,3 +98,4 @@ export class CursorStream extends Readable {
|
|
|
94
98
|
}
|
|
95
99
|
}
|
|
96
100
|
}
|
|
101
|
+
exports.CursorStream = CursorStream;
|
package/esm/client/cursor.js
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Cursor = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
6
|
+
const doublylinked_1 = tslib_1.__importDefault(require("doublylinked"));
|
|
7
|
+
const power_tasks_1 = require("power-tasks");
|
|
8
|
+
const putil_varhelpers_1 = require("putil-varhelpers");
|
|
9
|
+
const strict_typed_events_1 = require("strict-typed-events");
|
|
10
|
+
const cursor_stream_js_1 = require("./cursor-stream.js");
|
|
11
|
+
const helpers_js_1 = require("./helpers.js");
|
|
12
|
+
const debug = (0, debug_1.default)('sqb:cursor');
|
|
13
|
+
class Cursor extends (0, strict_typed_events_1.TypedEventEmitterClass)(strict_typed_events_1.AsyncEventEmitter) {
|
|
10
14
|
constructor(connection, fields, adapterCursor, request) {
|
|
11
15
|
super();
|
|
12
|
-
this._taskQueue = new TaskQueue();
|
|
13
|
-
this._fetchCache = new
|
|
16
|
+
this._taskQueue = new power_tasks_1.TaskQueue();
|
|
17
|
+
this._fetchCache = new doublylinked_1.default();
|
|
14
18
|
this._rowNum = 0;
|
|
15
19
|
this._fetchedAll = false;
|
|
16
20
|
this._fetchedRows = 0;
|
|
@@ -75,7 +79,7 @@ export class Cursor extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
75
79
|
if (this.fetchedRows)
|
|
76
80
|
throw new Error('Cache can be enabled before fetching rows');
|
|
77
81
|
if (!this._cache)
|
|
78
|
-
this._cache = new
|
|
82
|
+
this._cache = new doublylinked_1.default();
|
|
79
83
|
}
|
|
80
84
|
/**
|
|
81
85
|
* Closes cursor
|
|
@@ -157,7 +161,7 @@ export class Cursor extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
157
161
|
* Creates and returns a readable stream.
|
|
158
162
|
*/
|
|
159
163
|
toStream(options) {
|
|
160
|
-
return new CursorStream(this, options);
|
|
164
|
+
return new cursor_stream_js_1.CursorStream(this, options);
|
|
161
165
|
}
|
|
162
166
|
toString() {
|
|
163
167
|
return '[object ' + Object.getPrototypeOf(this).constructor.name + ']';
|
|
@@ -169,7 +173,7 @@ export class Cursor extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
169
173
|
*
|
|
170
174
|
*/
|
|
171
175
|
async _seek(step, silent) {
|
|
172
|
-
step = coerceToInt(step, 0);
|
|
176
|
+
step = (0, putil_varhelpers_1.coerceToInt)(step, 0);
|
|
173
177
|
if (!step || (step > 0 && this.isClosed))
|
|
174
178
|
return this.rowNum;
|
|
175
179
|
if (step < 0 && !this._cache)
|
|
@@ -226,9 +230,9 @@ export class Cursor extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
226
230
|
debug('Fetched %d rows from database', rows.length);
|
|
227
231
|
// Normalize rows
|
|
228
232
|
rows = this._request.objectRows
|
|
229
|
-
? normalizeRowsToObjectRows(this._fields, this._intlcur.rowType, rows, this._request)
|
|
230
|
-
: normalizeRowsToArrayRows(this._fields, this._intlcur.rowType, rows, this._request);
|
|
231
|
-
callFetchHooks(rows, this._request);
|
|
233
|
+
? (0, helpers_js_1.normalizeRowsToObjectRows)(this._fields, this._intlcur.rowType, rows, this._request)
|
|
234
|
+
: (0, helpers_js_1.normalizeRowsToArrayRows)(this._fields, this._intlcur.rowType, rows, this._request);
|
|
235
|
+
(0, helpers_js_1.callFetchHooks)(rows, this._request);
|
|
232
236
|
for (const [idx, row] of rows.entries()) {
|
|
233
237
|
this.emit('fetch', row, this._rowNum + idx + 1);
|
|
234
238
|
}
|
|
@@ -245,3 +249,4 @@ export class Cursor extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
245
249
|
return this.close();
|
|
246
250
|
}
|
|
247
251
|
}
|
|
252
|
+
exports.Cursor = Cursor;
|
package/esm/client/extensions.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AdapterRegistry = void 0;
|
|
4
|
+
class AdapterRegistry {
|
|
2
5
|
static get size() {
|
|
3
6
|
return this.adapters.length;
|
|
4
7
|
}
|
|
@@ -34,4 +37,5 @@ export class AdapterRegistry {
|
|
|
34
37
|
return !!this.adapters.find(x => x === extension);
|
|
35
38
|
}
|
|
36
39
|
}
|
|
40
|
+
exports.AdapterRegistry = AdapterRegistry;
|
|
37
41
|
AdapterRegistry.adapters = [];
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FieldInfoMap = void 0;
|
|
4
|
+
class FieldInfoMap {
|
|
2
5
|
constructor() {
|
|
3
6
|
Object.defineProperty(this, '_obj', {
|
|
4
7
|
enumerable: false,
|
|
@@ -46,3 +49,4 @@ export class FieldInfoMap {
|
|
|
46
49
|
return { ...this._obj };
|
|
47
50
|
}
|
|
48
51
|
}
|
|
52
|
+
exports.FieldInfoMap = FieldInfoMap;
|
package/esm/client/helpers.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applyNamingStrategy = applyNamingStrategy;
|
|
4
|
+
exports.wrapAdapterFields = wrapAdapterFields;
|
|
5
|
+
exports.normalizeRowsToObjectRows = normalizeRowsToObjectRows;
|
|
6
|
+
exports.normalizeRowsToArrayRows = normalizeRowsToArrayRows;
|
|
7
|
+
exports.callFetchHooks = callFetchHooks;
|
|
8
|
+
const putil_varhelpers_1 = require("putil-varhelpers");
|
|
9
|
+
const field_info_map_js_1 = require("./field-info-map.js");
|
|
10
|
+
function applyNamingStrategy(value, namingStrategy) {
|
|
4
11
|
if (typeof namingStrategy === 'string' && namingStrategy !== 'original') {
|
|
5
12
|
switch (namingStrategy.toLowerCase()) {
|
|
6
13
|
case 'lowercase':
|
|
@@ -9,13 +16,13 @@ export function applyNamingStrategy(value, namingStrategy) {
|
|
|
9
16
|
return value.toUpperCase();
|
|
10
17
|
case 'camelcase':
|
|
11
18
|
if (!value.match(/[a-z]/))
|
|
12
|
-
return camelCase(value.toLowerCase());
|
|
13
|
-
value = camelCase(value);
|
|
19
|
+
return (0, putil_varhelpers_1.camelCase)(value.toLowerCase());
|
|
20
|
+
value = (0, putil_varhelpers_1.camelCase)(value);
|
|
14
21
|
return value[0].toLowerCase() + value.substring(1);
|
|
15
22
|
case 'pascalcase':
|
|
16
23
|
if (!value.match(/[a-z]/))
|
|
17
|
-
return pascalCase(value.toLowerCase());
|
|
18
|
-
return pascalCase(value);
|
|
24
|
+
return (0, putil_varhelpers_1.pascalCase)(value.toLowerCase());
|
|
25
|
+
return (0, putil_varhelpers_1.pascalCase)(value);
|
|
19
26
|
default:
|
|
20
27
|
break;
|
|
21
28
|
}
|
|
@@ -24,13 +31,13 @@ export function applyNamingStrategy(value, namingStrategy) {
|
|
|
24
31
|
return namingStrategy(value);
|
|
25
32
|
return value;
|
|
26
33
|
}
|
|
27
|
-
|
|
34
|
+
function wrapAdapterFields(oldFields, fieldNaming) {
|
|
28
35
|
const mapFieldInfo = (f, index) => {
|
|
29
36
|
const name = applyNamingStrategy(f.fieldName, fieldNaming);
|
|
30
37
|
if (name)
|
|
31
38
|
return { ...f, name, index };
|
|
32
39
|
};
|
|
33
|
-
const result = new FieldInfoMap();
|
|
40
|
+
const result = new field_info_map_js_1.FieldInfoMap();
|
|
34
41
|
let i = 0;
|
|
35
42
|
oldFields.forEach(f => {
|
|
36
43
|
const x = mapFieldInfo(f, i);
|
|
@@ -41,10 +48,10 @@ export function wrapAdapterFields(oldFields, fieldNaming) {
|
|
|
41
48
|
});
|
|
42
49
|
return result;
|
|
43
50
|
}
|
|
44
|
-
|
|
51
|
+
function normalizeRowsToObjectRows(fields, rowType, oldRows, options) {
|
|
45
52
|
return normalizeRows(fields, rowType, oldRows, { ...options, objectRows: true });
|
|
46
53
|
}
|
|
47
|
-
|
|
54
|
+
function normalizeRowsToArrayRows(fields, rowType, oldRows, options) {
|
|
48
55
|
return normalizeRows(fields, rowType, oldRows, { ...options, objectRows: false });
|
|
49
56
|
}
|
|
50
57
|
function normalizeRows(fields, rowType, oldRows, options) {
|
|
@@ -115,7 +122,7 @@ function normalizeRows(fields, rowType, oldRows, options) {
|
|
|
115
122
|
return r;
|
|
116
123
|
});
|
|
117
124
|
}
|
|
118
|
-
|
|
125
|
+
function callFetchHooks(rows, request) {
|
|
119
126
|
const fetchHooks = request.fetchHooks;
|
|
120
127
|
if (!fetchHooks)
|
|
121
128
|
return;
|
package/esm/client/sqb-client.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqbClient = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
6
|
+
const lightning_pool_1 = require("lightning-pool");
|
|
7
|
+
const putil_varhelpers_1 = require("putil-varhelpers");
|
|
8
|
+
const strict_typed_events_1 = require("strict-typed-events");
|
|
9
|
+
const entity_metadata_js_1 = require("../orm/model/entity-metadata.js");
|
|
10
|
+
const repository_class_js_1 = require("../orm/repository.class.js");
|
|
11
|
+
const extensions_js_1 = require("./extensions.js");
|
|
12
|
+
const sqb_connection_js_1 = require("./sqb-connection.js");
|
|
13
|
+
const debug = (0, debug_1.default)('sqb:client');
|
|
10
14
|
const inspect = Symbol.for('nodejs.util.inspect.custom');
|
|
11
|
-
|
|
15
|
+
class SqbClient extends (0, strict_typed_events_1.TypedEventEmitterClass)(strict_typed_events_1.AsyncEventEmitter) {
|
|
12
16
|
constructor(config) {
|
|
13
17
|
super();
|
|
14
18
|
this._entities = {};
|
|
@@ -16,12 +20,12 @@ export class SqbClient extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
16
20
|
throw new TypeError('Configuration object required');
|
|
17
21
|
let adapter;
|
|
18
22
|
if (config.driver) {
|
|
19
|
-
adapter = AdapterRegistry.findDriver(config.driver);
|
|
23
|
+
adapter = extensions_js_1.AdapterRegistry.findDriver(config.driver);
|
|
20
24
|
if (!adapter)
|
|
21
25
|
throw new Error(`No database adapter registered for "${config.driver}" driver`);
|
|
22
26
|
}
|
|
23
27
|
else if (config.dialect) {
|
|
24
|
-
adapter = AdapterRegistry.findDialect(config.dialect);
|
|
28
|
+
adapter = extensions_js_1.AdapterRegistry.findDialect(config.dialect);
|
|
25
29
|
if (!adapter)
|
|
26
30
|
throw new Error(`No database adapter registered for "${config.dialect}" dialect`);
|
|
27
31
|
}
|
|
@@ -31,16 +35,16 @@ export class SqbClient extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
31
35
|
this._defaults = config.defaults || {};
|
|
32
36
|
const poolOptions = {};
|
|
33
37
|
const popts = config.pool || {};
|
|
34
|
-
poolOptions.acquireMaxRetries = coerceToInt(popts.acquireMaxRetries, 0);
|
|
35
|
-
poolOptions.acquireRetryWait = coerceToInt(popts.acquireRetryWait, 2000);
|
|
36
|
-
poolOptions.acquireTimeoutMillis = coerceToInt(popts.acquireTimeoutMillis, 0);
|
|
37
|
-
poolOptions.idleTimeoutMillis = coerceToInt(popts.idleTimeoutMillis, 30000);
|
|
38
|
-
poolOptions.max = coerceToInt(popts.max, 10);
|
|
39
|
-
poolOptions.maxQueue = coerceToInt(popts.maxQueue, 1000);
|
|
40
|
-
poolOptions.max = coerceToInt(popts.max, 10);
|
|
41
|
-
poolOptions.min = coerceToInt(popts.min, 0);
|
|
42
|
-
poolOptions.minIdle = coerceToInt(popts.minIdle, 0);
|
|
43
|
-
poolOptions.validation = coerceToBoolean(popts.validation, false);
|
|
38
|
+
poolOptions.acquireMaxRetries = (0, putil_varhelpers_1.coerceToInt)(popts.acquireMaxRetries, 0);
|
|
39
|
+
poolOptions.acquireRetryWait = (0, putil_varhelpers_1.coerceToInt)(popts.acquireRetryWait, 2000);
|
|
40
|
+
poolOptions.acquireTimeoutMillis = (0, putil_varhelpers_1.coerceToInt)(popts.acquireTimeoutMillis, 0);
|
|
41
|
+
poolOptions.idleTimeoutMillis = (0, putil_varhelpers_1.coerceToInt)(popts.idleTimeoutMillis, 30000);
|
|
42
|
+
poolOptions.max = (0, putil_varhelpers_1.coerceToInt)(popts.max, 10);
|
|
43
|
+
poolOptions.maxQueue = (0, putil_varhelpers_1.coerceToInt)(popts.maxQueue, 1000);
|
|
44
|
+
poolOptions.max = (0, putil_varhelpers_1.coerceToInt)(popts.max, 10);
|
|
45
|
+
poolOptions.min = (0, putil_varhelpers_1.coerceToInt)(popts.min, 0);
|
|
46
|
+
poolOptions.minIdle = (0, putil_varhelpers_1.coerceToInt)(popts.minIdle, 0);
|
|
47
|
+
poolOptions.validation = (0, putil_varhelpers_1.coerceToBoolean)(popts.validation, false);
|
|
44
48
|
const cfg = { ...config };
|
|
45
49
|
const poolFactory = {
|
|
46
50
|
create: () => adapter.connect(cfg),
|
|
@@ -48,7 +52,7 @@ export class SqbClient extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
48
52
|
reset: async (instance) => instance.reset(),
|
|
49
53
|
validate: instance => instance.test(),
|
|
50
54
|
};
|
|
51
|
-
this._pool = createPool(poolFactory, poolOptions);
|
|
55
|
+
this._pool = (0, lightning_pool_1.createPool)(poolFactory, poolOptions);
|
|
52
56
|
this._pool.on('closing', () => this.emit('closing'));
|
|
53
57
|
this._pool.on('close', () => this.emit('close'));
|
|
54
58
|
this._pool.on('terminate', () => this.emit('terminate'));
|
|
@@ -74,7 +78,7 @@ export class SqbClient extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
74
78
|
* Returns true if pool is closed
|
|
75
79
|
*/
|
|
76
80
|
get isClosed() {
|
|
77
|
-
return this._pool.state === PoolState.CLOSED;
|
|
81
|
+
return this._pool.state === lightning_pool_1.PoolState.CLOSED;
|
|
78
82
|
}
|
|
79
83
|
get pool() {
|
|
80
84
|
return this._pool;
|
|
@@ -93,7 +97,7 @@ export class SqbClient extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
93
97
|
const options = arg1;
|
|
94
98
|
const adapterConnection = await this._pool.acquire();
|
|
95
99
|
const opts = { autoCommit: this.defaults.autoCommit, ...options };
|
|
96
|
-
const connection = new SqbConnection(this, adapterConnection, opts);
|
|
100
|
+
const connection = new sqb_connection_js_1.SqbConnection(this, adapterConnection, opts);
|
|
97
101
|
await this.emitAsyncSerial('acquire', connection);
|
|
98
102
|
connection.on('execute', (request) => this.emit('execute', request));
|
|
99
103
|
connection.on('error', (error) => this.emit('error', error));
|
|
@@ -146,10 +150,10 @@ export class SqbClient extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
146
150
|
}
|
|
147
151
|
else
|
|
148
152
|
ctor = entity;
|
|
149
|
-
const entityDef = EntityMetadata.get(ctor);
|
|
153
|
+
const entityDef = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
150
154
|
if (!entityDef)
|
|
151
155
|
throw new Error(`You must provide an @Entity annotated constructor`);
|
|
152
|
-
return new Repository(entityDef, this, opts?.schema);
|
|
156
|
+
return new repository_class_js_1.Repository(entityDef, this, opts?.schema);
|
|
153
157
|
}
|
|
154
158
|
getEntity(name) {
|
|
155
159
|
return this._entities[name];
|
|
@@ -161,3 +165,4 @@ export class SqbClient extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
161
165
|
return this.toString();
|
|
162
166
|
}
|
|
163
167
|
}
|
|
168
|
+
exports.SqbClient = SqbClient;
|
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqbConnection = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const builder_1 = require("@sqb/builder");
|
|
6
|
+
const assert_1 = tslib_1.__importDefault(require("assert"));
|
|
7
|
+
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
8
|
+
const power_tasks_1 = require("power-tasks");
|
|
9
|
+
const putil_varhelpers_1 = require("putil-varhelpers");
|
|
10
|
+
const strict_typed_events_1 = require("strict-typed-events");
|
|
11
|
+
const entity_metadata_js_1 = require("../orm/model/entity-metadata.js");
|
|
12
|
+
const repository_class_js_1 = require("../orm/repository.class.js");
|
|
13
|
+
const cursor_js_1 = require("./cursor.js");
|
|
14
|
+
const helpers_js_1 = require("./helpers.js");
|
|
15
|
+
const debug = (0, debug_1.default)('sqb:connection');
|
|
16
|
+
class SqbConnection extends (0, strict_typed_events_1.TypedEventEmitterClass)(strict_typed_events_1.AsyncEventEmitter) {
|
|
13
17
|
constructor(client, adapterConnection, options) {
|
|
14
18
|
super();
|
|
15
19
|
this.client = client;
|
|
16
|
-
this._tasks = new TaskQueue();
|
|
20
|
+
this._tasks = new power_tasks_1.TaskQueue();
|
|
17
21
|
this._inTransaction = false;
|
|
18
22
|
this._refCount = 1;
|
|
19
23
|
this._intlcon = adapterConnection;
|
|
@@ -74,7 +78,7 @@ export class SqbConnection extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
74
78
|
await this.emitAsyncSerial('close');
|
|
75
79
|
const intlcon = this._intlcon;
|
|
76
80
|
this._intlcon = undefined;
|
|
77
|
-
this.client.pool.release(intlcon, e => {
|
|
81
|
+
this.client.pool.release(intlcon, (e) => {
|
|
78
82
|
if (e)
|
|
79
83
|
this.client.emit('error', e);
|
|
80
84
|
});
|
|
@@ -94,26 +98,26 @@ export class SqbConnection extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
94
98
|
}
|
|
95
99
|
else
|
|
96
100
|
ctor = entity;
|
|
97
|
-
const entityDef = EntityMetadata.get(ctor);
|
|
101
|
+
const entityDef = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
98
102
|
if (!entityDef)
|
|
99
103
|
throw new Error(`You must provide an @Entity annotated constructor`);
|
|
100
|
-
return new Repository(entityDef, this, opts?.schema);
|
|
104
|
+
return new repository_class_js_1.Repository(entityDef, this, opts?.schema);
|
|
101
105
|
}
|
|
102
106
|
async getSchema() {
|
|
103
|
-
|
|
104
|
-
|
|
107
|
+
assert_1.default.ok(this._intlcon, `Can't set schema, because connection is released`);
|
|
108
|
+
assert_1.default.ok(this._intlcon.getSchema, `${this.client.dialect} adapter does have Schema support`);
|
|
105
109
|
return await this._intlcon.getSchema();
|
|
106
110
|
}
|
|
107
111
|
async setSchema(schema) {
|
|
108
|
-
|
|
109
|
-
|
|
112
|
+
assert_1.default.ok(this._intlcon, `Can't set schema, because connection is released`);
|
|
113
|
+
assert_1.default.ok(this._intlcon.setSchema, `${this.client.dialect} adapter does have Schema support`);
|
|
110
114
|
await this._intlcon.setSchema(schema);
|
|
111
115
|
}
|
|
112
116
|
/**
|
|
113
117
|
* Executes a query
|
|
114
118
|
*/
|
|
115
119
|
async _execute(query, options) {
|
|
116
|
-
|
|
120
|
+
assert_1.default.ok(this._intlcon, `Can't execute query, because connection is released`);
|
|
117
121
|
const intlcon = this._intlcon;
|
|
118
122
|
this.retain();
|
|
119
123
|
try {
|
|
@@ -140,16 +144,16 @@ export class SqbConnection extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
140
144
|
throw new Error('Adapter did not returned fields info');
|
|
141
145
|
if (!response.rowType)
|
|
142
146
|
throw new Error('Adapter did not returned rowType');
|
|
143
|
-
result.fields = wrapAdapterFields(response.fields, request.fieldNaming);
|
|
147
|
+
result.fields = (0, helpers_js_1.wrapAdapterFields)(response.fields, request.fieldNaming);
|
|
144
148
|
result.rowType = response.rowType;
|
|
145
149
|
if (response.rows) {
|
|
146
150
|
result.rows = request.objectRows
|
|
147
|
-
? normalizeRowsToObjectRows(result.fields, response.rowType, response.rows, request)
|
|
148
|
-
: normalizeRowsToArrayRows(result.fields, response.rowType, response.rows, request);
|
|
149
|
-
callFetchHooks(result.rows, request);
|
|
151
|
+
? (0, helpers_js_1.normalizeRowsToObjectRows)(result.fields, response.rowType, response.rows, request)
|
|
152
|
+
: (0, helpers_js_1.normalizeRowsToArrayRows)(result.fields, response.rowType, response.rows, request);
|
|
153
|
+
(0, helpers_js_1.callFetchHooks)(result.rows, request);
|
|
150
154
|
}
|
|
151
155
|
else if (response.cursor) {
|
|
152
|
-
const cursor = (result.cursor = new Cursor(this, result.fields, response.cursor, request));
|
|
156
|
+
const cursor = (result.cursor = new cursor_js_1.Cursor(this, result.fields, response.cursor, request));
|
|
153
157
|
const hook = () => cursor.close();
|
|
154
158
|
cursor.once('close', () => this.off('close', hook));
|
|
155
159
|
this.on('close', hook);
|
|
@@ -227,20 +231,20 @@ export class SqbConnection extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
227
231
|
sql: '',
|
|
228
232
|
autoCommit: this.inTransaction
|
|
229
233
|
? false
|
|
230
|
-
: coerceToBoolean(coalesce(options.autoCommit, this._options?.autoCommit, defaults.autoCommit), true),
|
|
231
|
-
cursor: coerceToBoolean(coalesce(options.cursor, defaults.cursor), false),
|
|
232
|
-
objectRows: coerceToBoolean(coalesce(options.objectRows, defaults.objectRows), true),
|
|
233
|
-
ignoreNulls: coerceToBoolean(coalesce(options.ignoreNulls, defaults.ignoreNulls), false),
|
|
234
|
-
fetchRows: coerceToInt(coalesce(options.fetchRows, defaults.fetchRows), 100),
|
|
235
|
-
fieldNaming: coalesce(options.namingStrategy, defaults.fieldNaming),
|
|
236
|
-
transform: coalesce(options.transform, defaults.transform),
|
|
237
|
-
showSql: coerceToBoolean(coalesce(options.showSql, defaults.showSql), false),
|
|
238
|
-
prettyPrint: coerceToBoolean(coalesce(options.prettyPrint, defaults.prettyPrint), false),
|
|
239
|
-
action: coerceToString(options.action),
|
|
234
|
+
: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.autoCommit, this._options?.autoCommit, defaults.autoCommit), true),
|
|
235
|
+
cursor: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.cursor, defaults.cursor), false),
|
|
236
|
+
objectRows: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.objectRows, defaults.objectRows), true),
|
|
237
|
+
ignoreNulls: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.ignoreNulls, defaults.ignoreNulls), false),
|
|
238
|
+
fetchRows: (0, putil_varhelpers_1.coerceToInt)((0, putil_varhelpers_1.coalesce)(options.fetchRows, defaults.fetchRows), 100),
|
|
239
|
+
fieldNaming: (0, putil_varhelpers_1.coalesce)(options.namingStrategy, defaults.fieldNaming),
|
|
240
|
+
transform: (0, putil_varhelpers_1.coalesce)(options.transform, defaults.transform),
|
|
241
|
+
showSql: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.showSql, defaults.showSql), false),
|
|
242
|
+
prettyPrint: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.prettyPrint, defaults.prettyPrint), false),
|
|
243
|
+
action: (0, putil_varhelpers_1.coerceToString)(options.action),
|
|
240
244
|
fetchAsString: options.fetchAsString,
|
|
241
245
|
};
|
|
242
246
|
request.ignoreNulls = request.ignoreNulls && request.objectRows;
|
|
243
|
-
if (query instanceof classes.Query) {
|
|
247
|
+
if (query instanceof builder_1.classes.Query) {
|
|
244
248
|
if (this._intlcon.onGenerateQuery)
|
|
245
249
|
this._intlcon.onGenerateQuery(request, query);
|
|
246
250
|
const q = query.generate({
|
|
@@ -273,3 +277,4 @@ export class SqbConnection extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
273
277
|
return request;
|
|
274
278
|
}
|
|
275
279
|
}
|
|
280
|
+
exports.SqbConnection = SqbConnection;
|
package/esm/client/types.js
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataType = void 0;
|
|
4
|
+
var builder_1 = require("@sqb/builder");
|
|
5
|
+
Object.defineProperty(exports, "DataType", { enumerable: true, get: function () { return builder_1.DataType; } });
|