@itrocks/mysql 0.0.21 → 0.0.22

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.d.ts CHANGED
@@ -30,6 +30,7 @@ export declare class Mysql extends DataSource {
30
30
  database: string;
31
31
  };
32
32
  connection?: Connection;
33
+ saveQueue: WeakMap<object, Promise<void | Entity<object>>>;
33
34
  constructor(config: {
34
35
  host: string;
35
36
  user: string;
@@ -51,6 +52,7 @@ export declare class Mysql extends DataSource {
51
52
  }>[]>;
52
53
  readCollectionIds<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Identifier[]>;
53
54
  readMultiple<T extends object>(type: Type<T>, ids: Identifier[]): Promise<Entity<T>[]>;
55
+ runSerialized<T extends object>(object: MayEntity<T>, task: () => Promise<Entity<T>>): Promise<Entity<T>>;
54
56
  save<T extends object>(object: MayEntity<T>): Promise<Entity<T>>;
55
57
  saveCollection<T extends object>(object: Entity<T>, property: KeyOf<T>, value: (Identifier | MayEntity)[]): Promise<void>;
56
58
  saveComponents<T extends object>(object: Entity<T>, property: KeyOf<T>, components: (Identifier | MayEntity)[]): Promise<void>;
package/cjs/mysql.js CHANGED
@@ -41,6 +41,7 @@ function mysqlDependsOn(dependencies) {
41
41
  class Mysql extends storage_1.DataSource {
42
42
  config;
43
43
  connection;
44
+ saveQueue = new WeakMap();
44
45
  constructor(config) {
45
46
  super();
46
47
  this.config = config;
@@ -200,10 +201,18 @@ class Mysql extends storage_1.DataSource {
200
201
  const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
201
202
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
202
203
  }
204
+ async runSerialized(object, task) {
205
+ const prev = this.saveQueue.get(object) || Promise.resolve();
206
+ const next = prev.then(task, task);
207
+ this.saveQueue.set(object, next.then(() => { this.saveQueue.delete(object); }, () => { this.saveQueue.delete(object); }));
208
+ return next;
209
+ }
203
210
  async save(object) {
204
- return this.isObjectConnected(object)
205
- ? this.update(object)
206
- : this.insert(object);
211
+ return this.runSerialized(object, async () => {
212
+ return this.isObjectConnected(object)
213
+ ? this.update(object)
214
+ : this.insert(object);
215
+ });
207
216
  }
208
217
  async saveCollection(object, property, value) {
209
218
  if (property.endsWith('Ids')) {
package/esm/mysql.d.ts CHANGED
@@ -30,6 +30,7 @@ export declare class Mysql extends DataSource {
30
30
  database: string;
31
31
  };
32
32
  connection?: Connection;
33
+ saveQueue: WeakMap<object, Promise<void | Entity<object>>>;
33
34
  constructor(config: {
34
35
  host: string;
35
36
  user: string;
@@ -51,6 +52,7 @@ export declare class Mysql extends DataSource {
51
52
  }>[]>;
52
53
  readCollectionIds<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Identifier[]>;
53
54
  readMultiple<T extends object>(type: Type<T>, ids: Identifier[]): Promise<Entity<T>[]>;
55
+ runSerialized<T extends object>(object: MayEntity<T>, task: () => Promise<Entity<T>>): Promise<Entity<T>>;
54
56
  save<T extends object>(object: MayEntity<T>): Promise<Entity<T>>;
55
57
  saveCollection<T extends object>(object: Entity<T>, property: KeyOf<T>, value: (Identifier | MayEntity)[]): Promise<void>;
56
58
  saveComponents<T extends object>(object: Entity<T>, property: KeyOf<T>, components: (Identifier | MayEntity)[]): Promise<void>;
package/esm/mysql.js CHANGED
@@ -36,6 +36,7 @@ export function mysqlDependsOn(dependencies) {
36
36
  export class Mysql extends DataSource {
37
37
  config;
38
38
  connection;
39
+ saveQueue = new WeakMap();
39
40
  constructor(config) {
40
41
  super();
41
42
  this.config = config;
@@ -195,10 +196,18 @@ export class Mysql extends DataSource {
195
196
  const rows = await connection.query('SELECT ' + propertiesSql + ' FROM `' + depends.storeOf(type) + '` WHERE id IN (' + questionMarks + ')', ids);
196
197
  return Promise.all(rows.map(row => this.valuesFromDb(row, type)));
197
198
  }
199
+ async runSerialized(object, task) {
200
+ const prev = this.saveQueue.get(object) || Promise.resolve();
201
+ const next = prev.then(task, task);
202
+ this.saveQueue.set(object, next.then(() => { this.saveQueue.delete(object); }, () => { this.saveQueue.delete(object); }));
203
+ return next;
204
+ }
198
205
  async save(object) {
199
- return this.isObjectConnected(object)
200
- ? this.update(object)
201
- : this.insert(object);
206
+ return this.runSerialized(object, async () => {
207
+ return this.isObjectConnected(object)
208
+ ? this.update(object)
209
+ : this.insert(object);
210
+ });
202
211
  }
203
212
  async saveCollection(object, property, value) {
204
213
  if (property.endsWith('Ids')) {
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.21"
62
+ "version": "0.0.22"
63
63
  }