@sqb/connect 4.0.9 → 4.0.13

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 (89) hide show
  1. package/dist/client/Adapter.d.ts +57 -0
  2. package/dist/client/Adapter.js +2 -0
  3. package/dist/client/Cursor.d.ts +116 -0
  4. package/dist/client/Cursor.js +250 -0
  5. package/dist/client/CursorStream.d.ts +29 -0
  6. package/dist/client/CursorStream.js +98 -0
  7. package/dist/client/FieldInfoMap.d.ts +10 -0
  8. package/dist/client/FieldInfoMap.js +33 -0
  9. package/dist/client/SqbClient.d.ts +70 -0
  10. package/dist/client/SqbClient.js +165 -0
  11. package/dist/client/SqbConnection.d.ts +74 -0
  12. package/dist/client/SqbConnection.js +272 -0
  13. package/dist/client/extensions.d.ts +4 -0
  14. package/dist/client/extensions.js +14 -0
  15. package/dist/client/helpers.d.ts +9 -0
  16. package/dist/client/helpers.js +133 -0
  17. package/dist/client/types.d.ts +189 -0
  18. package/dist/client/types.js +5 -0
  19. package/dist/index.d.ts +28 -0
  20. package/dist/index.js +37 -0
  21. package/dist/orm/base-entity.class.d.ts +16 -0
  22. package/dist/orm/base-entity.class.js +56 -0
  23. package/dist/orm/commands/command.helper.d.ts +14 -0
  24. package/dist/orm/commands/command.helper.js +140 -0
  25. package/dist/orm/commands/count.command.d.ts +11 -0
  26. package/dist/orm/commands/count.command.js +30 -0
  27. package/dist/orm/commands/create.command.d.ts +20 -0
  28. package/dist/orm/commands/create.command.js +83 -0
  29. package/dist/orm/commands/destroy.command.d.ts +11 -0
  30. package/dist/orm/commands/destroy.command.js +30 -0
  31. package/dist/orm/commands/find.command.d.ts +44 -0
  32. package/dist/orm/commands/find.command.js +308 -0
  33. package/dist/orm/commands/row-converter.d.ts +55 -0
  34. package/dist/orm/commands/row-converter.js +187 -0
  35. package/dist/orm/commands/update.command.d.ts +22 -0
  36. package/dist/orm/commands/update.command.js +82 -0
  37. package/dist/orm/decorators/column.decorator.d.ts +4 -0
  38. package/dist/orm/decorators/column.decorator.js +14 -0
  39. package/dist/orm/decorators/embedded.decorator.d.ts +2 -0
  40. package/dist/orm/decorators/embedded.decorator.js +17 -0
  41. package/dist/orm/decorators/entity.decorator.d.ts +6 -0
  42. package/dist/orm/decorators/entity.decorator.js +23 -0
  43. package/dist/orm/decorators/events.decorator.d.ts +6 -0
  44. package/dist/orm/decorators/events.decorator.js +76 -0
  45. package/dist/orm/decorators/foreignkey.decorator.d.ts +2 -0
  46. package/dist/orm/decorators/foreignkey.decorator.js +13 -0
  47. package/dist/orm/decorators/index.decorator.d.ts +3 -0
  48. package/dist/orm/decorators/index.decorator.js +22 -0
  49. package/dist/orm/decorators/link.decorator.d.ts +23 -0
  50. package/dist/orm/decorators/link.decorator.js +88 -0
  51. package/dist/orm/decorators/primarykey.decorator.d.ts +3 -0
  52. package/dist/orm/decorators/primarykey.decorator.js +23 -0
  53. package/dist/orm/decorators/transform.decorator.d.ts +3 -0
  54. package/dist/orm/decorators/transform.decorator.js +24 -0
  55. package/dist/orm/model/abstract-entityelement.d.ts +8 -0
  56. package/dist/orm/model/abstract-entityelement.js +10 -0
  57. package/dist/orm/model/association-node.d.ts +9 -0
  58. package/dist/orm/model/association-node.js +25 -0
  59. package/dist/orm/model/association.d.ts +28 -0
  60. package/dist/orm/model/association.js +129 -0
  61. package/dist/orm/model/entity-association-element.d.ts +9 -0
  62. package/dist/orm/model/entity-association-element.js +13 -0
  63. package/dist/orm/model/entity-column-element.d.ts +68 -0
  64. package/dist/orm/model/entity-column-element.js +27 -0
  65. package/dist/orm/model/entity-model.d.ts +59 -0
  66. package/dist/orm/model/entity-model.js +295 -0
  67. package/dist/orm/model/entity-object-element.d.ts +12 -0
  68. package/dist/orm/model/entity-object-element.js +24 -0
  69. package/dist/orm/model/index-meta.d.ts +9 -0
  70. package/dist/orm/model/index-meta.js +16 -0
  71. package/dist/orm/model/link-chain.d.ts +15 -0
  72. package/dist/orm/model/link-chain.js +50 -0
  73. package/dist/orm/orm.const.d.ts +2 -0
  74. package/dist/orm/orm.const.js +5 -0
  75. package/dist/orm/orm.type.d.ts +112 -0
  76. package/dist/orm/orm.type.js +2 -0
  77. package/dist/orm/repository.class.d.ts +125 -0
  78. package/dist/orm/repository.class.js +217 -0
  79. package/dist/orm/util/entity-mapping.d.ts +12 -0
  80. package/dist/orm/util/entity-mapping.js +154 -0
  81. package/dist/orm/util/extract-keyvalues.d.ts +2 -0
  82. package/dist/orm/util/extract-keyvalues.js +60 -0
  83. package/dist/orm/util/orm.helper.d.ts +14 -0
  84. package/dist/orm/util/orm.helper.js +39 -0
  85. package/dist/orm/util/serialize-element.d.ts +2 -0
  86. package/dist/orm/util/serialize-element.js +43 -0
  87. package/dist/types.d.ts +2 -0
  88. package/dist/types.js +2 -0
  89. package/package.json +3 -3
@@ -0,0 +1,57 @@
1
+ import { Maybe } from 'ts-gems';
2
+ import { classes } from '@sqb/builder';
3
+ import { ClientConfiguration, QueryRequest, RowType, DataType } from './types';
4
+ export interface Adapter {
5
+ driver: string;
6
+ dialect: string;
7
+ features?: {
8
+ cursor?: boolean;
9
+ schema?: boolean;
10
+ fetchAsString?: DataType[];
11
+ };
12
+ connect: (config: ClientConfiguration) => Promise<Adapter.Connection>;
13
+ }
14
+ export declare namespace Adapter {
15
+ interface Connection {
16
+ sessionId: any;
17
+ execute: (request: QueryRequest) => Promise<Response>;
18
+ close: () => Promise<void>;
19
+ reset: () => Promise<void>;
20
+ test: () => Promise<void>;
21
+ startTransaction: () => Promise<void>;
22
+ setSavepoint?: (savepoint: string) => Promise<void>;
23
+ releaseSavepoint?: (savepoint: string) => Promise<void>;
24
+ rollbackSavepoint?: (savepoint: string) => Promise<void>;
25
+ commit: () => Promise<void>;
26
+ rollback: () => Promise<void>;
27
+ setSchema?: (schema: string) => Promise<void>;
28
+ getSchema?: () => Promise<string>;
29
+ onGenerateQuery?: (request: QueryRequest, query: classes.Query) => void;
30
+ getInTransaction?: () => boolean;
31
+ }
32
+ interface Cursor {
33
+ readonly isClosed: boolean;
34
+ readonly rowType: RowType;
35
+ close: () => Promise<void>;
36
+ fetch: (rows: number) => Promise<Maybe<any[]>>;
37
+ }
38
+ interface Response {
39
+ fields?: Field[];
40
+ rows?: Record<string, any>[] | any[][];
41
+ rowType?: RowType;
42
+ cursor?: Adapter.Cursor;
43
+ rowsAffected?: number;
44
+ }
45
+ interface Field {
46
+ fieldName: string;
47
+ dataType: string;
48
+ jsType: string;
49
+ isArray?: boolean;
50
+ elementDataType?: string;
51
+ nullable?: boolean;
52
+ fixedLength?: boolean;
53
+ size?: number;
54
+ precision?: number;
55
+ _inf: any;
56
+ }
57
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,116 @@
1
+ import { Adapter } from './Adapter';
2
+ import { SqbConnection } from './SqbConnection';
3
+ import { FieldInfoMap } from './FieldInfoMap';
4
+ import { ObjectRow, QueryRequest } from './types';
5
+ import { CursorStream, CursorStreamOptions } from './CursorStream';
6
+ interface CursorEvents {
7
+ close: () => void;
8
+ error: (error: unknown) => void;
9
+ eof: () => void;
10
+ reset: () => void;
11
+ move: (row: any, rowNum: number) => void;
12
+ fetch: (row: any, rowNum: number) => void;
13
+ }
14
+ declare const Cursor_base: import("strict-typed-events").Type<import("strict-typed-events").TypedEventEmitter<any, CursorEvents, CursorEvents>>;
15
+ export declare class Cursor extends Cursor_base {
16
+ private readonly _connection;
17
+ private readonly _fields;
18
+ private readonly _prefetchRows;
19
+ private readonly _request;
20
+ private _intlcur?;
21
+ private _taskQueue;
22
+ private _fetchCache;
23
+ private _rowNum;
24
+ private _fetchedAll;
25
+ private _fetchedRows;
26
+ private _row;
27
+ private _cache?;
28
+ constructor(connection: SqbConnection, fields: FieldInfoMap, adapterCursor: Adapter.Cursor, request: QueryRequest);
29
+ /**
30
+ * Returns the Connection instance
31
+ */
32
+ get connection(): SqbConnection;
33
+ /**
34
+ * Returns if cursor is before first record.
35
+ */
36
+ get isBof(): boolean;
37
+ /**
38
+ * Returns if cursor is closed.
39
+ */
40
+ get isClosed(): boolean;
41
+ /**
42
+ * Returns if cursor is after last record.
43
+ */
44
+ get isEof(): boolean;
45
+ /**
46
+ * Returns number of fetched record count from database.
47
+ */
48
+ get fetchedRows(): number;
49
+ /**
50
+ * Returns object instance which contains information about fields.
51
+ */
52
+ get fields(): FieldInfoMap;
53
+ /**
54
+ * Returns current row
55
+ */
56
+ get row(): any;
57
+ /**
58
+ * Returns current row number.
59
+ */
60
+ get rowNum(): number;
61
+ /**
62
+ * Enables cache
63
+ */
64
+ cached(): void;
65
+ /**
66
+ * Closes cursor
67
+ */
68
+ close(): Promise<void>;
69
+ /**
70
+ * If cache is enabled, this call fetches and keeps all records in the internal cache.
71
+ * Otherwise it throws error. Once all all records fetched,
72
+ * you can close Cursor safely and can continue to use it in memory.
73
+ * Returns number of fetched rows
74
+ */
75
+ fetchAll(): Promise<number>;
76
+ /**
77
+ * Moves cursor to given row number.
78
+ * cursor can move both forward and backward if cache enabled.
79
+ * Otherwise it throws error.
80
+ */
81
+ moveTo(rowNum: number): Promise<ObjectRow>;
82
+ /**
83
+ * Moves cursor forward by one row and returns that row.
84
+ * And also it allows iterating over rows easily.
85
+ */
86
+ next(): Promise<ObjectRow>;
87
+ /**
88
+ * Moves cursor back by one row and returns that row.
89
+ * And also it allows iterating over rows easily.
90
+ */
91
+ prev(): Promise<ObjectRow>;
92
+ /**
93
+ * Moves cursor before first row. (Required cache enabled)
94
+ */
95
+ reset(): void;
96
+ /**
97
+ * Moves cursor by given step. If caching is enabled,
98
+ * cursor can move both forward and backward. Otherwise it throws error.
99
+ */
100
+ seek(step: number): Promise<ObjectRow>;
101
+ /**
102
+ * Creates and returns a readable stream.
103
+ */
104
+ toStream(options?: CursorStreamOptions): CursorStream;
105
+ toString(): string;
106
+ inspect(): string;
107
+ /**
108
+ *
109
+ */
110
+ _seek(step: number, silent?: boolean): Promise<number>;
111
+ /**
112
+ *
113
+ */
114
+ _fetchRows(): Promise<void>;
115
+ }
116
+ export {};
@@ -0,0 +1,250 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Cursor = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const strict_typed_events_1 = require("strict-typed-events");
6
+ const doublylinked_1 = tslib_1.__importDefault(require("doublylinked"));
7
+ const putil_taskqueue_1 = tslib_1.__importDefault(require("putil-taskqueue"));
8
+ const debug_1 = tslib_1.__importDefault(require("debug"));
9
+ const putil_varhelpers_1 = require("putil-varhelpers");
10
+ const helpers_1 = require("./helpers");
11
+ const CursorStream_1 = require("./CursorStream");
12
+ const debug = (0, debug_1.default)('sqb:cursor');
13
+ class Cursor extends (0, strict_typed_events_1.TypedEventEmitterClass)(strict_typed_events_1.AsyncEventEmitter) {
14
+ constructor(connection, fields, adapterCursor, request) {
15
+ super();
16
+ this._taskQueue = new putil_taskqueue_1.default();
17
+ this._fetchCache = new doublylinked_1.default();
18
+ this._rowNum = 0;
19
+ this._fetchedAll = false;
20
+ this._fetchedRows = 0;
21
+ this._connection = connection;
22
+ this._intlcur = adapterCursor;
23
+ this._fields = fields;
24
+ this._request = request;
25
+ this._prefetchRows = request?.fetchRows || 100;
26
+ }
27
+ /**
28
+ * Returns the Connection instance
29
+ */
30
+ get connection() {
31
+ return this._connection;
32
+ }
33
+ /**
34
+ * Returns if cursor is before first record.
35
+ */
36
+ get isBof() {
37
+ return !this._rowNum;
38
+ }
39
+ /**
40
+ * Returns if cursor is closed.
41
+ */
42
+ get isClosed() {
43
+ return !this._intlcur;
44
+ }
45
+ /**
46
+ * Returns if cursor is after last record.
47
+ */
48
+ get isEof() {
49
+ return this._fetchedAll && this.rowNum > this._fetchedRows;
50
+ }
51
+ /**
52
+ * Returns number of fetched record count from database.
53
+ */
54
+ get fetchedRows() {
55
+ return this._fetchedRows;
56
+ }
57
+ /**
58
+ * Returns object instance which contains information about fields.
59
+ */
60
+ get fields() {
61
+ return this._fields;
62
+ }
63
+ /**
64
+ * Returns current row
65
+ */
66
+ get row() {
67
+ return this._row;
68
+ }
69
+ /**
70
+ * Returns current row number.
71
+ */
72
+ get rowNum() {
73
+ return this._rowNum;
74
+ }
75
+ /**
76
+ * Enables cache
77
+ */
78
+ cached() {
79
+ if (this.fetchedRows)
80
+ throw new Error('Cache can be enabled before fetching rows');
81
+ if (!this._cache)
82
+ this._cache = new doublylinked_1.default();
83
+ }
84
+ /**
85
+ * Closes cursor
86
+ */
87
+ async close() {
88
+ if (!this._intlcur)
89
+ return;
90
+ try {
91
+ await this._intlcur.close();
92
+ this._intlcur = undefined;
93
+ debug('close');
94
+ this.emit('close');
95
+ }
96
+ catch (err) {
97
+ debug('close-error:', err);
98
+ this.emit('error', err);
99
+ throw err;
100
+ }
101
+ }
102
+ /**
103
+ * If cache is enabled, this call fetches and keeps all records in the internal cache.
104
+ * Otherwise it throws error. Once all all records fetched,
105
+ * you can close Cursor safely and can continue to use it in memory.
106
+ * Returns number of fetched rows
107
+ */
108
+ async fetchAll() {
109
+ if (!this._cache)
110
+ throw new Error('fetchAll() method needs cache to be enabled');
111
+ const n = this.rowNum;
112
+ const v = await this._seek(Number.MAX_SAFE_INTEGER, true);
113
+ await this._seek(n - this.rowNum, true);
114
+ return v;
115
+ }
116
+ /**
117
+ * Moves cursor to given row number.
118
+ * cursor can move both forward and backward if cache enabled.
119
+ * Otherwise it throws error.
120
+ */
121
+ async moveTo(rowNum) {
122
+ await this._seek(rowNum - this.rowNum);
123
+ return this.row;
124
+ }
125
+ /**
126
+ * Moves cursor forward by one row and returns that row.
127
+ * And also it allows iterating over rows easily.
128
+ */
129
+ async next() {
130
+ debug('next');
131
+ await this._seek(1);
132
+ return this.row;
133
+ }
134
+ /**
135
+ * Moves cursor back by one row and returns that row.
136
+ * And also it allows iterating over rows easily.
137
+ */
138
+ async prev() {
139
+ await this._seek(-1);
140
+ return this.row;
141
+ }
142
+ /**
143
+ * Moves cursor before first row. (Required cache enabled)
144
+ */
145
+ reset() {
146
+ if (!this._cache)
147
+ throw new Error('reset() method needs cache to be enabled');
148
+ this._cache.reset();
149
+ this._rowNum = 0;
150
+ this.emit('reset');
151
+ }
152
+ /**
153
+ * Moves cursor by given step. If caching is enabled,
154
+ * cursor can move both forward and backward. Otherwise it throws error.
155
+ */
156
+ async seek(step) {
157
+ await this._seek(step);
158
+ return this.row;
159
+ }
160
+ /**
161
+ * Creates and returns a readable stream.
162
+ */
163
+ toStream(options) {
164
+ return new CursorStream_1.CursorStream(this, options);
165
+ }
166
+ toString() {
167
+ return '[object ' + Object.getPrototypeOf(this).constructor.name + ']';
168
+ }
169
+ inspect() {
170
+ return this.toString();
171
+ }
172
+ /**
173
+ *
174
+ */
175
+ async _seek(step, silent) {
176
+ step = (0, putil_varhelpers_1.coerceToInt)(step, 0);
177
+ if (!step || (step > 0 && this.isClosed))
178
+ return this.rowNum;
179
+ if (step < 0 && !this._cache)
180
+ throw new Error('To move cursor back, it needs cache to be enabled');
181
+ const _this = this;
182
+ await this._taskQueue.enqueue(async function () {
183
+ /* If moving backward */
184
+ if (step < 0) {
185
+ /* Seek cache */
186
+ while (step < 0 && _this._cache?.cursor) {
187
+ _this._row = _this._cache.prev();
188
+ _this._rowNum--;
189
+ step++;
190
+ }
191
+ return;
192
+ }
193
+ while (step > 0) {
194
+ if (_this.isEof)
195
+ return;
196
+ /* Seek cache */
197
+ while (step > 0 && _this._cache && (_this._row = _this._cache.next())) {
198
+ _this._rowNum++;
199
+ step--;
200
+ }
201
+ /* Fetch from prefetch cache */
202
+ while (step > 0 && (_this._row = _this._fetchCache.shift())) {
203
+ _this._rowNum++;
204
+ step--;
205
+ }
206
+ if (_this._fetchedAll) {
207
+ _this._rowNum++;
208
+ _this.emit('eof');
209
+ return;
210
+ }
211
+ if (!step || _this._fetchedAll)
212
+ return;
213
+ await _this._fetchRows();
214
+ }
215
+ });
216
+ if (!silent)
217
+ this.emit('move', this.row, this._rowNum);
218
+ return this._rowNum;
219
+ }
220
+ /**
221
+ *
222
+ */
223
+ async _fetchRows() {
224
+ if (!this._intlcur)
225
+ throw new Error('Cursor is closed');
226
+ let rows = await this._intlcur.fetch(this._prefetchRows);
227
+ if (rows && rows.length) {
228
+ debug('Fetched %d rows from database', rows.length);
229
+ // Normalize rows
230
+ rows = this._request.objectRows ?
231
+ (0, helpers_1.normalizeRowsToObjectRows)(this._fields, this._intlcur.rowType, rows, this._request) :
232
+ (0, helpers_1.normalizeRowsToArrayRows)(this._fields, this._intlcur.rowType, rows, this._request);
233
+ (0, helpers_1.callFetchHooks)(rows, this._request);
234
+ for (const [idx, row] of rows.entries()) {
235
+ this.emit('fetch', row, (this._rowNum + idx + 1));
236
+ }
237
+ /* Add rows to cache */
238
+ if (this._cache) {
239
+ this._cache.push(...rows);
240
+ }
241
+ else
242
+ this._fetchCache.push(...rows);
243
+ this._fetchedRows += rows.length;
244
+ return;
245
+ }
246
+ this._fetchedAll = true;
247
+ return this.close();
248
+ }
249
+ }
250
+ exports.Cursor = Cursor;
@@ -0,0 +1,29 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from 'stream';
3
+ import { Cursor } from './Cursor';
4
+ export interface CursorStreamOptions {
5
+ objectMode?: boolean;
6
+ limit?: number;
7
+ }
8
+ declare const inspect: unique symbol;
9
+ export declare class CursorStream extends Readable {
10
+ private readonly _cursor;
11
+ private readonly _objectMode?;
12
+ private readonly _limit;
13
+ private _rowNum;
14
+ private _eof;
15
+ constructor(cursor: Cursor, options?: CursorStreamOptions);
16
+ /**
17
+ * Returns if stream is closed.
18
+ */
19
+ get isClosed(): boolean;
20
+ /**
21
+ * Closes stream and releases the cursor
22
+ */
23
+ close(): Promise<void>;
24
+ toString(): string;
25
+ [inspect](): string;
26
+ _read(): void;
27
+ emit(event: string | symbol, ...args: any[]): boolean;
28
+ }
29
+ export {};
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CursorStream = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const stream_1 = require("stream");
6
+ const debug_1 = tslib_1.__importDefault(require("debug"));
7
+ const inspect = Symbol.for('nodejs.util.inspect.custom');
8
+ const debug = (0, debug_1.default)('sqb:cursorstream');
9
+ class CursorStream extends stream_1.Readable {
10
+ constructor(cursor, options) {
11
+ super(options);
12
+ this._rowNum = -1;
13
+ this._eof = false;
14
+ this._cursor = cursor;
15
+ this._objectMode = options?.objectMode;
16
+ this._limit = options?.limit || Number.MAX_SAFE_INTEGER;
17
+ this.on('end', () => {
18
+ this.close().catch(() => false);
19
+ });
20
+ cursor.once('close', () => this.emit('close'));
21
+ cursor.on('error', (err) => this.emit('error', err));
22
+ }
23
+ /**
24
+ * Returns if stream is closed.
25
+ */
26
+ get isClosed() {
27
+ return this._cursor.isClosed;
28
+ }
29
+ /**
30
+ * Closes stream and releases the cursor
31
+ */
32
+ close() {
33
+ this.pause();
34
+ this.unpipe();
35
+ return this._cursor.close();
36
+ }
37
+ toString() {
38
+ return '[object ' + Object.getPrototypeOf(this).constructor.name + ']';
39
+ }
40
+ [inspect]() {
41
+ return this.toString();
42
+ }
43
+ _read() {
44
+ if (this._rowNum >= this._limit) {
45
+ this.pause();
46
+ this.unpipe();
47
+ this.emit('end');
48
+ return;
49
+ }
50
+ this._cursor.next().then(row => {
51
+ if (this._eof)
52
+ return this.push(null);
53
+ let buf = '';
54
+ if (this._rowNum < 0) {
55
+ this._rowNum = 0;
56
+ if (!this._objectMode)
57
+ buf += '[';
58
+ }
59
+ if (!row) {
60
+ this._eof = true;
61
+ if (!this._objectMode) {
62
+ buf += ']';
63
+ this.push(buf);
64
+ }
65
+ else
66
+ this.push(null);
67
+ return;
68
+ }
69
+ this._rowNum++;
70
+ if (this._objectMode)
71
+ this.push(row);
72
+ else {
73
+ if (this._rowNum > 1)
74
+ buf += ',';
75
+ this.push(buf + JSON.stringify(row));
76
+ }
77
+ }).catch(err => {
78
+ /* istanbul ignore next */
79
+ if (typeof this.destroy == 'function')
80
+ this.destroy(err);
81
+ else
82
+ this.emit('error', err);
83
+ this.close().catch(() => 0);
84
+ });
85
+ }
86
+ emit(event, ...args) {
87
+ try {
88
+ if (event === 'error' && !this.listenerCount('error'))
89
+ return false;
90
+ return super.emit(event, ...args);
91
+ }
92
+ catch (ignored) {
93
+ debug('emit-error', ignored);
94
+ return false;
95
+ }
96
+ }
97
+ }
98
+ exports.CursorStream = CursorStream;
@@ -0,0 +1,10 @@
1
+ import { FieldInfo } from './types';
2
+ export declare class FieldInfoMap {
3
+ private _obj;
4
+ private _arr;
5
+ add(field: FieldInfo): void;
6
+ get(k: string | number): FieldInfo;
7
+ entries(): [string, FieldInfo][];
8
+ keys(): string[];
9
+ values(): FieldInfo[];
10
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FieldInfoMap = void 0;
4
+ class FieldInfoMap {
5
+ constructor() {
6
+ this._obj = {};
7
+ this._arr = [];
8
+ }
9
+ add(field) {
10
+ const idx = field.index != null ? field.index : this._arr.length;
11
+ this._arr[idx] = field;
12
+ field.index = idx;
13
+ this._obj = this._arr.reduce((a, f) => {
14
+ a[f.name.toUpperCase()] = f;
15
+ return a;
16
+ }, {});
17
+ }
18
+ get(k) {
19
+ if (typeof k === 'number')
20
+ return this._arr[k];
21
+ return this._obj[k.toUpperCase()];
22
+ }
23
+ entries() {
24
+ return Object.entries(this._obj);
25
+ }
26
+ keys() {
27
+ return Object.keys(this._obj);
28
+ }
29
+ values() {
30
+ return [...this._arr];
31
+ }
32
+ }
33
+ exports.FieldInfoMap = FieldInfoMap;
@@ -0,0 +1,70 @@
1
+ import { Pool as LightningPool } from 'lightning-pool';
2
+ import { Maybe, Type } from 'ts-gems';
3
+ import { classes } from '@sqb/builder';
4
+ import { ClientConfiguration, QueryExecuteOptions, TransactionFunction, ClientDefaults, ConnectionOptions, QueryRequest, ObjectQueryResult, ArrayQueryResult } from './types';
5
+ import { SqbConnection } from './SqbConnection';
6
+ import { Repository } from '../orm/repository.class';
7
+ declare const inspect: unique symbol;
8
+ interface SqbClientEvents {
9
+ execute: (request: QueryRequest) => void;
10
+ error: (error: Error) => void;
11
+ closing: () => void;
12
+ close: () => void;
13
+ acquire: (connection: SqbConnection) => Promise<void>;
14
+ terminate: () => void;
15
+ }
16
+ declare const SqbClient_base: Type<import("strict-typed-events").TypedEventEmitter<any, SqbClientEvents, SqbClientEvents>>;
17
+ export declare class SqbClient extends SqbClient_base {
18
+ private readonly _adapter;
19
+ private readonly _pool;
20
+ private readonly _defaults;
21
+ private readonly _entities;
22
+ constructor(config: ClientConfiguration);
23
+ get defaults(): ClientDefaults;
24
+ /**
25
+ * Returns dialect
26
+ */
27
+ get dialect(): string;
28
+ /**
29
+ * Returns database driver name
30
+ */
31
+ get driver(): string;
32
+ /**
33
+ * Returns true if pool is closed
34
+ */
35
+ get isClosed(): boolean;
36
+ get pool(): LightningPool;
37
+ /**
38
+ * Obtains a connection from the connection pool and executes the callback
39
+ */
40
+ acquire(fn: TransactionFunction, options?: ConnectionOptions): Promise<any>;
41
+ /**
42
+ * Obtains a connection from the connection pool.
43
+ */
44
+ acquire(options?: ConnectionOptions): Promise<SqbConnection>;
45
+ /**
46
+ * Shuts down the pool and destroys all resources.
47
+ */
48
+ close(terminateWait?: number): Promise<void>;
49
+ /**
50
+ * Executes a query or callback with a new acquired connection.
51
+ */
52
+ execute(query: string | classes.Query, options?: QueryExecuteOptions & {
53
+ objectRows: true;
54
+ }): Promise<ObjectQueryResult>;
55
+ execute(query: string | classes.Query, options?: QueryExecuteOptions & {
56
+ objectRows: false;
57
+ }): Promise<ArrayQueryResult>;
58
+ execute(query: string | classes.Query, options?: QueryExecuteOptions): Promise<ObjectQueryResult | ArrayQueryResult>;
59
+ /**
60
+ * Tests the pool
61
+ */
62
+ test(): Promise<void>;
63
+ getRepository<T>(entity: Type<T> | string, opts?: {
64
+ schema?: string;
65
+ }): Repository<T>;
66
+ getEntity<T>(name: string): Maybe<Type<T>>;
67
+ toString(): string;
68
+ [inspect](): string;
69
+ }
70
+ export {};