@sqb/connect 4.6.3 → 4.8.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/cjs/orm/commands/{destroy.command.js → delete.command.js} +3 -3
- package/cjs/orm/model/association.js +28 -17
- package/cjs/orm/repository.class.js +64 -63
- package/esm/orm/commands/{destroy.command.d.ts → delete.command.d.ts} +2 -2
- package/esm/orm/commands/{destroy.command.js → delete.command.js} +1 -1
- package/esm/orm/commands/find.command.d.ts +1 -1
- package/esm/orm/commands/update.command.d.ts +1 -1
- package/esm/orm/model/association.js +28 -17
- package/esm/orm/repository.class.d.ts +71 -57
- package/esm/orm/repository.class.js +64 -63
- package/package.json +3 -3
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.DeleteCommand = void 0;
|
|
4
4
|
const builder_1 = require("@sqb/builder");
|
|
5
5
|
const command_helper_js_1 = require("./command.helper.js");
|
|
6
|
-
class
|
|
6
|
+
class DeleteCommand {
|
|
7
7
|
// istanbul ignore next
|
|
8
8
|
constructor() {
|
|
9
9
|
throw new Error('This class is abstract');
|
|
@@ -27,4 +27,4 @@ class DestroyCommand {
|
|
|
27
27
|
return (resp && resp.rowsAffected) || 0;
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
exports.
|
|
30
|
+
exports.DeleteCommand = DeleteCommand;
|
|
@@ -66,8 +66,8 @@ class Association {
|
|
|
66
66
|
return;
|
|
67
67
|
const source = await this.resolveSource();
|
|
68
68
|
const target = await this.resolveTarget();
|
|
69
|
-
let sourceKey = this.sourceKey;
|
|
70
|
-
let targetKey = this.targetKey;
|
|
69
|
+
let sourceKey = this.sourceKey || '';
|
|
70
|
+
let targetKey = this.targetKey || '';
|
|
71
71
|
if (!(sourceKey && targetKey)) {
|
|
72
72
|
// Try to determine key fields from foreign key from source to target
|
|
73
73
|
let foreign = await entity_metadata_js_1.EntityMetadata.getForeignKeyFor(source, target);
|
|
@@ -91,23 +91,34 @@ class Association {
|
|
|
91
91
|
return;
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
94
|
+
if (this.many) {
|
|
95
|
+
if (!sourceKey) {
|
|
96
|
+
const primaryIndexColumns = entity_metadata_js_1.EntityMetadata.getPrimaryIndexColumns(source);
|
|
97
|
+
sourceKey = primaryIndexColumns && primaryIndexColumns.length === 1 ?
|
|
98
|
+
primaryIndexColumns[0].name : 'id';
|
|
99
|
+
}
|
|
100
|
+
if (!targetKey && sourceKey) {
|
|
101
|
+
// snake-case
|
|
102
|
+
let s = source.name[0].toLowerCase() + source.name.substring(1) + '_' + sourceKey;
|
|
103
|
+
if (!entity_metadata_js_1.EntityMetadata.getColumnField(target, s))
|
|
104
|
+
s = (0, putil_varhelpers_1.camelCase)(s);
|
|
105
|
+
targetKey = s;
|
|
106
|
+
}
|
|
102
107
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
+
else {
|
|
109
|
+
if (!targetKey) {
|
|
110
|
+
const primaryIndexColumns = entity_metadata_js_1.EntityMetadata.getPrimaryIndexColumns(target);
|
|
111
|
+
targetKey = primaryIndexColumns && primaryIndexColumns.length === 1 ?
|
|
112
|
+
primaryIndexColumns[0].name : 'id';
|
|
113
|
+
}
|
|
114
|
+
if (!sourceKey && targetKey) {
|
|
115
|
+
// snake-case
|
|
116
|
+
let s = target.name[0].toLowerCase() + target.name.substring(1) + '_' + targetKey;
|
|
117
|
+
if (!entity_metadata_js_1.EntityMetadata.getColumnField(source, s))
|
|
118
|
+
s = (0, putil_varhelpers_1.camelCase)(s);
|
|
119
|
+
sourceKey = s;
|
|
120
|
+
}
|
|
108
121
|
}
|
|
109
|
-
targetKey = detailKey;
|
|
110
|
-
sourceKey = masterKey;
|
|
111
122
|
}
|
|
112
123
|
this._targetProperty = entity_metadata_js_1.EntityMetadata.getColumnField(target, targetKey);
|
|
113
124
|
if (!this._targetProperty)
|
|
@@ -5,7 +5,7 @@ const strict_typed_events_1 = require("strict-typed-events");
|
|
|
5
5
|
const sqb_connection_js_1 = require("../client/sqb-connection.js");
|
|
6
6
|
const count_command_js_1 = require("./commands/count.command.js");
|
|
7
7
|
const create_command_js_1 = require("./commands/create.command.js");
|
|
8
|
-
const
|
|
8
|
+
const delete_command_js_1 = require("./commands/delete.command.js");
|
|
9
9
|
const find_command_js_1 = require("./commands/find.command.js");
|
|
10
10
|
const update_command_js_1 = require("./commands/update.command.js");
|
|
11
11
|
const extract_keyvalues_js_1 = require("./util/extract-keyvalues.js");
|
|
@@ -27,12 +27,17 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
27
27
|
}
|
|
28
28
|
create(values, options) {
|
|
29
29
|
return this._execute(async (connection) => {
|
|
30
|
-
|
|
30
|
+
const keyValue = await this._create(values, { ...options, connection, returning: true });
|
|
31
|
+
const result = keyValue &&
|
|
32
|
+
await this._find(keyValue, { ...options, connection });
|
|
33
|
+
if (!result)
|
|
34
|
+
throw new Error('Unable to insert new row');
|
|
35
|
+
return result;
|
|
31
36
|
}, options);
|
|
32
37
|
}
|
|
33
38
|
createOnly(values, options) {
|
|
34
39
|
return this._execute(async (connection) => {
|
|
35
|
-
|
|
40
|
+
await this._create(values, { ...options, connection, returning: false });
|
|
36
41
|
}, options);
|
|
37
42
|
}
|
|
38
43
|
exists(keyValue, options) {
|
|
@@ -45,49 +50,80 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
45
50
|
return this._count({ ...options, connection });
|
|
46
51
|
}, options);
|
|
47
52
|
}
|
|
48
|
-
|
|
53
|
+
find(keyValue, options) {
|
|
49
54
|
return this._execute(async (connection) => {
|
|
50
|
-
return this.
|
|
55
|
+
return this._find(keyValue, { ...options, connection });
|
|
51
56
|
}, options);
|
|
52
57
|
}
|
|
53
58
|
findOne(options) {
|
|
54
59
|
return this._execute(async (connection) => {
|
|
55
|
-
return this._findOne({
|
|
60
|
+
return await this._findOne({
|
|
61
|
+
...options,
|
|
62
|
+
connection,
|
|
63
|
+
});
|
|
56
64
|
}, options);
|
|
57
65
|
}
|
|
58
|
-
|
|
66
|
+
findMany(options) {
|
|
59
67
|
return this._execute(async (connection) => {
|
|
60
|
-
return this.
|
|
68
|
+
return this._findMany({ ...options, connection });
|
|
61
69
|
}, options);
|
|
62
70
|
}
|
|
63
|
-
|
|
71
|
+
delete(keyValue, options) {
|
|
64
72
|
return this._execute(async (connection) => {
|
|
65
|
-
return this.
|
|
73
|
+
return this._delete(keyValue, { ...options, connection });
|
|
66
74
|
}, options);
|
|
67
75
|
}
|
|
68
|
-
|
|
76
|
+
deleteMany(options) {
|
|
69
77
|
return this._execute(async (connection) => {
|
|
70
|
-
return this.
|
|
78
|
+
return this._deleteMany({ ...options, connection });
|
|
71
79
|
}, options);
|
|
72
80
|
}
|
|
73
81
|
update(keyValue, values, options) {
|
|
74
82
|
return this._execute(async (connection) => {
|
|
75
83
|
const opts = { ...options, connection };
|
|
76
|
-
const keyValues = await this.
|
|
84
|
+
const keyValues = await this._updateByPk(keyValue, values, opts);
|
|
77
85
|
if (keyValues)
|
|
78
|
-
return this.
|
|
86
|
+
return this._find(keyValues, opts);
|
|
79
87
|
}, options);
|
|
80
88
|
}
|
|
81
89
|
updateOnly(keyValue, values, options) {
|
|
82
90
|
return this._execute(async (connection) => {
|
|
83
|
-
return !!(await this.
|
|
91
|
+
return !!(await this._updateByPk(keyValue, values, { ...options, connection }));
|
|
84
92
|
}, options);
|
|
85
93
|
}
|
|
86
|
-
|
|
94
|
+
updateMany(values, options) {
|
|
87
95
|
return this._execute(async (connection) => {
|
|
88
96
|
return this._updateAll(values, { ...options, connection });
|
|
89
97
|
});
|
|
90
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
*
|
|
101
|
+
* @deprecated
|
|
102
|
+
*/
|
|
103
|
+
destroy(keyValue, options) {
|
|
104
|
+
return this.deleteOne(keyValue, options);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
*
|
|
108
|
+
* @deprecated
|
|
109
|
+
*/
|
|
110
|
+
destroyAll(options) {
|
|
111
|
+
return this.deleteMany(options);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
*
|
|
115
|
+
* @deprecated
|
|
116
|
+
*/
|
|
117
|
+
findAll(options) {
|
|
118
|
+
return this.findMany(options);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
*
|
|
122
|
+
* @deprecated
|
|
123
|
+
*/
|
|
124
|
+
updateAll(values, options) {
|
|
125
|
+
return this.updateMany(values, options);
|
|
126
|
+
}
|
|
91
127
|
async _execute(fn, opts) {
|
|
92
128
|
let connection = opts?.connection;
|
|
93
129
|
if (!connection && this._executor instanceof sqb_connection_js_1.SqbConnection)
|
|
@@ -107,29 +143,14 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
107
143
|
async _create(values, options) {
|
|
108
144
|
if (!values)
|
|
109
145
|
throw new TypeError('You must provide values');
|
|
110
|
-
await this._emit('before-create', values, options);
|
|
111
146
|
const keyValues = await create_command_js_1.CreateCommand.execute({
|
|
112
147
|
...options,
|
|
113
148
|
entity: this._entity,
|
|
114
|
-
values
|
|
115
|
-
returning: true
|
|
149
|
+
values
|
|
116
150
|
});
|
|
117
|
-
|
|
118
|
-
if (!result)
|
|
151
|
+
if (options.returning && !keyValues)
|
|
119
152
|
throw new Error('Unable to insert new row');
|
|
120
|
-
|
|
121
|
-
return result;
|
|
122
|
-
}
|
|
123
|
-
async _createOnly(values, options) {
|
|
124
|
-
if (!values)
|
|
125
|
-
throw new TypeError('You must provide values');
|
|
126
|
-
await this._emit('before-create', values, options);
|
|
127
|
-
await create_command_js_1.CreateCommand.execute({
|
|
128
|
-
...options,
|
|
129
|
-
entity: this._entity,
|
|
130
|
-
values,
|
|
131
|
-
returning: false
|
|
132
|
-
});
|
|
153
|
+
return keyValues;
|
|
133
154
|
}
|
|
134
155
|
async _exists(keyValue, options) {
|
|
135
156
|
const filter = [(0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true)];
|
|
@@ -151,21 +172,20 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
151
172
|
entity: this._entity
|
|
152
173
|
});
|
|
153
174
|
}
|
|
154
|
-
async
|
|
175
|
+
async _findMany(options) {
|
|
155
176
|
return await find_command_js_1.FindCommand.execute({
|
|
156
177
|
...options,
|
|
157
178
|
entity: this._entity,
|
|
158
179
|
});
|
|
159
180
|
}
|
|
160
181
|
async _findOne(options) {
|
|
161
|
-
const rows = await
|
|
182
|
+
const rows = await this._findMany({
|
|
162
183
|
...options,
|
|
163
|
-
entity: this._entity,
|
|
164
184
|
limit: 1
|
|
165
185
|
});
|
|
166
186
|
return rows && rows[0];
|
|
167
187
|
}
|
|
168
|
-
async
|
|
188
|
+
async _find(keyValue, options) {
|
|
169
189
|
const filter = [(0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true)];
|
|
170
190
|
if (options && options.filter) {
|
|
171
191
|
if (Array.isArray(options.filter))
|
|
@@ -175,8 +195,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
175
195
|
}
|
|
176
196
|
return await this._findOne({ ...options, filter, offset: 0 });
|
|
177
197
|
}
|
|
178
|
-
async
|
|
179
|
-
await this._emit('before-destroy', keyValue, options);
|
|
198
|
+
async _delete(keyValue, options) {
|
|
180
199
|
const filter = [(0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true)];
|
|
181
200
|
if (options && options.filter) {
|
|
182
201
|
if (Array.isArray(options.filter))
|
|
@@ -184,29 +203,23 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
184
203
|
else
|
|
185
204
|
filter.push(options.filter);
|
|
186
205
|
}
|
|
187
|
-
|
|
206
|
+
return !!(await delete_command_js_1.DeleteCommand.execute({
|
|
188
207
|
...options,
|
|
189
208
|
filter,
|
|
190
209
|
entity: this._entity,
|
|
191
210
|
}));
|
|
192
|
-
await this._emit('after-destroy', result, keyValue, options);
|
|
193
|
-
return result;
|
|
194
211
|
}
|
|
195
|
-
async
|
|
196
|
-
|
|
197
|
-
const rowsAffected = destroy_command_js_1.DestroyCommand.execute({
|
|
212
|
+
async _deleteMany(options) {
|
|
213
|
+
return delete_command_js_1.DeleteCommand.execute({
|
|
198
214
|
...options,
|
|
199
215
|
entity: this._entity,
|
|
200
216
|
filter: options?.filter,
|
|
201
217
|
params: options?.params
|
|
202
218
|
});
|
|
203
|
-
await this._emit('after-destroy-all', rowsAffected, options);
|
|
204
|
-
return rowsAffected;
|
|
205
219
|
}
|
|
206
|
-
async
|
|
220
|
+
async _updateByPk(keyValue, values, options) {
|
|
207
221
|
if (!values)
|
|
208
222
|
throw new TypeError('You must provide values');
|
|
209
|
-
await this._emit('before-update', keyValue, values, options);
|
|
210
223
|
const keyValues = (0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true);
|
|
211
224
|
const filter = [keyValues];
|
|
212
225
|
if (options.filter) {
|
|
@@ -223,28 +236,16 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
223
236
|
values: updateValues,
|
|
224
237
|
filter
|
|
225
238
|
});
|
|
226
|
-
await this._emit('after-update', rowsAffected, keyValues, options);
|
|
227
239
|
return rowsAffected ? keyValues : undefined;
|
|
228
240
|
}
|
|
229
241
|
async _updateAll(values, options) {
|
|
230
242
|
if (!values)
|
|
231
243
|
throw new TypeError('You must provide values');
|
|
232
|
-
await
|
|
233
|
-
const rowsAffected = await update_command_js_1.UpdateCommand.execute({
|
|
244
|
+
return await update_command_js_1.UpdateCommand.execute({
|
|
234
245
|
...options,
|
|
235
246
|
entity: this._entity,
|
|
236
247
|
values
|
|
237
248
|
});
|
|
238
|
-
await this._emit('after-update-all', rowsAffected, values, options);
|
|
239
|
-
return rowsAffected;
|
|
240
|
-
}
|
|
241
|
-
async _emit(event, ...args) {
|
|
242
|
-
const events = this._entity.eventListeners && this._entity.eventListeners[event];
|
|
243
|
-
if (events) {
|
|
244
|
-
for (const fn of events) {
|
|
245
|
-
await fn(this, ...args);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
249
|
}
|
|
249
250
|
}
|
|
250
251
|
exports.Repository = Repository;
|
|
@@ -4,8 +4,8 @@ import { Repository } from '../repository.class.js';
|
|
|
4
4
|
export type DestroyCommandArgs = {
|
|
5
5
|
entity: EntityMetadata;
|
|
6
6
|
connection: SqbConnection;
|
|
7
|
-
} & Repository.
|
|
8
|
-
export declare class
|
|
7
|
+
} & Repository.DeleteManyOptions;
|
|
8
|
+
export declare class DeleteCommand {
|
|
9
9
|
protected constructor();
|
|
10
10
|
static execute(args: DestroyCommandArgs): Promise<number>;
|
|
11
11
|
}
|
|
@@ -6,7 +6,7 @@ import { RowConverter } from './row-converter.js';
|
|
|
6
6
|
export type FindCommandArgs = {
|
|
7
7
|
entity: EntityMetadata;
|
|
8
8
|
connection: SqbConnection;
|
|
9
|
-
} & Repository.
|
|
9
|
+
} & Repository.FindManyOptions;
|
|
10
10
|
export declare class FindCommand {
|
|
11
11
|
maxEagerFetch: number;
|
|
12
12
|
maxSubQueries: number;
|
|
@@ -63,8 +63,8 @@ export class Association {
|
|
|
63
63
|
return;
|
|
64
64
|
const source = await this.resolveSource();
|
|
65
65
|
const target = await this.resolveTarget();
|
|
66
|
-
let sourceKey = this.sourceKey;
|
|
67
|
-
let targetKey = this.targetKey;
|
|
66
|
+
let sourceKey = this.sourceKey || '';
|
|
67
|
+
let targetKey = this.targetKey || '';
|
|
68
68
|
if (!(sourceKey && targetKey)) {
|
|
69
69
|
// Try to determine key fields from foreign key from source to target
|
|
70
70
|
let foreign = await EntityMetadata.getForeignKeyFor(source, target);
|
|
@@ -88,23 +88,34 @@ export class Association {
|
|
|
88
88
|
return;
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
91
|
+
if (this.many) {
|
|
92
|
+
if (!sourceKey) {
|
|
93
|
+
const primaryIndexColumns = EntityMetadata.getPrimaryIndexColumns(source);
|
|
94
|
+
sourceKey = primaryIndexColumns && primaryIndexColumns.length === 1 ?
|
|
95
|
+
primaryIndexColumns[0].name : 'id';
|
|
96
|
+
}
|
|
97
|
+
if (!targetKey && sourceKey) {
|
|
98
|
+
// snake-case
|
|
99
|
+
let s = source.name[0].toLowerCase() + source.name.substring(1) + '_' + sourceKey;
|
|
100
|
+
if (!EntityMetadata.getColumnField(target, s))
|
|
101
|
+
s = camelCase(s);
|
|
102
|
+
targetKey = s;
|
|
103
|
+
}
|
|
99
104
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
+
else {
|
|
106
|
+
if (!targetKey) {
|
|
107
|
+
const primaryIndexColumns = EntityMetadata.getPrimaryIndexColumns(target);
|
|
108
|
+
targetKey = primaryIndexColumns && primaryIndexColumns.length === 1 ?
|
|
109
|
+
primaryIndexColumns[0].name : 'id';
|
|
110
|
+
}
|
|
111
|
+
if (!sourceKey && targetKey) {
|
|
112
|
+
// snake-case
|
|
113
|
+
let s = target.name[0].toLowerCase() + target.name.substring(1) + '_' + targetKey;
|
|
114
|
+
if (!EntityMetadata.getColumnField(source, s))
|
|
115
|
+
s = camelCase(s);
|
|
116
|
+
sourceKey = s;
|
|
117
|
+
}
|
|
105
118
|
}
|
|
106
|
-
targetKey = detailKey;
|
|
107
|
-
sourceKey = masterKey;
|
|
108
119
|
}
|
|
109
120
|
this._targetProperty = EntityMetadata.getColumnField(target, targetKey);
|
|
110
121
|
if (!this._targetProperty)
|
|
@@ -1,66 +1,63 @@
|
|
|
1
|
-
import { Type } from 'ts-gems';
|
|
1
|
+
import { StrictOmit, Type } from 'ts-gems';
|
|
2
2
|
import { FieldInfoMap } from '../client/field-info-map.js';
|
|
3
3
|
import { SqbClient } from '../client/sqb-client.js';
|
|
4
4
|
import { SqbConnection } from '../client/sqb-connection.js';
|
|
5
5
|
import { QueryRequest, TransactionFunction } from '../client/types.js';
|
|
6
6
|
import { EntityInput, EntityOutput } from '../types.js';
|
|
7
7
|
import { EntityMetadata } from './model/entity-metadata.js';
|
|
8
|
+
interface Projection {
|
|
9
|
+
pick?: string[];
|
|
10
|
+
omit?: string[];
|
|
11
|
+
include?: string[];
|
|
12
|
+
}
|
|
13
|
+
interface Filtering {
|
|
14
|
+
filter?: any;
|
|
15
|
+
params?: any;
|
|
16
|
+
}
|
|
8
17
|
export declare namespace Repository {
|
|
9
18
|
type TransformRowFunction = (fields: FieldInfoMap, row: object, obj: object) => void;
|
|
10
19
|
interface CommandOptions {
|
|
11
20
|
connection?: SqbConnection;
|
|
12
21
|
}
|
|
13
|
-
interface CreateOptions extends CommandOptions {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
include?: string[];
|
|
22
|
+
interface CreateOptions extends CommandOptions, Projection {
|
|
23
|
+
}
|
|
24
|
+
interface CountOptions extends CommandOptions, Filtering {
|
|
17
25
|
}
|
|
18
|
-
interface
|
|
19
|
-
filter?: any;
|
|
20
|
-
params?: any;
|
|
26
|
+
interface ExistsOptions extends CommandOptions, Filtering {
|
|
21
27
|
}
|
|
22
|
-
interface
|
|
23
|
-
filter?: any;
|
|
24
|
-
params?: any;
|
|
28
|
+
interface DeleteOptions extends CommandOptions, Filtering {
|
|
25
29
|
}
|
|
26
|
-
interface
|
|
27
|
-
filter?: any;
|
|
28
|
-
params?: any;
|
|
30
|
+
interface DeleteManyOptions extends CommandOptions, Filtering {
|
|
29
31
|
}
|
|
30
|
-
interface
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
include?: string[];
|
|
34
|
-
filter?: any;
|
|
35
|
-
params?: any;
|
|
32
|
+
interface FindOptions extends CommandOptions, Projection, Filtering {
|
|
33
|
+
}
|
|
34
|
+
interface FindOneOptions extends CommandOptions, Projection, Filtering {
|
|
36
35
|
sort?: string[];
|
|
37
36
|
offset?: number;
|
|
38
|
-
distinct?: boolean;
|
|
39
|
-
onTransformRow?: TransformRowFunction;
|
|
40
37
|
}
|
|
41
|
-
interface
|
|
38
|
+
interface FindManyOptions extends FindOneOptions {
|
|
42
39
|
limit?: number;
|
|
40
|
+
distinct?: boolean;
|
|
43
41
|
maxEagerFetch?: number;
|
|
44
42
|
maxSubQueries?: number;
|
|
43
|
+
onTransformRow?: TransformRowFunction;
|
|
45
44
|
}
|
|
46
|
-
interface
|
|
47
|
-
pick?: string[];
|
|
48
|
-
omit?: string[];
|
|
49
|
-
include?: string[];
|
|
50
|
-
filter?: any;
|
|
51
|
-
params?: any;
|
|
52
|
-
}
|
|
53
|
-
interface UpdateOptions extends CommandOptions {
|
|
54
|
-
pick?: string[];
|
|
55
|
-
omit?: string[];
|
|
56
|
-
include?: string[];
|
|
57
|
-
filter?: any;
|
|
58
|
-
params?: any;
|
|
45
|
+
interface UpdateOptions extends CommandOptions, Projection, Filtering {
|
|
59
46
|
}
|
|
60
|
-
interface
|
|
61
|
-
filter?: any;
|
|
62
|
-
params?: any;
|
|
47
|
+
interface UpdateManyOptions extends CommandOptions, Filtering {
|
|
63
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* @deprecated
|
|
51
|
+
*/
|
|
52
|
+
type FindAllOptions = FindManyOptions;
|
|
53
|
+
/**
|
|
54
|
+
* @deprecated
|
|
55
|
+
*/
|
|
56
|
+
type DestroyOptions = DeleteOptions;
|
|
57
|
+
/**
|
|
58
|
+
* @deprecated
|
|
59
|
+
*/
|
|
60
|
+
type UpdateAllOptions = UpdateManyOptions;
|
|
64
61
|
}
|
|
65
62
|
interface RepositoryEvents {
|
|
66
63
|
execute: (request: QueryRequest) => void;
|
|
@@ -76,51 +73,68 @@ export declare class Repository<T> extends Repository_base {
|
|
|
76
73
|
get entity(): EntityMetadata;
|
|
77
74
|
get type(): Type<T>;
|
|
78
75
|
create(values: EntityInput<T>, options?: Repository.CreateOptions): Promise<EntityOutput<T>>;
|
|
79
|
-
createOnly(values: EntityInput<T>, options?: Repository.CreateOptions): Promise<void>;
|
|
76
|
+
createOnly(values: EntityInput<T>, options?: StrictOmit<Repository.CreateOptions, keyof Projection>): Promise<void>;
|
|
80
77
|
exists(keyValue: any | Record<string, any>, options?: Repository.ExistsOptions): Promise<boolean>;
|
|
81
78
|
count(options?: Repository.CountOptions): Promise<number>;
|
|
82
|
-
|
|
79
|
+
find(keyValue: any | Record<string, any>, options?: Repository.FindOptions): Promise<EntityOutput<T> | undefined>;
|
|
83
80
|
findOne(options?: Repository.FindOneOptions): Promise<EntityOutput<T> | undefined>;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
81
|
+
findMany(options?: Repository.FindManyOptions): Promise<EntityOutput<T>[]>;
|
|
82
|
+
delete(keyValue: any | Record<string, any>, options?: Repository.DeleteOptions): Promise<boolean>;
|
|
83
|
+
deleteMany(options?: Repository.DeleteManyOptions): Promise<number>;
|
|
87
84
|
update(keyValue: any | Record<string, any>, values: EntityInput<T>, options?: Repository.UpdateOptions): Promise<EntityOutput<T> | undefined>;
|
|
88
|
-
updateOnly(keyValue: any | Record<string, any>, values: EntityInput<T>, options?: Repository.UpdateOptions): Promise<
|
|
89
|
-
|
|
85
|
+
updateOnly(keyValue: any | Record<string, any>, values: EntityInput<T>, options?: StrictOmit<Repository.UpdateOptions, keyof Projection>): Promise<boolean>;
|
|
86
|
+
updateMany(values: EntityInput<T>, options?: Repository.UpdateManyOptions): Promise<number>;
|
|
87
|
+
/**
|
|
88
|
+
*
|
|
89
|
+
* @deprecated
|
|
90
|
+
*/
|
|
91
|
+
destroy(keyValue: any | Record<string, any>, options?: Repository.DeleteOptions): Promise<boolean>;
|
|
92
|
+
/**
|
|
93
|
+
*
|
|
94
|
+
* @deprecated
|
|
95
|
+
*/
|
|
96
|
+
destroyAll(options?: Repository.DeleteManyOptions): Promise<number>;
|
|
97
|
+
/**
|
|
98
|
+
*
|
|
99
|
+
* @deprecated
|
|
100
|
+
*/
|
|
101
|
+
findAll(options?: Repository.FindManyOptions): Promise<EntityOutput<T>[]>;
|
|
102
|
+
/**
|
|
103
|
+
*
|
|
104
|
+
* @deprecated
|
|
105
|
+
*/
|
|
106
|
+
updateAll(values: EntityInput<T>, options?: Repository.UpdateManyOptions): Promise<number>;
|
|
90
107
|
protected _execute(fn: TransactionFunction, opts?: Repository.CommandOptions): Promise<any>;
|
|
91
108
|
protected _create(values: EntityInput<T>, options: Repository.CreateOptions & {
|
|
92
109
|
connection: SqbConnection;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
connection: SqbConnection;
|
|
96
|
-
}): Promise<void>;
|
|
110
|
+
returning?: boolean;
|
|
111
|
+
}): Promise<any>;
|
|
97
112
|
protected _exists(keyValue: any | Record<string, any>, options: Repository.ExistsOptions & {
|
|
98
113
|
connection: SqbConnection;
|
|
99
114
|
}): Promise<boolean>;
|
|
100
115
|
protected _count(options: Repository.CountOptions & {
|
|
101
116
|
connection: SqbConnection;
|
|
102
117
|
}): Promise<number>;
|
|
103
|
-
protected
|
|
118
|
+
protected _findMany(options: Repository.FindManyOptions & {
|
|
104
119
|
connection: SqbConnection;
|
|
105
120
|
}): Promise<EntityOutput<T>[]>;
|
|
106
121
|
protected _findOne(options: Repository.FindOneOptions & {
|
|
107
122
|
connection: SqbConnection;
|
|
108
123
|
}): Promise<EntityOutput<T> | undefined>;
|
|
109
|
-
protected
|
|
124
|
+
protected _find(keyValue: any | Record<string, any>, options: Repository.FindOptions & {
|
|
110
125
|
connection: SqbConnection;
|
|
111
126
|
}): Promise<EntityOutput<T> | undefined>;
|
|
112
|
-
protected
|
|
127
|
+
protected _delete(keyValue: any | Record<string, any>, options: Repository.DeleteOptions & {
|
|
113
128
|
connection: SqbConnection;
|
|
114
129
|
}): Promise<boolean>;
|
|
115
|
-
protected
|
|
130
|
+
protected _deleteMany(options: Repository.DeleteManyOptions & {
|
|
116
131
|
connection: SqbConnection;
|
|
117
132
|
}): Promise<number>;
|
|
118
|
-
protected
|
|
133
|
+
protected _updateByPk(keyValue: any | Record<string, any>, values: EntityInput<T>, options: Repository.UpdateOptions & {
|
|
119
134
|
connection: SqbConnection;
|
|
120
135
|
}): Promise<Record<string, any> | undefined>;
|
|
121
|
-
protected _updateAll(values: EntityInput<T>, options: Repository.
|
|
136
|
+
protected _updateAll(values: EntityInput<T>, options: Repository.UpdateManyOptions & {
|
|
122
137
|
connection: SqbConnection;
|
|
123
138
|
}): Promise<number>;
|
|
124
|
-
protected _emit(event: string, ...args: any[]): Promise<void>;
|
|
125
139
|
}
|
|
126
140
|
export {};
|
|
@@ -2,7 +2,7 @@ import { AsyncEventEmitter, TypedEventEmitterClass } from 'strict-typed-events';
|
|
|
2
2
|
import { SqbConnection } from '../client/sqb-connection.js';
|
|
3
3
|
import { CountCommand } from './commands/count.command.js';
|
|
4
4
|
import { CreateCommand } from './commands/create.command.js';
|
|
5
|
-
import {
|
|
5
|
+
import { DeleteCommand } from './commands/delete.command.js';
|
|
6
6
|
import { FindCommand } from './commands/find.command.js';
|
|
7
7
|
import { UpdateCommand } from './commands/update.command.js';
|
|
8
8
|
import { extractKeyValues } from './util/extract-keyvalues.js';
|
|
@@ -24,12 +24,17 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
24
24
|
}
|
|
25
25
|
create(values, options) {
|
|
26
26
|
return this._execute(async (connection) => {
|
|
27
|
-
|
|
27
|
+
const keyValue = await this._create(values, { ...options, connection, returning: true });
|
|
28
|
+
const result = keyValue &&
|
|
29
|
+
await this._find(keyValue, { ...options, connection });
|
|
30
|
+
if (!result)
|
|
31
|
+
throw new Error('Unable to insert new row');
|
|
32
|
+
return result;
|
|
28
33
|
}, options);
|
|
29
34
|
}
|
|
30
35
|
createOnly(values, options) {
|
|
31
36
|
return this._execute(async (connection) => {
|
|
32
|
-
|
|
37
|
+
await this._create(values, { ...options, connection, returning: false });
|
|
33
38
|
}, options);
|
|
34
39
|
}
|
|
35
40
|
exists(keyValue, options) {
|
|
@@ -42,49 +47,80 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
42
47
|
return this._count({ ...options, connection });
|
|
43
48
|
}, options);
|
|
44
49
|
}
|
|
45
|
-
|
|
50
|
+
find(keyValue, options) {
|
|
46
51
|
return this._execute(async (connection) => {
|
|
47
|
-
return this.
|
|
52
|
+
return this._find(keyValue, { ...options, connection });
|
|
48
53
|
}, options);
|
|
49
54
|
}
|
|
50
55
|
findOne(options) {
|
|
51
56
|
return this._execute(async (connection) => {
|
|
52
|
-
return this._findOne({
|
|
57
|
+
return await this._findOne({
|
|
58
|
+
...options,
|
|
59
|
+
connection,
|
|
60
|
+
});
|
|
53
61
|
}, options);
|
|
54
62
|
}
|
|
55
|
-
|
|
63
|
+
findMany(options) {
|
|
56
64
|
return this._execute(async (connection) => {
|
|
57
|
-
return this.
|
|
65
|
+
return this._findMany({ ...options, connection });
|
|
58
66
|
}, options);
|
|
59
67
|
}
|
|
60
|
-
|
|
68
|
+
delete(keyValue, options) {
|
|
61
69
|
return this._execute(async (connection) => {
|
|
62
|
-
return this.
|
|
70
|
+
return this._delete(keyValue, { ...options, connection });
|
|
63
71
|
}, options);
|
|
64
72
|
}
|
|
65
|
-
|
|
73
|
+
deleteMany(options) {
|
|
66
74
|
return this._execute(async (connection) => {
|
|
67
|
-
return this.
|
|
75
|
+
return this._deleteMany({ ...options, connection });
|
|
68
76
|
}, options);
|
|
69
77
|
}
|
|
70
78
|
update(keyValue, values, options) {
|
|
71
79
|
return this._execute(async (connection) => {
|
|
72
80
|
const opts = { ...options, connection };
|
|
73
|
-
const keyValues = await this.
|
|
81
|
+
const keyValues = await this._updateByPk(keyValue, values, opts);
|
|
74
82
|
if (keyValues)
|
|
75
|
-
return this.
|
|
83
|
+
return this._find(keyValues, opts);
|
|
76
84
|
}, options);
|
|
77
85
|
}
|
|
78
86
|
updateOnly(keyValue, values, options) {
|
|
79
87
|
return this._execute(async (connection) => {
|
|
80
|
-
return !!(await this.
|
|
88
|
+
return !!(await this._updateByPk(keyValue, values, { ...options, connection }));
|
|
81
89
|
}, options);
|
|
82
90
|
}
|
|
83
|
-
|
|
91
|
+
updateMany(values, options) {
|
|
84
92
|
return this._execute(async (connection) => {
|
|
85
93
|
return this._updateAll(values, { ...options, connection });
|
|
86
94
|
});
|
|
87
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
*
|
|
98
|
+
* @deprecated
|
|
99
|
+
*/
|
|
100
|
+
destroy(keyValue, options) {
|
|
101
|
+
return this.deleteOne(keyValue, options);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
*
|
|
105
|
+
* @deprecated
|
|
106
|
+
*/
|
|
107
|
+
destroyAll(options) {
|
|
108
|
+
return this.deleteMany(options);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
*
|
|
112
|
+
* @deprecated
|
|
113
|
+
*/
|
|
114
|
+
findAll(options) {
|
|
115
|
+
return this.findMany(options);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
*
|
|
119
|
+
* @deprecated
|
|
120
|
+
*/
|
|
121
|
+
updateAll(values, options) {
|
|
122
|
+
return this.updateMany(values, options);
|
|
123
|
+
}
|
|
88
124
|
async _execute(fn, opts) {
|
|
89
125
|
let connection = opts?.connection;
|
|
90
126
|
if (!connection && this._executor instanceof SqbConnection)
|
|
@@ -104,29 +140,14 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
104
140
|
async _create(values, options) {
|
|
105
141
|
if (!values)
|
|
106
142
|
throw new TypeError('You must provide values');
|
|
107
|
-
await this._emit('before-create', values, options);
|
|
108
143
|
const keyValues = await CreateCommand.execute({
|
|
109
144
|
...options,
|
|
110
145
|
entity: this._entity,
|
|
111
|
-
values
|
|
112
|
-
returning: true
|
|
146
|
+
values
|
|
113
147
|
});
|
|
114
|
-
|
|
115
|
-
if (!result)
|
|
148
|
+
if (options.returning && !keyValues)
|
|
116
149
|
throw new Error('Unable to insert new row');
|
|
117
|
-
|
|
118
|
-
return result;
|
|
119
|
-
}
|
|
120
|
-
async _createOnly(values, options) {
|
|
121
|
-
if (!values)
|
|
122
|
-
throw new TypeError('You must provide values');
|
|
123
|
-
await this._emit('before-create', values, options);
|
|
124
|
-
await CreateCommand.execute({
|
|
125
|
-
...options,
|
|
126
|
-
entity: this._entity,
|
|
127
|
-
values,
|
|
128
|
-
returning: false
|
|
129
|
-
});
|
|
150
|
+
return keyValues;
|
|
130
151
|
}
|
|
131
152
|
async _exists(keyValue, options) {
|
|
132
153
|
const filter = [extractKeyValues(this._entity, keyValue, true)];
|
|
@@ -148,21 +169,20 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
148
169
|
entity: this._entity
|
|
149
170
|
});
|
|
150
171
|
}
|
|
151
|
-
async
|
|
172
|
+
async _findMany(options) {
|
|
152
173
|
return await FindCommand.execute({
|
|
153
174
|
...options,
|
|
154
175
|
entity: this._entity,
|
|
155
176
|
});
|
|
156
177
|
}
|
|
157
178
|
async _findOne(options) {
|
|
158
|
-
const rows = await
|
|
179
|
+
const rows = await this._findMany({
|
|
159
180
|
...options,
|
|
160
|
-
entity: this._entity,
|
|
161
181
|
limit: 1
|
|
162
182
|
});
|
|
163
183
|
return rows && rows[0];
|
|
164
184
|
}
|
|
165
|
-
async
|
|
185
|
+
async _find(keyValue, options) {
|
|
166
186
|
const filter = [extractKeyValues(this._entity, keyValue, true)];
|
|
167
187
|
if (options && options.filter) {
|
|
168
188
|
if (Array.isArray(options.filter))
|
|
@@ -172,8 +192,7 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
172
192
|
}
|
|
173
193
|
return await this._findOne({ ...options, filter, offset: 0 });
|
|
174
194
|
}
|
|
175
|
-
async
|
|
176
|
-
await this._emit('before-destroy', keyValue, options);
|
|
195
|
+
async _delete(keyValue, options) {
|
|
177
196
|
const filter = [extractKeyValues(this._entity, keyValue, true)];
|
|
178
197
|
if (options && options.filter) {
|
|
179
198
|
if (Array.isArray(options.filter))
|
|
@@ -181,29 +200,23 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
181
200
|
else
|
|
182
201
|
filter.push(options.filter);
|
|
183
202
|
}
|
|
184
|
-
|
|
203
|
+
return !!(await DeleteCommand.execute({
|
|
185
204
|
...options,
|
|
186
205
|
filter,
|
|
187
206
|
entity: this._entity,
|
|
188
207
|
}));
|
|
189
|
-
await this._emit('after-destroy', result, keyValue, options);
|
|
190
|
-
return result;
|
|
191
208
|
}
|
|
192
|
-
async
|
|
193
|
-
|
|
194
|
-
const rowsAffected = DestroyCommand.execute({
|
|
209
|
+
async _deleteMany(options) {
|
|
210
|
+
return DeleteCommand.execute({
|
|
195
211
|
...options,
|
|
196
212
|
entity: this._entity,
|
|
197
213
|
filter: options?.filter,
|
|
198
214
|
params: options?.params
|
|
199
215
|
});
|
|
200
|
-
await this._emit('after-destroy-all', rowsAffected, options);
|
|
201
|
-
return rowsAffected;
|
|
202
216
|
}
|
|
203
|
-
async
|
|
217
|
+
async _updateByPk(keyValue, values, options) {
|
|
204
218
|
if (!values)
|
|
205
219
|
throw new TypeError('You must provide values');
|
|
206
|
-
await this._emit('before-update', keyValue, values, options);
|
|
207
220
|
const keyValues = extractKeyValues(this._entity, keyValue, true);
|
|
208
221
|
const filter = [keyValues];
|
|
209
222
|
if (options.filter) {
|
|
@@ -220,27 +233,15 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
220
233
|
values: updateValues,
|
|
221
234
|
filter
|
|
222
235
|
});
|
|
223
|
-
await this._emit('after-update', rowsAffected, keyValues, options);
|
|
224
236
|
return rowsAffected ? keyValues : undefined;
|
|
225
237
|
}
|
|
226
238
|
async _updateAll(values, options) {
|
|
227
239
|
if (!values)
|
|
228
240
|
throw new TypeError('You must provide values');
|
|
229
|
-
await
|
|
230
|
-
const rowsAffected = await UpdateCommand.execute({
|
|
241
|
+
return await UpdateCommand.execute({
|
|
231
242
|
...options,
|
|
232
243
|
entity: this._entity,
|
|
233
244
|
values
|
|
234
245
|
});
|
|
235
|
-
await this._emit('after-update-all', rowsAffected, values, options);
|
|
236
|
-
return rowsAffected;
|
|
237
|
-
}
|
|
238
|
-
async _emit(event, ...args) {
|
|
239
|
-
const events = this._entity.eventListeners && this._entity.eventListeners[event];
|
|
240
|
-
if (events) {
|
|
241
|
-
for (const fn of events) {
|
|
242
|
-
await fn(this, ...args);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
246
|
}
|
|
246
247
|
}
|
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.8.0",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Eray Hanoglu <e.hanoglu@panates.com>",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@types/lodash": "^4.14.194"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
|
-
"@sqb/builder": "^4.
|
|
49
|
+
"@sqb/builder": "^4.8.0"
|
|
50
50
|
},
|
|
51
51
|
"type": "module",
|
|
52
52
|
"types": "esm/index.d.ts",
|
|
@@ -84,4 +84,4 @@
|
|
|
84
84
|
"sqlite",
|
|
85
85
|
"mysql"
|
|
86
86
|
]
|
|
87
|
-
}
|
|
87
|
+
}
|