@itrocks/mysql 0.0.19 → 0.0.21

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/mysql.js CHANGED
@@ -6,6 +6,8 @@ exports.mysqlDependsOn = mysqlDependsOn;
6
6
  const class_type_1 = require("@itrocks/class-type");
7
7
  const class_type_2 = require("@itrocks/class-type");
8
8
  const class_type_3 = require("@itrocks/class-type");
9
+ const class_type_4 = require("@itrocks/class-type");
10
+ const composition_1 = require("@itrocks/composition");
9
11
  const property_type_1 = require("@itrocks/property-type");
10
12
  const reflect_1 = require("@itrocks/reflect");
11
13
  const reflect_2 = require("@itrocks/reflect");
@@ -24,7 +26,7 @@ const depends = {
24
26
  QueryFunction: class {
25
27
  },
26
28
  queryFunctionCall: () => [undefined, ' = ?'],
27
- storeOf: target => (0, class_type_3.typeOf)(target).name.toLowerCase()
29
+ storeOf: target => (0, class_type_4.typeOf)(target).name.toLowerCase()
28
30
  };
29
31
  function joinTableName(object1, object2) {
30
32
  if (typeof object1 !== 'string')
@@ -129,13 +131,13 @@ class Mysql extends storage_1.DataSource {
129
131
  const propertyType = property.type;
130
132
  if (propertyType instanceof property_type_1.CollectionType)
131
133
  continue;
132
- const propertyName = ((0, class_type_2.isAnyType)(propertyType.type) && depends.storeOf(propertyType.type))
134
+ const propertyName = ((0, class_type_3.isAnyType)(propertyType.type) && depends.storeOf(propertyType.type))
133
135
  ? property.name + 'Id'
134
136
  : property.name;
135
137
  const columnName = depends.columnOf(propertyName);
136
138
  sql.push((columnName.length !== propertyName.length)
137
- ? columnName + ' ' + propertyName
138
- : propertyName);
139
+ ? ('`' + columnName + '` `' + propertyName + '`')
140
+ : ('`' + propertyName + '`'));
139
141
  }
140
142
  return sql.join(', ');
141
143
  }
@@ -204,15 +206,59 @@ class Mysql extends storage_1.DataSource {
204
206
  : this.insert(object);
205
207
  }
206
208
  async saveCollection(object, property, value) {
207
- return depends.componentOf(object, property.endsWith('Ids') ? property.slice(0, -3) : property)
209
+ if (property.endsWith('Ids')) {
210
+ property = property.slice(0, -3);
211
+ }
212
+ return depends.componentOf(object, property)
208
213
  ? this.saveComponents(object, property, value)
209
214
  : this.saveLinks(object, property, value);
210
215
  }
211
216
  async saveComponents(object, property, components) {
212
- // TODO
217
+ const connection = this.connection ?? await this.connect();
218
+ const propertyType = new reflect_2.ReflectProperty(object, property).collectionType.elementType.type;
219
+ const stored = await this.readCollectionIds(object, property, propertyType);
220
+ const saved = new Array;
221
+ let compositeProperty;
222
+ for (const component of components) {
223
+ if (typeof component !== 'object') {
224
+ saved.push(component);
225
+ continue;
226
+ }
227
+ if (compositeProperty === undefined) {
228
+ const objectType = (0, class_type_4.typeOf)(object);
229
+ for (const candidate of new reflect_1.ReflectClass(component).properties) {
230
+ if (!(0, composition_1.compositeOf)(component, candidate.name))
231
+ continue;
232
+ const candidateType = candidate.type.type;
233
+ if (!(0, class_type_3.isAnyType)(candidateType))
234
+ continue;
235
+ if (!(0, class_type_1.inherits)(objectType, candidateType))
236
+ continue;
237
+ compositeProperty = candidate;
238
+ break;
239
+ }
240
+ }
241
+ if (compositeProperty) {
242
+ // @ts-ignore TS2322 Don't understand this error
243
+ component[compositeProperty.name] = object;
244
+ }
245
+ saved.push((await this.save(component)).id);
246
+ }
247
+ let componentTable;
248
+ for (const storedId of stored) {
249
+ if (saved.includes(storedId))
250
+ continue;
251
+ if (!componentTable) {
252
+ componentTable = depends.storeOf(propertyType);
253
+ if (!componentTable) {
254
+ throw 'Missing @Store on type ' + propertyType.name
255
+ + ' used by @Component ' + new reflect_1.ReflectClass(object).name + '.' + property;
256
+ }
257
+ }
258
+ await connection.query('DELETE FROM `' + componentTable + '` WHERE id = ?', [storedId]);
259
+ }
213
260
  }
214
261
  async saveLinks(object, property, links) {
215
- property = property.endsWith('Ids') ? property.slice(0, -3) : property;
216
262
  const connection = this.connection ?? await this.connect();
217
263
  const objectTable = depends.storeOf(object);
218
264
  const propertyType = new reflect_2.ReflectProperty(object, property).collectionType.elementType.type;
@@ -222,7 +268,7 @@ class Mysql extends storage_1.DataSource {
222
268
  const objectColumn = depends.columnOf(objectTable) + '_id';
223
269
  const objectId = object.id;
224
270
  const stored = await this.readCollectionIds(object, property, propertyType);
225
- const saved = [];
271
+ const saved = new Array;
226
272
  for (const link of links) {
227
273
  const linkId = (typeof link === 'object')
228
274
  ? (this.isObjectConnected(link) ? link.id : (await this.save(link)).id)
@@ -304,7 +350,7 @@ class Mysql extends storage_1.DataSource {
304
350
  deferred.push((object) => this.saveCollection(object, property, value));
305
351
  continue;
306
352
  }
307
- if ((0, class_type_1.isAnyFunction)(value)) {
353
+ if ((0, class_type_2.isAnyFunction)(value)) {
308
354
  deferred.push(value);
309
355
  continue;
310
356
  }
package/esm/mysql.js CHANGED
@@ -1,6 +1,8 @@
1
+ import { inherits } from '@itrocks/class-type';
1
2
  import { isAnyFunction } from '@itrocks/class-type';
2
3
  import { isAnyType } from '@itrocks/class-type';
3
4
  import { typeOf } from '@itrocks/class-type';
5
+ import { compositeOf } from '@itrocks/composition';
4
6
  import { CollectionType } from '@itrocks/property-type';
5
7
  import { ReflectClass } from '@itrocks/reflect';
6
8
  import { ReflectProperty } from '@itrocks/reflect';
@@ -129,8 +131,8 @@ export class Mysql extends DataSource {
129
131
  : property.name;
130
132
  const columnName = depends.columnOf(propertyName);
131
133
  sql.push((columnName.length !== propertyName.length)
132
- ? columnName + ' ' + propertyName
133
- : propertyName);
134
+ ? ('`' + columnName + '` `' + propertyName + '`')
135
+ : ('`' + propertyName + '`'));
134
136
  }
135
137
  return sql.join(', ');
136
138
  }
@@ -199,15 +201,59 @@ export class Mysql extends DataSource {
199
201
  : this.insert(object);
200
202
  }
201
203
  async saveCollection(object, property, value) {
202
- return depends.componentOf(object, property.endsWith('Ids') ? property.slice(0, -3) : property)
204
+ if (property.endsWith('Ids')) {
205
+ property = property.slice(0, -3);
206
+ }
207
+ return depends.componentOf(object, property)
203
208
  ? this.saveComponents(object, property, value)
204
209
  : this.saveLinks(object, property, value);
205
210
  }
206
211
  async saveComponents(object, property, components) {
207
- // TODO
212
+ const connection = this.connection ?? await this.connect();
213
+ const propertyType = new ReflectProperty(object, property).collectionType.elementType.type;
214
+ const stored = await this.readCollectionIds(object, property, propertyType);
215
+ const saved = new Array;
216
+ let compositeProperty;
217
+ for (const component of components) {
218
+ if (typeof component !== 'object') {
219
+ saved.push(component);
220
+ continue;
221
+ }
222
+ if (compositeProperty === undefined) {
223
+ const objectType = typeOf(object);
224
+ for (const candidate of new ReflectClass(component).properties) {
225
+ if (!compositeOf(component, candidate.name))
226
+ continue;
227
+ const candidateType = candidate.type.type;
228
+ if (!isAnyType(candidateType))
229
+ continue;
230
+ if (!inherits(objectType, candidateType))
231
+ continue;
232
+ compositeProperty = candidate;
233
+ break;
234
+ }
235
+ }
236
+ if (compositeProperty) {
237
+ // @ts-ignore TS2322 Don't understand this error
238
+ component[compositeProperty.name] = object;
239
+ }
240
+ saved.push((await this.save(component)).id);
241
+ }
242
+ let componentTable;
243
+ for (const storedId of stored) {
244
+ if (saved.includes(storedId))
245
+ continue;
246
+ if (!componentTable) {
247
+ componentTable = depends.storeOf(propertyType);
248
+ if (!componentTable) {
249
+ throw 'Missing @Store on type ' + propertyType.name
250
+ + ' used by @Component ' + new ReflectClass(object).name + '.' + property;
251
+ }
252
+ }
253
+ await connection.query('DELETE FROM `' + componentTable + '` WHERE id = ?', [storedId]);
254
+ }
208
255
  }
209
256
  async saveLinks(object, property, links) {
210
- property = property.endsWith('Ids') ? property.slice(0, -3) : property;
211
257
  const connection = this.connection ?? await this.connect();
212
258
  const objectTable = depends.storeOf(object);
213
259
  const propertyType = new ReflectProperty(object, property).collectionType.elementType.type;
@@ -217,7 +263,7 @@ export class Mysql extends DataSource {
217
263
  const objectColumn = depends.columnOf(objectTable) + '_id';
218
264
  const objectId = object.id;
219
265
  const stored = await this.readCollectionIds(object, property, propertyType);
220
- const saved = [];
266
+ const saved = new Array;
221
267
  for (const link of links) {
222
268
  const linkId = (typeof link === 'object')
223
269
  ? (this.isObjectConnected(link) ? link.id : (await this.save(link)).id)
package/package.json CHANGED
@@ -59,5 +59,5 @@
59
59
  "build:esm": "tsc -p tsconfig.esm.json"
60
60
  },
61
61
  "types": "./esm/mysql.d.ts",
62
- "version": "0.0.19"
62
+ "version": "0.0.21"
63
63
  }