@sqb/connect 4.7.0 → 4.8.2

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.
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DestroyCommand = void 0;
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 DestroyCommand {
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.DestroyCommand = DestroyCommand;
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 destroy_command_js_1 = require("./commands/destroy.command.js");
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
- return this._create(values, { ...options, connection });
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
- return this._createOnly(values, { ...options, connection });
40
+ await this._create(values, { ...options, connection, returning: false });
36
41
  }, options);
37
42
  }
38
43
  exists(keyValue, options) {
@@ -45,47 +50,50 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
45
50
  return this._count({ ...options, connection });
46
51
  }, options);
47
52
  }
48
- findByPk(keyValue, options) {
53
+ find(keyValue, options) {
49
54
  return this._execute(async (connection) => {
50
- return this._findByPk(keyValue, { ...options, connection });
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({ ...options, connection });
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._findAll({ ...options, connection });
68
+ return this._findMany({ ...options, connection });
61
69
  }, options);
62
70
  }
63
- deleteByPk(keyValue, options) {
71
+ delete(keyValue, options) {
64
72
  return this._execute(async (connection) => {
65
- return this._destroy(keyValue, { ...options, connection });
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._destroyAll({ ...options, connection });
78
+ return this._deleteMany({ ...options, connection });
71
79
  }, options);
72
80
  }
73
- updateByPk(keyValue, values, options) {
81
+ update(keyValue, values, options) {
74
82
  return this._execute(async (connection) => {
75
83
  const opts = { ...options, connection };
76
- const keyValues = await this._updateByPk(keyValue, values, opts);
84
+ const keyValues = await this._update(keyValue, values, opts);
77
85
  if (keyValues)
78
- return this._findByPk(keyValues, opts);
86
+ return this._find(keyValues, opts);
79
87
  }, options);
80
88
  }
81
- updateByPkOnly(keyValue, values, options) {
89
+ updateOnly(keyValue, values, options) {
82
90
  return this._execute(async (connection) => {
83
- return !!(await this._updateByPk(keyValue, values, { ...options, connection }));
91
+ return !!(await this._update(keyValue, values, { ...options, connection }));
84
92
  }, options);
85
93
  }
86
94
  updateMany(values, options) {
87
95
  return this._execute(async (connection) => {
88
- return this._updateAll(values, { ...options, connection });
96
+ return this._updateMany(values, { ...options, connection });
89
97
  });
90
98
  }
91
99
  /**
@@ -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
- const result = keyValues && (await this.findByPk(keyValues, options));
160
- if (!result)
151
+ if (options.returning && !keyValues)
161
152
  throw new Error('Unable to insert new row');
162
- await this._emit('after-create', result, options);
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 _findAll(options) {
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 find_command_js_1.FindCommand.execute({
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 _findByPk(keyValue, options) {
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 _destroy(keyValue, options) {
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
- const result = !!(await destroy_command_js_1.DestroyCommand.execute({
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 _destroyAll(options) {
238
- await this._emit('before-destroy-all', options);
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
- async _updateByPk(keyValue, values, options) {
220
+ async _update(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
- async _updateAll(values, options) {
241
+ async _updateMany(values, options) {
272
242
  if (!values)
273
243
  throw new TypeError('You must provide values');
274
- await this._emit('before-update-all', values, options);
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.DestroyOptions;
8
- export declare class DestroyCommand {
7
+ } & Repository.DeleteManyOptions;
8
+ export declare class DeleteCommand {
9
9
  protected constructor();
10
10
  static execute(args: DestroyCommandArgs): Promise<number>;
11
11
  }
@@ -1,6 +1,6 @@
1
1
  import { And, Delete } from '@sqb/builder';
2
2
  import { prepareFilter } from './command.helper.js';
3
- export class DestroyCommand {
3
+ export class DeleteCommand {
4
4
  // istanbul ignore next
5
5
  constructor() {
6
6
  throw new Error('This class is abstract');
@@ -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.FindAllOptions;
9
+ } & Repository.FindManyOptions;
10
10
  export declare class FindCommand {
11
11
  maxEagerFetch: number;
12
12
  maxSubQueries: number;
@@ -5,7 +5,7 @@ export type UpdateCommandArgs = {
5
5
  entity: EntityMetadata;
6
6
  connection: SqbConnection;
7
7
  values: any;
8
- } & Repository.UpdateAllOptions;
8
+ } & Repository.UpdateManyOptions;
9
9
  type UpdateCommandContext = {
10
10
  entity: EntityMetadata;
11
11
  queryParams: any;
@@ -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
- pick?: string[];
15
- omit?: string[];
16
- include?: string[];
22
+ interface CreateOptions extends CommandOptions, Projection {
23
+ }
24
+ interface CountOptions extends CommandOptions, Filtering {
25
+ }
26
+ interface ExistsOptions extends CommandOptions, Filtering {
17
27
  }
18
- interface CountOptions extends CommandOptions {
19
- filter?: any;
20
- params?: any;
28
+ interface DeleteOptions extends CommandOptions, Filtering {
21
29
  }
22
- interface ExistsOptions extends CommandOptions {
23
- filter?: any;
24
- params?: any;
30
+ interface DeleteManyOptions extends CommandOptions, Filtering {
25
31
  }
26
- interface DestroyOptions extends CommandOptions {
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 FindAllOptions extends FindOneOptions {
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 GetOptions extends CommandOptions {
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 UpdateAllOptions extends CommandOptions {
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
- findByPk(keyValue: any | Record<string, any>, options?: Repository.GetOptions): Promise<EntityOutput<T> | undefined>;
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.FindAllOptions): Promise<EntityOutput<T>[]>;
85
- deleteByPk(keyValue: any | Record<string, any>, options?: Repository.DestroyOptions): Promise<boolean>;
86
- deleteMany(options?: Repository.DestroyOptions): Promise<number>;
87
- updateByPk(keyValue: any | Record<string, any>, values: EntityInput<T>, options?: Repository.UpdateOptions): Promise<EntityOutput<T> | undefined>;
88
- updateByPkOnly(keyValue: any | Record<string, any>, values: EntityInput<T>, options?: Repository.UpdateOptions): Promise<boolean>;
89
- updateMany(values: EntityInput<T>, options?: Repository.UpdateAllOptions): Promise<number>;
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
- findAll(options?: Repository.FindAllOptions): Promise<EntityOutput<T>[]>;
91
+ destroy(keyValue: any | Record<string, any>, options?: Repository.DeleteOptions): Promise<boolean>;
105
92
  /**
106
93
  *
107
94
  * @deprecated
108
95
  */
109
- update(keyValue: any | Record<string, any>, values: EntityInput<T>, options?: Repository.UpdateOptions): Promise<EntityOutput<T> | undefined>;
96
+ destroyAll(options?: Repository.DeleteManyOptions): Promise<number>;
110
97
  /**
111
98
  *
112
99
  * @deprecated
113
100
  */
114
- updateOnly(keyValue: any | Record<string, any>, values: EntityInput<T>, options?: Repository.UpdateOptions): Promise<boolean>;
101
+ findAll(options?: Repository.FindManyOptions): Promise<EntityOutput<T>[]>;
115
102
  /**
116
103
  *
117
104
  * @deprecated
118
105
  */
119
- updateAll(values: EntityInput<T>, options?: Repository.UpdateAllOptions): Promise<number>;
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
- }): Promise<EntityOutput<T>>;
124
- protected _createOnly(values: EntityInput<T>, options: Repository.CreateOptions & {
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 _findAll(options: Repository.FindAllOptions & {
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 _findByPk(keyValue: any | Record<string, any>, options: Repository.GetOptions & {
124
+ protected _find(keyValue: any | Record<string, any>, options: Repository.FindOptions & {
140
125
  connection: SqbConnection;
141
126
  }): Promise<EntityOutput<T> | undefined>;
142
- protected _destroy(keyValue: any | Record<string, any>, options: Repository.DestroyOptions & {
127
+ protected _delete(keyValue: any | Record<string, any>, options: Repository.DeleteOptions & {
143
128
  connection: SqbConnection;
144
129
  }): Promise<boolean>;
145
- protected _destroyAll(options: Repository.DestroyOptions & {
130
+ protected _deleteMany(options: Repository.DeleteManyOptions & {
146
131
  connection: SqbConnection;
147
132
  }): Promise<number>;
148
- protected _updateByPk(keyValue: any | Record<string, any>, values: EntityInput<T>, options: Repository.UpdateOptions & {
133
+ protected _update(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.UpdateAllOptions & {
136
+ protected _updateMany(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 { DestroyCommand } from './commands/destroy.command.js';
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
- return this._create(values, { ...options, connection });
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
- return this._createOnly(values, { ...options, connection });
37
+ await this._create(values, { ...options, connection, returning: false });
33
38
  }, options);
34
39
  }
35
40
  exists(keyValue, options) {
@@ -42,47 +47,50 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
42
47
  return this._count({ ...options, connection });
43
48
  }, options);
44
49
  }
45
- findByPk(keyValue, options) {
50
+ find(keyValue, options) {
46
51
  return this._execute(async (connection) => {
47
- return this._findByPk(keyValue, { ...options, connection });
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({ ...options, connection });
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._findAll({ ...options, connection });
65
+ return this._findMany({ ...options, connection });
58
66
  }, options);
59
67
  }
60
- deleteByPk(keyValue, options) {
68
+ delete(keyValue, options) {
61
69
  return this._execute(async (connection) => {
62
- return this._destroy(keyValue, { ...options, connection });
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._destroyAll({ ...options, connection });
75
+ return this._deleteMany({ ...options, connection });
68
76
  }, options);
69
77
  }
70
- updateByPk(keyValue, values, options) {
78
+ update(keyValue, values, options) {
71
79
  return this._execute(async (connection) => {
72
80
  const opts = { ...options, connection };
73
- const keyValues = await this._updateByPk(keyValue, values, opts);
81
+ const keyValues = await this._update(keyValue, values, opts);
74
82
  if (keyValues)
75
- return this._findByPk(keyValues, opts);
83
+ return this._find(keyValues, opts);
76
84
  }, options);
77
85
  }
78
- updateByPkOnly(keyValue, values, options) {
86
+ updateOnly(keyValue, values, options) {
79
87
  return this._execute(async (connection) => {
80
- return !!(await this._updateByPk(keyValue, values, { ...options, connection }));
88
+ return !!(await this._update(keyValue, values, { ...options, connection }));
81
89
  }, options);
82
90
  }
83
91
  updateMany(values, options) {
84
92
  return this._execute(async (connection) => {
85
- return this._updateAll(values, { ...options, connection });
93
+ return this._updateMany(values, { ...options, connection });
86
94
  });
87
95
  }
88
96
  /**
@@ -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
- const result = keyValues && (await this.findByPk(keyValues, options));
157
- if (!result)
148
+ if (options.returning && !keyValues)
158
149
  throw new Error('Unable to insert new row');
159
- await this._emit('after-create', result, options);
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 _findAll(options) {
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 FindCommand.execute({
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 _findByPk(keyValue, options) {
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 _destroy(keyValue, options) {
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
- const result = !!(await DestroyCommand.execute({
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 _destroyAll(options) {
235
- await this._emit('before-destroy-all', options);
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
- async _updateByPk(keyValue, values, options) {
217
+ async _update(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
- async _updateAll(values, options) {
238
+ async _updateMany(values, options) {
269
239
  if (!values)
270
240
  throw new TypeError('You must provide values');
271
- await this._emit('before-update-all', values, options);
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.7.0",
4
+ "version": "4.8.2",
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.7.0"
49
+ "@sqb/builder": "^4.8.2"
50
50
  },
51
51
  "type": "module",
52
52
  "types": "esm/index.d.ts",