@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
|
@@ -0,0 +1,337 @@
|
|
|
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 { serializeColumn } from '../util/serialize-field.js';
|
|
5
|
+
import { Association } from './association.js';
|
|
6
|
+
import { AssociationFieldMetadata } from './association-field-metadata.js';
|
|
7
|
+
import { ColumnFieldMetadata } from './column-field-metadata.js';
|
|
8
|
+
import { EmbeddedFieldMetadata } from './embedded-field-metadata.js';
|
|
9
|
+
export var EntityMetadata;
|
|
10
|
+
(function (EntityMetadata) {
|
|
11
|
+
function define(ctor) {
|
|
12
|
+
const own = getOwn(ctor);
|
|
13
|
+
if (own)
|
|
14
|
+
return own;
|
|
15
|
+
const baseMeta = get(ctor);
|
|
16
|
+
const meta = {
|
|
17
|
+
ctor: ctor,
|
|
18
|
+
name: ctor.name,
|
|
19
|
+
fields: {},
|
|
20
|
+
indexes: [],
|
|
21
|
+
foreignKeys: [],
|
|
22
|
+
eventListeners: {}
|
|
23
|
+
};
|
|
24
|
+
Reflect.defineMetadata(ENTITY_METADATA_KEY, meta, ctor);
|
|
25
|
+
// Merge base entity columns into this one
|
|
26
|
+
if (baseMeta) {
|
|
27
|
+
EntityMetadata.mixin(meta, baseMeta);
|
|
28
|
+
}
|
|
29
|
+
ctor.prototype.toJSON = function () {
|
|
30
|
+
const obj = {};
|
|
31
|
+
const fieldKeys = Object.keys(meta.fields);
|
|
32
|
+
const l = fieldKeys.length;
|
|
33
|
+
let key;
|
|
34
|
+
let v;
|
|
35
|
+
for (let i = 0; i < l; i++) {
|
|
36
|
+
key = fieldKeys[i];
|
|
37
|
+
const col = EntityMetadata.getField(meta, key);
|
|
38
|
+
if (col) {
|
|
39
|
+
v = this[col.name];
|
|
40
|
+
if (v !== undefined)
|
|
41
|
+
obj[col.name] = serializeColumn(col, v);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return obj;
|
|
45
|
+
};
|
|
46
|
+
return meta;
|
|
47
|
+
}
|
|
48
|
+
EntityMetadata.define = define;
|
|
49
|
+
function get(ctor) {
|
|
50
|
+
return Reflect.getMetadata(ENTITY_METADATA_KEY, ctor);
|
|
51
|
+
}
|
|
52
|
+
EntityMetadata.get = get;
|
|
53
|
+
function getOwn(ctor) {
|
|
54
|
+
return Reflect.getOwnMetadata(ENTITY_METADATA_KEY, ctor);
|
|
55
|
+
}
|
|
56
|
+
EntityMetadata.getOwn = getOwn;
|
|
57
|
+
function getField(entity, fieldName) {
|
|
58
|
+
return fieldName ? entity.fields[fieldName.toLowerCase()] : undefined;
|
|
59
|
+
}
|
|
60
|
+
EntityMetadata.getField = getField;
|
|
61
|
+
function getColumnField(entity, fieldName) {
|
|
62
|
+
const el = getField(entity, fieldName);
|
|
63
|
+
if (el && !isColumnField(el))
|
|
64
|
+
throw new Error(`"${el.name}" requested as "column" but it is "${el.kind}"`);
|
|
65
|
+
return el;
|
|
66
|
+
}
|
|
67
|
+
EntityMetadata.getColumnField = getColumnField;
|
|
68
|
+
function getEmbeddedField(entity, fieldName) {
|
|
69
|
+
const el = getField(entity, fieldName);
|
|
70
|
+
if (el && !isEmbeddedField(el))
|
|
71
|
+
throw new Error(`"${el.name}" requested as "embedded" but it is "${el.kind}"`);
|
|
72
|
+
return el;
|
|
73
|
+
}
|
|
74
|
+
EntityMetadata.getEmbeddedField = getEmbeddedField;
|
|
75
|
+
function getAssociationField(entity, fieldName) {
|
|
76
|
+
const el = getField(entity, fieldName);
|
|
77
|
+
if (el && !isAssociationField(el))
|
|
78
|
+
throw new Error(`"${el.name}" requested as "association" but it is "${el.kind}"`);
|
|
79
|
+
return el;
|
|
80
|
+
}
|
|
81
|
+
EntityMetadata.getAssociationField = getAssociationField;
|
|
82
|
+
function findField(entity, predicate) {
|
|
83
|
+
return Object.values(entity.fields).find(predicate);
|
|
84
|
+
}
|
|
85
|
+
EntityMetadata.findField = findField;
|
|
86
|
+
function getColumnFieldByFieldName(entity, fieldName) {
|
|
87
|
+
if (!fieldName)
|
|
88
|
+
return;
|
|
89
|
+
fieldName = fieldName.toLowerCase();
|
|
90
|
+
for (const prop of Object.values(entity.fields)) {
|
|
91
|
+
if (isColumnField(prop) && prop.fieldName.toLowerCase() === fieldName)
|
|
92
|
+
return prop;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
EntityMetadata.getColumnFieldByFieldName = getColumnFieldByFieldName;
|
|
96
|
+
function getFieldNames(entity, filter) {
|
|
97
|
+
if (filter) {
|
|
98
|
+
const out = [];
|
|
99
|
+
for (const el of Object.values(entity.fields)) {
|
|
100
|
+
if (el && (!filter || filter(el)))
|
|
101
|
+
out.push(el.name);
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
// Create a cached name array
|
|
106
|
+
if (!Object.prototype.hasOwnProperty.call(entity, '_fieldNames'))
|
|
107
|
+
Object.defineProperty(entity, '_fieldNames', {
|
|
108
|
+
enumerable: false,
|
|
109
|
+
configurable: true,
|
|
110
|
+
writable: true,
|
|
111
|
+
value: Object.values(entity.fields).map(m => m.name)
|
|
112
|
+
});
|
|
113
|
+
return entity._fieldNames;
|
|
114
|
+
}
|
|
115
|
+
EntityMetadata.getFieldNames = getFieldNames;
|
|
116
|
+
function getColumnFieldNames(entity) {
|
|
117
|
+
return getFieldNames(entity, isColumnField);
|
|
118
|
+
}
|
|
119
|
+
EntityMetadata.getColumnFieldNames = getColumnFieldNames;
|
|
120
|
+
function getEmbeddedFieldNames(entity) {
|
|
121
|
+
return getFieldNames(entity, isEmbeddedField);
|
|
122
|
+
}
|
|
123
|
+
EntityMetadata.getEmbeddedFieldNames = getEmbeddedFieldNames;
|
|
124
|
+
function getAssociationFieldNames(entity) {
|
|
125
|
+
return getFieldNames(entity, isAssociationField);
|
|
126
|
+
}
|
|
127
|
+
EntityMetadata.getAssociationFieldNames = getAssociationFieldNames;
|
|
128
|
+
function getNonAssociationFieldNames(entity) {
|
|
129
|
+
return getFieldNames(entity, x => !isAssociationField(x));
|
|
130
|
+
}
|
|
131
|
+
EntityMetadata.getNonAssociationFieldNames = getNonAssociationFieldNames;
|
|
132
|
+
function getInsertColumnNames(entity) {
|
|
133
|
+
return getFieldNames(entity, x => isColumnField(x) && !x.noInsert);
|
|
134
|
+
}
|
|
135
|
+
EntityMetadata.getInsertColumnNames = getInsertColumnNames;
|
|
136
|
+
function getUpdateColumnNames(entity) {
|
|
137
|
+
return getFieldNames(entity, x => isColumnField(x) && !x.noUpdate);
|
|
138
|
+
}
|
|
139
|
+
EntityMetadata.getUpdateColumnNames = getUpdateColumnNames;
|
|
140
|
+
function getPrimaryIndex(entity) {
|
|
141
|
+
return entity.indexes && entity.indexes.find(idx => idx.primary);
|
|
142
|
+
}
|
|
143
|
+
EntityMetadata.getPrimaryIndex = getPrimaryIndex;
|
|
144
|
+
function getPrimaryIndexColumns(entity) {
|
|
145
|
+
const idx = getPrimaryIndex(entity);
|
|
146
|
+
const out = [];
|
|
147
|
+
if (idx) {
|
|
148
|
+
for (const k of idx.columns) {
|
|
149
|
+
const col = getColumnField(entity, k);
|
|
150
|
+
if (!col)
|
|
151
|
+
throw new Error(`Data column "${k}" in primary index of ${entity.name} does not exists`);
|
|
152
|
+
out.push(col);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
EntityMetadata.getPrimaryIndexColumns = getPrimaryIndexColumns;
|
|
158
|
+
async function getForeignKeyFor(src, trg) {
|
|
159
|
+
if (!src.foreignKeys)
|
|
160
|
+
return;
|
|
161
|
+
for (const f of src.foreignKeys) {
|
|
162
|
+
if (await f.resolveTarget() === trg)
|
|
163
|
+
return f;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
EntityMetadata.getForeignKeyFor = getForeignKeyFor;
|
|
167
|
+
function addIndex(entity, index) {
|
|
168
|
+
entity.indexes = entity.indexes || [];
|
|
169
|
+
index = {
|
|
170
|
+
...index,
|
|
171
|
+
columns: Array.isArray(index.columns) ? index.columns : [index.columns]
|
|
172
|
+
};
|
|
173
|
+
if (index.primary)
|
|
174
|
+
entity.indexes.forEach(idx => delete idx.primary);
|
|
175
|
+
entity.indexes.push(index);
|
|
176
|
+
}
|
|
177
|
+
EntityMetadata.addIndex = addIndex;
|
|
178
|
+
function addForeignKey(entity, propertyKey, target, targetKey) {
|
|
179
|
+
entity.foreignKeys = entity.foreignKeys || [];
|
|
180
|
+
const fk = new Association(entity.name + '.' + propertyKey, {
|
|
181
|
+
source: entity.ctor,
|
|
182
|
+
sourceKey: propertyKey,
|
|
183
|
+
target,
|
|
184
|
+
targetKey
|
|
185
|
+
});
|
|
186
|
+
entity.foreignKeys.push(fk);
|
|
187
|
+
}
|
|
188
|
+
EntityMetadata.addForeignKey = addForeignKey;
|
|
189
|
+
function addEventListener(entity, event, fn) {
|
|
190
|
+
if (typeof fn !== 'function')
|
|
191
|
+
throw new Error('Property must be a function');
|
|
192
|
+
entity.eventListeners = entity.eventListeners || {};
|
|
193
|
+
entity.eventListeners[event] = entity.eventListeners[event] || [];
|
|
194
|
+
entity.eventListeners[event].push(fn);
|
|
195
|
+
}
|
|
196
|
+
EntityMetadata.addEventListener = addEventListener;
|
|
197
|
+
function defineColumnField(entity, name, options = {}) {
|
|
198
|
+
delete entity._fieldNames;
|
|
199
|
+
let prop = EntityMetadata.getField(entity, name);
|
|
200
|
+
if (isColumnField(prop))
|
|
201
|
+
options = { ...prop, ...options };
|
|
202
|
+
if (!options.type) {
|
|
203
|
+
switch (options.dataType) {
|
|
204
|
+
case DataType.BOOL:
|
|
205
|
+
options.type = Boolean;
|
|
206
|
+
break;
|
|
207
|
+
case DataType.VARCHAR:
|
|
208
|
+
case DataType.CHAR:
|
|
209
|
+
case DataType.TEXT:
|
|
210
|
+
options.type = String;
|
|
211
|
+
break;
|
|
212
|
+
case DataType.NUMBER:
|
|
213
|
+
case DataType.DOUBLE:
|
|
214
|
+
case DataType.FLOAT:
|
|
215
|
+
case DataType.INTEGER:
|
|
216
|
+
case DataType.SMALLINT:
|
|
217
|
+
options.type = Number;
|
|
218
|
+
break;
|
|
219
|
+
case DataType.TIMESTAMP:
|
|
220
|
+
case DataType.TIMESTAMPTZ:
|
|
221
|
+
options.type = Date;
|
|
222
|
+
break;
|
|
223
|
+
case DataType.BINARY:
|
|
224
|
+
options.type = Buffer;
|
|
225
|
+
break;
|
|
226
|
+
default:
|
|
227
|
+
options.type = String;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (!options.dataType) {
|
|
231
|
+
switch (options.type) {
|
|
232
|
+
case Boolean:
|
|
233
|
+
options.dataType = DataType.BOOL;
|
|
234
|
+
break;
|
|
235
|
+
case Number:
|
|
236
|
+
options.dataType = DataType.NUMBER;
|
|
237
|
+
break;
|
|
238
|
+
case Date:
|
|
239
|
+
options.dataType = DataType.TIMESTAMP;
|
|
240
|
+
break;
|
|
241
|
+
case Array:
|
|
242
|
+
options.dataType = DataType.VARCHAR;
|
|
243
|
+
options.isArray = true;
|
|
244
|
+
break;
|
|
245
|
+
case Buffer:
|
|
246
|
+
options.dataType = DataType.BINARY;
|
|
247
|
+
break;
|
|
248
|
+
default:
|
|
249
|
+
options.dataType = DataType.VARCHAR;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
prop = ColumnFieldMetadata.create(entity, name, options);
|
|
253
|
+
entity.fields[name.toLowerCase()] = prop;
|
|
254
|
+
return prop;
|
|
255
|
+
}
|
|
256
|
+
EntityMetadata.defineColumnField = defineColumnField;
|
|
257
|
+
function defineEmbeddedField(entity, name, type, options) {
|
|
258
|
+
delete entity._fieldNames;
|
|
259
|
+
let prop = EntityMetadata.getField(entity, name);
|
|
260
|
+
if (isEmbeddedField(prop))
|
|
261
|
+
options = { ...prop, ...options };
|
|
262
|
+
prop = EmbeddedFieldMetadata.create(entity, name, type, options);
|
|
263
|
+
entity.fields[name.toLowerCase()] = prop;
|
|
264
|
+
return prop;
|
|
265
|
+
}
|
|
266
|
+
EntityMetadata.defineEmbeddedField = defineEmbeddedField;
|
|
267
|
+
function defineAssociationField(entity, propertyKey, association, options) {
|
|
268
|
+
delete entity._fieldNames;
|
|
269
|
+
const prop = AssociationFieldMetadata.create(entity, propertyKey, association, options);
|
|
270
|
+
let l = association;
|
|
271
|
+
let i = 1;
|
|
272
|
+
while (l) {
|
|
273
|
+
l.name = entity.name + '.' + propertyKey + '#' + (i++);
|
|
274
|
+
l = l.next;
|
|
275
|
+
}
|
|
276
|
+
entity.fields[propertyKey.toLowerCase()] = prop;
|
|
277
|
+
return prop;
|
|
278
|
+
}
|
|
279
|
+
EntityMetadata.defineAssociationField = defineAssociationField;
|
|
280
|
+
function setPrimaryKeys(entity, column, options) {
|
|
281
|
+
addIndex(entity, {
|
|
282
|
+
...options,
|
|
283
|
+
columns: Array.isArray(column) ? column : [column],
|
|
284
|
+
unique: true,
|
|
285
|
+
primary: true
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
EntityMetadata.setPrimaryKeys = setPrimaryKeys;
|
|
289
|
+
function mixin(derived, base, filter) {
|
|
290
|
+
const hasField = (k) => !filter || filter(k);
|
|
291
|
+
delete derived._fieldNames;
|
|
292
|
+
if (!derived.tableName) {
|
|
293
|
+
derived.tableName = base.tableName;
|
|
294
|
+
derived.schema = base.schema;
|
|
295
|
+
derived.comment = base.comment;
|
|
296
|
+
}
|
|
297
|
+
// Copy indexes
|
|
298
|
+
if (base.indexes && base.indexes.length) {
|
|
299
|
+
const hasPrimaryIndex = !!getPrimaryIndex(derived);
|
|
300
|
+
for (const idx of base.indexes) {
|
|
301
|
+
if (!idx.columns.find(x => !hasField(x))) {
|
|
302
|
+
if (hasPrimaryIndex)
|
|
303
|
+
addIndex(derived, { ...idx, primary: undefined });
|
|
304
|
+
else
|
|
305
|
+
addIndex(derived, idx);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
// Copy foreign indexes
|
|
310
|
+
if (base.foreignKeys && base.foreignKeys.length) {
|
|
311
|
+
derived.foreignKeys = derived.foreignKeys || [];
|
|
312
|
+
for (const fk of base.foreignKeys) {
|
|
313
|
+
if (!fk.sourceKey || hasField(fk.sourceKey)) {
|
|
314
|
+
const newFk = new Association(fk.name, { ...fk, source: derived.ctor });
|
|
315
|
+
derived.foreignKeys.push(newFk);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
// Copy event listeners
|
|
320
|
+
if (base.eventListeners) {
|
|
321
|
+
for (const [event, arr] of Object.entries(base.eventListeners)) {
|
|
322
|
+
arr.forEach(fn => EntityMetadata.addEventListener(derived, event, fn));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// Copy fields
|
|
326
|
+
derived.fields = derived.fields || {};
|
|
327
|
+
for (const [n, p] of Object.entries(base.fields)) {
|
|
328
|
+
if (!hasField(n))
|
|
329
|
+
continue;
|
|
330
|
+
const o = Object.assign({}, p);
|
|
331
|
+
o.entity = derived;
|
|
332
|
+
Object.setPrototypeOf(o, Object.getPrototypeOf(p));
|
|
333
|
+
derived.fields[n] = o;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
EntityMetadata.mixin = mixin;
|
|
337
|
+
})(EntityMetadata || (EntityMetadata = {}));
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FieldKind } from '../orm.type.js';
|
|
2
|
+
import type { EntityMetadata } from './entity-metadata.js';
|
|
3
|
+
export interface FieldMetadata {
|
|
4
|
+
readonly entity: EntityMetadata;
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly kind: FieldKind;
|
|
7
|
+
/**
|
|
8
|
+
* Indicates whether or not to hide this field by default when making queries.
|
|
9
|
+
*/
|
|
10
|
+
hidden?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Indicates whether or not to include this field by default when making queries.
|
|
13
|
+
* If this set to "true" the field must be requested using "include" or "pick" option.
|
|
14
|
+
*/
|
|
15
|
+
exclusive?: boolean;
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Type } from 'ts-gems';
|
|
2
|
+
import { TypeResolver, TypeThunk } from '../orm.type.js';
|
|
3
|
+
import { AssociationNode } from './association-node.js';
|
|
4
|
+
export declare class LinkChain<T> {
|
|
5
|
+
target: TypeThunk<T>;
|
|
6
|
+
first: AssociationNode;
|
|
7
|
+
current: AssociationNode;
|
|
8
|
+
constructor(target: TypeThunk<T>, targetKey?: keyof T, sourceKey?: string, many?: boolean);
|
|
9
|
+
where(conditions: object | object[]): this;
|
|
10
|
+
linkToOne<K>(target: Type<K> | TypeResolver<K>, targetColumn?: keyof K, parentColumn?: keyof T): LinkChain<K>;
|
|
11
|
+
linkToMany<K>(target: Type<K> | TypeResolver<K>, targetColumn?: keyof K, parentColumn?: keyof T): LinkChain<K>;
|
|
12
|
+
private _newNode;
|
|
13
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { AssociationNode } from './association-node.js';
|
|
2
|
+
export class LinkChain {
|
|
3
|
+
target;
|
|
4
|
+
first;
|
|
5
|
+
current;
|
|
6
|
+
constructor(target, targetKey, sourceKey, many) {
|
|
7
|
+
this.target = target;
|
|
8
|
+
this.first = this.current = new AssociationNode('', {
|
|
9
|
+
many,
|
|
10
|
+
target,
|
|
11
|
+
source: null,
|
|
12
|
+
sourceKey,
|
|
13
|
+
targetKey: targetKey
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
where(conditions) {
|
|
17
|
+
this.current.conditions = this.current.conditions || [];
|
|
18
|
+
if (Array.isArray(conditions))
|
|
19
|
+
this.current.conditions.push(...conditions);
|
|
20
|
+
else
|
|
21
|
+
this.current.conditions.push(conditions);
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
linkToOne(target, targetColumn, parentColumn) {
|
|
25
|
+
return this._newNode(target, targetColumn, parentColumn);
|
|
26
|
+
}
|
|
27
|
+
linkToMany(target, targetColumn, parentColumn) {
|
|
28
|
+
return this._newNode(target, targetColumn, parentColumn, true);
|
|
29
|
+
}
|
|
30
|
+
_newNode(target, targetKey, parentKey, many = false) {
|
|
31
|
+
const child = new AssociationNode(this.current.name, {
|
|
32
|
+
many,
|
|
33
|
+
source: this.current.target,
|
|
34
|
+
target,
|
|
35
|
+
sourceKey: parentKey,
|
|
36
|
+
targetKey: targetKey
|
|
37
|
+
});
|
|
38
|
+
child.prior = this.current;
|
|
39
|
+
this.current.next = child;
|
|
40
|
+
this.current = child;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
File without changes
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Type } from 'ts-gems';
|
|
2
2
|
export declare type Ctor = Type | Function;
|
|
3
|
-
export declare type
|
|
4
|
-
export declare type AssociationKind = 'to' | 'to-many' | 'from' | 'from-many';
|
|
3
|
+
export declare type FieldKind = 'column' | 'object' | 'association';
|
|
5
4
|
/**
|
|
6
5
|
* Indicates auto generation strategy
|
|
7
6
|
*/
|
|
@@ -11,11 +10,11 @@ export declare type TypeResolver<T> = () => Type<T> | Promise<Type<T>>;
|
|
|
11
10
|
export declare type TypeThunk<T = any> = Type<T> | TypeResolver<T>;
|
|
12
11
|
export declare type EnumValue = (FieldValue)[] | Object;
|
|
13
12
|
export declare type FieldValue = string | number | boolean | Date | null;
|
|
14
|
-
export declare type DefaultValueGetter = (obj?: any) => FieldValue;
|
|
13
|
+
export declare type DefaultValueGetter = (obj?: any) => FieldValue | undefined;
|
|
15
14
|
export interface AssociationSettings {
|
|
16
15
|
source: TypeThunk;
|
|
17
16
|
target: TypeThunk;
|
|
18
17
|
sourceKey?: string;
|
|
19
18
|
targetKey?: string;
|
|
20
|
-
|
|
19
|
+
many?: boolean;
|
|
21
20
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { DeepPartial, Type } from 'ts-gems';
|
|
2
|
-
import { FieldInfoMap } from '../client/
|
|
3
|
-
import { SqbClient } from '../client/
|
|
4
|
-
import { SqbConnection } from '../client/
|
|
5
|
-
import { QueryRequest, TransactionFunction } from '../client/types';
|
|
6
|
-
import {
|
|
7
|
-
import { EntityMetadata } from './model/entity-metadata';
|
|
2
|
+
import { FieldInfoMap } from '../client/field-info-map.js';
|
|
3
|
+
import { SqbClient } from '../client/sqb-client.js';
|
|
4
|
+
import { SqbConnection } from '../client/sqb-connection.js';
|
|
5
|
+
import { QueryRequest, TransactionFunction } from '../client/types.js';
|
|
6
|
+
import { EntityData } from '../types.js';
|
|
7
|
+
import { EntityMetadata } from './model/entity-metadata.js';
|
|
8
8
|
export declare namespace Repository {
|
|
9
9
|
type TransformRowFunction = (fields: FieldInfoMap, row: object, obj: object) => void;
|
|
10
10
|
interface CommandOptions {
|
|
11
11
|
connection?: SqbConnection;
|
|
12
12
|
}
|
|
13
13
|
interface CreateOptions extends CommandOptions {
|
|
14
|
-
|
|
14
|
+
pick?: string[];
|
|
15
|
+
omit?: string[];
|
|
15
16
|
include?: string[];
|
|
16
|
-
exclude?: string[];
|
|
17
17
|
}
|
|
18
18
|
interface CountOptions extends CommandOptions {
|
|
19
19
|
filter?: any;
|
|
@@ -28,9 +28,9 @@ export declare namespace Repository {
|
|
|
28
28
|
params?: any;
|
|
29
29
|
}
|
|
30
30
|
interface FindOneOptions extends CommandOptions {
|
|
31
|
-
|
|
31
|
+
pick?: string[];
|
|
32
|
+
omit?: string[];
|
|
32
33
|
include?: string[];
|
|
33
|
-
exclude?: string[];
|
|
34
34
|
filter?: any;
|
|
35
35
|
params?: any;
|
|
36
36
|
sort?: string[];
|
|
@@ -44,16 +44,16 @@ export declare namespace Repository {
|
|
|
44
44
|
maxSubQueries?: number;
|
|
45
45
|
}
|
|
46
46
|
interface GetOptions extends CommandOptions {
|
|
47
|
-
|
|
47
|
+
pick?: string[];
|
|
48
|
+
omit?: string[];
|
|
48
49
|
include?: string[];
|
|
49
|
-
exclude?: string[];
|
|
50
50
|
filter?: any;
|
|
51
51
|
params?: any;
|
|
52
52
|
}
|
|
53
53
|
interface UpdateOptions extends CommandOptions {
|
|
54
|
-
|
|
54
|
+
pick?: string[];
|
|
55
|
+
omit?: string[];
|
|
55
56
|
include?: string[];
|
|
56
|
-
exclude?: string[];
|
|
57
57
|
filter?: any;
|
|
58
58
|
params?: any;
|
|
59
59
|
}
|
|
@@ -75,8 +75,8 @@ export declare class Repository<T> extends Repository_base {
|
|
|
75
75
|
constructor(entityDef: EntityMetadata, executor: SqbClient | SqbConnection, schema?: string);
|
|
76
76
|
get entity(): EntityMetadata;
|
|
77
77
|
get type(): Type<T>;
|
|
78
|
-
create(values:
|
|
79
|
-
createOnly(values:
|
|
78
|
+
create(values: EntityData<T>, options?: Repository.CreateOptions): Promise<DeepPartial<T>>;
|
|
79
|
+
createOnly(values: EntityData<T>, options?: Repository.CreateOptions): Promise<void>;
|
|
80
80
|
exists(keyValue: any | Record<string, any>, options?: Repository.ExistsOptions): Promise<boolean>;
|
|
81
81
|
count(options?: Repository.CountOptions): Promise<number>;
|
|
82
82
|
findAll(options?: Repository.FindAllOptions): Promise<DeepPartial<T>[]>;
|
|
@@ -84,14 +84,14 @@ export declare class Repository<T> extends Repository_base {
|
|
|
84
84
|
findByPk(keyValue: any | Record<string, any>, options?: Repository.GetOptions): Promise<DeepPartial<T> | undefined>;
|
|
85
85
|
destroy(keyValue: any | Record<string, any>, options?: Repository.DestroyOptions): Promise<boolean>;
|
|
86
86
|
destroyAll(options?: Repository.DestroyOptions): Promise<number>;
|
|
87
|
-
update(keyValue: any | Record<string, any>, values:
|
|
88
|
-
updateOnly(keyValue: any | Record<string, any>, values:
|
|
89
|
-
updateAll(values:
|
|
87
|
+
update(keyValue: any | Record<string, any>, values: EntityData<T>, options?: Repository.UpdateOptions): Promise<DeepPartial<T> | undefined>;
|
|
88
|
+
updateOnly(keyValue: any | Record<string, any>, values: EntityData<T>, options?: Repository.UpdateOptions): Promise<DeepPartial<T>>;
|
|
89
|
+
updateAll(values: EntityData<T>, options?: Repository.UpdateAllOptions): Promise<number>;
|
|
90
90
|
protected _execute(fn: TransactionFunction, opts?: Repository.CommandOptions): Promise<any>;
|
|
91
|
-
protected _create(values:
|
|
91
|
+
protected _create(values: EntityData<T>, options: Repository.CreateOptions & {
|
|
92
92
|
connection: SqbConnection;
|
|
93
93
|
}): Promise<DeepPartial<T>>;
|
|
94
|
-
protected _createOnly(values:
|
|
94
|
+
protected _createOnly(values: EntityData<T>, options: Repository.CreateOptions & {
|
|
95
95
|
connection: SqbConnection;
|
|
96
96
|
}): Promise<void>;
|
|
97
97
|
protected _exists(keyValue: any | Record<string, any>, options: Repository.ExistsOptions & {
|
|
@@ -115,10 +115,10 @@ export declare class Repository<T> extends Repository_base {
|
|
|
115
115
|
protected _destroyAll(options: Repository.DestroyOptions & {
|
|
116
116
|
connection: SqbConnection;
|
|
117
117
|
}): Promise<number>;
|
|
118
|
-
protected _update(keyValue: any | Record<string, any>, values:
|
|
118
|
+
protected _update(keyValue: any | Record<string, any>, values: EntityData<T>, options: Repository.UpdateOptions & {
|
|
119
119
|
connection: SqbConnection;
|
|
120
120
|
}): Promise<Record<string, any> | undefined>;
|
|
121
|
-
protected _updateAll(values:
|
|
121
|
+
protected _updateAll(values: EntityData<T>, options: Repository.UpdateAllOptions & {
|
|
122
122
|
connection: SqbConnection;
|
|
123
123
|
}): Promise<number>;
|
|
124
124
|
protected _emit(event: string, ...args: any[]): Promise<void>;
|