@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.
Files changed (144) hide show
  1. package/{types/client → client}/cursor-stream.d.ts +1 -1
  2. package/{types/client → client}/cursor.d.ts +4 -4
  3. package/{types/client → client}/sqb-connection.d.ts +4 -4
  4. package/{esm/orm → orm}/commands/command.helper.js +2 -3
  5. package/{types/orm → orm}/commands/count.command.d.ts +2 -2
  6. package/{types/orm → orm}/commands/create.command.d.ts +1 -1
  7. package/{esm/orm → orm}/commands/create.command.js +3 -5
  8. package/{types/orm → orm}/commands/delete.command.d.ts +2 -2
  9. package/{types/orm → orm}/commands/find.command.d.ts +2 -2
  10. package/{esm/orm → orm}/commands/find.command.js +3 -4
  11. package/{types/orm → orm}/commands/row-converter.d.ts +1 -1
  12. package/{types/orm → orm}/commands/update.command.d.ts +2 -2
  13. package/{esm/orm → orm}/commands/update.command.js +3 -5
  14. package/{types/orm → orm}/model/association-field-metadata.d.ts +0 -5
  15. package/{types/orm → orm}/model/association.d.ts +3 -3
  16. package/{esm/orm → orm}/model/association.js +1 -1
  17. package/{types/orm → orm}/model/column-field-metadata.d.ts +2 -8
  18. package/{types/orm → orm}/model/embedded-field-metadata.d.ts +0 -1
  19. package/{esm/orm → orm}/model/embedded-field-metadata.js +0 -14
  20. package/{types/orm → orm}/model/entity-metadata.d.ts +3 -3
  21. package/{esm/orm → orm}/model/entity-metadata.js +21 -6
  22. package/orm/model/index-metadata.js +1 -0
  23. package/orm/orm.type.js +1 -0
  24. package/{types/orm → orm}/repository.class.d.ts +2 -2
  25. package/{esm/orm → orm}/repository.class.js +1 -1
  26. package/{types/orm → orm}/util/orm.helper.d.ts +5 -3
  27. package/{esm/orm → orm}/util/orm.helper.js +14 -0
  28. package/package.json +10 -26
  29. package/cjs/client/adapter.js +0 -2
  30. package/cjs/client/cursor-stream.js +0 -104
  31. package/cjs/client/cursor.js +0 -261
  32. package/cjs/client/extensions.js +0 -41
  33. package/cjs/client/field-info-map.js +0 -54
  34. package/cjs/client/helpers.js +0 -140
  35. package/cjs/client/sqb-client.js +0 -175
  36. package/cjs/client/sqb-connection.js +0 -293
  37. package/cjs/client/sqb-error.js +0 -15
  38. package/cjs/client/types.js +0 -5
  39. package/cjs/index.js +0 -41
  40. package/cjs/orm/backward.js +0 -24
  41. package/cjs/orm/base-entity.js +0 -29
  42. package/cjs/orm/commands/command.helper.js +0 -170
  43. package/cjs/orm/commands/count.command.js +0 -30
  44. package/cjs/orm/commands/create.command.js +0 -92
  45. package/cjs/orm/commands/delete.command.js +0 -30
  46. package/cjs/orm/commands/find.command.js +0 -301
  47. package/cjs/orm/commands/row-converter.js +0 -190
  48. package/cjs/orm/commands/update.command.js +0 -83
  49. package/cjs/orm/decorators/column.decorator.js +0 -32
  50. package/cjs/orm/decorators/embedded.decorator.js +0 -15
  51. package/cjs/orm/decorators/entity.decorator.js +0 -180
  52. package/cjs/orm/decorators/events.decorator.js +0 -63
  53. package/cjs/orm/decorators/foreignkey.decorator.js +0 -12
  54. package/cjs/orm/decorators/index.decorator.js +0 -20
  55. package/cjs/orm/decorators/link.decorator.js +0 -72
  56. package/cjs/orm/decorators/primarykey.decorator.js +0 -24
  57. package/cjs/orm/decorators/transform.decorator.js +0 -21
  58. package/cjs/orm/model/association-field-metadata.js +0 -24
  59. package/cjs/orm/model/association-node.js +0 -28
  60. package/cjs/orm/model/association.js +0 -141
  61. package/cjs/orm/model/column-field-metadata.js +0 -32
  62. package/cjs/orm/model/embedded-field-metadata.js +0 -28
  63. package/cjs/orm/model/entity-metadata.js +0 -327
  64. package/cjs/orm/model/field-metadata.js +0 -2
  65. package/cjs/orm/model/index-metadata.js +0 -2
  66. package/cjs/orm/model/link-chain.js +0 -47
  67. package/cjs/orm/orm.const.js +0 -6
  68. package/cjs/orm/orm.type.js +0 -2
  69. package/cjs/orm/repository.class.js +0 -293
  70. package/cjs/orm/util/apply-mixins.js +0 -16
  71. package/cjs/orm/util/extract-keyvalues.js +0 -63
  72. package/cjs/orm/util/orm.helper.js +0 -34
  73. package/cjs/orm/util/parse-fields-projection.js +0 -68
  74. package/cjs/orm/util/serialize-field.js +0 -49
  75. package/cjs/package.json +0 -3
  76. package/esm/orm/model/association-field-metadata.js +0 -20
  77. package/esm/orm/model/column-field-metadata.js +0 -28
  78. package/esm/package.json +0 -3
  79. package/types/index.d.cts +0 -33
  80. /package/{types/client → client}/adapter.d.ts +0 -0
  81. /package/{esm/client → client}/adapter.js +0 -0
  82. /package/{esm/client → client}/cursor-stream.js +0 -0
  83. /package/{esm/client → client}/cursor.js +0 -0
  84. /package/{types/client → client}/extensions.d.ts +0 -0
  85. /package/{esm/client → client}/extensions.js +0 -0
  86. /package/{types/client → client}/field-info-map.d.ts +0 -0
  87. /package/{esm/client → client}/field-info-map.js +0 -0
  88. /package/{types/client → client}/helpers.d.ts +0 -0
  89. /package/{esm/client → client}/helpers.js +0 -0
  90. /package/{types/client → client}/sqb-client.d.ts +0 -0
  91. /package/{esm/client → client}/sqb-client.js +0 -0
  92. /package/{esm/client → client}/sqb-connection.js +0 -0
  93. /package/{types/client → client}/sqb-error.d.ts +0 -0
  94. /package/{esm/client → client}/sqb-error.js +0 -0
  95. /package/{types/client → client}/types.d.ts +0 -0
  96. /package/{esm/client → client}/types.js +0 -0
  97. /package/{types/index.d.ts → index.d.ts} +0 -0
  98. /package/{esm/index.js → index.js} +0 -0
  99. /package/{types/orm → orm}/backward.d.ts +0 -0
  100. /package/{esm/orm → orm}/backward.js +0 -0
  101. /package/{types/orm → orm}/base-entity.d.ts +0 -0
  102. /package/{esm/orm → orm}/base-entity.js +0 -0
  103. /package/{types/orm → orm}/commands/command.helper.d.ts +0 -0
  104. /package/{esm/orm → orm}/commands/count.command.js +0 -0
  105. /package/{esm/orm → orm}/commands/delete.command.js +0 -0
  106. /package/{esm/orm → orm}/commands/row-converter.js +0 -0
  107. /package/{types/orm → orm}/decorators/column.decorator.d.ts +0 -0
  108. /package/{esm/orm → orm}/decorators/column.decorator.js +0 -0
  109. /package/{types/orm → orm}/decorators/embedded.decorator.d.ts +0 -0
  110. /package/{esm/orm → orm}/decorators/embedded.decorator.js +0 -0
  111. /package/{types/orm → orm}/decorators/entity.decorator.d.ts +0 -0
  112. /package/{esm/orm → orm}/decorators/entity.decorator.js +0 -0
  113. /package/{types/orm → orm}/decorators/events.decorator.d.ts +0 -0
  114. /package/{esm/orm → orm}/decorators/events.decorator.js +0 -0
  115. /package/{types/orm → orm}/decorators/foreignkey.decorator.d.ts +0 -0
  116. /package/{esm/orm → orm}/decorators/foreignkey.decorator.js +0 -0
  117. /package/{types/orm → orm}/decorators/index.decorator.d.ts +0 -0
  118. /package/{esm/orm → orm}/decorators/index.decorator.js +0 -0
  119. /package/{types/orm → orm}/decorators/link.decorator.d.ts +0 -0
  120. /package/{esm/orm → orm}/decorators/link.decorator.js +0 -0
  121. /package/{types/orm → orm}/decorators/primarykey.decorator.d.ts +0 -0
  122. /package/{esm/orm → orm}/decorators/primarykey.decorator.js +0 -0
  123. /package/{types/orm → orm}/decorators/transform.decorator.d.ts +0 -0
  124. /package/{esm/orm → orm}/decorators/transform.decorator.js +0 -0
  125. /package/{esm/orm/model/field-metadata.js → orm/model/association-field-metadata.js} +0 -0
  126. /package/{types/orm → orm}/model/association-node.d.ts +0 -0
  127. /package/{esm/orm → orm}/model/association-node.js +0 -0
  128. /package/{esm/orm/model/index-metadata.js → orm/model/column-field-metadata.js} +0 -0
  129. /package/{types/orm → orm}/model/field-metadata.d.ts +0 -0
  130. /package/{esm/orm/orm.type.js → orm/model/field-metadata.js} +0 -0
  131. /package/{types/orm → orm}/model/index-metadata.d.ts +0 -0
  132. /package/{types/orm → orm}/model/link-chain.d.ts +0 -0
  133. /package/{esm/orm → orm}/model/link-chain.js +0 -0
  134. /package/{types/orm → orm}/orm.const.d.ts +0 -0
  135. /package/{esm/orm → orm}/orm.const.js +0 -0
  136. /package/{types/orm → orm}/orm.type.d.ts +0 -0
  137. /package/{types/orm → orm}/util/apply-mixins.d.ts +0 -0
  138. /package/{esm/orm → orm}/util/apply-mixins.js +0 -0
  139. /package/{types/orm → orm}/util/extract-keyvalues.d.ts +0 -0
  140. /package/{esm/orm → orm}/util/extract-keyvalues.js +0 -0
  141. /package/{types/orm → orm}/util/parse-fields-projection.d.ts +0 -0
  142. /package/{esm/orm → orm}/util/parse-fields-projection.js +0 -0
  143. /package/{types/orm → orm}/util/serialize-field.d.ts +0 -0
  144. /package/{esm/orm → orm}/util/serialize-field.js +0 -0
@@ -1,261 +0,0 @@
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) {
14
- _connection;
15
- _fields;
16
- _prefetchRows;
17
- _request;
18
- _intlcur;
19
- _taskQueue = new power_tasks_1.TaskQueue();
20
- _fetchCache = new doublylinked_1.default();
21
- _rowNum = 0;
22
- _fetchedAll = false;
23
- _fetchedRows = 0;
24
- _row;
25
- _cache;
26
- constructor(connection, fields, adapterCursor, request) {
27
- super();
28
- this._connection = connection;
29
- this._intlcur = adapterCursor;
30
- this._fields = fields;
31
- this._request = request;
32
- this._prefetchRows = request?.fetchRows || 100;
33
- }
34
- /**
35
- * Returns the Connection instance
36
- */
37
- get connection() {
38
- return this._connection;
39
- }
40
- /**
41
- * Returns if cursor is before first record.
42
- */
43
- get isBof() {
44
- return !this._rowNum;
45
- }
46
- /**
47
- * Returns if cursor is closed.
48
- */
49
- get isClosed() {
50
- return !this._intlcur;
51
- }
52
- /**
53
- * Returns if cursor is after last record.
54
- */
55
- get isEof() {
56
- return this._fetchedAll && this.rowNum > this._fetchedRows;
57
- }
58
- /**
59
- * Returns number of fetched record count from database.
60
- */
61
- get fetchedRows() {
62
- return this._fetchedRows;
63
- }
64
- /**
65
- * Returns object instance which contains information about fields.
66
- */
67
- get fields() {
68
- return this._fields;
69
- }
70
- /**
71
- * Returns current row
72
- */
73
- get row() {
74
- return this._row;
75
- }
76
- /**
77
- * Returns current row number.
78
- */
79
- get rowNum() {
80
- return this._rowNum;
81
- }
82
- /**
83
- * Enables cache
84
- */
85
- cached() {
86
- if (this.fetchedRows)
87
- throw new Error('Cache can be enabled before fetching rows');
88
- if (!this._cache)
89
- this._cache = new doublylinked_1.default();
90
- }
91
- /**
92
- * Closes cursor
93
- */
94
- async close() {
95
- if (!this._intlcur)
96
- return;
97
- try {
98
- await this._intlcur.close();
99
- this._intlcur = undefined;
100
- debug('close');
101
- this.emit('close');
102
- }
103
- catch (err) {
104
- debug('close-error:', err);
105
- this.emit('error', err);
106
- throw err;
107
- }
108
- }
109
- /**
110
- * If cache is enabled, this call fetches and keeps all records in the internal cache.
111
- * Otherwise it throws error. Once all all records fetched,
112
- * you can close Cursor safely and can continue to use it in memory.
113
- * Returns number of fetched rows
114
- */
115
- async fetchAll() {
116
- if (!this._cache)
117
- throw new Error('fetchAll() method needs cache to be enabled');
118
- const n = this.rowNum;
119
- const v = await this._seek(Number.MAX_SAFE_INTEGER, true);
120
- await this._seek(n - this.rowNum, true);
121
- return v;
122
- }
123
- /**
124
- * Moves cursor to given row number.
125
- * cursor can move both forward and backward if cache enabled.
126
- * Otherwise it throws error.
127
- */
128
- async moveTo(rowNum) {
129
- await this._seek(rowNum - this.rowNum);
130
- return this.row;
131
- }
132
- /**
133
- * Moves cursor forward by one row and returns that row.
134
- * And also it allows iterating over rows easily.
135
- */
136
- async next() {
137
- debug('next');
138
- await this._seek(1);
139
- return this.row;
140
- }
141
- /**
142
- * Moves cursor back by one row and returns that row.
143
- * And also it allows iterating over rows easily.
144
- */
145
- async prev() {
146
- await this._seek(-1);
147
- return this.row;
148
- }
149
- /**
150
- * Moves cursor before first row. (Required cache enabled)
151
- */
152
- reset() {
153
- if (!this._cache)
154
- throw new Error('reset() method needs cache to be enabled');
155
- this._cache.reset();
156
- this._rowNum = 0;
157
- this.emit('reset');
158
- }
159
- /**
160
- * Moves cursor by given step. If caching is enabled,
161
- * cursor can move both forward and backward. Otherwise it throws error.
162
- */
163
- async seek(step) {
164
- await this._seek(step);
165
- return this.row;
166
- }
167
- /**
168
- * Creates and returns a readable stream.
169
- */
170
- toStream(options) {
171
- return new cursor_stream_js_1.CursorStream(this, options);
172
- }
173
- toString() {
174
- return '[object ' + Object.getPrototypeOf(this).constructor.name + ']';
175
- }
176
- inspect() {
177
- return this.toString();
178
- }
179
- /**
180
- *
181
- */
182
- async _seek(step, silent) {
183
- step = (0, putil_varhelpers_1.coerceToInt)(step, 0);
184
- if (!step || (step > 0 && this.isClosed))
185
- return this.rowNum;
186
- if (step < 0 && !this._cache)
187
- throw new Error('To move cursor back, it needs cache to be enabled');
188
- const _this = this;
189
- await this._taskQueue
190
- .enqueue(async () => {
191
- /* If moving backward */
192
- if (step < 0) {
193
- /* Seek cache */
194
- while (step < 0 && _this._cache?.cursor) {
195
- _this._row = _this._cache.prev();
196
- _this._rowNum--;
197
- step++;
198
- }
199
- return;
200
- }
201
- while (step > 0) {
202
- if (_this.isEof)
203
- return;
204
- /* Seek cache */
205
- while (step > 0 &&
206
- _this._cache &&
207
- (_this._row = _this._cache.next())) {
208
- _this._rowNum++;
209
- step--;
210
- }
211
- /* Fetch from prefetch cache */
212
- while (step > 0 && (_this._row = _this._fetchCache.shift())) {
213
- _this._rowNum++;
214
- step--;
215
- }
216
- if (_this._fetchedAll) {
217
- _this._rowNum++;
218
- _this.emit('eof');
219
- return;
220
- }
221
- if (!step || _this._fetchedAll)
222
- return;
223
- await _this._fetchRows();
224
- }
225
- })
226
- .toPromise();
227
- if (!silent)
228
- this.emit('move', this.row, this._rowNum);
229
- return this._rowNum;
230
- }
231
- /**
232
- *
233
- */
234
- async _fetchRows() {
235
- if (!this._intlcur)
236
- throw new Error('Cursor is closed');
237
- let rows = await this._intlcur.fetch(this._prefetchRows);
238
- if (rows && rows.length) {
239
- debug('Fetched %d rows from database', rows.length);
240
- // Normalize rows
241
- rows = this._request.objectRows
242
- ? (0, helpers_js_1.normalizeRowsToObjectRows)(this._fields, this._intlcur.rowType, rows, this._request)
243
- : (0, helpers_js_1.normalizeRowsToArrayRows)(this._fields, this._intlcur.rowType, rows, this._request);
244
- (0, helpers_js_1.callFetchHooks)(rows, this._request);
245
- for (const [idx, row] of rows.entries()) {
246
- this.emit('fetch', row, this._rowNum + idx + 1);
247
- }
248
- /* Add rows to cache */
249
- if (this._cache) {
250
- this._cache.push(...rows);
251
- }
252
- else
253
- this._fetchCache.push(...rows);
254
- this._fetchedRows += rows.length;
255
- return;
256
- }
257
- this._fetchedAll = true;
258
- return this.close();
259
- }
260
- }
261
- exports.Cursor = Cursor;
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AdapterRegistry = void 0;
4
- class AdapterRegistry {
5
- static adapters = [];
6
- static get size() {
7
- return this.adapters.length;
8
- }
9
- static register(...adapters) {
10
- for (const adapter of adapters) {
11
- if (!adapter.driver)
12
- throw new TypeError('A DatabaseAdapter must contain "driver" property');
13
- this.adapters.push(adapter);
14
- }
15
- }
16
- static forEach(callback, thisArg) {
17
- return this.adapters.forEach(callback, thisArg);
18
- }
19
- static items() {
20
- return this.adapters.values();
21
- }
22
- static unRegister(...extensions) {
23
- this.adapters = extensions.filter(x => !extensions.includes(x));
24
- }
25
- static getAll(dialect) {
26
- return this.adapters.filter(x => x.dialect === dialect);
27
- }
28
- static get(index) {
29
- return this.adapters[index];
30
- }
31
- static findDialect(dialect) {
32
- return this.adapters.find(x => x.dialect === dialect);
33
- }
34
- static findDriver(dialect) {
35
- return this.adapters.find(x => x.driver === dialect);
36
- }
37
- static has(extension) {
38
- return !!this.adapters.find(x => x === extension);
39
- }
40
- }
41
- exports.AdapterRegistry = AdapterRegistry;
@@ -1,54 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FieldInfoMap = void 0;
4
- class FieldInfoMap {
5
- _obj;
6
- _arr;
7
- constructor() {
8
- Object.defineProperty(this, '_obj', {
9
- enumerable: false,
10
- configurable: false,
11
- writable: true,
12
- value: {},
13
- });
14
- Object.defineProperty(this, '_arr', {
15
- enumerable: false,
16
- configurable: false,
17
- writable: true,
18
- value: [],
19
- });
20
- }
21
- add(field) {
22
- const idx = field.index != null ? field.index : this._arr.length;
23
- this._arr[idx] = field;
24
- field.index = idx;
25
- const _obj = this._arr.reduce((a, f) => {
26
- a[f.name.toUpperCase()] = f;
27
- return a;
28
- }, {});
29
- Object.defineProperty(this, '_obj', {
30
- enumerable: false,
31
- configurable: false,
32
- writable: true,
33
- value: _obj,
34
- });
35
- }
36
- get(k) {
37
- if (typeof k === 'number')
38
- return this._arr[k];
39
- return this._obj[k.toUpperCase()];
40
- }
41
- entries() {
42
- return Object.entries(this._obj);
43
- }
44
- keys() {
45
- return Object.keys(this._obj);
46
- }
47
- values() {
48
- return [...this._arr];
49
- }
50
- toJSON() {
51
- return { ...this._obj };
52
- }
53
- }
54
- exports.FieldInfoMap = FieldInfoMap;
@@ -1,140 +0,0 @@
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) {
11
- if (typeof namingStrategy === 'string' && namingStrategy !== 'original') {
12
- switch (namingStrategy.toLowerCase()) {
13
- case 'lowercase':
14
- return value.toLowerCase();
15
- case 'uppercase':
16
- return value.toUpperCase();
17
- case 'camelcase':
18
- if (!value.match(/[a-z]/))
19
- return (0, putil_varhelpers_1.camelCase)(value.toLowerCase());
20
- value = (0, putil_varhelpers_1.camelCase)(value);
21
- return value[0].toLowerCase() + value.substring(1);
22
- case 'pascalcase':
23
- if (!value.match(/[a-z]/))
24
- return (0, putil_varhelpers_1.pascalCase)(value.toLowerCase());
25
- return (0, putil_varhelpers_1.pascalCase)(value);
26
- default:
27
- break;
28
- }
29
- }
30
- else if (typeof namingStrategy === 'function')
31
- return namingStrategy(value);
32
- return value;
33
- }
34
- function wrapAdapterFields(oldFields, fieldNaming) {
35
- const mapFieldInfo = (f, index) => {
36
- const name = applyNamingStrategy(f.fieldName, fieldNaming);
37
- if (name)
38
- return { ...f, name, index };
39
- };
40
- const result = new field_info_map_js_1.FieldInfoMap();
41
- let i = 0;
42
- oldFields.forEach(f => {
43
- const x = mapFieldInfo(f, i);
44
- if (x) {
45
- i++;
46
- result.add(x);
47
- }
48
- });
49
- return result;
50
- }
51
- function normalizeRowsToObjectRows(fields, rowType, oldRows, options) {
52
- return normalizeRows(fields, rowType, oldRows, {
53
- ...options,
54
- objectRows: true,
55
- });
56
- }
57
- function normalizeRowsToArrayRows(fields, rowType, oldRows, options) {
58
- return normalizeRows(fields, rowType, oldRows, {
59
- ...options,
60
- objectRows: false,
61
- });
62
- }
63
- function normalizeRows(fields, rowType, oldRows, options) {
64
- const ignoreNulls = options.ignoreNulls && options.objectRows;
65
- const transform = options.transform;
66
- const coerceValue = (v, f) => {
67
- if (v === undefined)
68
- return;
69
- if (transform)
70
- v = transform(v, f);
71
- if (v === null && ignoreNulls)
72
- return;
73
- return v;
74
- };
75
- if (options.objectRows) {
76
- if (rowType === 'array') {
77
- return oldRows.map((row) => {
78
- const r = {};
79
- for (const f of fields.values()) {
80
- const v = coerceValue(row[f.index], f);
81
- if (v !== undefined)
82
- r[f.name] = v;
83
- }
84
- return r;
85
- });
86
- }
87
- let hasFieldNaming = false;
88
- for (const f of fields.values()) {
89
- if (f.name !== f.fieldName) {
90
- hasFieldNaming = true;
91
- break;
92
- }
93
- }
94
- if (hasFieldNaming || transform) {
95
- return oldRows.map((row) => {
96
- const r = {};
97
- for (const f of fields.values()) {
98
- const v = coerceValue(row[f.fieldName], f);
99
- if (v !== undefined)
100
- r[f.name] = v;
101
- }
102
- return r;
103
- });
104
- }
105
- if (ignoreNulls) {
106
- oldRows.forEach(row => {
107
- for (const [k, v] of Object.entries(row)) {
108
- if (v === null)
109
- delete row[k];
110
- }
111
- });
112
- }
113
- return oldRows;
114
- }
115
- if (rowType === 'array') {
116
- oldRows.forEach((row) => {
117
- for (const [i, v] of row.entries()) {
118
- row[i] = coerceValue(v, fields.get(i));
119
- }
120
- });
121
- return oldRows;
122
- }
123
- return oldRows.map((row) => {
124
- const r = [];
125
- for (const f of fields.values()) {
126
- r[f.index] = coerceValue(row[f.fieldName], f);
127
- }
128
- return r;
129
- });
130
- }
131
- function callFetchHooks(rows, request) {
132
- const fetchHooks = request.fetchHooks;
133
- if (!fetchHooks)
134
- return;
135
- for (const row of rows) {
136
- for (const fn of fetchHooks) {
137
- fn(row, request);
138
- }
139
- }
140
- }
@@ -1,175 +0,0 @@
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');
14
- const inspect = Symbol.for('nodejs.util.inspect.custom');
15
- class SqbClient extends (0, strict_typed_events_1.TypedEventEmitterClass)(strict_typed_events_1.AsyncEventEmitter) {
16
- _adapter;
17
- _pool;
18
- _defaults;
19
- _entities = {};
20
- constructor(config) {
21
- super();
22
- if (!(config && typeof config === 'object'))
23
- throw new TypeError('Configuration object required');
24
- let adapter;
25
- if (config.driver) {
26
- adapter = extensions_js_1.AdapterRegistry.findDriver(config.driver);
27
- if (!adapter)
28
- throw new Error(`No database adapter registered for "${config.driver}" driver`);
29
- }
30
- else if (config.dialect) {
31
- adapter = extensions_js_1.AdapterRegistry.findDialect(config.dialect);
32
- if (!adapter)
33
- throw new Error(`No database adapter registered for "${config.dialect}" dialect`);
34
- }
35
- if (!adapter)
36
- throw new Error(`You must provide one of "driver" or "dialect" properties`);
37
- this._adapter = adapter;
38
- this._defaults = config.defaults || {};
39
- const poolOptions = {};
40
- const popts = config.pool || {};
41
- poolOptions.acquireMaxRetries = (0, putil_varhelpers_1.coerceToInt)(popts.acquireMaxRetries, 0);
42
- poolOptions.acquireRetryWait = (0, putil_varhelpers_1.coerceToInt)(popts.acquireRetryWait, 2000);
43
- poolOptions.acquireTimeoutMillis = (0, putil_varhelpers_1.coerceToInt)(popts.acquireTimeoutMillis, 0);
44
- poolOptions.idleTimeoutMillis = (0, putil_varhelpers_1.coerceToInt)(popts.idleTimeoutMillis, 30000);
45
- poolOptions.max = (0, putil_varhelpers_1.coerceToInt)(popts.max, 10);
46
- poolOptions.maxQueue = (0, putil_varhelpers_1.coerceToInt)(popts.maxQueue, 1000);
47
- poolOptions.max = (0, putil_varhelpers_1.coerceToInt)(popts.max, 10);
48
- poolOptions.min = (0, putil_varhelpers_1.coerceToInt)(popts.min, 0);
49
- poolOptions.minIdle = (0, putil_varhelpers_1.coerceToInt)(popts.minIdle, 0);
50
- poolOptions.validation = (0, putil_varhelpers_1.coerceToBoolean)(popts.validation, false);
51
- const cfg = { ...config };
52
- const poolFactory = {
53
- create: () => adapter.connect(cfg),
54
- destroy: instance => instance.close(),
55
- reset: async (instance) => instance.reset(),
56
- validate: instance => instance.test(),
57
- };
58
- this._pool = (0, lightning_pool_1.createPool)(poolFactory, poolOptions);
59
- this._pool.on('closing', () => this.emit('closing'));
60
- this._pool.on('close', () => this.emit('close'));
61
- this._pool.on('terminate', () => this.emit('terminate'));
62
- // @ts-ignore
63
- this._pool.on('error', (...args) => this.emit('error', ...args));
64
- }
65
- get defaults() {
66
- return this._defaults;
67
- }
68
- /**
69
- * Returns dialect
70
- */
71
- get dialect() {
72
- return this._adapter.dialect;
73
- }
74
- /**
75
- * Returns database driver name
76
- */
77
- get driver() {
78
- return this._adapter.driver;
79
- }
80
- /**
81
- * Returns true if pool is closed
82
- */
83
- get isClosed() {
84
- return this._pool.state === lightning_pool_1.PoolState.CLOSED;
85
- }
86
- get pool() {
87
- return this._pool;
88
- }
89
- async acquire(arg0, arg1) {
90
- debug('acquire');
91
- if (typeof arg0 === 'function') {
92
- const connection = await this.acquire(arg1);
93
- try {
94
- return await arg0(connection);
95
- }
96
- finally {
97
- connection.release();
98
- }
99
- }
100
- const options = arg1;
101
- const adapterConnection = await this._pool.acquire();
102
- const opts = { autoCommit: this.defaults.autoCommit, ...options };
103
- const connection = new sqb_connection_js_1.SqbConnection(this, adapterConnection, opts);
104
- await this.emitAsyncSerial('acquire', connection);
105
- connection.on('execute', (request) => this.emit('execute', request));
106
- connection.on('error', (error) => this.emit('error', error));
107
- connection.on('close', () => this.emitAsyncSerial('connection-return', connection));
108
- return connection;
109
- }
110
- /**
111
- * Shuts down the pool and destroys all resources.
112
- */
113
- async close(terminateWait) {
114
- const ms = terminateWait == null ? Infinity : 0;
115
- return this._pool.close(ms);
116
- }
117
- /**
118
- * Executes a query or callback with a new acquired connection.
119
- */
120
- async execute(query, options) {
121
- debug('execute');
122
- const connection = await this.acquire();
123
- try {
124
- const qr = await connection.execute(query, options);
125
- if (qr && qr.cursor) {
126
- connection.retain();
127
- qr.cursor.once('close', () => connection.release());
128
- }
129
- return qr;
130
- }
131
- finally {
132
- connection.release();
133
- }
134
- }
135
- /**
136
- * Tests the pool
137
- */
138
- async test() {
139
- const connection = await this.acquire();
140
- try {
141
- await connection.test();
142
- }
143
- finally {
144
- connection.release();
145
- }
146
- }
147
- getRepository(entity, opts) {
148
- let ctor;
149
- if (typeof entity === 'string') {
150
- ctor = this.getEntity(entity);
151
- if (!ctor)
152
- throw new Error(`Repository "${entity}" is not registered`);
153
- }
154
- else
155
- ctor = entity;
156
- const entityDef = entity_metadata_js_1.EntityMetadata.get(ctor);
157
- if (!entityDef)
158
- throw new Error(`You must provide an @Entity annotated constructor`);
159
- return new repository_class_js_1.Repository(entityDef, this, opts?.schema);
160
- }
161
- getEntity(name) {
162
- return this._entities[name];
163
- }
164
- toString() {
165
- return ('[object ' +
166
- Object.getPrototypeOf(this).constructor.name +
167
- '(' +
168
- this.dialect +
169
- ')]');
170
- }
171
- [inspect]() {
172
- return this.toString();
173
- }
174
- }
175
- exports.SqbClient = SqbClient;