@sqb/connect 4.15.0 → 4.16.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/esm/client/adapter.js +1 -2
- package/esm/client/cursor-stream.js +4 -9
- package/esm/client/cursor.js +17 -22
- package/esm/client/extensions.js +1 -5
- package/esm/client/field-info-map.js +1 -5
- package/esm/client/helpers.js +12 -19
- package/esm/client/sqb-client.js +27 -32
- package/esm/client/sqb-connection.js +36 -41
- package/esm/client/types.js +1 -5
- package/esm/index.js +32 -40
- package/esm/orm/backward.js +12 -19
- package/esm/orm/base-entity.js +6 -10
- package/esm/orm/commands/command.helper.js +28 -34
- package/esm/orm/commands/count.command.js +6 -10
- package/esm/orm/commands/create.command.js +14 -18
- package/esm/orm/commands/delete.command.js +6 -10
- package/esm/orm/commands/find.command.js +30 -34
- package/esm/orm/commands/row-converter.js +3 -7
- package/esm/orm/commands/update.command.js +14 -18
- package/esm/orm/decorators/column.decorator.js +7 -10
- package/esm/orm/decorators/embedded.decorator.js +4 -7
- package/esm/orm/decorators/entity.decorator.js +53 -56
- package/esm/orm/decorators/events.decorator.js +19 -27
- package/esm/orm/decorators/foreignkey.decorator.js +4 -7
- package/esm/orm/decorators/index.decorator.js +6 -9
- package/esm/orm/decorators/link.decorator.js +8 -11
- package/esm/orm/decorators/primarykey.decorator.js +8 -11
- package/esm/orm/decorators/transform.decorator.js +7 -11
- package/esm/orm/model/association-field-metadata.js +4 -8
- package/esm/orm/model/association-node.js +2 -6
- package/esm/orm/model/association.js +16 -20
- package/esm/orm/model/column-field-metadata.js +4 -8
- package/esm/orm/model/embedded-field-metadata.js +4 -7
- package/esm/orm/model/entity-metadata.js +47 -50
- package/esm/orm/model/field-metadata.js +1 -2
- package/esm/orm/model/index-metadata.js +1 -2
- package/esm/orm/model/link-chain.js +4 -8
- package/esm/orm/orm.const.js +3 -6
- package/esm/orm/orm.type.js +1 -2
- package/esm/orm/repository.class.js +22 -26
- package/esm/orm/util/apply-mixins.js +1 -4
- package/esm/orm/util/extract-keyvalues.js +6 -9
- package/esm/orm/util/orm.helper.js +10 -20
- package/esm/orm/util/parse-fields-projection.js +6 -11
- package/esm/orm/util/serialize-field.js +10 -13
- package/package.json +5 -5
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const column_field_metadata_js_1 = require("./column-field-metadata.js");
|
|
10
|
-
const embedded_field_metadata_js_1 = require("./embedded-field-metadata.js");
|
|
11
|
-
var EntityMetadata;
|
|
1
|
+
import { DataType } from '@sqb/builder';
|
|
2
|
+
import { ENTITY_METADATA_KEY } from '../orm.const.js';
|
|
3
|
+
import { isAssociationField, isColumnField, isEmbeddedField } from '../util/orm.helper.js';
|
|
4
|
+
import { Association } from './association.js';
|
|
5
|
+
import { AssociationFieldMetadata } from './association-field-metadata.js';
|
|
6
|
+
import { ColumnFieldMetadata } from './column-field-metadata.js';
|
|
7
|
+
import { EmbeddedFieldMetadata } from './embedded-field-metadata.js';
|
|
8
|
+
export var EntityMetadata;
|
|
12
9
|
(function (EntityMetadata) {
|
|
13
10
|
function define(ctor) {
|
|
14
11
|
const own = getOwn(ctor);
|
|
@@ -23,7 +20,7 @@ var EntityMetadata;
|
|
|
23
20
|
foreignKeys: [],
|
|
24
21
|
eventListeners: {},
|
|
25
22
|
};
|
|
26
|
-
Reflect.defineMetadata(
|
|
23
|
+
Reflect.defineMetadata(ENTITY_METADATA_KEY, meta, ctor);
|
|
27
24
|
// Merge base entity columns into this one
|
|
28
25
|
if (baseMeta) {
|
|
29
26
|
EntityMetadata.mixin(meta, baseMeta);
|
|
@@ -32,11 +29,11 @@ var EntityMetadata;
|
|
|
32
29
|
}
|
|
33
30
|
EntityMetadata.define = define;
|
|
34
31
|
function get(ctor) {
|
|
35
|
-
return Reflect.getMetadata(
|
|
32
|
+
return Reflect.getMetadata(ENTITY_METADATA_KEY, ctor);
|
|
36
33
|
}
|
|
37
34
|
EntityMetadata.get = get;
|
|
38
35
|
function getOwn(ctor) {
|
|
39
|
-
return Reflect.getOwnMetadata(
|
|
36
|
+
return Reflect.getOwnMetadata(ENTITY_METADATA_KEY, ctor);
|
|
40
37
|
}
|
|
41
38
|
EntityMetadata.getOwn = getOwn;
|
|
42
39
|
function getField(entity, fieldName) {
|
|
@@ -45,21 +42,21 @@ var EntityMetadata;
|
|
|
45
42
|
EntityMetadata.getField = getField;
|
|
46
43
|
function getColumnField(entity, fieldName) {
|
|
47
44
|
const el = getField(entity, fieldName);
|
|
48
|
-
if (el && !
|
|
45
|
+
if (el && !isColumnField(el))
|
|
49
46
|
throw new Error(`"${el.name}" requested as "column" but it is "${el.kind}"`);
|
|
50
47
|
return el;
|
|
51
48
|
}
|
|
52
49
|
EntityMetadata.getColumnField = getColumnField;
|
|
53
50
|
function getEmbeddedField(entity, fieldName) {
|
|
54
51
|
const el = getField(entity, fieldName);
|
|
55
|
-
if (el && !
|
|
52
|
+
if (el && !isEmbeddedField(el))
|
|
56
53
|
throw new Error(`"${el.name}" requested as "embedded" but it is "${el.kind}"`);
|
|
57
54
|
return el;
|
|
58
55
|
}
|
|
59
56
|
EntityMetadata.getEmbeddedField = getEmbeddedField;
|
|
60
57
|
function getAssociationField(entity, fieldName) {
|
|
61
58
|
const el = getField(entity, fieldName);
|
|
62
|
-
if (el && !
|
|
59
|
+
if (el && !isAssociationField(el)) {
|
|
63
60
|
throw new Error(`"${el.name}" requested as "association" but it is "${el.kind}"`);
|
|
64
61
|
}
|
|
65
62
|
return el;
|
|
@@ -74,7 +71,7 @@ var EntityMetadata;
|
|
|
74
71
|
return;
|
|
75
72
|
fieldName = fieldName.toLowerCase();
|
|
76
73
|
for (const prop of Object.values(entity.fields)) {
|
|
77
|
-
if (
|
|
74
|
+
if (isColumnField(prop) && prop.fieldName.toLowerCase() === fieldName)
|
|
78
75
|
return prop;
|
|
79
76
|
}
|
|
80
77
|
}
|
|
@@ -101,27 +98,27 @@ var EntityMetadata;
|
|
|
101
98
|
}
|
|
102
99
|
EntityMetadata.getFieldNames = getFieldNames;
|
|
103
100
|
function getColumnFieldNames(entity) {
|
|
104
|
-
return getFieldNames(entity,
|
|
101
|
+
return getFieldNames(entity, isColumnField);
|
|
105
102
|
}
|
|
106
103
|
EntityMetadata.getColumnFieldNames = getColumnFieldNames;
|
|
107
104
|
function getEmbeddedFieldNames(entity) {
|
|
108
|
-
return getFieldNames(entity,
|
|
105
|
+
return getFieldNames(entity, isEmbeddedField);
|
|
109
106
|
}
|
|
110
107
|
EntityMetadata.getEmbeddedFieldNames = getEmbeddedFieldNames;
|
|
111
108
|
function getAssociationFieldNames(entity) {
|
|
112
|
-
return getFieldNames(entity,
|
|
109
|
+
return getFieldNames(entity, isAssociationField);
|
|
113
110
|
}
|
|
114
111
|
EntityMetadata.getAssociationFieldNames = getAssociationFieldNames;
|
|
115
112
|
function getNonAssociationFieldNames(entity) {
|
|
116
|
-
return getFieldNames(entity, x => !
|
|
113
|
+
return getFieldNames(entity, x => !isAssociationField(x));
|
|
117
114
|
}
|
|
118
115
|
EntityMetadata.getNonAssociationFieldNames = getNonAssociationFieldNames;
|
|
119
116
|
function getInsertColumnNames(entity) {
|
|
120
|
-
return getFieldNames(entity, x =>
|
|
117
|
+
return getFieldNames(entity, x => isColumnField(x) && !x.noInsert);
|
|
121
118
|
}
|
|
122
119
|
EntityMetadata.getInsertColumnNames = getInsertColumnNames;
|
|
123
120
|
function getUpdateColumnNames(entity) {
|
|
124
|
-
return getFieldNames(entity, x =>
|
|
121
|
+
return getFieldNames(entity, x => isColumnField(x) && !x.noUpdate);
|
|
125
122
|
}
|
|
126
123
|
EntityMetadata.getUpdateColumnNames = getUpdateColumnNames;
|
|
127
124
|
function getPrimaryIndex(entity) {
|
|
@@ -164,7 +161,7 @@ var EntityMetadata;
|
|
|
164
161
|
EntityMetadata.addIndex = addIndex;
|
|
165
162
|
function addForeignKey(entity, propertyKey, target, targetKey) {
|
|
166
163
|
entity.foreignKeys = entity.foreignKeys || [];
|
|
167
|
-
const fk = new
|
|
164
|
+
const fk = new Association(entity.name + '.' + propertyKey, {
|
|
168
165
|
source: entity.ctor,
|
|
169
166
|
sourceKey: propertyKey,
|
|
170
167
|
target,
|
|
@@ -184,30 +181,30 @@ var EntityMetadata;
|
|
|
184
181
|
function defineColumnField(entity, name, options = {}) {
|
|
185
182
|
delete entity._fieldNames;
|
|
186
183
|
let prop = EntityMetadata.getField(entity, name);
|
|
187
|
-
if (
|
|
184
|
+
if (isColumnField(prop))
|
|
188
185
|
options = { ...prop, ...options };
|
|
189
186
|
if (!options.type) {
|
|
190
187
|
switch (options.dataType) {
|
|
191
|
-
case
|
|
188
|
+
case DataType.BOOL:
|
|
192
189
|
options.type = Boolean;
|
|
193
190
|
break;
|
|
194
|
-
case
|
|
195
|
-
case
|
|
196
|
-
case
|
|
191
|
+
case DataType.VARCHAR:
|
|
192
|
+
case DataType.CHAR:
|
|
193
|
+
case DataType.TEXT:
|
|
197
194
|
options.type = String;
|
|
198
195
|
break;
|
|
199
|
-
case
|
|
200
|
-
case
|
|
201
|
-
case
|
|
202
|
-
case
|
|
203
|
-
case
|
|
196
|
+
case DataType.NUMBER:
|
|
197
|
+
case DataType.DOUBLE:
|
|
198
|
+
case DataType.FLOAT:
|
|
199
|
+
case DataType.INTEGER:
|
|
200
|
+
case DataType.SMALLINT:
|
|
204
201
|
options.type = Number;
|
|
205
202
|
break;
|
|
206
|
-
case
|
|
207
|
-
case
|
|
203
|
+
case DataType.TIMESTAMP:
|
|
204
|
+
case DataType.TIMESTAMPTZ:
|
|
208
205
|
options.type = Date;
|
|
209
206
|
break;
|
|
210
|
-
case
|
|
207
|
+
case DataType.BINARY:
|
|
211
208
|
options.type = Buffer;
|
|
212
209
|
break;
|
|
213
210
|
default:
|
|
@@ -217,26 +214,26 @@ var EntityMetadata;
|
|
|
217
214
|
if (!options.dataType) {
|
|
218
215
|
switch (options.type) {
|
|
219
216
|
case Boolean:
|
|
220
|
-
options.dataType =
|
|
217
|
+
options.dataType = DataType.BOOL;
|
|
221
218
|
break;
|
|
222
219
|
case Number:
|
|
223
|
-
options.dataType =
|
|
220
|
+
options.dataType = DataType.NUMBER;
|
|
224
221
|
break;
|
|
225
222
|
case Date:
|
|
226
|
-
options.dataType =
|
|
223
|
+
options.dataType = DataType.TIMESTAMP;
|
|
227
224
|
break;
|
|
228
225
|
case Array:
|
|
229
|
-
options.dataType =
|
|
226
|
+
options.dataType = DataType.VARCHAR;
|
|
230
227
|
options.isArray = true;
|
|
231
228
|
break;
|
|
232
229
|
case Buffer:
|
|
233
|
-
options.dataType =
|
|
230
|
+
options.dataType = DataType.BINARY;
|
|
234
231
|
break;
|
|
235
232
|
default:
|
|
236
|
-
options.dataType =
|
|
233
|
+
options.dataType = DataType.VARCHAR;
|
|
237
234
|
}
|
|
238
235
|
}
|
|
239
|
-
prop =
|
|
236
|
+
prop = ColumnFieldMetadata.create(entity, name, options);
|
|
240
237
|
entity.fields[name.toLowerCase()] = prop;
|
|
241
238
|
return prop;
|
|
242
239
|
}
|
|
@@ -244,16 +241,16 @@ var EntityMetadata;
|
|
|
244
241
|
function defineEmbeddedField(entity, name, type, options) {
|
|
245
242
|
delete entity._fieldNames;
|
|
246
243
|
let prop = EntityMetadata.getField(entity, name);
|
|
247
|
-
if (
|
|
244
|
+
if (isEmbeddedField(prop))
|
|
248
245
|
options = { ...prop, ...options };
|
|
249
|
-
prop =
|
|
246
|
+
prop = EmbeddedFieldMetadata.create(entity, name, type, options);
|
|
250
247
|
entity.fields[name.toLowerCase()] = prop;
|
|
251
248
|
return prop;
|
|
252
249
|
}
|
|
253
250
|
EntityMetadata.defineEmbeddedField = defineEmbeddedField;
|
|
254
251
|
function defineAssociationField(entity, propertyKey, association, options) {
|
|
255
252
|
delete entity._fieldNames;
|
|
256
|
-
const prop =
|
|
253
|
+
const prop = AssociationFieldMetadata.create(entity, propertyKey, association, options);
|
|
257
254
|
let l = association;
|
|
258
255
|
let i = 1;
|
|
259
256
|
while (l) {
|
|
@@ -298,7 +295,7 @@ var EntityMetadata;
|
|
|
298
295
|
derived.foreignKeys = derived.foreignKeys || [];
|
|
299
296
|
for (const fk of base.foreignKeys) {
|
|
300
297
|
if (!fk.sourceKey || hasField(fk.sourceKey)) {
|
|
301
|
-
const newFk = new
|
|
298
|
+
const newFk = new Association(fk.name, { ...fk, source: derived.ctor });
|
|
302
299
|
derived.foreignKeys.push(newFk);
|
|
303
300
|
}
|
|
304
301
|
}
|
|
@@ -321,4 +318,4 @@ var EntityMetadata;
|
|
|
321
318
|
}
|
|
322
319
|
}
|
|
323
320
|
EntityMetadata.mixin = mixin;
|
|
324
|
-
})(EntityMetadata || (
|
|
321
|
+
})(EntityMetadata || (EntityMetadata = {}));
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.LinkChain = void 0;
|
|
4
|
-
const association_node_js_1 = require("./association-node.js");
|
|
5
|
-
class LinkChain {
|
|
1
|
+
import { AssociationNode } from './association-node.js';
|
|
2
|
+
export class LinkChain {
|
|
6
3
|
constructor(target, targetKey, sourceKey, many) {
|
|
7
4
|
this.target = target;
|
|
8
|
-
this.first = this.current = new
|
|
5
|
+
this.first = this.current = new AssociationNode('', {
|
|
9
6
|
many,
|
|
10
7
|
target,
|
|
11
8
|
source: null,
|
|
@@ -28,7 +25,7 @@ class LinkChain {
|
|
|
28
25
|
return this._newNode(target, targetColumn, parentColumn, true);
|
|
29
26
|
}
|
|
30
27
|
_newNode(target, targetKey, parentKey, many = false) {
|
|
31
|
-
const child = new
|
|
28
|
+
const child = new AssociationNode(this.current.name, {
|
|
32
29
|
many,
|
|
33
30
|
source: this.current.target,
|
|
34
31
|
target,
|
|
@@ -41,4 +38,3 @@ class LinkChain {
|
|
|
41
38
|
return this;
|
|
42
39
|
}
|
|
43
40
|
}
|
|
44
|
-
exports.LinkChain = LinkChain;
|
package/esm/orm/orm.const.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.ENTITY_METADATA_KEY = Symbol.for('SQB_ENTITY_METADATA');
|
|
5
|
-
exports.REPOSITORY_KEY = Symbol.for('SQB_REPOSITORY');
|
|
6
|
-
exports.DECORATOR_FACTORY = Symbol.for('decorator.factory');
|
|
1
|
+
export const ENTITY_METADATA_KEY = Symbol.for('SQB_ENTITY_METADATA');
|
|
2
|
+
export const REPOSITORY_KEY = Symbol.for('SQB_REPOSITORY');
|
|
3
|
+
export const DECORATOR_FACTORY = Symbol.for('decorator.factory');
|
package/esm/orm/orm.type.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const find_command_js_1 = require("./commands/find.command.js");
|
|
10
|
-
const update_command_js_1 = require("./commands/update.command.js");
|
|
11
|
-
const extract_keyvalues_js_1 = require("./util/extract-keyvalues.js");
|
|
1
|
+
import { AsyncEventEmitter, TypedEventEmitterClass } from 'strict-typed-events';
|
|
2
|
+
import { SqbConnection } from '../client/sqb-connection.js';
|
|
3
|
+
import { CountCommand } from './commands/count.command.js';
|
|
4
|
+
import { CreateCommand } from './commands/create.command.js';
|
|
5
|
+
import { DeleteCommand } from './commands/delete.command.js';
|
|
6
|
+
import { FindCommand } from './commands/find.command.js';
|
|
7
|
+
import { UpdateCommand } from './commands/update.command.js';
|
|
8
|
+
import { extractKeyValues } from './util/extract-keyvalues.js';
|
|
12
9
|
/**
|
|
13
10
|
* @class Repository
|
|
14
11
|
* @template T - The data type class type of the record
|
|
15
12
|
*/
|
|
16
|
-
class Repository extends
|
|
13
|
+
export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
17
14
|
constructor(entityDef, executor, schema) {
|
|
18
15
|
super();
|
|
19
16
|
this._executor = executor;
|
|
@@ -137,7 +134,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
137
134
|
}
|
|
138
135
|
async _execute(fn, opts) {
|
|
139
136
|
let connection = opts?.connection;
|
|
140
|
-
if (!connection && this._executor instanceof
|
|
137
|
+
if (!connection && this._executor instanceof SqbConnection)
|
|
141
138
|
connection = this._executor;
|
|
142
139
|
if (connection) {
|
|
143
140
|
if (this._schema)
|
|
@@ -154,7 +151,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
154
151
|
async _create(values, options) {
|
|
155
152
|
if (!values)
|
|
156
153
|
throw new TypeError('You must provide values');
|
|
157
|
-
const keyValues = await
|
|
154
|
+
const keyValues = await CreateCommand.execute({
|
|
158
155
|
...options,
|
|
159
156
|
entity: this._entity,
|
|
160
157
|
values,
|
|
@@ -164,7 +161,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
164
161
|
return keyValues;
|
|
165
162
|
}
|
|
166
163
|
async _exists(keyValue, options) {
|
|
167
|
-
const filter = [
|
|
164
|
+
const filter = [extractKeyValues(this._entity, keyValue, true)];
|
|
168
165
|
if (options && options.filter) {
|
|
169
166
|
if (Array.isArray(options.filter))
|
|
170
167
|
filter.push(...options.filter);
|
|
@@ -184,7 +181,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
184
181
|
else
|
|
185
182
|
filter.push(options.filter);
|
|
186
183
|
}
|
|
187
|
-
const resp = await
|
|
184
|
+
const resp = await FindCommand.execute({
|
|
188
185
|
...options,
|
|
189
186
|
filter,
|
|
190
187
|
entity: this._entity,
|
|
@@ -192,13 +189,13 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
192
189
|
return resp.length > 0;
|
|
193
190
|
}
|
|
194
191
|
async _count(options) {
|
|
195
|
-
return
|
|
192
|
+
return CountCommand.execute({
|
|
196
193
|
...options,
|
|
197
194
|
entity: this._entity,
|
|
198
195
|
});
|
|
199
196
|
}
|
|
200
197
|
async _findMany(options) {
|
|
201
|
-
return await
|
|
198
|
+
return await FindCommand.execute({
|
|
202
199
|
...options,
|
|
203
200
|
entity: this._entity,
|
|
204
201
|
});
|
|
@@ -211,7 +208,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
211
208
|
return rows && rows[0];
|
|
212
209
|
}
|
|
213
210
|
async _find(keyValue, options) {
|
|
214
|
-
const filter = [
|
|
211
|
+
const filter = [extractKeyValues(this._entity, keyValue, true)];
|
|
215
212
|
if (options && options.filter) {
|
|
216
213
|
if (Array.isArray(options.filter))
|
|
217
214
|
filter.push(...options.filter);
|
|
@@ -221,21 +218,21 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
221
218
|
return await this._findOne({ ...options, filter, offset: 0 });
|
|
222
219
|
}
|
|
223
220
|
async _delete(keyValue, options) {
|
|
224
|
-
const filter = [
|
|
221
|
+
const filter = [extractKeyValues(this._entity, keyValue, true)];
|
|
225
222
|
if (options && options.filter) {
|
|
226
223
|
if (Array.isArray(options.filter))
|
|
227
224
|
filter.push(...options.filter);
|
|
228
225
|
else
|
|
229
226
|
filter.push(options.filter);
|
|
230
227
|
}
|
|
231
|
-
return !!(await
|
|
228
|
+
return !!(await DeleteCommand.execute({
|
|
232
229
|
...options,
|
|
233
230
|
filter,
|
|
234
231
|
entity: this._entity,
|
|
235
232
|
}));
|
|
236
233
|
}
|
|
237
234
|
async _deleteMany(options) {
|
|
238
|
-
return
|
|
235
|
+
return DeleteCommand.execute({
|
|
239
236
|
...options,
|
|
240
237
|
entity: this._entity,
|
|
241
238
|
filter: options?.filter,
|
|
@@ -245,7 +242,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
245
242
|
async _update(keyValue, values, options) {
|
|
246
243
|
if (!values)
|
|
247
244
|
throw new TypeError('You must provide values');
|
|
248
|
-
const keyValues =
|
|
245
|
+
const keyValues = extractKeyValues(this._entity, keyValue, true);
|
|
249
246
|
const filter = [keyValues];
|
|
250
247
|
if (options.filter) {
|
|
251
248
|
if (Array.isArray(options.filter))
|
|
@@ -255,7 +252,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
255
252
|
}
|
|
256
253
|
const updateValues = { ...values };
|
|
257
254
|
Object.keys(keyValues).forEach(k => delete updateValues[k]);
|
|
258
|
-
const rowsAffected = await
|
|
255
|
+
const rowsAffected = await UpdateCommand.execute({
|
|
259
256
|
...options,
|
|
260
257
|
entity: this._entity,
|
|
261
258
|
values: updateValues,
|
|
@@ -266,11 +263,10 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
266
263
|
async _updateMany(values, options) {
|
|
267
264
|
if (!values)
|
|
268
265
|
throw new TypeError('You must provide values');
|
|
269
|
-
return await
|
|
266
|
+
return await UpdateCommand.execute({
|
|
270
267
|
...options,
|
|
271
268
|
entity: this._entity,
|
|
272
269
|
values,
|
|
273
270
|
});
|
|
274
271
|
}
|
|
275
272
|
}
|
|
276
|
-
exports.Repository = Repository;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.applyMixins = applyMixins;
|
|
4
|
-
function applyMixins(derivedCtor, baseCtor, filter) {
|
|
1
|
+
export function applyMixins(derivedCtor, baseCtor, filter) {
|
|
5
2
|
for (const name of Object.getOwnPropertyNames(baseCtor.prototype)) {
|
|
6
3
|
if (name === 'constructor' ||
|
|
7
4
|
name === '__proto__' ||
|
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const orm_helper_js_1 = require("./orm.helper.js");
|
|
6
|
-
function extractKeyValues(entityDef, valueOrInstance, keepOther) {
|
|
7
|
-
const primaryIndex = entity_metadata_js_1.EntityMetadata.getPrimaryIndex(entityDef);
|
|
1
|
+
import { EntityMetadata } from '../model/entity-metadata.js';
|
|
2
|
+
import { isColumnField } from './orm.helper.js';
|
|
3
|
+
export function extractKeyValues(entityDef, valueOrInstance, keepOther) {
|
|
4
|
+
const primaryIndex = EntityMetadata.getPrimaryIndex(entityDef);
|
|
8
5
|
if (!primaryIndex)
|
|
9
6
|
throw new Error(`No primary fields defined for "${entityDef.name}" entity`);
|
|
10
7
|
const validateCol = k => {
|
|
11
|
-
const col =
|
|
8
|
+
const col = EntityMetadata.getField(entityDef, k);
|
|
12
9
|
if (!col)
|
|
13
10
|
throw new Error(`Unknown column (${k}) defined as primary key in entity "${entityDef.name}"`);
|
|
14
|
-
if (!
|
|
11
|
+
if (!isColumnField(col)) {
|
|
15
12
|
throw new Error(`Column (${k}) defined as primary key in entity "${entityDef.name}" is not a data column`);
|
|
16
13
|
}
|
|
17
14
|
};
|
|
@@ -1,26 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.isAssociationField = exports.isEmbeddedField = void 0;
|
|
4
|
-
exports.isClass = isClass;
|
|
5
|
-
exports.isEntityClass = isEntityClass;
|
|
6
|
-
exports.isColumnField = isColumnField;
|
|
7
|
-
exports.resolveEntity = resolveEntity;
|
|
8
|
-
exports.resolveEntityMeta = resolveEntityMeta;
|
|
9
|
-
const orm_const_js_1 = require("../orm.const.js");
|
|
10
|
-
function isClass(fn) {
|
|
1
|
+
import { ENTITY_METADATA_KEY } from '../orm.const.js';
|
|
2
|
+
export function isClass(fn) {
|
|
11
3
|
return typeof fn === 'function' && /^\s*class/.test(fn.toString());
|
|
12
4
|
}
|
|
13
|
-
function isEntityClass(fn) {
|
|
14
|
-
return isClass(fn) && Reflect.hasMetadata(
|
|
5
|
+
export function isEntityClass(fn) {
|
|
6
|
+
return isClass(fn) && Reflect.hasMetadata(ENTITY_METADATA_KEY, fn);
|
|
15
7
|
}
|
|
16
|
-
function isColumnField(f) {
|
|
8
|
+
export function isColumnField(f) {
|
|
17
9
|
return !!(f && f.name && f.kind === 'column');
|
|
18
10
|
}
|
|
19
|
-
const isEmbeddedField = (f) => !!(f && f.name && f.kind === 'object');
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
exports.isAssociationField = isAssociationField;
|
|
23
|
-
async function resolveEntity(ctorThunk) {
|
|
11
|
+
export const isEmbeddedField = (f) => !!(f && f.name && f.kind === 'object');
|
|
12
|
+
export const isAssociationField = (f) => !!(f && f.name && f.kind === 'association');
|
|
13
|
+
export async function resolveEntity(ctorThunk) {
|
|
24
14
|
if (typeof ctorThunk !== 'function')
|
|
25
15
|
return;
|
|
26
16
|
if (!isClass(ctorThunk))
|
|
@@ -28,7 +18,7 @@ async function resolveEntity(ctorThunk) {
|
|
|
28
18
|
if (isEntityClass(ctorThunk))
|
|
29
19
|
return ctorThunk;
|
|
30
20
|
}
|
|
31
|
-
async function resolveEntityMeta(ctorThunk) {
|
|
21
|
+
export async function resolveEntityMeta(ctorThunk) {
|
|
32
22
|
const ctor = await resolveEntity(ctorThunk);
|
|
33
|
-
return ctor && Reflect.getMetadata(
|
|
23
|
+
return ctor && Reflect.getMetadata(ENTITY_METADATA_KEY, ctor);
|
|
34
24
|
}
|
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FieldsProjection = void 0;
|
|
4
|
-
exports.parseFieldsProjection = parseFieldsProjection;
|
|
5
|
-
const fast_tokenizer_1 = require("fast-tokenizer");
|
|
1
|
+
import { splitString } from 'fast-tokenizer';
|
|
6
2
|
const FIELD_PATTERN = /^([+-])?([a-z_$]\w*)$/i;
|
|
7
3
|
const NO_DOT_BRACKET_PATTERN = /[^.]\(/g;
|
|
8
|
-
class FieldsProjection {
|
|
4
|
+
export class FieldsProjection {
|
|
9
5
|
}
|
|
10
|
-
exports.FieldsProjection = FieldsProjection;
|
|
11
6
|
(function (FieldsProjection) {
|
|
12
7
|
class Item {
|
|
13
8
|
}
|
|
14
9
|
FieldsProjection.Item = Item;
|
|
15
|
-
})(FieldsProjection || (
|
|
16
|
-
function parseFieldsProjection(projection, keepCase) {
|
|
10
|
+
})(FieldsProjection || (FieldsProjection = {}));
|
|
11
|
+
export function parseFieldsProjection(projection, keepCase) {
|
|
17
12
|
const arr = Array.isArray(projection) ? projection : [projection];
|
|
18
13
|
if (!(arr && arr.length))
|
|
19
14
|
return;
|
|
@@ -28,7 +23,7 @@ function parseFieldsProjection(projection, keepCase) {
|
|
|
28
23
|
function parse(input, target) {
|
|
29
24
|
/** Add dot before brackets which is required to split fields */
|
|
30
25
|
input = input.replace(NO_DOT_BRACKET_PATTERN, s => s.charAt(0) + '.' + s.substring(1));
|
|
31
|
-
const fields =
|
|
26
|
+
const fields = splitString(input, {
|
|
32
27
|
delimiters: '.',
|
|
33
28
|
brackets: true,
|
|
34
29
|
keepBrackets: false,
|
|
@@ -36,7 +31,7 @@ function parse(input, target) {
|
|
|
36
31
|
for (let i = 0; i < fields.length; i++) {
|
|
37
32
|
const f = fields[i];
|
|
38
33
|
if (f.includes(',')) {
|
|
39
|
-
const subFields =
|
|
34
|
+
const subFields = splitString(f, {
|
|
40
35
|
delimiters: ',',
|
|
41
36
|
brackets: true,
|
|
42
37
|
keepBrackets: true,
|
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const orm_helper_js_1 = require("./orm.helper.js");
|
|
6
|
-
function serializeColumn(col, v) {
|
|
7
|
-
if ((0, orm_helper_js_1.isColumnField)(col)) {
|
|
1
|
+
import { DataType } from '@sqb/builder';
|
|
2
|
+
import { isColumnField } from './orm.helper.js';
|
|
3
|
+
export function serializeColumn(col, v) {
|
|
4
|
+
if (isColumnField(col)) {
|
|
8
5
|
if (col.isArray) {
|
|
9
6
|
if (Array.isArray(v))
|
|
10
|
-
return v.map(x => serializeDataValue(col.dataType ||
|
|
11
|
-
return [serializeDataValue(col.dataType ||
|
|
7
|
+
return v.map(x => serializeDataValue(col.dataType || DataType.VARCHAR, x));
|
|
8
|
+
return [serializeDataValue(col.dataType || DataType.VARCHAR, v)];
|
|
12
9
|
}
|
|
13
|
-
return serializeDataValue(col.dataType ||
|
|
10
|
+
return serializeDataValue(col.dataType || DataType.VARCHAR, v);
|
|
14
11
|
}
|
|
15
12
|
if (typeof v === 'object' && typeof v.toJSON === 'function')
|
|
16
13
|
return v.toJSON();
|
|
@@ -20,19 +17,19 @@ const padZero = (n) => (n < 9 ? '0' : '') + n;
|
|
|
20
17
|
function serializeDataValue(dataType, v) {
|
|
21
18
|
if (v == null)
|
|
22
19
|
return;
|
|
23
|
-
if (v instanceof Date && (dataType ===
|
|
20
|
+
if (v instanceof Date && (dataType === DataType.DATE || dataType === DataType.TIMESTAMP)) {
|
|
24
21
|
return (v.getFullYear() +
|
|
25
22
|
'-' +
|
|
26
23
|
padZero(v.getMonth() + 1) +
|
|
27
24
|
'-' +
|
|
28
25
|
padZero(v.getDate()) +
|
|
29
|
-
(dataType ===
|
|
26
|
+
(dataType === DataType.TIMESTAMP
|
|
30
27
|
? 'T' + padZero(v.getHours()) + ':' + padZero(v.getMinutes()) + ':' + padZero(v.getSeconds())
|
|
31
28
|
: ''));
|
|
32
29
|
}
|
|
33
30
|
if (v instanceof Buffer)
|
|
34
31
|
return v.toString('base64');
|
|
35
|
-
if (typeof v === 'number' && (dataType ===
|
|
32
|
+
if (typeof v === 'number' && (dataType === DataType.SMALLINT || dataType === DataType.INTEGER))
|
|
36
33
|
return Math.trunc(v);
|
|
37
34
|
if (typeof v === 'bigint')
|
|
38
35
|
return v.toString();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqb/connect",
|
|
3
3
|
"description": "Multi-dialect database connection framework written with TypeScript",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.16.0",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"scripts": {
|
|
@@ -27,21 +27,21 @@
|
|
|
27
27
|
"fast-tokenizer": "^1.4.0",
|
|
28
28
|
"lightning-pool": "^4.5.0",
|
|
29
29
|
"lodash": "^4.17.21",
|
|
30
|
-
"power-tasks": "^1.
|
|
30
|
+
"power-tasks": "^1.9.0",
|
|
31
31
|
"putil-isplainobject": "^1.1.5",
|
|
32
32
|
"putil-merge": "^3.13.0",
|
|
33
33
|
"putil-promisify": "^1.10.1",
|
|
34
34
|
"putil-varhelpers": "^1.6.5",
|
|
35
|
-
"strict-typed-events": "^2.
|
|
35
|
+
"strict-typed-events": "^2.6.0",
|
|
36
36
|
"ts-gems": "^3.5.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/debug": "^4.1.12",
|
|
40
40
|
"@types/lodash": "^4.17.7",
|
|
41
|
-
"postgrejs": "^2.
|
|
41
|
+
"postgrejs": "^2.17.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@sqb/builder": "^4.
|
|
44
|
+
"@sqb/builder": "^4.16.0",
|
|
45
45
|
"reflect-metadata": "^0.2.2"
|
|
46
46
|
},
|
|
47
47
|
"main": "./cjs/index.js",
|