@sqb/connect 4.20.7 → 4.21.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/{types/client → client}/cursor-stream.d.ts +1 -1
- package/{types/client → client}/cursor.d.ts +4 -4
- package/{types/client → client}/sqb-connection.d.ts +4 -4
- package/{esm/orm → orm}/commands/command.helper.js +2 -3
- package/{types/orm → orm}/commands/count.command.d.ts +2 -2
- package/{types/orm → orm}/commands/create.command.d.ts +1 -1
- package/{esm/orm → orm}/commands/create.command.js +3 -5
- package/{types/orm → orm}/commands/delete.command.d.ts +2 -2
- package/{types/orm → orm}/commands/find.command.d.ts +2 -2
- package/{esm/orm → orm}/commands/find.command.js +3 -4
- package/{types/orm → orm}/commands/row-converter.d.ts +1 -1
- package/{types/orm → orm}/commands/update.command.d.ts +2 -2
- package/{esm/orm → orm}/commands/update.command.js +3 -5
- package/{types/orm → orm}/model/association-field-metadata.d.ts +0 -5
- package/{types/orm → orm}/model/association.d.ts +3 -3
- package/{esm/orm → orm}/model/association.js +1 -1
- package/{types/orm → orm}/model/column-field-metadata.d.ts +2 -8
- package/{types/orm → orm}/model/embedded-field-metadata.d.ts +0 -1
- package/{esm/orm → orm}/model/embedded-field-metadata.js +0 -14
- package/{types/orm → orm}/model/entity-metadata.d.ts +3 -3
- package/{esm/orm → orm}/model/entity-metadata.js +21 -6
- package/orm/model/index-metadata.js +1 -0
- package/orm/orm.type.js +1 -0
- package/{types/orm → orm}/repository.class.d.ts +2 -2
- package/{esm/orm → orm}/repository.class.js +1 -1
- package/{types/orm → orm}/util/orm.helper.d.ts +5 -3
- package/{esm/orm → orm}/util/orm.helper.js +14 -0
- package/package.json +10 -26
- package/cjs/client/adapter.js +0 -2
- package/cjs/client/cursor-stream.js +0 -104
- package/cjs/client/cursor.js +0 -261
- package/cjs/client/extensions.js +0 -41
- package/cjs/client/field-info-map.js +0 -54
- package/cjs/client/helpers.js +0 -140
- package/cjs/client/sqb-client.js +0 -175
- package/cjs/client/sqb-connection.js +0 -293
- package/cjs/client/sqb-error.js +0 -15
- package/cjs/client/types.js +0 -5
- package/cjs/index.js +0 -41
- package/cjs/orm/backward.js +0 -24
- package/cjs/orm/base-entity.js +0 -29
- package/cjs/orm/commands/command.helper.js +0 -170
- package/cjs/orm/commands/count.command.js +0 -30
- package/cjs/orm/commands/create.command.js +0 -92
- package/cjs/orm/commands/delete.command.js +0 -30
- package/cjs/orm/commands/find.command.js +0 -301
- package/cjs/orm/commands/row-converter.js +0 -190
- package/cjs/orm/commands/update.command.js +0 -83
- package/cjs/orm/decorators/column.decorator.js +0 -32
- package/cjs/orm/decorators/embedded.decorator.js +0 -15
- package/cjs/orm/decorators/entity.decorator.js +0 -180
- package/cjs/orm/decorators/events.decorator.js +0 -63
- package/cjs/orm/decorators/foreignkey.decorator.js +0 -12
- package/cjs/orm/decorators/index.decorator.js +0 -20
- package/cjs/orm/decorators/link.decorator.js +0 -72
- package/cjs/orm/decorators/primarykey.decorator.js +0 -24
- package/cjs/orm/decorators/transform.decorator.js +0 -21
- package/cjs/orm/model/association-field-metadata.js +0 -24
- package/cjs/orm/model/association-node.js +0 -28
- package/cjs/orm/model/association.js +0 -141
- package/cjs/orm/model/column-field-metadata.js +0 -32
- package/cjs/orm/model/embedded-field-metadata.js +0 -28
- package/cjs/orm/model/entity-metadata.js +0 -327
- package/cjs/orm/model/field-metadata.js +0 -2
- package/cjs/orm/model/index-metadata.js +0 -2
- package/cjs/orm/model/link-chain.js +0 -47
- package/cjs/orm/orm.const.js +0 -6
- package/cjs/orm/orm.type.js +0 -2
- package/cjs/orm/repository.class.js +0 -293
- package/cjs/orm/util/apply-mixins.js +0 -16
- package/cjs/orm/util/extract-keyvalues.js +0 -63
- package/cjs/orm/util/orm.helper.js +0 -34
- package/cjs/orm/util/parse-fields-projection.js +0 -68
- package/cjs/orm/util/serialize-field.js +0 -49
- package/cjs/package.json +0 -3
- package/esm/orm/model/association-field-metadata.js +0 -20
- package/esm/orm/model/column-field-metadata.js +0 -28
- package/esm/package.json +0 -3
- package/types/index.d.cts +0 -33
- /package/{types/client → client}/adapter.d.ts +0 -0
- /package/{esm/client → client}/adapter.js +0 -0
- /package/{esm/client → client}/cursor-stream.js +0 -0
- /package/{esm/client → client}/cursor.js +0 -0
- /package/{types/client → client}/extensions.d.ts +0 -0
- /package/{esm/client → client}/extensions.js +0 -0
- /package/{types/client → client}/field-info-map.d.ts +0 -0
- /package/{esm/client → client}/field-info-map.js +0 -0
- /package/{types/client → client}/helpers.d.ts +0 -0
- /package/{esm/client → client}/helpers.js +0 -0
- /package/{types/client → client}/sqb-client.d.ts +0 -0
- /package/{esm/client → client}/sqb-client.js +0 -0
- /package/{esm/client → client}/sqb-connection.js +0 -0
- /package/{types/client → client}/sqb-error.d.ts +0 -0
- /package/{esm/client → client}/sqb-error.js +0 -0
- /package/{types/client → client}/types.d.ts +0 -0
- /package/{esm/client → client}/types.js +0 -0
- /package/{types/index.d.ts → index.d.ts} +0 -0
- /package/{esm/index.js → index.js} +0 -0
- /package/{types/orm → orm}/backward.d.ts +0 -0
- /package/{esm/orm → orm}/backward.js +0 -0
- /package/{types/orm → orm}/base-entity.d.ts +0 -0
- /package/{esm/orm → orm}/base-entity.js +0 -0
- /package/{types/orm → orm}/commands/command.helper.d.ts +0 -0
- /package/{esm/orm → orm}/commands/count.command.js +0 -0
- /package/{esm/orm → orm}/commands/delete.command.js +0 -0
- /package/{esm/orm → orm}/commands/row-converter.js +0 -0
- /package/{types/orm → orm}/decorators/column.decorator.d.ts +0 -0
- /package/{esm/orm → orm}/decorators/column.decorator.js +0 -0
- /package/{types/orm → orm}/decorators/embedded.decorator.d.ts +0 -0
- /package/{esm/orm → orm}/decorators/embedded.decorator.js +0 -0
- /package/{types/orm → orm}/decorators/entity.decorator.d.ts +0 -0
- /package/{esm/orm → orm}/decorators/entity.decorator.js +0 -0
- /package/{types/orm → orm}/decorators/events.decorator.d.ts +0 -0
- /package/{esm/orm → orm}/decorators/events.decorator.js +0 -0
- /package/{types/orm → orm}/decorators/foreignkey.decorator.d.ts +0 -0
- /package/{esm/orm → orm}/decorators/foreignkey.decorator.js +0 -0
- /package/{types/orm → orm}/decorators/index.decorator.d.ts +0 -0
- /package/{esm/orm → orm}/decorators/index.decorator.js +0 -0
- /package/{types/orm → orm}/decorators/link.decorator.d.ts +0 -0
- /package/{esm/orm → orm}/decorators/link.decorator.js +0 -0
- /package/{types/orm → orm}/decorators/primarykey.decorator.d.ts +0 -0
- /package/{esm/orm → orm}/decorators/primarykey.decorator.js +0 -0
- /package/{types/orm → orm}/decorators/transform.decorator.d.ts +0 -0
- /package/{esm/orm → orm}/decorators/transform.decorator.js +0 -0
- /package/{esm/orm/model/field-metadata.js → orm/model/association-field-metadata.js} +0 -0
- /package/{types/orm → orm}/model/association-node.d.ts +0 -0
- /package/{esm/orm → orm}/model/association-node.js +0 -0
- /package/{esm/orm/model/index-metadata.js → orm/model/column-field-metadata.js} +0 -0
- /package/{types/orm → orm}/model/field-metadata.d.ts +0 -0
- /package/{esm/orm/orm.type.js → orm/model/field-metadata.js} +0 -0
- /package/{types/orm → orm}/model/index-metadata.d.ts +0 -0
- /package/{types/orm → orm}/model/link-chain.d.ts +0 -0
- /package/{esm/orm → orm}/model/link-chain.js +0 -0
- /package/{types/orm → orm}/orm.const.d.ts +0 -0
- /package/{esm/orm → orm}/orm.const.js +0 -0
- /package/{types/orm → orm}/orm.type.d.ts +0 -0
- /package/{types/orm → orm}/util/apply-mixins.d.ts +0 -0
- /package/{esm/orm → orm}/util/apply-mixins.js +0 -0
- /package/{types/orm → orm}/util/extract-keyvalues.d.ts +0 -0
- /package/{esm/orm → orm}/util/extract-keyvalues.js +0 -0
- /package/{types/orm → orm}/util/parse-fields-projection.d.ts +0 -0
- /package/{esm/orm → orm}/util/parse-fields-projection.js +0 -0
- /package/{types/orm → orm}/util/serialize-field.d.ts +0 -0
- /package/{esm/orm → orm}/util/serialize-field.js +0 -0
|
@@ -1,293 +0,0 @@
|
|
|
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 sqb_error_js_1 = require("./sqb-error.js");
|
|
16
|
-
const debug = (0, debug_1.default)('sqb:connection');
|
|
17
|
-
class SqbConnection extends (0, strict_typed_events_1.TypedEventEmitterClass)(strict_typed_events_1.AsyncEventEmitter) {
|
|
18
|
-
client;
|
|
19
|
-
_intlcon;
|
|
20
|
-
_tasks = new power_tasks_1.TaskQueue();
|
|
21
|
-
_options;
|
|
22
|
-
_inTransaction = false;
|
|
23
|
-
_refCount = 1;
|
|
24
|
-
constructor(client, adapterConnection, options) {
|
|
25
|
-
super();
|
|
26
|
-
this.client = client;
|
|
27
|
-
this._intlcon = adapterConnection;
|
|
28
|
-
this._options = options || {};
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Returns session id
|
|
32
|
-
*/
|
|
33
|
-
get sessionId() {
|
|
34
|
-
return this._intlcon && this._intlcon.sessionId;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Returns reference counter value
|
|
38
|
-
*/
|
|
39
|
-
get refCount() {
|
|
40
|
-
return this._refCount;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Returns true if transaction started
|
|
44
|
-
*/
|
|
45
|
-
get inTransaction() {
|
|
46
|
-
if (this._intlcon && typeof this._intlcon.getInTransaction === 'function') {
|
|
47
|
-
this._inTransaction = this._intlcon.getInTransaction();
|
|
48
|
-
}
|
|
49
|
-
return this._inTransaction;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Increases internal reference counter to keep session alive
|
|
53
|
-
*/
|
|
54
|
-
retain() {
|
|
55
|
-
this._refCount++;
|
|
56
|
-
debug('[%s] retain | refCount: %s', this.sessionId, this._refCount);
|
|
57
|
-
this.emit('retain', this._refCount);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Decreases the internal reference counter.
|
|
61
|
-
* When reference count is 0, connection returns to the pool.
|
|
62
|
-
* Returns true if connection released.
|
|
63
|
-
*/
|
|
64
|
-
release() {
|
|
65
|
-
if (!this._intlcon)
|
|
66
|
-
return true;
|
|
67
|
-
const ref = --this._refCount;
|
|
68
|
-
this.emit('release', this._refCount);
|
|
69
|
-
debug('[%s] release | refCount: %s', this.sessionId, ref);
|
|
70
|
-
if (!ref) {
|
|
71
|
-
this.close().catch(() => 0);
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Immediately releases the connection.
|
|
78
|
-
*/
|
|
79
|
-
async close() {
|
|
80
|
-
if (!this._intlcon)
|
|
81
|
-
return;
|
|
82
|
-
await this.emitAsyncSerial('close');
|
|
83
|
-
const intlcon = this._intlcon;
|
|
84
|
-
this._intlcon = undefined;
|
|
85
|
-
this.client.pool.release(intlcon, (e) => {
|
|
86
|
-
if (e)
|
|
87
|
-
this.client.emit('error', e);
|
|
88
|
-
});
|
|
89
|
-
debug('[%s] closed', intlcon.sessionId);
|
|
90
|
-
}
|
|
91
|
-
async execute(query, options) {
|
|
92
|
-
if (!this._intlcon)
|
|
93
|
-
throw new Error(`Can't execute query, because connection is released`);
|
|
94
|
-
return this._tasks.enqueue(() => this._execute(query, options)).toPromise();
|
|
95
|
-
}
|
|
96
|
-
getRepository(entity, opts) {
|
|
97
|
-
let ctor;
|
|
98
|
-
if (typeof entity === 'string') {
|
|
99
|
-
ctor = this.client.getEntity(entity);
|
|
100
|
-
if (!ctor)
|
|
101
|
-
throw new Error(`Repository "${entity}" is not registered`);
|
|
102
|
-
}
|
|
103
|
-
else
|
|
104
|
-
ctor = entity;
|
|
105
|
-
const entityDef = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
106
|
-
if (!entityDef)
|
|
107
|
-
throw new Error(`You must provide an @Entity annotated constructor`);
|
|
108
|
-
return new repository_class_js_1.Repository(entityDef, this, opts?.schema);
|
|
109
|
-
}
|
|
110
|
-
async getSchema() {
|
|
111
|
-
assert_1.default.ok(this._intlcon, `Can't set schema, because connection is released`);
|
|
112
|
-
assert_1.default.ok(this._intlcon.getSchema, `${this.client.dialect} adapter does have Schema support`);
|
|
113
|
-
return await this._intlcon.getSchema();
|
|
114
|
-
}
|
|
115
|
-
async setSchema(schema) {
|
|
116
|
-
assert_1.default.ok(this._intlcon, `Can't set schema, because connection is released`);
|
|
117
|
-
assert_1.default.ok(this._intlcon.setSchema, `${this.client.dialect} adapter does have Schema support`);
|
|
118
|
-
await this._intlcon.setSchema(schema);
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Executes a query
|
|
122
|
-
*/
|
|
123
|
-
async _execute(query, options) {
|
|
124
|
-
assert_1.default.ok(this._intlcon, `Can't execute query, because connection is released`);
|
|
125
|
-
const intlcon = this._intlcon;
|
|
126
|
-
this.retain();
|
|
127
|
-
let request;
|
|
128
|
-
try {
|
|
129
|
-
const startTime = Date.now();
|
|
130
|
-
request = this._prepareQueryRequest(query, options);
|
|
131
|
-
debug('[%s] execute | %o', this.sessionId, request);
|
|
132
|
-
this.emit('execute', request);
|
|
133
|
-
// Call execute hooks
|
|
134
|
-
if (request.executeHooks) {
|
|
135
|
-
for (const fn of request.executeHooks) {
|
|
136
|
-
await fn(this, request);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
const response = await intlcon.execute(request);
|
|
140
|
-
if (!response)
|
|
141
|
-
throw new Error('Database adapter returned an empty response');
|
|
142
|
-
const result = {
|
|
143
|
-
executeTime: Date.now() - startTime,
|
|
144
|
-
};
|
|
145
|
-
if (request.showSql)
|
|
146
|
-
result.query = request;
|
|
147
|
-
if (response.rows || response.cursor) {
|
|
148
|
-
if (!response.fields)
|
|
149
|
-
throw new Error('Adapter did not returned fields info');
|
|
150
|
-
if (!response.rowType)
|
|
151
|
-
throw new Error('Adapter did not returned rowType');
|
|
152
|
-
result.fields = (0, helpers_js_1.wrapAdapterFields)(response.fields, request.fieldNaming);
|
|
153
|
-
result.rowType = response.rowType;
|
|
154
|
-
if (response.rows) {
|
|
155
|
-
result.rows = request.objectRows
|
|
156
|
-
? (0, helpers_js_1.normalizeRowsToObjectRows)(result.fields, response.rowType, response.rows, request)
|
|
157
|
-
: (0, helpers_js_1.normalizeRowsToArrayRows)(result.fields, response.rowType, response.rows, request);
|
|
158
|
-
(0, helpers_js_1.callFetchHooks)(result.rows, request);
|
|
159
|
-
}
|
|
160
|
-
else if (response.cursor) {
|
|
161
|
-
const cursor = (result.cursor = new cursor_js_1.Cursor(this, result.fields, response.cursor, request));
|
|
162
|
-
const hook = () => cursor.close();
|
|
163
|
-
cursor.once('close', () => this.off('close', hook));
|
|
164
|
-
this.on('close', hook);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (response.rowsAffected)
|
|
168
|
-
result.rowsAffected = response.rowsAffected;
|
|
169
|
-
return result;
|
|
170
|
-
}
|
|
171
|
-
catch (e) {
|
|
172
|
-
const error = new sqb_error_js_1.SQBError(e.message, e);
|
|
173
|
-
error.query = query;
|
|
174
|
-
error.queryOptions = options;
|
|
175
|
-
error.request = request;
|
|
176
|
-
this.emit('error', error);
|
|
177
|
-
throw error;
|
|
178
|
-
}
|
|
179
|
-
finally {
|
|
180
|
-
this.release();
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
async startTransaction() {
|
|
184
|
-
if (!this._intlcon)
|
|
185
|
-
throw new Error('Can not call startTransaction() on a released connection');
|
|
186
|
-
await this._intlcon.startTransaction();
|
|
187
|
-
this._inTransaction = true;
|
|
188
|
-
this.emit('start-transaction');
|
|
189
|
-
}
|
|
190
|
-
async commit() {
|
|
191
|
-
if (!this._intlcon)
|
|
192
|
-
throw new Error('Can not call commit() on a released connection');
|
|
193
|
-
await this._intlcon.commit();
|
|
194
|
-
this._inTransaction = false;
|
|
195
|
-
this.emit('commit');
|
|
196
|
-
}
|
|
197
|
-
async rollback() {
|
|
198
|
-
if (!this._intlcon)
|
|
199
|
-
throw new Error('Can not call rollback() on a released connection');
|
|
200
|
-
await this._intlcon.rollback();
|
|
201
|
-
this._inTransaction = false;
|
|
202
|
-
this.emit('rollback');
|
|
203
|
-
}
|
|
204
|
-
async setSavepoint(savepoint) {
|
|
205
|
-
if (!this._intlcon)
|
|
206
|
-
throw new Error('Can not call setSavepoint() on a released connection');
|
|
207
|
-
if (typeof this._intlcon.setSavepoint !== 'function') {
|
|
208
|
-
throw new Error(this.client.driver + ' does not support setSavepoint method');
|
|
209
|
-
}
|
|
210
|
-
if (!this.inTransaction)
|
|
211
|
-
await this.startTransaction();
|
|
212
|
-
await this._intlcon.setSavepoint(savepoint);
|
|
213
|
-
this.emit('set-savepoint');
|
|
214
|
-
}
|
|
215
|
-
async releaseSavepoint(savepoint) {
|
|
216
|
-
if (!this._intlcon)
|
|
217
|
-
throw new Error('Can not call releaseSavepoint() on a released connection');
|
|
218
|
-
if (typeof this._intlcon.releaseSavepoint !== 'function') {
|
|
219
|
-
throw new Error(this.client.driver + ' does not support releaseSavepoint method');
|
|
220
|
-
}
|
|
221
|
-
await this._intlcon.releaseSavepoint(savepoint);
|
|
222
|
-
this.emit('release-savepoint');
|
|
223
|
-
}
|
|
224
|
-
async rollbackSavepoint(savepoint) {
|
|
225
|
-
if (!this._intlcon)
|
|
226
|
-
throw new Error('Can not call rollbackSavepoint() on a released connection');
|
|
227
|
-
if (typeof this._intlcon.rollbackSavepoint !== 'function') {
|
|
228
|
-
throw new Error(this.client.driver + ' does not support rollbackSavepoint method');
|
|
229
|
-
}
|
|
230
|
-
await this._intlcon.rollbackSavepoint(savepoint);
|
|
231
|
-
this.emit('rollback-savepoint');
|
|
232
|
-
}
|
|
233
|
-
async test() {
|
|
234
|
-
if (!this._intlcon)
|
|
235
|
-
throw new Error('Can not call test() on a released connection');
|
|
236
|
-
await this._intlcon.test();
|
|
237
|
-
}
|
|
238
|
-
_prepareQueryRequest(query, options = {}) {
|
|
239
|
-
if (!this._intlcon)
|
|
240
|
-
throw new Error('Session released');
|
|
241
|
-
const defaults = this.client.defaults;
|
|
242
|
-
const request = {
|
|
243
|
-
dialect: this.client.dialect,
|
|
244
|
-
sql: '',
|
|
245
|
-
autoCommit: this.inTransaction
|
|
246
|
-
? false
|
|
247
|
-
: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.autoCommit, this._options?.autoCommit, defaults.autoCommit), true),
|
|
248
|
-
cursor: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.cursor, defaults.cursor), false),
|
|
249
|
-
objectRows: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.objectRows, defaults.objectRows), true),
|
|
250
|
-
ignoreNulls: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.ignoreNulls, defaults.ignoreNulls), false),
|
|
251
|
-
fetchRows: (0, putil_varhelpers_1.coerceToInt)((0, putil_varhelpers_1.coalesce)(options.fetchRows, defaults.fetchRows), 100),
|
|
252
|
-
fieldNaming: (0, putil_varhelpers_1.coalesce)(options.namingStrategy, defaults.fieldNaming),
|
|
253
|
-
transform: (0, putil_varhelpers_1.coalesce)(options.transform, defaults.transform),
|
|
254
|
-
showSql: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.showSql, defaults.showSql), false),
|
|
255
|
-
prettyPrint: (0, putil_varhelpers_1.coerceToBoolean)((0, putil_varhelpers_1.coalesce)(options.prettyPrint, defaults.prettyPrint), false),
|
|
256
|
-
action: (0, putil_varhelpers_1.coerceToString)(options.action),
|
|
257
|
-
fetchAsString: options.fetchAsString,
|
|
258
|
-
};
|
|
259
|
-
request.ignoreNulls = request.ignoreNulls && request.objectRows;
|
|
260
|
-
if (query instanceof builder_1.classes.Query) {
|
|
261
|
-
if (this._intlcon.onGenerateQuery)
|
|
262
|
-
this._intlcon.onGenerateQuery(request, query);
|
|
263
|
-
const q = query.generate({
|
|
264
|
-
dialect: request.dialect,
|
|
265
|
-
dialectVersion: request.dialectVersion,
|
|
266
|
-
params: options.params,
|
|
267
|
-
strictParams: true,
|
|
268
|
-
prettyPrint: request.prettyPrint,
|
|
269
|
-
});
|
|
270
|
-
request.sql = q.sql;
|
|
271
|
-
request.params = q.params;
|
|
272
|
-
request.paramOptions = q.paramOptions;
|
|
273
|
-
if (q.returningFields)
|
|
274
|
-
request.returningFields = q.returningFields;
|
|
275
|
-
if (query.listenerCount('execute'))
|
|
276
|
-
request.executeHooks = query.listeners('execute');
|
|
277
|
-
if (query.listenerCount('fetch'))
|
|
278
|
-
request.fetchHooks = query.listeners('fetch');
|
|
279
|
-
}
|
|
280
|
-
else {
|
|
281
|
-
// noinspection SuspiciousTypeOfGuard
|
|
282
|
-
if (typeof query === 'string') {
|
|
283
|
-
request.sql = query;
|
|
284
|
-
request.params = options.params;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
// @ts-ignore
|
|
288
|
-
if (!request.sql)
|
|
289
|
-
throw new Error('No sql given');
|
|
290
|
-
return request;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
exports.SqbConnection = SqbConnection;
|
package/cjs/client/sqb-error.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SQBError = void 0;
|
|
4
|
-
class SQBError extends Error {
|
|
5
|
-
cause;
|
|
6
|
-
queryOptions;
|
|
7
|
-
request;
|
|
8
|
-
constructor(message, cause) {
|
|
9
|
-
super(message);
|
|
10
|
-
this.cause = cause;
|
|
11
|
-
if (cause)
|
|
12
|
-
this.stack = cause.stack;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.SQBError = SQBError;
|
package/cjs/client/types.js
DELETED
package/cjs/index.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isEntityClass = exports.isEmbeddedField = exports.isColumnField = exports.isAssociationField = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
require("reflect-metadata");
|
|
6
|
-
tslib_1.__exportStar(require("./client/adapter.js"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./client/cursor.js"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./client/extensions.js"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./client/sqb-client.js"), exports);
|
|
10
|
-
tslib_1.__exportStar(require("./client/sqb-connection.js"), exports);
|
|
11
|
-
tslib_1.__exportStar(require("./client/sqb-error.js"), exports);
|
|
12
|
-
tslib_1.__exportStar(require("./client/types.js"), exports);
|
|
13
|
-
tslib_1.__exportStar(require("./orm/backward.js"), exports);
|
|
14
|
-
tslib_1.__exportStar(require("./orm/base-entity.js"), exports);
|
|
15
|
-
tslib_1.__exportStar(require("./orm/decorators/column.decorator.js"), exports);
|
|
16
|
-
tslib_1.__exportStar(require("./orm/decorators/embedded.decorator.js"), exports);
|
|
17
|
-
tslib_1.__exportStar(require("./orm/decorators/entity.decorator.js"), exports);
|
|
18
|
-
tslib_1.__exportStar(require("./orm/decorators/events.decorator.js"), exports);
|
|
19
|
-
tslib_1.__exportStar(require("./orm/decorators/foreignkey.decorator.js"), exports);
|
|
20
|
-
tslib_1.__exportStar(require("./orm/decorators/index.decorator.js"), exports);
|
|
21
|
-
tslib_1.__exportStar(require("./orm/decorators/link.decorator.js"), exports);
|
|
22
|
-
tslib_1.__exportStar(require("./orm/decorators/primarykey.decorator.js"), exports);
|
|
23
|
-
tslib_1.__exportStar(require("./orm/decorators/transform.decorator.js"), exports);
|
|
24
|
-
tslib_1.__exportStar(require("./orm/model/association.js"), exports);
|
|
25
|
-
tslib_1.__exportStar(require("./orm/model/association-field-metadata.js"), exports);
|
|
26
|
-
tslib_1.__exportStar(require("./orm/model/association-node.js"), exports);
|
|
27
|
-
tslib_1.__exportStar(require("./orm/model/column-field-metadata.js"), exports);
|
|
28
|
-
tslib_1.__exportStar(require("./orm/model/embedded-field-metadata.js"), exports);
|
|
29
|
-
tslib_1.__exportStar(require("./orm/model/entity-metadata.js"), exports);
|
|
30
|
-
tslib_1.__exportStar(require("./orm/model/field-metadata.js"), exports);
|
|
31
|
-
tslib_1.__exportStar(require("./orm/model/index-metadata.js"), exports);
|
|
32
|
-
tslib_1.__exportStar(require("./orm/model/link-chain.js"), exports);
|
|
33
|
-
tslib_1.__exportStar(require("./orm/orm.const.js"), exports);
|
|
34
|
-
tslib_1.__exportStar(require("./orm/orm.type.js"), exports);
|
|
35
|
-
tslib_1.__exportStar(require("./orm/repository.class.js"), exports);
|
|
36
|
-
var orm_helper_js_1 = require("./orm/util/orm.helper.js");
|
|
37
|
-
Object.defineProperty(exports, "isAssociationField", { enumerable: true, get: function () { return orm_helper_js_1.isAssociationField; } });
|
|
38
|
-
Object.defineProperty(exports, "isColumnField", { enumerable: true, get: function () { return orm_helper_js_1.isColumnField; } });
|
|
39
|
-
Object.defineProperty(exports, "isEmbeddedField", { enumerable: true, get: function () { return orm_helper_js_1.isEmbeddedField; } });
|
|
40
|
-
Object.defineProperty(exports, "isEntityClass", { enumerable: true, get: function () { return orm_helper_js_1.isEntityClass; } });
|
|
41
|
-
tslib_1.__exportStar(require("./orm/util/parse-fields-projection.js"), exports);
|
package/cjs/orm/backward.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UnionEntity = exports.PickEntity = exports.OmitEntity = void 0;
|
|
4
|
-
exports.getInsertColumnNames = getInsertColumnNames;
|
|
5
|
-
exports.getUpdateColumnNames = getUpdateColumnNames;
|
|
6
|
-
exports.getNonAssociationElementNames = getNonAssociationElementNames;
|
|
7
|
-
exports.mixinEntities = mixinEntities;
|
|
8
|
-
const entity_decorator_js_1 = require("./decorators/entity.decorator.js");
|
|
9
|
-
function getInsertColumnNames(ctor) {
|
|
10
|
-
return entity_decorator_js_1.Entity.getInsertColumnNames(ctor);
|
|
11
|
-
}
|
|
12
|
-
function getUpdateColumnNames(ctor) {
|
|
13
|
-
return entity_decorator_js_1.Entity.getUpdateColumnNames(ctor);
|
|
14
|
-
}
|
|
15
|
-
function getNonAssociationElementNames(ctor) {
|
|
16
|
-
return entity_decorator_js_1.Entity.getNonAssociationFieldNames(ctor);
|
|
17
|
-
}
|
|
18
|
-
function mixinEntities(derivedCtor, ...bases) {
|
|
19
|
-
// @ts-ignore
|
|
20
|
-
return entity_decorator_js_1.Entity.mixin(derivedCtor, ...bases);
|
|
21
|
-
}
|
|
22
|
-
exports.OmitEntity = entity_decorator_js_1.Entity.Omit;
|
|
23
|
-
exports.PickEntity = entity_decorator_js_1.Entity.Pick;
|
|
24
|
-
exports.UnionEntity = entity_decorator_js_1.Entity.Union;
|
package/cjs/orm/base-entity.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseEntity = void 0;
|
|
4
|
-
const entity_decorator_js_1 = require("./decorators/entity.decorator.js");
|
|
5
|
-
const orm_const_js_1 = require("./orm.const.js");
|
|
6
|
-
class BaseEntity {
|
|
7
|
-
[orm_const_js_1.REPOSITORY_KEY];
|
|
8
|
-
constructor(partial) {
|
|
9
|
-
const fields = entity_decorator_js_1.Entity.getColumnFieldNames(Object.getPrototypeOf(this).constructor);
|
|
10
|
-
if (fields && partial) {
|
|
11
|
-
for (const k of fields)
|
|
12
|
-
if (partial[k] !== undefined)
|
|
13
|
-
this[k] = partial[k];
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
async destroy() {
|
|
17
|
-
const repo = this[orm_const_js_1.REPOSITORY_KEY];
|
|
18
|
-
return !!(repo && repo.destroy(this));
|
|
19
|
-
}
|
|
20
|
-
async exists() {
|
|
21
|
-
const repo = this[orm_const_js_1.REPOSITORY_KEY];
|
|
22
|
-
return !!(repo && repo.exists(this));
|
|
23
|
-
}
|
|
24
|
-
toJSON() {
|
|
25
|
-
// this method is a placeholder and will be overwritten by declareEntity() method
|
|
26
|
-
return this;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.BaseEntity = BaseEntity;
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.joinAssociationGetFirst = joinAssociationGetFirst;
|
|
4
|
-
exports.joinAssociationGetLast = joinAssociationGetLast;
|
|
5
|
-
exports.joinAssociation = joinAssociation;
|
|
6
|
-
exports.prepareFilter = prepareFilter;
|
|
7
|
-
const builder_1 = require("@sqb/builder");
|
|
8
|
-
const embedded_field_metadata_js_1 = require("../model/embedded-field-metadata.js");
|
|
9
|
-
const entity_metadata_js_1 = require("../model/entity-metadata.js");
|
|
10
|
-
const orm_helper_js_1 = require("../util/orm.helper.js");
|
|
11
|
-
async function joinAssociationGetFirst(joinInfos, association, parentAlias, innerJoin) {
|
|
12
|
-
const joins = await joinAssociation(joinInfos, association, parentAlias, innerJoin);
|
|
13
|
-
return joins[0];
|
|
14
|
-
}
|
|
15
|
-
async function joinAssociationGetLast(joinInfos, association, parentAlias, innerJoin) {
|
|
16
|
-
const joins = await joinAssociation(joinInfos, association, parentAlias, innerJoin);
|
|
17
|
-
return joins[joins.length - 1];
|
|
18
|
-
}
|
|
19
|
-
async function joinAssociation(joinInfos, association, parentAlias, innerJoin) {
|
|
20
|
-
let joinInfo;
|
|
21
|
-
let node = association;
|
|
22
|
-
const result = [];
|
|
23
|
-
while (node) {
|
|
24
|
-
joinInfo = joinInfos.find(j => j.association === node && j.parentAlias === parentAlias);
|
|
25
|
-
if (!joinInfo) {
|
|
26
|
-
const targetEntity = await node.resolveTarget();
|
|
27
|
-
const sourceEntity = await node.resolveSource();
|
|
28
|
-
const keyCol = await node.resolveSourceProperty();
|
|
29
|
-
const targetCol = await node.resolveTargetProperty();
|
|
30
|
-
const joinAlias = 'J' + (joinInfos.length + 1);
|
|
31
|
-
const join = innerJoin
|
|
32
|
-
? (0, builder_1.InnerJoin)(targetEntity.tableName + ' as ' + joinAlias)
|
|
33
|
-
: (0, builder_1.LeftOuterJoin)(targetEntity.tableName + ' as ' + joinAlias);
|
|
34
|
-
join.on((0, builder_1.Eq)((0, builder_1.Field)(joinAlias + '.' + targetCol.fieldName, targetCol.dataType, targetCol.isArray), (0, builder_1.Field)(parentAlias + '.' + keyCol.fieldName, keyCol.dataType, keyCol.isArray)));
|
|
35
|
-
if (node.conditions)
|
|
36
|
-
await prepareFilter(targetEntity, node.conditions, join._conditions, joinAlias);
|
|
37
|
-
joinInfo = {
|
|
38
|
-
association: node,
|
|
39
|
-
sourceEntity,
|
|
40
|
-
targetEntity,
|
|
41
|
-
parentAlias,
|
|
42
|
-
joinAlias,
|
|
43
|
-
join,
|
|
44
|
-
};
|
|
45
|
-
joinInfos.push(joinInfo);
|
|
46
|
-
}
|
|
47
|
-
result.push(joinInfo);
|
|
48
|
-
if (!node.next)
|
|
49
|
-
break;
|
|
50
|
-
parentAlias = joinInfo.joinAlias;
|
|
51
|
-
node = node.next;
|
|
52
|
-
}
|
|
53
|
-
return result;
|
|
54
|
-
}
|
|
55
|
-
async function prepareFilter(entityDef, filter, trgOp, tableAlias = 'T') {
|
|
56
|
-
let srcOp;
|
|
57
|
-
if ((0, builder_1.isLogicalOperator)(filter) && filter._operatorType === trgOp._operatorType)
|
|
58
|
-
srcOp = filter;
|
|
59
|
-
else {
|
|
60
|
-
srcOp = (0, builder_1.And)();
|
|
61
|
-
if (Array.isArray(filter))
|
|
62
|
-
srcOp.add(...filter);
|
|
63
|
-
else
|
|
64
|
-
srcOp.add(filter);
|
|
65
|
-
}
|
|
66
|
-
// const associationPathCache: Record<string, any> = {};
|
|
67
|
-
for (const item of srcOp._items) {
|
|
68
|
-
if ((0, builder_1.isLogicalOperator)(item)) {
|
|
69
|
-
const ctor = Object.getPrototypeOf(item).constructor;
|
|
70
|
-
const logOp = new ctor();
|
|
71
|
-
await prepareFilter(entityDef, item, logOp, tableAlias);
|
|
72
|
-
trgOp.add(logOp);
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
if ((0, builder_1.isCompOperator)(item)) {
|
|
76
|
-
if (typeof item._left === 'string') {
|
|
77
|
-
const itemPath = item._left.split('.');
|
|
78
|
-
const l = itemPath.length;
|
|
79
|
-
let pt;
|
|
80
|
-
let _curEntity = entityDef;
|
|
81
|
-
let _curAlias = tableAlias;
|
|
82
|
-
let _curPrefix = '';
|
|
83
|
-
let _curSuffix = '';
|
|
84
|
-
let subSelect;
|
|
85
|
-
let currentOp = trgOp;
|
|
86
|
-
let i = 0;
|
|
87
|
-
// const parentPath = itemPath
|
|
88
|
-
// .slice(0, l - 2)
|
|
89
|
-
// .join('.')
|
|
90
|
-
// .toLowerCase();
|
|
91
|
-
// const cached = associationPathCache[parentPath];
|
|
92
|
-
// if (cached) {
|
|
93
|
-
// /** Jump to last item */
|
|
94
|
-
// i = l - 1;
|
|
95
|
-
// _curEntity = cached._curEntity;
|
|
96
|
-
// _curAlias = cached._curAlias;
|
|
97
|
-
// currentOp = cached.currentOp;
|
|
98
|
-
// }
|
|
99
|
-
for (i; i < l; i++) {
|
|
100
|
-
pt = itemPath[i];
|
|
101
|
-
const col = entity_metadata_js_1.EntityMetadata.getField(_curEntity, pt);
|
|
102
|
-
if (!col)
|
|
103
|
-
throw new Error(`Unknown property (${item._left}) defined in filter`);
|
|
104
|
-
/** if last item on path */
|
|
105
|
-
if (i === l - 1) {
|
|
106
|
-
if (!(0, orm_helper_js_1.isColumnField)(col))
|
|
107
|
-
throw new Error(`Invalid column expression (${item._left}) defined in filter`);
|
|
108
|
-
const ctor = Object.getPrototypeOf(item).constructor;
|
|
109
|
-
currentOp.add(new ctor((0, builder_1.Field)(_curAlias + '.' + _curPrefix + col.fieldName + _curSuffix, col.dataType, col.isArray), item._right));
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
/** if not last item on path */
|
|
113
|
-
if ((0, orm_helper_js_1.isColumnField)(col))
|
|
114
|
-
throw new Error(`Invalid column (${item._left}) defined in filter`);
|
|
115
|
-
if ((0, orm_helper_js_1.isEmbeddedField)(col)) {
|
|
116
|
-
_curEntity = await embedded_field_metadata_js_1.EmbeddedFieldMetadata.resolveType(col);
|
|
117
|
-
_curPrefix = _curPrefix + (col.fieldNamePrefix || '');
|
|
118
|
-
_curSuffix = (col.fieldNameSuffix || '') + _curSuffix;
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
if (!(0, orm_helper_js_1.isAssociationField)(col))
|
|
122
|
-
throw new Error(`Invalid column (${item._left}) defined in filter`);
|
|
123
|
-
let node;
|
|
124
|
-
_curEntity = await col.association.resolveTarget();
|
|
125
|
-
if (!subSelect) {
|
|
126
|
-
const keyCol = await col.association.resolveSourceProperty();
|
|
127
|
-
const targetCol = await col.association.resolveTargetProperty();
|
|
128
|
-
subSelect = (0, builder_1.Select)((0, builder_1.Raw)('1')).from(_curEntity.tableName + ' K');
|
|
129
|
-
subSelect.where((0, builder_1.Eq)((0, builder_1.Field)('K.' + targetCol.fieldName, targetCol.dataType, targetCol.isArray), (0, builder_1.Field)(tableAlias + '.' + keyCol.fieldName, keyCol.dataType, keyCol.isArray)));
|
|
130
|
-
currentOp.add((0, builder_1.Exists)(subSelect));
|
|
131
|
-
if (currentOp._operatorType !== builder_1.OperatorType.and) {
|
|
132
|
-
currentOp = builder_1.op.or();
|
|
133
|
-
subSelect.where(currentOp);
|
|
134
|
-
}
|
|
135
|
-
else
|
|
136
|
-
currentOp = subSelect._where;
|
|
137
|
-
if (col.association.conditions) {
|
|
138
|
-
await prepareFilter(_curEntity, col.association.conditions, trgOp, 'K');
|
|
139
|
-
}
|
|
140
|
-
node = col.association.next;
|
|
141
|
-
_curAlias = 'K';
|
|
142
|
-
}
|
|
143
|
-
else
|
|
144
|
-
node = col.association;
|
|
145
|
-
while (node) {
|
|
146
|
-
const targetEntity = await node.resolveTarget();
|
|
147
|
-
const sourceColumn = await node.resolveSourceProperty();
|
|
148
|
-
const targetColumn = await node.resolveTargetProperty();
|
|
149
|
-
const joinAlias = 'J' + ((subSelect?._joins?.length || 0) + 1);
|
|
150
|
-
subSelect.join((0, builder_1.InnerJoin)(targetEntity.tableName + ' ' + joinAlias).on((0, builder_1.Eq)((0, builder_1.Field)(joinAlias + '.' + targetColumn.fieldName, targetColumn.dataType, targetColumn.isArray), (0, builder_1.Field)(_curAlias + '.' + sourceColumn.fieldName, sourceColumn.dataType, sourceColumn.isArray))));
|
|
151
|
-
_curEntity = targetEntity;
|
|
152
|
-
_curAlias = joinAlias;
|
|
153
|
-
node = node.next;
|
|
154
|
-
}
|
|
155
|
-
// /** If next path is the last one */
|
|
156
|
-
// if (i === l - 2) {
|
|
157
|
-
// associationPathCache[parentPath] = {
|
|
158
|
-
// _curEntity,
|
|
159
|
-
// _curAlias,
|
|
160
|
-
// currentOp,
|
|
161
|
-
// };
|
|
162
|
-
// }
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
continue;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
trgOp.add(item);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CountCommand = void 0;
|
|
4
|
-
const builder_1 = require("@sqb/builder");
|
|
5
|
-
const command_helper_js_1 = require("./command.helper.js");
|
|
6
|
-
class CountCommand {
|
|
7
|
-
// istanbul ignore next
|
|
8
|
-
constructor() {
|
|
9
|
-
throw new Error('This class is abstract');
|
|
10
|
-
}
|
|
11
|
-
static async execute(args) {
|
|
12
|
-
const { connection, entity, filter, params } = args;
|
|
13
|
-
let where;
|
|
14
|
-
if (filter) {
|
|
15
|
-
where = (0, builder_1.And)();
|
|
16
|
-
await (0, command_helper_js_1.prepareFilter)(entity, filter, where, 'T');
|
|
17
|
-
}
|
|
18
|
-
const query = (0, builder_1.Select)((0, builder_1.Count)()).from(entity.tableName + ' T');
|
|
19
|
-
if (where)
|
|
20
|
-
query.where(where);
|
|
21
|
-
// Execute query
|
|
22
|
-
const resp = await connection.execute(query, {
|
|
23
|
-
params,
|
|
24
|
-
objectRows: false,
|
|
25
|
-
cursor: false,
|
|
26
|
-
});
|
|
27
|
-
return (resp && resp.rows && resp.rows[0][0]) || 0;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.CountCommand = CountCommand;
|