@sqb/connect 4.6.3 → 4.7.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.
@@ -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
- const master = source;
95
- const detail = target;
96
- let masterKey = sourceKey || '';
97
- let detailKey = targetKey || '';
98
- if (!detailKey) {
99
- const primaryIndexColumns = entity_metadata_js_1.EntityMetadata.getPrimaryIndexColumns(detail);
100
- detailKey = primaryIndexColumns && primaryIndexColumns.length === 1 ?
101
- primaryIndexColumns[0].name : 'id';
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
- if (!masterKey) {
104
- // snake-case
105
- masterKey = detail.name[0].toLowerCase() + detail.name.substring(1) + '_' + detailKey;
106
- if (!entity_metadata_js_1.EntityMetadata.getColumnField(master, masterKey))
107
- masterKey = (0, putil_varhelpers_1.camelCase)(masterKey);
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)
@@ -45,9 +45,9 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
45
45
  return this._count({ ...options, connection });
46
46
  }, options);
47
47
  }
48
- findAll(options) {
48
+ findByPk(keyValue, options) {
49
49
  return this._execute(async (connection) => {
50
- return this._findAll({ ...options, connection });
50
+ return this._findByPk(keyValue, { ...options, connection });
51
51
  }, options);
52
52
  }
53
53
  findOne(options) {
@@ -55,39 +55,81 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
55
55
  return this._findOne({ ...options, connection });
56
56
  }, options);
57
57
  }
58
- findByPk(keyValue, options) {
58
+ findMany(options) {
59
59
  return this._execute(async (connection) => {
60
- return this._findByPk(keyValue, { ...options, connection });
60
+ return this._findAll({ ...options, connection });
61
61
  }, options);
62
62
  }
63
- destroy(keyValue, options) {
63
+ deleteByPk(keyValue, options) {
64
64
  return this._execute(async (connection) => {
65
65
  return this._destroy(keyValue, { ...options, connection });
66
66
  }, options);
67
67
  }
68
- destroyAll(options) {
68
+ deleteMany(options) {
69
69
  return this._execute(async (connection) => {
70
70
  return this._destroyAll({ ...options, connection });
71
71
  }, options);
72
72
  }
73
- update(keyValue, values, options) {
73
+ updateByPk(keyValue, values, options) {
74
74
  return this._execute(async (connection) => {
75
75
  const opts = { ...options, connection };
76
- const keyValues = await this._update(keyValue, values, opts);
76
+ const keyValues = await this._updateByPk(keyValue, values, opts);
77
77
  if (keyValues)
78
78
  return this._findByPk(keyValues, opts);
79
79
  }, options);
80
80
  }
81
- updateOnly(keyValue, values, options) {
81
+ updateByPkOnly(keyValue, values, options) {
82
82
  return this._execute(async (connection) => {
83
- return !!(await this._update(keyValue, values, { ...options, connection }));
83
+ return !!(await this._updateByPk(keyValue, values, { ...options, connection }));
84
84
  }, options);
85
85
  }
86
- updateAll(values, options) {
86
+ updateMany(values, options) {
87
87
  return this._execute(async (connection) => {
88
88
  return this._updateAll(values, { ...options, connection });
89
89
  });
90
90
  }
91
+ /**
92
+ *
93
+ * @deprecated
94
+ */
95
+ destroy(keyValue, options) {
96
+ return this.deleteOne(keyValue, options);
97
+ }
98
+ /**
99
+ *
100
+ * @deprecated
101
+ */
102
+ destroyAll(options) {
103
+ return this.deleteMany(options);
104
+ }
105
+ /**
106
+ *
107
+ * @deprecated
108
+ */
109
+ findAll(options) {
110
+ return this.findMany(options);
111
+ }
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
+ /**
127
+ *
128
+ * @deprecated
129
+ */
130
+ updateAll(values, options) {
131
+ return this.updateMany(values, options);
132
+ }
91
133
  async _execute(fn, opts) {
92
134
  let connection = opts?.connection;
93
135
  if (!connection && this._executor instanceof sqb_connection_js_1.SqbConnection)
@@ -203,7 +245,7 @@ class Repository extends (0, strict_typed_events_1.TypedEventEmitterClass)(stric
203
245
  await this._emit('after-destroy-all', rowsAffected, options);
204
246
  return rowsAffected;
205
247
  }
206
- async _update(keyValue, values, options) {
248
+ async _updateByPk(keyValue, values, options) {
207
249
  if (!values)
208
250
  throw new TypeError('You must provide values');
209
251
  await this._emit('before-update', keyValue, values, options);
@@ -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
- const master = source;
92
- const detail = target;
93
- let masterKey = sourceKey || '';
94
- let detailKey = targetKey || '';
95
- if (!detailKey) {
96
- const primaryIndexColumns = EntityMetadata.getPrimaryIndexColumns(detail);
97
- detailKey = primaryIndexColumns && primaryIndexColumns.length === 1 ?
98
- primaryIndexColumns[0].name : 'id';
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
- if (!masterKey) {
101
- // snake-case
102
- masterKey = detail.name[0].toLowerCase() + detail.name.substring(1) + '_' + detailKey;
103
- if (!EntityMetadata.getColumnField(master, masterKey))
104
- masterKey = camelCase(masterKey);
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)
@@ -79,13 +79,43 @@ export declare class Repository<T> extends Repository_base {
79
79
  createOnly(values: EntityInput<T>, options?: Repository.CreateOptions): Promise<void>;
80
80
  exists(keyValue: any | Record<string, any>, options?: Repository.ExistsOptions): Promise<boolean>;
81
81
  count(options?: Repository.CountOptions): Promise<number>;
82
- findAll(options?: Repository.FindAllOptions): Promise<EntityOutput<T>[]>;
83
- findOne(options?: Repository.FindOneOptions): Promise<EntityOutput<T> | undefined>;
84
82
  findByPk(keyValue: any | Record<string, any>, options?: Repository.GetOptions): Promise<EntityOutput<T> | undefined>;
83
+ 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
+ */
85
94
  destroy(keyValue: any | Record<string, any>, options?: Repository.DestroyOptions): Promise<boolean>;
95
+ /**
96
+ *
97
+ * @deprecated
98
+ */
86
99
  destroyAll(options?: Repository.DestroyOptions): Promise<number>;
100
+ /**
101
+ *
102
+ * @deprecated
103
+ */
104
+ findAll(options?: Repository.FindAllOptions): Promise<EntityOutput<T>[]>;
105
+ /**
106
+ *
107
+ * @deprecated
108
+ */
87
109
  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<EntityOutput<T>>;
110
+ /**
111
+ *
112
+ * @deprecated
113
+ */
114
+ updateOnly(keyValue: any | Record<string, any>, values: EntityInput<T>, options?: Repository.UpdateOptions): Promise<boolean>;
115
+ /**
116
+ *
117
+ * @deprecated
118
+ */
89
119
  updateAll(values: EntityInput<T>, options?: Repository.UpdateAllOptions): Promise<number>;
90
120
  protected _execute(fn: TransactionFunction, opts?: Repository.CommandOptions): Promise<any>;
91
121
  protected _create(values: EntityInput<T>, options: Repository.CreateOptions & {
@@ -115,7 +145,7 @@ export declare class Repository<T> extends Repository_base {
115
145
  protected _destroyAll(options: Repository.DestroyOptions & {
116
146
  connection: SqbConnection;
117
147
  }): Promise<number>;
118
- protected _update(keyValue: any | Record<string, any>, values: EntityInput<T>, options: Repository.UpdateOptions & {
148
+ protected _updateByPk(keyValue: any | Record<string, any>, values: EntityInput<T>, options: Repository.UpdateOptions & {
119
149
  connection: SqbConnection;
120
150
  }): Promise<Record<string, any> | undefined>;
121
151
  protected _updateAll(values: EntityInput<T>, options: Repository.UpdateAllOptions & {
@@ -42,9 +42,9 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
42
42
  return this._count({ ...options, connection });
43
43
  }, options);
44
44
  }
45
- findAll(options) {
45
+ findByPk(keyValue, options) {
46
46
  return this._execute(async (connection) => {
47
- return this._findAll({ ...options, connection });
47
+ return this._findByPk(keyValue, { ...options, connection });
48
48
  }, options);
49
49
  }
50
50
  findOne(options) {
@@ -52,39 +52,81 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
52
52
  return this._findOne({ ...options, connection });
53
53
  }, options);
54
54
  }
55
- findByPk(keyValue, options) {
55
+ findMany(options) {
56
56
  return this._execute(async (connection) => {
57
- return this._findByPk(keyValue, { ...options, connection });
57
+ return this._findAll({ ...options, connection });
58
58
  }, options);
59
59
  }
60
- destroy(keyValue, options) {
60
+ deleteByPk(keyValue, options) {
61
61
  return this._execute(async (connection) => {
62
62
  return this._destroy(keyValue, { ...options, connection });
63
63
  }, options);
64
64
  }
65
- destroyAll(options) {
65
+ deleteMany(options) {
66
66
  return this._execute(async (connection) => {
67
67
  return this._destroyAll({ ...options, connection });
68
68
  }, options);
69
69
  }
70
- update(keyValue, values, options) {
70
+ updateByPk(keyValue, values, options) {
71
71
  return this._execute(async (connection) => {
72
72
  const opts = { ...options, connection };
73
- const keyValues = await this._update(keyValue, values, opts);
73
+ const keyValues = await this._updateByPk(keyValue, values, opts);
74
74
  if (keyValues)
75
75
  return this._findByPk(keyValues, opts);
76
76
  }, options);
77
77
  }
78
- updateOnly(keyValue, values, options) {
78
+ updateByPkOnly(keyValue, values, options) {
79
79
  return this._execute(async (connection) => {
80
- return !!(await this._update(keyValue, values, { ...options, connection }));
80
+ return !!(await this._updateByPk(keyValue, values, { ...options, connection }));
81
81
  }, options);
82
82
  }
83
- updateAll(values, options) {
83
+ updateMany(values, options) {
84
84
  return this._execute(async (connection) => {
85
85
  return this._updateAll(values, { ...options, connection });
86
86
  });
87
87
  }
88
+ /**
89
+ *
90
+ * @deprecated
91
+ */
92
+ destroy(keyValue, options) {
93
+ return this.deleteOne(keyValue, options);
94
+ }
95
+ /**
96
+ *
97
+ * @deprecated
98
+ */
99
+ destroyAll(options) {
100
+ return this.deleteMany(options);
101
+ }
102
+ /**
103
+ *
104
+ * @deprecated
105
+ */
106
+ findAll(options) {
107
+ return this.findMany(options);
108
+ }
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
+ /**
124
+ *
125
+ * @deprecated
126
+ */
127
+ updateAll(values, options) {
128
+ return this.updateMany(values, options);
129
+ }
88
130
  async _execute(fn, opts) {
89
131
  let connection = opts?.connection;
90
132
  if (!connection && this._executor instanceof SqbConnection)
@@ -200,7 +242,7 @@ export class Repository extends TypedEventEmitterClass(AsyncEventEmitter) {
200
242
  await this._emit('after-destroy-all', rowsAffected, options);
201
243
  return rowsAffected;
202
244
  }
203
- async _update(keyValue, values, options) {
245
+ async _updateByPk(keyValue, values, options) {
204
246
  if (!values)
205
247
  throw new TypeError('You must provide values');
206
248
  await this._emit('before-update', keyValue, values, options);
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.6.3",
4
+ "version": "4.7.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.6.3"
49
+ "@sqb/builder": "^4.7.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
+ }