@sqb/connect 4.2.1 → 4.5.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/README.md +67 -0
- package/{dist/client/Adapter.js → cjs/client/adapter.js} +0 -0
- package/{dist/client/CursorStream.js → cjs/client/cursor-stream.js} +5 -2
- package/{dist/client/Cursor.js → cjs/client/cursor.js} +18 -11
- package/{dist → cjs}/client/extensions.js +0 -0
- package/{dist/client/FieldInfoMap.js → cjs/client/field-info-map.js} +2 -0
- package/{dist → cjs}/client/helpers.js +2 -2
- package/{dist/client/SqbClient.js → cjs/client/sqb-client.js} +16 -10
- package/{dist/client/SqbConnection.js → cjs/client/sqb-connection.js} +17 -14
- package/{dist → cjs}/client/types.js +0 -0
- package/cjs/index.js +42 -0
- package/{dist → cjs}/orm/backward.js +8 -8
- package/{dist → cjs}/orm/base-entity.js +8 -7
- package/{dist → cjs}/orm/commands/command.helper.js +9 -9
- package/{dist → cjs}/orm/commands/count.command.js +2 -2
- package/{dist → cjs}/orm/commands/create.command.js +12 -12
- package/{dist → cjs}/orm/commands/destroy.command.js +2 -2
- package/{dist → cjs}/orm/commands/find.command.js +100 -88
- package/{dist → cjs}/orm/commands/row-converter.js +10 -7
- package/{dist → cjs}/orm/commands/update.command.js +10 -10
- package/{dist → cjs}/orm/decorators/column.decorator.js +3 -3
- package/{dist → cjs}/orm/decorators/embedded.decorator.js +3 -3
- package/cjs/orm/decorators/entity.decorator.js +179 -0
- package/{dist → cjs}/orm/decorators/events.decorator.js +13 -13
- package/{dist → cjs}/orm/decorators/foreignkey.decorator.js +3 -3
- package/{dist → cjs}/orm/decorators/index.decorator.js +5 -5
- package/cjs/orm/decorators/link.decorator.js +70 -0
- package/{dist → cjs}/orm/decorators/primarykey.decorator.js +7 -7
- package/{dist → cjs}/orm/decorators/transform.decorator.js +5 -5
- package/cjs/orm/model/association-field-metadata.js +24 -0
- package/{dist → cjs}/orm/model/association-node.js +5 -2
- package/{dist → cjs}/orm/model/association.js +31 -39
- package/{dist/orm/model/column-element-metadata.js → cjs/orm/model/column-field-metadata.js} +8 -8
- package/{dist/orm/model/embedded-element-metadata.js → cjs/orm/model/embedded-field-metadata.js} +8 -8
- package/{dist → cjs}/orm/model/entity-metadata.js +90 -91
- package/{dist/orm/model/element-metadata.js → cjs/orm/model/field-metadata.js} +0 -0
- package/{dist → cjs}/orm/model/index-metadata.js +0 -0
- package/{dist → cjs}/orm/model/link-chain.js +12 -15
- package/{dist → cjs}/orm/orm.const.js +0 -0
- package/{dist → cjs}/orm/orm.type.js +0 -0
- package/{dist → cjs}/orm/repository.class.js +25 -22
- package/{dist → cjs}/orm/util/apply-mixins.js +0 -0
- package/{dist → cjs}/orm/util/extract-keyvalues.js +5 -5
- package/{dist → cjs}/orm/util/orm.helper.js +10 -10
- package/{dist/orm/util/serialize-element.js → cjs/orm/util/serialize-field.js} +2 -2
- package/cjs/package.json +3 -0
- package/{dist → cjs}/types.js +0 -0
- package/{dist/client/Adapter.d.ts → esm/client/adapter.d.ts} +1 -1
- package/esm/client/adapter.js +1 -0
- package/{dist/client/CursorStream.d.ts → esm/client/cursor-stream.d.ts} +1 -1
- package/esm/client/cursor-stream.js +96 -0
- package/{dist/client/Cursor.d.ts → esm/client/cursor.d.ts} +5 -5
- package/esm/client/cursor.js +252 -0
- package/{dist → esm}/client/extensions.d.ts +1 -1
- package/esm/client/extensions.js +9 -0
- package/{dist/client/FieldInfoMap.d.ts → esm/client/field-info-map.d.ts} +1 -1
- package/esm/client/field-info-map.js +50 -0
- package/{dist → esm}/client/helpers.d.ts +3 -3
- package/esm/client/helpers.js +125 -0
- package/{dist/client/SqbClient.d.ts → esm/client/sqb-client.d.ts} +4 -10
- package/esm/client/sqb-client.js +166 -0
- package/{dist/client/SqbConnection.d.ts → esm/client/sqb-connection.d.ts} +4 -4
- package/esm/client/sqb-connection.js +270 -0
- package/{dist → esm}/client/types.d.ts +5 -13
- package/esm/client/types.js +1 -0
- package/esm/index.d.ts +33 -0
- package/esm/index.js +32 -0
- package/{dist → esm}/orm/backward.d.ts +1 -1
- package/esm/orm/backward.js +17 -0
- package/{dist → esm}/orm/base-entity.d.ts +1 -1
- package/esm/orm/base-entity.js +25 -0
- package/{dist → esm}/orm/commands/command.helper.d.ts +2 -2
- package/esm/orm/commands/command.helper.js +135 -0
- package/{dist → esm}/orm/commands/count.command.d.ts +3 -3
- package/esm/orm/commands/count.command.js +26 -0
- package/{dist → esm}/orm/commands/create.command.d.ts +2 -2
- package/esm/orm/commands/create.command.js +85 -0
- package/{dist → esm}/orm/commands/destroy.command.d.ts +3 -3
- package/esm/orm/commands/destroy.command.js +26 -0
- package/{dist → esm}/orm/commands/find.command.d.ts +9 -9
- package/esm/orm/commands/find.command.js +318 -0
- package/{dist → esm}/orm/commands/row-converter.d.ts +5 -5
- package/esm/orm/commands/row-converter.js +186 -0
- package/{dist → esm}/orm/commands/update.command.d.ts +3 -3
- package/esm/orm/commands/update.command.js +80 -0
- package/esm/orm/decorators/column.decorator.d.ts +4 -0
- package/esm/orm/decorators/column.decorator.js +19 -0
- package/esm/orm/decorators/embedded.decorator.d.ts +3 -0
- package/esm/orm/decorators/embedded.decorator.js +12 -0
- package/{dist → esm}/orm/decorators/entity.decorator.d.ts +17 -17
- package/esm/orm/decorators/entity.decorator.js +175 -0
- package/{dist → esm}/orm/decorators/events.decorator.d.ts +0 -0
- package/esm/orm/decorators/events.decorator.js +55 -0
- package/{dist → esm}/orm/decorators/foreignkey.decorator.d.ts +1 -1
- package/esm/orm/decorators/foreignkey.decorator.js +9 -0
- package/{dist → esm}/orm/decorators/index.decorator.d.ts +1 -1
- package/esm/orm/decorators/index.decorator.js +17 -0
- package/esm/orm/decorators/link.decorator.d.ts +13 -0
- package/esm/orm/decorators/link.decorator.js +66 -0
- package/{dist → esm}/orm/decorators/primarykey.decorator.d.ts +1 -1
- package/esm/orm/decorators/primarykey.decorator.js +20 -0
- package/{dist → esm}/orm/decorators/transform.decorator.d.ts +1 -1
- package/esm/orm/decorators/transform.decorator.js +19 -0
- package/esm/orm/model/association-field-metadata.d.ts +12 -0
- package/esm/orm/model/association-field-metadata.js +20 -0
- package/{dist → esm}/orm/model/association-node.d.ts +1 -1
- package/esm/orm/model/association-node.js +24 -0
- package/{dist → esm}/orm/model/association.d.ts +6 -7
- package/esm/orm/model/association.js +118 -0
- package/{dist/orm/model/column-element-metadata.d.ts → esm/orm/model/column-field-metadata.d.ts} +10 -14
- package/esm/orm/model/column-field-metadata.js +28 -0
- package/esm/orm/model/embedded-field-metadata.d.ts +14 -0
- package/esm/orm/model/embedded-field-metadata.js +25 -0
- package/esm/orm/model/entity-metadata.d.ts +50 -0
- package/esm/orm/model/entity-metadata.js +337 -0
- package/esm/orm/model/field-metadata.d.ts +16 -0
- package/esm/orm/model/field-metadata.js +1 -0
- package/{dist → esm}/orm/model/index-metadata.d.ts +0 -0
- package/esm/orm/model/index-metadata.js +1 -0
- package/esm/orm/model/link-chain.d.ts +13 -0
- package/esm/orm/model/link-chain.js +43 -0
- package/{dist → esm}/orm/orm.const.d.ts +0 -0
- package/esm/orm/orm.const.js +2 -0
- package/{dist → esm}/orm/orm.type.d.ts +3 -4
- package/esm/orm/orm.type.js +1 -0
- package/{dist → esm}/orm/repository.class.d.ts +23 -23
- package/esm/orm/repository.class.js +238 -0
- package/{dist → esm}/orm/util/apply-mixins.d.ts +0 -0
- package/esm/orm/util/apply-mixins.js +9 -0
- package/{dist → esm}/orm/util/extract-keyvalues.d.ts +1 -1
- package/esm/orm/util/extract-keyvalues.js +57 -0
- package/esm/orm/util/orm.helper.d.ts +13 -0
- package/esm/orm/util/orm.helper.js +28 -0
- package/esm/orm/util/serialize-field.d.ts +2 -0
- package/esm/orm/util/serialize-field.js +39 -0
- package/esm/types.d.ts +7 -0
- package/esm/types.js +1 -0
- package/package.json +54 -41
- package/dist/index.d.ts +0 -33
- package/dist/index.js +0 -42
- package/dist/orm/decorators/column.decorator.d.ts +0 -4
- package/dist/orm/decorators/embedded.decorator.d.ts +0 -3
- package/dist/orm/decorators/entity.decorator.js +0 -179
- package/dist/orm/decorators/link.decorator.d.ts +0 -23
- package/dist/orm/decorators/link.decorator.js +0 -88
- package/dist/orm/model/association-element-metadata.d.ts +0 -10
- package/dist/orm/model/association-element-metadata.js +0 -15
- package/dist/orm/model/element-metadata.d.ts +0 -7
- package/dist/orm/model/embedded-element-metadata.d.ts +0 -14
- package/dist/orm/model/entity-metadata.d.ts +0 -50
- package/dist/orm/model/link-chain.d.ts +0 -15
- package/dist/orm/util/orm.helper.d.ts +0 -13
- package/dist/orm/util/serialize-element.d.ts +0 -2
- package/dist/types.d.ts +0 -2
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EntityMetadata = void 0;
|
|
4
4
|
const builder_1 = require("@sqb/builder");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
5
|
+
const orm_const_js_1 = require("../orm.const.js");
|
|
6
|
+
const orm_helper_js_1 = require("../util/orm.helper.js");
|
|
7
|
+
const serialize_field_js_1 = require("../util/serialize-field.js");
|
|
8
|
+
const association_js_1 = require("./association.js");
|
|
9
|
+
const association_field_metadata_js_1 = require("./association-field-metadata.js");
|
|
10
|
+
const column_field_metadata_js_1 = require("./column-field-metadata.js");
|
|
11
|
+
const embedded_field_metadata_js_1 = require("./embedded-field-metadata.js");
|
|
12
12
|
var EntityMetadata;
|
|
13
13
|
(function (EntityMetadata) {
|
|
14
14
|
function define(ctor) {
|
|
@@ -19,29 +19,29 @@ var EntityMetadata;
|
|
|
19
19
|
const meta = {
|
|
20
20
|
ctor: ctor,
|
|
21
21
|
name: ctor.name,
|
|
22
|
-
|
|
22
|
+
fields: {},
|
|
23
23
|
indexes: [],
|
|
24
24
|
foreignKeys: [],
|
|
25
25
|
eventListeners: {}
|
|
26
26
|
};
|
|
27
|
-
Reflect.defineMetadata(
|
|
27
|
+
Reflect.defineMetadata(orm_const_js_1.ENTITY_METADATA_KEY, meta, ctor);
|
|
28
28
|
// Merge base entity columns into this one
|
|
29
29
|
if (baseMeta) {
|
|
30
30
|
EntityMetadata.mixin(meta, baseMeta);
|
|
31
31
|
}
|
|
32
32
|
ctor.prototype.toJSON = function () {
|
|
33
33
|
const obj = {};
|
|
34
|
-
const
|
|
35
|
-
const l =
|
|
34
|
+
const fieldKeys = Object.keys(meta.fields);
|
|
35
|
+
const l = fieldKeys.length;
|
|
36
36
|
let key;
|
|
37
37
|
let v;
|
|
38
38
|
for (let i = 0; i < l; i++) {
|
|
39
|
-
key =
|
|
40
|
-
const col = EntityMetadata.
|
|
39
|
+
key = fieldKeys[i];
|
|
40
|
+
const col = EntityMetadata.getField(meta, key);
|
|
41
41
|
if (col) {
|
|
42
42
|
v = this[col.name];
|
|
43
43
|
if (v !== undefined)
|
|
44
|
-
obj[col.name] = (0,
|
|
44
|
+
obj[col.name] = (0, serialize_field_js_1.serializeColumn)(col, v);
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
return obj;
|
|
@@ -50,94 +50,94 @@ var EntityMetadata;
|
|
|
50
50
|
}
|
|
51
51
|
EntityMetadata.define = define;
|
|
52
52
|
function get(ctor) {
|
|
53
|
-
return Reflect.getMetadata(
|
|
53
|
+
return Reflect.getMetadata(orm_const_js_1.ENTITY_METADATA_KEY, ctor);
|
|
54
54
|
}
|
|
55
55
|
EntityMetadata.get = get;
|
|
56
56
|
function getOwn(ctor) {
|
|
57
|
-
return Reflect.getOwnMetadata(
|
|
57
|
+
return Reflect.getOwnMetadata(orm_const_js_1.ENTITY_METADATA_KEY, ctor);
|
|
58
58
|
}
|
|
59
59
|
EntityMetadata.getOwn = getOwn;
|
|
60
|
-
function
|
|
61
|
-
return
|
|
60
|
+
function getField(entity, fieldName) {
|
|
61
|
+
return fieldName ? entity.fields[fieldName.toLowerCase()] : undefined;
|
|
62
62
|
}
|
|
63
|
-
EntityMetadata.
|
|
64
|
-
function
|
|
65
|
-
const el =
|
|
66
|
-
if (el && !(0,
|
|
63
|
+
EntityMetadata.getField = getField;
|
|
64
|
+
function getColumnField(entity, fieldName) {
|
|
65
|
+
const el = getField(entity, fieldName);
|
|
66
|
+
if (el && !(0, orm_helper_js_1.isColumnField)(el))
|
|
67
67
|
throw new Error(`"${el.name}" requested as "column" but it is "${el.kind}"`);
|
|
68
68
|
return el;
|
|
69
69
|
}
|
|
70
|
-
EntityMetadata.
|
|
71
|
-
function
|
|
72
|
-
const el =
|
|
73
|
-
if (el && !(0,
|
|
70
|
+
EntityMetadata.getColumnField = getColumnField;
|
|
71
|
+
function getEmbeddedField(entity, fieldName) {
|
|
72
|
+
const el = getField(entity, fieldName);
|
|
73
|
+
if (el && !(0, orm_helper_js_1.isEmbeddedField)(el))
|
|
74
74
|
throw new Error(`"${el.name}" requested as "embedded" but it is "${el.kind}"`);
|
|
75
75
|
return el;
|
|
76
76
|
}
|
|
77
|
-
EntityMetadata.
|
|
78
|
-
function
|
|
79
|
-
const el =
|
|
80
|
-
if (el && !(0,
|
|
77
|
+
EntityMetadata.getEmbeddedField = getEmbeddedField;
|
|
78
|
+
function getAssociationField(entity, fieldName) {
|
|
79
|
+
const el = getField(entity, fieldName);
|
|
80
|
+
if (el && !(0, orm_helper_js_1.isAssociationField)(el))
|
|
81
81
|
throw new Error(`"${el.name}" requested as "association" but it is "${el.kind}"`);
|
|
82
82
|
return el;
|
|
83
83
|
}
|
|
84
|
-
EntityMetadata.
|
|
85
|
-
function
|
|
86
|
-
return Object.values(entity.
|
|
84
|
+
EntityMetadata.getAssociationField = getAssociationField;
|
|
85
|
+
function findField(entity, predicate) {
|
|
86
|
+
return Object.values(entity.fields).find(predicate);
|
|
87
87
|
}
|
|
88
|
-
EntityMetadata.
|
|
89
|
-
function
|
|
88
|
+
EntityMetadata.findField = findField;
|
|
89
|
+
function getColumnFieldByFieldName(entity, fieldName) {
|
|
90
90
|
if (!fieldName)
|
|
91
91
|
return;
|
|
92
92
|
fieldName = fieldName.toLowerCase();
|
|
93
|
-
for (const prop of Object.values(entity.
|
|
94
|
-
if ((0,
|
|
93
|
+
for (const prop of Object.values(entity.fields)) {
|
|
94
|
+
if ((0, orm_helper_js_1.isColumnField)(prop) && prop.fieldName.toLowerCase() === fieldName)
|
|
95
95
|
return prop;
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
EntityMetadata.
|
|
99
|
-
function
|
|
98
|
+
EntityMetadata.getColumnFieldByFieldName = getColumnFieldByFieldName;
|
|
99
|
+
function getFieldNames(entity, filter) {
|
|
100
100
|
if (filter) {
|
|
101
101
|
const out = [];
|
|
102
|
-
for (const el of Object.values(entity.
|
|
102
|
+
for (const el of Object.values(entity.fields)) {
|
|
103
103
|
if (el && (!filter || filter(el)))
|
|
104
104
|
out.push(el.name);
|
|
105
105
|
}
|
|
106
106
|
return out;
|
|
107
107
|
}
|
|
108
108
|
// Create a cached name array
|
|
109
|
-
if (!Object.prototype.hasOwnProperty.call(entity, '
|
|
110
|
-
Object.defineProperty(entity, '
|
|
109
|
+
if (!Object.prototype.hasOwnProperty.call(entity, '_fieldNames'))
|
|
110
|
+
Object.defineProperty(entity, '_fieldNames', {
|
|
111
111
|
enumerable: false,
|
|
112
112
|
configurable: true,
|
|
113
113
|
writable: true,
|
|
114
|
-
value: Object.values(entity.
|
|
114
|
+
value: Object.values(entity.fields).map(m => m.name)
|
|
115
115
|
});
|
|
116
|
-
return entity.
|
|
116
|
+
return entity._fieldNames;
|
|
117
117
|
}
|
|
118
|
-
EntityMetadata.
|
|
119
|
-
function
|
|
120
|
-
return
|
|
118
|
+
EntityMetadata.getFieldNames = getFieldNames;
|
|
119
|
+
function getColumnFieldNames(entity) {
|
|
120
|
+
return getFieldNames(entity, orm_helper_js_1.isColumnField);
|
|
121
121
|
}
|
|
122
|
-
EntityMetadata.
|
|
123
|
-
function
|
|
124
|
-
return
|
|
122
|
+
EntityMetadata.getColumnFieldNames = getColumnFieldNames;
|
|
123
|
+
function getEmbeddedFieldNames(entity) {
|
|
124
|
+
return getFieldNames(entity, orm_helper_js_1.isEmbeddedField);
|
|
125
125
|
}
|
|
126
|
-
EntityMetadata.
|
|
127
|
-
function
|
|
128
|
-
return
|
|
126
|
+
EntityMetadata.getEmbeddedFieldNames = getEmbeddedFieldNames;
|
|
127
|
+
function getAssociationFieldNames(entity) {
|
|
128
|
+
return getFieldNames(entity, orm_helper_js_1.isAssociationField);
|
|
129
129
|
}
|
|
130
|
-
EntityMetadata.
|
|
131
|
-
function
|
|
132
|
-
return
|
|
130
|
+
EntityMetadata.getAssociationFieldNames = getAssociationFieldNames;
|
|
131
|
+
function getNonAssociationFieldNames(entity) {
|
|
132
|
+
return getFieldNames(entity, x => !(0, orm_helper_js_1.isAssociationField)(x));
|
|
133
133
|
}
|
|
134
|
-
EntityMetadata.
|
|
134
|
+
EntityMetadata.getNonAssociationFieldNames = getNonAssociationFieldNames;
|
|
135
135
|
function getInsertColumnNames(entity) {
|
|
136
|
-
return
|
|
136
|
+
return getFieldNames(entity, x => (0, orm_helper_js_1.isColumnField)(x) && !x.noInsert);
|
|
137
137
|
}
|
|
138
138
|
EntityMetadata.getInsertColumnNames = getInsertColumnNames;
|
|
139
139
|
function getUpdateColumnNames(entity) {
|
|
140
|
-
return
|
|
140
|
+
return getFieldNames(entity, x => (0, orm_helper_js_1.isColumnField)(x) && !x.noUpdate);
|
|
141
141
|
}
|
|
142
142
|
EntityMetadata.getUpdateColumnNames = getUpdateColumnNames;
|
|
143
143
|
function getPrimaryIndex(entity) {
|
|
@@ -149,7 +149,7 @@ var EntityMetadata;
|
|
|
149
149
|
const out = [];
|
|
150
150
|
if (idx) {
|
|
151
151
|
for (const k of idx.columns) {
|
|
152
|
-
const col =
|
|
152
|
+
const col = getColumnField(entity, k);
|
|
153
153
|
if (!col)
|
|
154
154
|
throw new Error(`Data column "${k}" in primary index of ${entity.name} does not exists`);
|
|
155
155
|
out.push(col);
|
|
@@ -180,7 +180,7 @@ var EntityMetadata;
|
|
|
180
180
|
EntityMetadata.addIndex = addIndex;
|
|
181
181
|
function addForeignKey(entity, propertyKey, target, targetKey) {
|
|
182
182
|
entity.foreignKeys = entity.foreignKeys || [];
|
|
183
|
-
const fk = new
|
|
183
|
+
const fk = new association_js_1.Association(entity.name + '.' + propertyKey, {
|
|
184
184
|
source: entity.ctor,
|
|
185
185
|
sourceKey: propertyKey,
|
|
186
186
|
target,
|
|
@@ -197,10 +197,10 @@ var EntityMetadata;
|
|
|
197
197
|
entity.eventListeners[event].push(fn);
|
|
198
198
|
}
|
|
199
199
|
EntityMetadata.addEventListener = addEventListener;
|
|
200
|
-
function
|
|
201
|
-
delete entity.
|
|
202
|
-
let prop = EntityMetadata.
|
|
203
|
-
if ((0,
|
|
200
|
+
function defineColumnField(entity, name, options = {}) {
|
|
201
|
+
delete entity._fieldNames;
|
|
202
|
+
let prop = EntityMetadata.getField(entity, name);
|
|
203
|
+
if ((0, orm_helper_js_1.isColumnField)(prop))
|
|
204
204
|
options = { ...prop, ...options };
|
|
205
205
|
if (!options.type) {
|
|
206
206
|
switch (options.dataType) {
|
|
@@ -252,34 +252,34 @@ var EntityMetadata;
|
|
|
252
252
|
options.dataType = builder_1.DataType.VARCHAR;
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
|
-
prop =
|
|
256
|
-
entity.
|
|
255
|
+
prop = column_field_metadata_js_1.ColumnFieldMetadata.create(entity, name, options);
|
|
256
|
+
entity.fields[name.toLowerCase()] = prop;
|
|
257
257
|
return prop;
|
|
258
258
|
}
|
|
259
|
-
EntityMetadata.
|
|
260
|
-
function
|
|
261
|
-
delete entity.
|
|
262
|
-
let prop = EntityMetadata.
|
|
263
|
-
if ((0,
|
|
259
|
+
EntityMetadata.defineColumnField = defineColumnField;
|
|
260
|
+
function defineEmbeddedField(entity, name, type, options) {
|
|
261
|
+
delete entity._fieldNames;
|
|
262
|
+
let prop = EntityMetadata.getField(entity, name);
|
|
263
|
+
if ((0, orm_helper_js_1.isEmbeddedField)(prop))
|
|
264
264
|
options = { ...prop, ...options };
|
|
265
|
-
prop =
|
|
266
|
-
entity.
|
|
265
|
+
prop = embedded_field_metadata_js_1.EmbeddedFieldMetadata.create(entity, name, type, options);
|
|
266
|
+
entity.fields[name.toLowerCase()] = prop;
|
|
267
267
|
return prop;
|
|
268
268
|
}
|
|
269
|
-
EntityMetadata.
|
|
270
|
-
function
|
|
271
|
-
delete entity.
|
|
272
|
-
const prop =
|
|
269
|
+
EntityMetadata.defineEmbeddedField = defineEmbeddedField;
|
|
270
|
+
function defineAssociationField(entity, propertyKey, association, options) {
|
|
271
|
+
delete entity._fieldNames;
|
|
272
|
+
const prop = association_field_metadata_js_1.AssociationFieldMetadata.create(entity, propertyKey, association, options);
|
|
273
273
|
let l = association;
|
|
274
274
|
let i = 1;
|
|
275
275
|
while (l) {
|
|
276
276
|
l.name = entity.name + '.' + propertyKey + '#' + (i++);
|
|
277
277
|
l = l.next;
|
|
278
278
|
}
|
|
279
|
-
entity.
|
|
279
|
+
entity.fields[propertyKey.toLowerCase()] = prop;
|
|
280
280
|
return prop;
|
|
281
281
|
}
|
|
282
|
-
EntityMetadata.
|
|
282
|
+
EntityMetadata.defineAssociationField = defineAssociationField;
|
|
283
283
|
function setPrimaryKeys(entity, column, options) {
|
|
284
284
|
addIndex(entity, {
|
|
285
285
|
...options,
|
|
@@ -290,9 +290,8 @@ var EntityMetadata;
|
|
|
290
290
|
}
|
|
291
291
|
EntityMetadata.setPrimaryKeys = setPrimaryKeys;
|
|
292
292
|
function mixin(derived, base, filter) {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
delete derived._elementNames;
|
|
293
|
+
const hasField = (k) => !filter || filter(k);
|
|
294
|
+
delete derived._fieldNames;
|
|
296
295
|
if (!derived.tableName) {
|
|
297
296
|
derived.tableName = base.tableName;
|
|
298
297
|
derived.schema = base.schema;
|
|
@@ -302,7 +301,7 @@ var EntityMetadata;
|
|
|
302
301
|
if (base.indexes && base.indexes.length) {
|
|
303
302
|
const hasPrimaryIndex = !!getPrimaryIndex(derived);
|
|
304
303
|
for (const idx of base.indexes) {
|
|
305
|
-
if (!idx.columns.find(x => !
|
|
304
|
+
if (!idx.columns.find(x => !hasField(x))) {
|
|
306
305
|
if (hasPrimaryIndex)
|
|
307
306
|
addIndex(derived, { ...idx, primary: undefined });
|
|
308
307
|
else
|
|
@@ -314,8 +313,8 @@ var EntityMetadata;
|
|
|
314
313
|
if (base.foreignKeys && base.foreignKeys.length) {
|
|
315
314
|
derived.foreignKeys = derived.foreignKeys || [];
|
|
316
315
|
for (const fk of base.foreignKeys) {
|
|
317
|
-
if (!fk.sourceKey ||
|
|
318
|
-
const newFk = new
|
|
316
|
+
if (!fk.sourceKey || hasField(fk.sourceKey)) {
|
|
317
|
+
const newFk = new association_js_1.Association(fk.name, { ...fk, source: derived.ctor });
|
|
319
318
|
derived.foreignKeys.push(newFk);
|
|
320
319
|
}
|
|
321
320
|
}
|
|
@@ -326,15 +325,15 @@ var EntityMetadata;
|
|
|
326
325
|
arr.forEach(fn => EntityMetadata.addEventListener(derived, event, fn));
|
|
327
326
|
}
|
|
328
327
|
}
|
|
329
|
-
// Copy
|
|
330
|
-
derived.
|
|
331
|
-
for (const [n, p] of Object.entries(base.
|
|
332
|
-
if (!
|
|
328
|
+
// Copy fields
|
|
329
|
+
derived.fields = derived.fields || {};
|
|
330
|
+
for (const [n, p] of Object.entries(base.fields)) {
|
|
331
|
+
if (!hasField(n))
|
|
333
332
|
continue;
|
|
334
333
|
const o = Object.assign({}, p);
|
|
335
334
|
o.entity = derived;
|
|
336
335
|
Object.setPrototypeOf(o, Object.getPrototypeOf(p));
|
|
337
|
-
derived.
|
|
336
|
+
derived.fields[n] = o;
|
|
338
337
|
}
|
|
339
338
|
}
|
|
340
339
|
EntityMetadata.mixin = mixin;
|
|
File without changes
|
|
File without changes
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LinkChain = void 0;
|
|
4
|
-
const
|
|
4
|
+
const association_node_js_1 = require("./association-node.js");
|
|
5
5
|
class LinkChain {
|
|
6
|
-
|
|
6
|
+
target;
|
|
7
|
+
first;
|
|
8
|
+
current;
|
|
9
|
+
constructor(target, targetKey, sourceKey, many) {
|
|
7
10
|
this.target = target;
|
|
8
|
-
this.first = this.current = new
|
|
9
|
-
|
|
11
|
+
this.first = this.current = new association_node_js_1.AssociationNode('', {
|
|
12
|
+
many,
|
|
10
13
|
target,
|
|
11
14
|
source: null,
|
|
12
15
|
sourceKey,
|
|
@@ -22,20 +25,14 @@ class LinkChain {
|
|
|
22
25
|
return this;
|
|
23
26
|
}
|
|
24
27
|
linkToOne(target, targetColumn, parentColumn) {
|
|
25
|
-
return this._newNode(target,
|
|
28
|
+
return this._newNode(target, targetColumn, parentColumn);
|
|
26
29
|
}
|
|
27
30
|
linkToMany(target, targetColumn, parentColumn) {
|
|
28
|
-
return this._newNode(target,
|
|
31
|
+
return this._newNode(target, targetColumn, parentColumn, true);
|
|
29
32
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
linkFromMany(target, targetColumn, parentColumn) {
|
|
34
|
-
return this._newNode(target, 'from-many', targetColumn, parentColumn);
|
|
35
|
-
}
|
|
36
|
-
_newNode(target, kind, targetKey, parentKey) {
|
|
37
|
-
const child = new association_node_1.AssociationNode(this.current.name, {
|
|
38
|
-
kind,
|
|
33
|
+
_newNode(target, targetKey, parentKey, many = false) {
|
|
34
|
+
const child = new association_node_js_1.AssociationNode(this.current.name, {
|
|
35
|
+
many,
|
|
39
36
|
source: this.current.target,
|
|
40
37
|
target,
|
|
41
38
|
sourceKey: parentKey,
|
|
File without changes
|
|
File without changes
|
|
@@ -2,14 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Repository = void 0;
|
|
4
4
|
const strict_typed_events_1 = require("strict-typed-events");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
5
|
+
const sqb_connection_js_1 = require("../client/sqb-connection.js");
|
|
6
|
+
const count_command_js_1 = require("./commands/count.command.js");
|
|
7
|
+
const create_command_js_1 = require("./commands/create.command.js");
|
|
8
|
+
const destroy_command_js_1 = require("./commands/destroy.command.js");
|
|
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");
|
|
12
12
|
class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(strict_typed_events_1.AsyncEventEmitter) {
|
|
13
|
+
_executor;
|
|
14
|
+
_entity;
|
|
15
|
+
_schema;
|
|
13
16
|
constructor(entityDef, executor, schema) {
|
|
14
17
|
super();
|
|
15
18
|
this._executor = executor;
|
|
@@ -87,7 +90,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
87
90
|
}
|
|
88
91
|
async _execute(fn, opts) {
|
|
89
92
|
let connection = opts?.connection;
|
|
90
|
-
if (!connection && this._executor instanceof
|
|
93
|
+
if (!connection && this._executor instanceof sqb_connection_js_1.SqbConnection)
|
|
91
94
|
connection = this._executor;
|
|
92
95
|
if (connection) {
|
|
93
96
|
if (this._schema)
|
|
@@ -103,7 +106,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
103
106
|
}
|
|
104
107
|
async _create(values, options) {
|
|
105
108
|
await this._emit('before-create', values, options);
|
|
106
|
-
const keyValues = await
|
|
109
|
+
const keyValues = await create_command_js_1.CreateCommand.execute({
|
|
107
110
|
...options,
|
|
108
111
|
entity: this._entity,
|
|
109
112
|
values,
|
|
@@ -117,7 +120,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
117
120
|
}
|
|
118
121
|
async _createOnly(values, options) {
|
|
119
122
|
await this._emit('before-create', values, options);
|
|
120
|
-
await
|
|
123
|
+
await create_command_js_1.CreateCommand.execute({
|
|
121
124
|
...options,
|
|
122
125
|
entity: this._entity,
|
|
123
126
|
values,
|
|
@@ -125,33 +128,33 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
125
128
|
});
|
|
126
129
|
}
|
|
127
130
|
async _exists(keyValue, options) {
|
|
128
|
-
const filter = [(0,
|
|
131
|
+
const filter = [(0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true)];
|
|
129
132
|
if (options && options.filter) {
|
|
130
133
|
if (Array.isArray(options.filter))
|
|
131
134
|
filter.push(...options.filter);
|
|
132
135
|
else
|
|
133
136
|
filter.push(options.filter);
|
|
134
137
|
}
|
|
135
|
-
return !!(await
|
|
138
|
+
return !!(await count_command_js_1.CountCommand.execute({
|
|
136
139
|
...options,
|
|
137
140
|
filter,
|
|
138
141
|
entity: this._entity
|
|
139
142
|
}));
|
|
140
143
|
}
|
|
141
144
|
async _count(options) {
|
|
142
|
-
return
|
|
145
|
+
return count_command_js_1.CountCommand.execute({
|
|
143
146
|
...options,
|
|
144
147
|
entity: this._entity
|
|
145
148
|
});
|
|
146
149
|
}
|
|
147
150
|
async _findAll(options) {
|
|
148
|
-
return await
|
|
151
|
+
return await find_command_js_1.FindCommand.execute({
|
|
149
152
|
...options,
|
|
150
153
|
entity: this._entity,
|
|
151
154
|
});
|
|
152
155
|
}
|
|
153
156
|
async _findOne(options) {
|
|
154
|
-
const rows = await
|
|
157
|
+
const rows = await find_command_js_1.FindCommand.execute({
|
|
155
158
|
...options,
|
|
156
159
|
entity: this._entity,
|
|
157
160
|
limit: 1
|
|
@@ -159,7 +162,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
159
162
|
return rows && rows[0];
|
|
160
163
|
}
|
|
161
164
|
async _findByPk(keyValue, options) {
|
|
162
|
-
const filter = [(0,
|
|
165
|
+
const filter = [(0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true)];
|
|
163
166
|
if (options && options.filter) {
|
|
164
167
|
if (Array.isArray(options.filter))
|
|
165
168
|
filter.push(...options.filter);
|
|
@@ -170,14 +173,14 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
170
173
|
}
|
|
171
174
|
async _destroy(keyValue, options) {
|
|
172
175
|
await this._emit('before-destroy', keyValue, options);
|
|
173
|
-
const filter = [(0,
|
|
176
|
+
const filter = [(0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true)];
|
|
174
177
|
if (options && options.filter) {
|
|
175
178
|
if (Array.isArray(options.filter))
|
|
176
179
|
filter.push(...options.filter);
|
|
177
180
|
else
|
|
178
181
|
filter.push(options.filter);
|
|
179
182
|
}
|
|
180
|
-
const result = !!(await
|
|
183
|
+
const result = !!(await destroy_command_js_1.DestroyCommand.execute({
|
|
181
184
|
...options,
|
|
182
185
|
filter,
|
|
183
186
|
entity: this._entity,
|
|
@@ -187,7 +190,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
187
190
|
}
|
|
188
191
|
async _destroyAll(options) {
|
|
189
192
|
await this._emit('before-destroy-all', options);
|
|
190
|
-
const rowsAffected =
|
|
193
|
+
const rowsAffected = destroy_command_js_1.DestroyCommand.execute({
|
|
191
194
|
...options,
|
|
192
195
|
entity: this._entity,
|
|
193
196
|
filter: options?.filter,
|
|
@@ -198,7 +201,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
198
201
|
}
|
|
199
202
|
async _update(keyValue, values, options) {
|
|
200
203
|
await this._emit('before-update', keyValue, values, options);
|
|
201
|
-
const keyValues = (0,
|
|
204
|
+
const keyValues = (0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true);
|
|
202
205
|
const filter = [keyValues];
|
|
203
206
|
if (options.filter) {
|
|
204
207
|
if (Array.isArray(options.filter))
|
|
@@ -208,7 +211,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
208
211
|
}
|
|
209
212
|
const updateValues = { ...values };
|
|
210
213
|
Object.keys(keyValues).forEach(k => delete updateValues[k]);
|
|
211
|
-
const rowsAffected = await
|
|
214
|
+
const rowsAffected = await update_command_js_1.UpdateCommand.execute({
|
|
212
215
|
...options,
|
|
213
216
|
entity: this._entity,
|
|
214
217
|
values: updateValues,
|
|
@@ -219,7 +222,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
219
222
|
}
|
|
220
223
|
async _updateAll(values, options) {
|
|
221
224
|
await this._emit('before-update-all', values, options);
|
|
222
|
-
const rowsAffected = await
|
|
225
|
+
const rowsAffected = await update_command_js_1.UpdateCommand.execute({
|
|
223
226
|
...options,
|
|
224
227
|
entity: this._entity,
|
|
225
228
|
values
|
|
File without changes
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.extractKeyValues = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const entity_metadata_js_1 = require("../model/entity-metadata.js");
|
|
5
|
+
const orm_helper_js_1 = require("./orm.helper.js");
|
|
6
6
|
function extractKeyValues(entityDef, valueOrInstance, keepOther) {
|
|
7
|
-
const primaryIndex =
|
|
7
|
+
const primaryIndex = entity_metadata_js_1.EntityMetadata.getPrimaryIndex(entityDef);
|
|
8
8
|
if (!primaryIndex)
|
|
9
9
|
throw new Error(`No primary fields defined for "${entityDef.name}" entity`);
|
|
10
10
|
const validateCol = (k) => {
|
|
11
|
-
const col =
|
|
11
|
+
const col = entity_metadata_js_1.EntityMetadata.getField(entityDef, k);
|
|
12
12
|
if (!col)
|
|
13
13
|
throw new Error(`Unknown column (${k}) defined as primary key in entity "${entityDef.name}"`);
|
|
14
|
-
if (!(0,
|
|
14
|
+
if (!(0, orm_helper_js_1.isColumnField)(col))
|
|
15
15
|
throw new Error(`Column (${k}) defined as primary key in entity "${entityDef.name}" is not a data column`);
|
|
16
16
|
};
|
|
17
17
|
// if entity's primary key has more than one key field
|
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolveEntityMeta = exports.resolveEntity = exports.
|
|
4
|
-
const
|
|
3
|
+
exports.resolveEntityMeta = exports.resolveEntity = exports.isAssociationField = exports.isEmbeddedField = exports.isColumnField = exports.isEntityClass = exports.isClass = void 0;
|
|
4
|
+
const orm_const_js_1 = require("../orm.const.js");
|
|
5
5
|
function isClass(fn) {
|
|
6
6
|
return typeof fn === 'function' && /^\s*class/.test(fn.toString());
|
|
7
7
|
}
|
|
8
8
|
exports.isClass = isClass;
|
|
9
9
|
function isEntityClass(fn) {
|
|
10
|
-
return isClass(fn) && Reflect.hasMetadata(
|
|
10
|
+
return isClass(fn) && Reflect.hasMetadata(orm_const_js_1.ENTITY_METADATA_KEY, fn);
|
|
11
11
|
}
|
|
12
12
|
exports.isEntityClass = isEntityClass;
|
|
13
|
-
function
|
|
13
|
+
function isColumnField(f) {
|
|
14
14
|
return !!(f && f.name && f.kind === 'column');
|
|
15
15
|
}
|
|
16
|
-
exports.
|
|
17
|
-
const
|
|
16
|
+
exports.isColumnField = isColumnField;
|
|
17
|
+
const isEmbeddedField = (f) => {
|
|
18
18
|
return !!(f && f.name && f.kind === 'object');
|
|
19
19
|
};
|
|
20
|
-
exports.
|
|
21
|
-
const
|
|
20
|
+
exports.isEmbeddedField = isEmbeddedField;
|
|
21
|
+
const isAssociationField = (f) => {
|
|
22
22
|
return !!(f && f.name && f.kind === 'association');
|
|
23
23
|
};
|
|
24
|
-
exports.
|
|
24
|
+
exports.isAssociationField = isAssociationField;
|
|
25
25
|
async function resolveEntity(ctorThunk) {
|
|
26
26
|
if (typeof ctorThunk !== 'function')
|
|
27
27
|
return;
|
|
@@ -33,6 +33,6 @@ async function resolveEntity(ctorThunk) {
|
|
|
33
33
|
exports.resolveEntity = resolveEntity;
|
|
34
34
|
async function resolveEntityMeta(ctorThunk) {
|
|
35
35
|
const ctor = await resolveEntity(ctorThunk);
|
|
36
|
-
return ctor && Reflect.getMetadata(
|
|
36
|
+
return ctor && Reflect.getMetadata(orm_const_js_1.ENTITY_METADATA_KEY, ctor);
|
|
37
37
|
}
|
|
38
38
|
exports.resolveEntityMeta = resolveEntityMeta;
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.serializeColumn = void 0;
|
|
4
4
|
const builder_1 = require("@sqb/builder");
|
|
5
|
-
const
|
|
5
|
+
const orm_helper_js_1 = require("./orm.helper.js");
|
|
6
6
|
function serializeColumn(col, v) {
|
|
7
|
-
if ((0,
|
|
7
|
+
if ((0, orm_helper_js_1.isColumnField)(col)) {
|
|
8
8
|
if (col.isArray) {
|
|
9
9
|
if (Array.isArray(v))
|
|
10
10
|
return v.map(x => serializeDataValue(col.dataType || builder_1.DataType.VARCHAR, x));
|
package/cjs/package.json
ADDED
package/{dist → cjs}/types.js
RENAMED
|
File without changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Maybe } from 'ts-gems';
|
|
2
2
|
import { classes } from '@sqb/builder';
|
|
3
|
-
import { ClientConfiguration, DataType, QueryRequest, RowType } from './types';
|
|
3
|
+
import { ClientConfiguration, DataType, QueryRequest, RowType } from './types.js';
|
|
4
4
|
export interface Adapter {
|
|
5
5
|
driver: string;
|
|
6
6
|
dialect: string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|