@sqb/connect 4.7.0 → 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/repository.class.js +32 -73
- 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/repository.class.d.ts +55 -71
- package/esm/orm/repository.class.js +32 -73
- package/package.json +2 -2
|
@@ -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;
|
|
@@ -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,40 +50,43 @@ 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
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
91
|
return !!(await this._updateByPk(keyValue, values, { ...options, connection }));
|
|
84
92
|
}, options);
|
|
@@ -109,20 +117,6 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
109
117
|
findAll(options) {
|
|
110
118
|
return this.findMany(options);
|
|
111
119
|
}
|
|
112
|
-
/**
|
|
113
|
-
*
|
|
114
|
-
* @deprecated
|
|
115
|
-
*/
|
|
116
|
-
update(keyValue, values, options) {
|
|
117
|
-
return this.updateByPk(keyValue, values, options);
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
*
|
|
121
|
-
* @deprecated
|
|
122
|
-
*/
|
|
123
|
-
updateOnly(keyValue, values, options) {
|
|
124
|
-
return this.updateByPkOnly(keyValue, values, options);
|
|
125
|
-
}
|
|
126
120
|
/**
|
|
127
121
|
*
|
|
128
122
|
* @deprecated
|
|
@@ -149,29 +143,14 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
149
143
|
async _create(values, options) {
|
|
150
144
|
if (!values)
|
|
151
145
|
throw new TypeError('You must provide values');
|
|
152
|
-
await this._emit('before-create', values, options);
|
|
153
146
|
const keyValues = await create_command_js_1.CreateCommand.execute({
|
|
154
147
|
...options,
|
|
155
148
|
entity: this._entity,
|
|
156
|
-
values
|
|
157
|
-
returning: true
|
|
149
|
+
values
|
|
158
150
|
});
|
|
159
|
-
|
|
160
|
-
if (!result)
|
|
151
|
+
if (options.returning && !keyValues)
|
|
161
152
|
throw new Error('Unable to insert new row');
|
|
162
|
-
|
|
163
|
-
return result;
|
|
164
|
-
}
|
|
165
|
-
async _createOnly(values, options) {
|
|
166
|
-
if (!values)
|
|
167
|
-
throw new TypeError('You must provide values');
|
|
168
|
-
await this._emit('before-create', values, options);
|
|
169
|
-
await create_command_js_1.CreateCommand.execute({
|
|
170
|
-
...options,
|
|
171
|
-
entity: this._entity,
|
|
172
|
-
values,
|
|
173
|
-
returning: false
|
|
174
|
-
});
|
|
153
|
+
return keyValues;
|
|
175
154
|
}
|
|
176
155
|
async _exists(keyValue, options) {
|
|
177
156
|
const filter = [(0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true)];
|
|
@@ -193,21 +172,20 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
193
172
|
entity: this._entity
|
|
194
173
|
});
|
|
195
174
|
}
|
|
196
|
-
async
|
|
175
|
+
async _findMany(options) {
|
|
197
176
|
return await find_command_js_1.FindCommand.execute({
|
|
198
177
|
...options,
|
|
199
178
|
entity: this._entity,
|
|
200
179
|
});
|
|
201
180
|
}
|
|
202
181
|
async _findOne(options) {
|
|
203
|
-
const rows = await
|
|
182
|
+
const rows = await this._findMany({
|
|
204
183
|
...options,
|
|
205
|
-
entity: this._entity,
|
|
206
184
|
limit: 1
|
|
207
185
|
});
|
|
208
186
|
return rows && rows[0];
|
|
209
187
|
}
|
|
210
|
-
async
|
|
188
|
+
async _find(keyValue, options) {
|
|
211
189
|
const filter = [(0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true)];
|
|
212
190
|
if (options && options.filter) {
|
|
213
191
|
if (Array.isArray(options.filter))
|
|
@@ -217,8 +195,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
217
195
|
}
|
|
218
196
|
return await this._findOne({ ...options, filter, offset: 0 });
|
|
219
197
|
}
|
|
220
|
-
async
|
|
221
|
-
await this._emit('before-destroy', keyValue, options);
|
|
198
|
+
async _delete(keyValue, options) {
|
|
222
199
|
const filter = [(0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true)];
|
|
223
200
|
if (options && options.filter) {
|
|
224
201
|
if (Array.isArray(options.filter))
|
|
@@ -226,29 +203,23 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
226
203
|
else
|
|
227
204
|
filter.push(options.filter);
|
|
228
205
|
}
|
|
229
|
-
|
|
206
|
+
return !!(await delete_command_js_1.DeleteCommand.execute({
|
|
230
207
|
...options,
|
|
231
208
|
filter,
|
|
232
209
|
entity: this._entity,
|
|
233
210
|
}));
|
|
234
|
-
await this._emit('after-destroy', result, keyValue, options);
|
|
235
|
-
return result;
|
|
236
211
|
}
|
|
237
|
-
async
|
|
238
|
-
|
|
239
|
-
const rowsAffected = destroy_command_js_1.DestroyCommand.execute({
|
|
212
|
+
async _deleteMany(options) {
|
|
213
|
+
return delete_command_js_1.DeleteCommand.execute({
|
|
240
214
|
...options,
|
|
241
215
|
entity: this._entity,
|
|
242
216
|
filter: options?.filter,
|
|
243
217
|
params: options?.params
|
|
244
218
|
});
|
|
245
|
-
await this._emit('after-destroy-all', rowsAffected, options);
|
|
246
|
-
return rowsAffected;
|
|
247
219
|
}
|
|
248
220
|
async _updateByPk(keyValue, values, options) {
|
|
249
221
|
if (!values)
|
|
250
222
|
throw new TypeError('You must provide values');
|
|
251
|
-
await this._emit('before-update', keyValue, values, options);
|
|
252
223
|
const keyValues = (0, extract_keyvalues_js_1.extractKeyValues)(this._entity, keyValue, true);
|
|
253
224
|
const filter = [keyValues];
|
|
254
225
|
if (options.filter) {
|
|
@@ -265,28 +236,16 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
|
|
|
265
236
|
values: updateValues,
|
|
266
237
|
filter
|
|
267
238
|
});
|
|
268
|
-
await this._emit('after-update', rowsAffected, keyValues, options);
|
|
269
239
|
return rowsAffected ? keyValues : undefined;
|
|
270
240
|
}
|
|
271
241
|
async _updateAll(values, options) {
|
|
272
242
|
if (!values)
|
|
273
243
|
throw new TypeError('You must provide values');
|
|
274
|
-
await
|
|
275
|
-
const rowsAffected = await update_command_js_1.UpdateCommand.execute({
|
|
244
|
+
return await update_command_js_1.UpdateCommand.execute({
|
|
276
245
|
...options,
|
|
277
246
|
entity: this._entity,
|
|
278
247
|
values
|
|
279
248
|
});
|
|
280
|
-
await this._emit('after-update-all', rowsAffected, values, options);
|
|
281
|
-
return rowsAffected;
|
|
282
|
-
}
|
|
283
|
-
async _emit(event, ...args) {
|
|
284
|
-
const events = this._entity.eventListeners && this._entity.eventListeners[event];
|
|
285
|
-
if (events) {
|
|
286
|
-
for (const fn of events) {
|
|
287
|
-
await fn(this, ...args);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
249
|
}
|
|
291
250
|
}
|
|
292
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;
|
|
@@ -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
|
-
|
|
22
|
+
interface CreateOptions extends CommandOptions, Projection {
|
|
23
|
+
}
|
|
24
|
+
interface CountOptions extends CommandOptions, Filtering {
|
|
25
|
+
}
|
|
26
|
+
interface ExistsOptions extends CommandOptions, Filtering {
|
|
17
27
|
}
|
|
18
|
-
interface
|
|
19
|
-
filter?: any;
|
|
20
|
-
params?: any;
|
|
28
|
+
interface DeleteOptions extends CommandOptions, Filtering {
|
|
21
29
|
}
|
|
22
|
-
interface
|
|
23
|
-
filter?: any;
|
|
24
|
-
params?: any;
|
|
30
|
+
interface DeleteManyOptions extends CommandOptions, Filtering {
|
|
25
31
|
}
|
|
26
|
-
interface
|
|
27
|
-
filter?: any;
|
|
28
|
-
params?: any;
|
|
32
|
+
interface FindOptions extends CommandOptions, Projection, Filtering {
|
|
29
33
|
}
|
|
30
|
-
interface FindOneOptions extends CommandOptions {
|
|
31
|
-
pick?: string[];
|
|
32
|
-
omit?: string[];
|
|
33
|
-
include?: string[];
|
|
34
|
-
filter?: any;
|
|
35
|
-
params?: any;
|
|
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,81 +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
|
-
findMany(options?: Repository.
|
|
85
|
-
|
|
86
|
-
deleteMany(options?: Repository.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
updateMany(values: EntityInput<T>, options?: Repository.
|
|
90
|
-
/**
|
|
91
|
-
*
|
|
92
|
-
* @deprecated
|
|
93
|
-
*/
|
|
94
|
-
destroy(keyValue: any | Record<string, any>, options?: Repository.DestroyOptions): Promise<boolean>;
|
|
95
|
-
/**
|
|
96
|
-
*
|
|
97
|
-
* @deprecated
|
|
98
|
-
*/
|
|
99
|
-
destroyAll(options?: Repository.DestroyOptions): Promise<number>;
|
|
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>;
|
|
84
|
+
update(keyValue: any | Record<string, any>, values: EntityInput<T>, options?: Repository.UpdateOptions): Promise<EntityOutput<T> | undefined>;
|
|
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>;
|
|
100
87
|
/**
|
|
101
88
|
*
|
|
102
89
|
* @deprecated
|
|
103
90
|
*/
|
|
104
|
-
|
|
91
|
+
destroy(keyValue: any | Record<string, any>, options?: Repository.DeleteOptions): Promise<boolean>;
|
|
105
92
|
/**
|
|
106
93
|
*
|
|
107
94
|
* @deprecated
|
|
108
95
|
*/
|
|
109
|
-
|
|
96
|
+
destroyAll(options?: Repository.DeleteManyOptions): Promise<number>;
|
|
110
97
|
/**
|
|
111
98
|
*
|
|
112
99
|
* @deprecated
|
|
113
100
|
*/
|
|
114
|
-
|
|
101
|
+
findAll(options?: Repository.FindManyOptions): Promise<EntityOutput<T>[]>;
|
|
115
102
|
/**
|
|
116
103
|
*
|
|
117
104
|
* @deprecated
|
|
118
105
|
*/
|
|
119
|
-
updateAll(values: EntityInput<T>, options?: Repository.
|
|
106
|
+
updateAll(values: EntityInput<T>, options?: Repository.UpdateManyOptions): Promise<number>;
|
|
120
107
|
protected _execute(fn: TransactionFunction, opts?: Repository.CommandOptions): Promise<any>;
|
|
121
108
|
protected _create(values: EntityInput<T>, options: Repository.CreateOptions & {
|
|
122
109
|
connection: SqbConnection;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
connection: SqbConnection;
|
|
126
|
-
}): Promise<void>;
|
|
110
|
+
returning?: boolean;
|
|
111
|
+
}): Promise<any>;
|
|
127
112
|
protected _exists(keyValue: any | Record<string, any>, options: Repository.ExistsOptions & {
|
|
128
113
|
connection: SqbConnection;
|
|
129
114
|
}): Promise<boolean>;
|
|
130
115
|
protected _count(options: Repository.CountOptions & {
|
|
131
116
|
connection: SqbConnection;
|
|
132
117
|
}): Promise<number>;
|
|
133
|
-
protected
|
|
118
|
+
protected _findMany(options: Repository.FindManyOptions & {
|
|
134
119
|
connection: SqbConnection;
|
|
135
120
|
}): Promise<EntityOutput<T>[]>;
|
|
136
121
|
protected _findOne(options: Repository.FindOneOptions & {
|
|
137
122
|
connection: SqbConnection;
|
|
138
123
|
}): Promise<EntityOutput<T> | undefined>;
|
|
139
|
-
protected
|
|
124
|
+
protected _find(keyValue: any | Record<string, any>, options: Repository.FindOptions & {
|
|
140
125
|
connection: SqbConnection;
|
|
141
126
|
}): Promise<EntityOutput<T> | undefined>;
|
|
142
|
-
protected
|
|
127
|
+
protected _delete(keyValue: any | Record<string, any>, options: Repository.DeleteOptions & {
|
|
143
128
|
connection: SqbConnection;
|
|
144
129
|
}): Promise<boolean>;
|
|
145
|
-
protected
|
|
130
|
+
protected _deleteMany(options: Repository.DeleteManyOptions & {
|
|
146
131
|
connection: SqbConnection;
|
|
147
132
|
}): Promise<number>;
|
|
148
133
|
protected _updateByPk(keyValue: any | Record<string, any>, values: EntityInput<T>, options: Repository.UpdateOptions & {
|
|
149
134
|
connection: SqbConnection;
|
|
150
135
|
}): Promise<Record<string, any> | undefined>;
|
|
151
|
-
protected _updateAll(values: EntityInput<T>, options: Repository.
|
|
136
|
+
protected _updateAll(values: EntityInput<T>, options: Repository.UpdateManyOptions & {
|
|
152
137
|
connection: SqbConnection;
|
|
153
138
|
}): Promise<number>;
|
|
154
|
-
protected _emit(event: string, ...args: any[]): Promise<void>;
|
|
155
139
|
}
|
|
156
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,40 +47,43 @@ 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
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
88
|
return !!(await this._updateByPk(keyValue, values, { ...options, connection }));
|
|
81
89
|
}, options);
|
|
@@ -106,20 +114,6 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
106
114
|
findAll(options) {
|
|
107
115
|
return this.findMany(options);
|
|
108
116
|
}
|
|
109
|
-
/**
|
|
110
|
-
*
|
|
111
|
-
* @deprecated
|
|
112
|
-
*/
|
|
113
|
-
update(keyValue, values, options) {
|
|
114
|
-
return this.updateByPk(keyValue, values, options);
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
*
|
|
118
|
-
* @deprecated
|
|
119
|
-
*/
|
|
120
|
-
updateOnly(keyValue, values, options) {
|
|
121
|
-
return this.updateByPkOnly(keyValue, values, options);
|
|
122
|
-
}
|
|
123
117
|
/**
|
|
124
118
|
*
|
|
125
119
|
* @deprecated
|
|
@@ -146,29 +140,14 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
146
140
|
async _create(values, options) {
|
|
147
141
|
if (!values)
|
|
148
142
|
throw new TypeError('You must provide values');
|
|
149
|
-
await this._emit('before-create', values, options);
|
|
150
143
|
const keyValues = await CreateCommand.execute({
|
|
151
144
|
...options,
|
|
152
145
|
entity: this._entity,
|
|
153
|
-
values
|
|
154
|
-
returning: true
|
|
146
|
+
values
|
|
155
147
|
});
|
|
156
|
-
|
|
157
|
-
if (!result)
|
|
148
|
+
if (options.returning && !keyValues)
|
|
158
149
|
throw new Error('Unable to insert new row');
|
|
159
|
-
|
|
160
|
-
return result;
|
|
161
|
-
}
|
|
162
|
-
async _createOnly(values, options) {
|
|
163
|
-
if (!values)
|
|
164
|
-
throw new TypeError('You must provide values');
|
|
165
|
-
await this._emit('before-create', values, options);
|
|
166
|
-
await CreateCommand.execute({
|
|
167
|
-
...options,
|
|
168
|
-
entity: this._entity,
|
|
169
|
-
values,
|
|
170
|
-
returning: false
|
|
171
|
-
});
|
|
150
|
+
return keyValues;
|
|
172
151
|
}
|
|
173
152
|
async _exists(keyValue, options) {
|
|
174
153
|
const filter = [extractKeyValues(this._entity, keyValue, true)];
|
|
@@ -190,21 +169,20 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
190
169
|
entity: this._entity
|
|
191
170
|
});
|
|
192
171
|
}
|
|
193
|
-
async
|
|
172
|
+
async _findMany(options) {
|
|
194
173
|
return await FindCommand.execute({
|
|
195
174
|
...options,
|
|
196
175
|
entity: this._entity,
|
|
197
176
|
});
|
|
198
177
|
}
|
|
199
178
|
async _findOne(options) {
|
|
200
|
-
const rows = await
|
|
179
|
+
const rows = await this._findMany({
|
|
201
180
|
...options,
|
|
202
|
-
entity: this._entity,
|
|
203
181
|
limit: 1
|
|
204
182
|
});
|
|
205
183
|
return rows && rows[0];
|
|
206
184
|
}
|
|
207
|
-
async
|
|
185
|
+
async _find(keyValue, options) {
|
|
208
186
|
const filter = [extractKeyValues(this._entity, keyValue, true)];
|
|
209
187
|
if (options && options.filter) {
|
|
210
188
|
if (Array.isArray(options.filter))
|
|
@@ -214,8 +192,7 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
214
192
|
}
|
|
215
193
|
return await this._findOne({ ...options, filter, offset: 0 });
|
|
216
194
|
}
|
|
217
|
-
async
|
|
218
|
-
await this._emit('before-destroy', keyValue, options);
|
|
195
|
+
async _delete(keyValue, options) {
|
|
219
196
|
const filter = [extractKeyValues(this._entity, keyValue, true)];
|
|
220
197
|
if (options && options.filter) {
|
|
221
198
|
if (Array.isArray(options.filter))
|
|
@@ -223,29 +200,23 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
223
200
|
else
|
|
224
201
|
filter.push(options.filter);
|
|
225
202
|
}
|
|
226
|
-
|
|
203
|
+
return !!(await DeleteCommand.execute({
|
|
227
204
|
...options,
|
|
228
205
|
filter,
|
|
229
206
|
entity: this._entity,
|
|
230
207
|
}));
|
|
231
|
-
await this._emit('after-destroy', result, keyValue, options);
|
|
232
|
-
return result;
|
|
233
208
|
}
|
|
234
|
-
async
|
|
235
|
-
|
|
236
|
-
const rowsAffected = DestroyCommand.execute({
|
|
209
|
+
async _deleteMany(options) {
|
|
210
|
+
return DeleteCommand.execute({
|
|
237
211
|
...options,
|
|
238
212
|
entity: this._entity,
|
|
239
213
|
filter: options?.filter,
|
|
240
214
|
params: options?.params
|
|
241
215
|
});
|
|
242
|
-
await this._emit('after-destroy-all', rowsAffected, options);
|
|
243
|
-
return rowsAffected;
|
|
244
216
|
}
|
|
245
217
|
async _updateByPk(keyValue, values, options) {
|
|
246
218
|
if (!values)
|
|
247
219
|
throw new TypeError('You must provide values');
|
|
248
|
-
await this._emit('before-update', keyValue, values, options);
|
|
249
220
|
const keyValues = extractKeyValues(this._entity, keyValue, true);
|
|
250
221
|
const filter = [keyValues];
|
|
251
222
|
if (options.filter) {
|
|
@@ -262,27 +233,15 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
|
|
|
262
233
|
values: updateValues,
|
|
263
234
|
filter
|
|
264
235
|
});
|
|
265
|
-
await this._emit('after-update', rowsAffected, keyValues, options);
|
|
266
236
|
return rowsAffected ? keyValues : undefined;
|
|
267
237
|
}
|
|
268
238
|
async _updateAll(values, options) {
|
|
269
239
|
if (!values)
|
|
270
240
|
throw new TypeError('You must provide values');
|
|
271
|
-
await
|
|
272
|
-
const rowsAffected = await UpdateCommand.execute({
|
|
241
|
+
return await UpdateCommand.execute({
|
|
273
242
|
...options,
|
|
274
243
|
entity: this._entity,
|
|
275
244
|
values
|
|
276
245
|
});
|
|
277
|
-
await this._emit('after-update-all', rowsAffected, values, options);
|
|
278
|
-
return rowsAffected;
|
|
279
|
-
}
|
|
280
|
-
async _emit(event, ...args) {
|
|
281
|
-
const events = this._entity.eventListeners && this._entity.eventListeners[event];
|
|
282
|
-
if (events) {
|
|
283
|
-
for (const fn of events) {
|
|
284
|
-
await fn(this, ...args);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
246
|
}
|
|
288
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",
|