@ddd-ts-kit/knex 0.102.0 → 0.103.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/README.fr.md CHANGED
@@ -1,5 +1,5 @@
1
1
  [English](README.md) · **Français**
2
- <!-- translation-of: README.md sha256:b7ff60f1d479a493 -->
2
+ <!-- translation-of: README.md sha256:1c144cd7c3e0fb65 -->
3
3
 
4
4
  # @ddd-ts-kit/knex
5
5
 
@@ -386,7 +386,10 @@ n'a écrit aucune ligne, et fait avancer `invoice.version` sinon. `insertIgnorin
386
386
  sauf si la clé existe, et dit s'il l'a fait ; contrairement à une violation d'unicité attrapée, il
387
387
  laisse utilisable la transaction PostgreSQL englobante, et il cache le seul endroit où les
388
388
  dialectes diffèrent (`RETURNING` sur PostgreSQL et SQLite, le nombre de lignes affectées sur
389
- MySQL).
389
+ MySQL). Il n'ignore qu'un conflit sur `conflict` (l'id) : une ligne qui prend une valeur qu'une
390
+ autre ligne détient sous une contrainte d'unicité (un e-mail) est refusée par la base, et
391
+ `optimisticUpdate` lève `UniqueViolationError` (code `CONFLICT`, 409), pas une mise à jour
392
+ perdue ; sur MySQL, qui n'a pas de cible de conflit et dont `INSERT IGNORE` écarte aussi cette ligne, l'utilitaire demande si une ligne détient la clé quand rien n'a été écrit, et lève `UniqueViolationError` quand aucune ne la détient.
390
393
 
391
394
  ![Deux transactions chargent la facture inv-1 à la version 3 ; la première sauvegarde, sa mise à jour trouve la version 3 et la ligne passe à la version 4 ; la seconde sauvegarde ensuite, sa mise à jour attend toujours la version 3, n'écrit aucune ligne, et optimisticUpdate lève ConcurrencyError, code CONCURRENCY_CONFLICT](https://raw.githubusercontent.com/gbourgeat/ddd-ts-kit/main/docs/assets/diagrams/persistence-optimistic-concurrency.fr.svg)
392
395
 
package/README.md CHANGED
@@ -371,7 +371,10 @@ In the explicit style, without the decorator, the same binding is one line of
371
371
  statement wrote no row, and moves `invoice.version` on otherwise. `insertIgnoringConflict`
372
372
  inserts unless the key exists and says whether it did; unlike a caught unique violation it
373
373
  leaves the surrounding PostgreSQL transaction usable, and it hides the one place where the
374
- dialects differ (`RETURNING` on PostgreSQL and SQLite, the affected-row count on MySQL).
374
+ dialects differ (`RETURNING` on PostgreSQL and SQLite, the affected-row count on MySQL). It
375
+ ignores a conflict on `conflict` alone (the id): a row that takes a value another row holds
376
+ under a unique constraint (an email) is refused by the database, and `optimisticUpdate` raises
377
+ `UniqueViolationError` (code `CONFLICT`, 409), not a lost update; on MySQL, which has no conflict target and whose `INSERT IGNORE` skips that row too, the helper asks whether a row holds the key when nothing was written, and raises `UniqueViolationError` when none does.
375
378
 
376
379
  ![Two transactions load invoice inv-1 at version 3; the first saves, its update matches version 3 and the row moves to version 4; the second saves afterwards, its update still expects version 3, writes no row, and optimisticUpdate throws ConcurrencyError, code CONCURRENCY_CONFLICT](https://raw.githubusercontent.com/gbourgeat/ddd-ts-kit/main/docs/assets/diagrams/persistence-optimistic-concurrency.svg)
377
380
 
@@ -9,6 +9,12 @@ export interface IgnoringInsert {
9
9
  * catching the unique violation, it leaves the surrounding transaction usable on PostgreSQL.
10
10
  * It serves the processed-event store and the first save of an aggregate alike.
11
11
  *
12
+ * Only a conflict on `conflict` is ignored, on PostgreSQL and SQLite: a row that takes a value
13
+ * another row holds under another unique constraint (an email) is refused by the database, as
14
+ * any statement would be. MySQL has no conflict target and its `INSERT IGNORE` skips that row
15
+ * too: when it wrote nothing, one query asks whether a row holds the key, and when none does,
16
+ * a `UniqueViolationError` is raised, as the other databases answer.
17
+ *
12
18
  * `table` is the query builder of the target table, built on `executor`:
13
19
  * `insertIgnoringConflict(db, db('invoices').withSchema('billing'), { row, conflict: 'id' })`.
14
20
  */
@@ -1 +1 @@
1
- {"version":3,"file":"insert-ignoring-conflict.d.ts","sourceRoot":"","sources":["../../src/sql/insert-ignoring-conflict.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAGjC,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAChD,+DAA+D;IAC/D,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;CAC/C;AAED;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAC1C,QAAQ,EAAE,IAAI,EACd,KAAK,EAAE,IAAI,CAAC,YAAY,EACxB,EAAE,GAAG,EAAE,QAAQ,EAAE,EAAE,cAAc,GAChC,OAAO,CAAC,OAAO,CAAC,CAYlB"}
1
+ {"version":3,"file":"insert-ignoring-conflict.d.ts","sourceRoot":"","sources":["../../src/sql/insert-ignoring-conflict.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAIjC,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAChD,+DAA+D;IAC/D,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;CAC/C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,sBAAsB,CAC1C,QAAQ,EAAE,IAAI,EACd,KAAK,EAAE,IAAI,CAAC,YAAY,EACxB,EAAE,GAAG,EAAE,QAAQ,EAAE,EAAE,cAAc,GAChC,OAAO,CAAC,OAAO,CAAC,CAclB"}
@@ -1,22 +1,40 @@
1
+ import { UniqueViolationError } from '@ddd-ts-kit/kernel';
1
2
  import { dialectOf } from './dialect-of.js';
2
3
  /**
3
4
  * Inserts one row unless its key already exists, and says whether it did insert. Unlike
4
5
  * catching the unique violation, it leaves the surrounding transaction usable on PostgreSQL.
5
6
  * It serves the processed-event store and the first save of an aggregate alike.
6
7
  *
8
+ * Only a conflict on `conflict` is ignored, on PostgreSQL and SQLite: a row that takes a value
9
+ * another row holds under another unique constraint (an email) is refused by the database, as
10
+ * any statement would be. MySQL has no conflict target and its `INSERT IGNORE` skips that row
11
+ * too: when it wrote nothing, one query asks whether a row holds the key, and when none does,
12
+ * a `UniqueViolationError` is raised, as the other databases answer.
13
+ *
7
14
  * `table` is the query builder of the target table, built on `executor`:
8
15
  * `insertIgnoringConflict(db, db('invoices').withSchema('billing'), { row, conflict: 'id' })`.
9
16
  */
10
17
  export async function insertIgnoringConflict(executor, table, { row, conflict }) {
11
18
  const columns = typeof conflict === 'string' ? [conflict] : [...conflict];
19
+ // Before the insert, which changes the builder: the lookup of the key on MySQL reads the same table.
20
+ const lookup = table.clone();
12
21
  const insert = table.insert(row).onConflict(columns).ignore();
13
22
  if (dialectOf(executor) === 'mysql') {
14
- return wroteOnMySql(executor, insert);
23
+ return (await wroteOnMySql(executor, insert)) || keyTaken(lookup, row, columns);
15
24
  }
16
25
  // RETURNING yields the row only when it was written.
17
26
  const inserted = await insert.returning(columns[0] ?? '*');
18
27
  return inserted.length === 1;
19
28
  }
29
+ /** Whether a row holds the key of `row`; when none does, the row was skipped for another unique value. */
30
+ async function keyTaken(lookup, row, columns) {
31
+ const key = Object.fromEntries(columns.map((column) => [column, row[column]]));
32
+ const holder = await lookup.where(key).first(columns[0] ?? '*');
33
+ if (holder === undefined) {
34
+ throw new UniqueViolationError(columns.map((column) => String(row[column])).join(':'));
35
+ }
36
+ return false;
37
+ }
20
38
  // No RETURNING in MySQL and knex only reports the insert id: the driver's own result header
21
39
  // is the one place that says whether INSERT IGNORE wrote the row.
22
40
  async function wroteOnMySql(executor, insert) {
@@ -1 +1 @@
1
- {"version":3,"file":"insert-ignoring-conflict.js","sourceRoot":"","sources":["../../src/sql/insert-ignoring-conflict.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAQzC;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,QAAc,EACd,KAAwB,EACxB,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CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC;IAEzE,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,MAAM,IAAI,oBAAoB,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IACzF,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED,4FAA4F;AAC5F,kEAAkE;AAClE,KAAK,UAAU,YAAY,CAAC,QAAc,EAAE,MAAyB;IACnE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC,KAAK,EAAE,CAAC,QAAQ,EAAE,CAAC;IACpD,MAAM,MAAM,GAAY,MAAM,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IAC1D,MAAM,MAAM,GAAY,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAEnE,OAAO,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,IAAI,cAAc,IAAI,MAAM,IAAI,MAAM,CAAC,YAAY,KAAK,CAAC,CAAC;AAChH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ddd-ts-kit/knex",
3
- "version": "0.102.0",
3
+ "version": "0.103.0",
4
4
  "description": "Knex adapters for the kernel ports: unit of work, transactional outbox store, processed-event store, PostgreSQL event store, background job store, process store.",
5
5
  "keywords": [
6
6
  "ddd",
@@ -38,16 +38,16 @@
38
38
  "dist"
39
39
  ],
40
40
  "dependencies": {
41
- "@ddd-ts-kit/kernel": "0.102.0",
42
- "@ddd-ts-kit/events": "0.102.0",
43
- "@ddd-ts-kit/scheduling": "0.102.0"
41
+ "@ddd-ts-kit/kernel": "0.103.0",
42
+ "@ddd-ts-kit/events": "0.103.0",
43
+ "@ddd-ts-kit/scheduling": "0.103.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "knex": "^3.0.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "knex": "^3.3.0",
50
- "@ddd-ts-kit/testing": "0.102.0",
50
+ "@ddd-ts-kit/testing": "0.103.0",
51
51
  "pg": "^8.23.1",
52
52
  "@types/pg": "^8.23.1",
53
53
  "@electric-sql/pglite": "0.5.11",