@ddd-ts-kit/drizzle 0.102.0 → 0.104.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:262bc6a4dfeddf99 -->
2
+ <!-- translation-of: README.md sha256:a710655453f3ce8a -->
3
3
 
4
4
  # @ddd-ts-kit/drizzle
5
5
 
@@ -241,7 +241,7 @@ import { and, eq } from 'drizzle-orm';
241
241
  import { integer, text, type PgQueryResultHKT } from 'drizzle-orm/pg-core';
242
242
  import { AggregateRoot } from '@ddd-ts-kit/kernel';
243
243
  import { Repository } from '@ddd-ts-kit/kernel/declarations';
244
- import { contextSchema, DrizzleContext, optimisticUpdate } from '@ddd-ts-kit/drizzle/postgres';
244
+ import { contextSchema, DrizzleContext, optimisticUpdate, primaryKeyOf } from '@ddd-ts-kit/drizzle/postgres';
245
245
 
246
246
  // L'agrégat et son port de l'exemple précédent, réduits à leur forme.
247
247
  type OrderStatus = 'open' | 'placed';
@@ -299,7 +299,7 @@ export class DrizzleOrderRepository implements OrderRepository {
299
299
  const row = OrderMapper.toRow(order, next);
300
300
  const written =
301
301
  expected === 0
302
- ? await db.insert(orders).values(row).onConflictDoNothing().returning({ id: orders.id })
302
+ ? await db.insert(orders).values(row).onConflictDoNothing({ target: primaryKeyOf(orders) }).returning({ id: orders.id })
303
303
  : await db
304
304
  .update(orders)
305
305
  .set(row)
@@ -324,9 +324,17 @@ schéma relationnel (`drizzle(url, { schema })`), déclarez `Database` comme
324
324
 
325
325
  | `expected` | Instruction | Aucune ligne écrite signifie |
326
326
  |---|---|---|
327
- | `0` : jamais enregistré | `INSERT ... ON CONFLICT DO NOTHING` | l'id est déjà pris |
327
+ | `0` : jamais enregistré | `INSERT ... ON CONFLICT (id) DO NOTHING` | l'id est déjà pris |
328
328
  | `n` | `UPDATE ... SET version = n + 1 WHERE id = ... AND version = n` | une autre transaction l'a enregistré depuis sa lecture |
329
329
 
330
+ La cible du conflit est la clé primaire seule, telle que la table la déclare
331
+ (`onConflictDoNothing({ target: primaryKeyOf(orders) })`, l'id, ou chaque colonne d'une clé qui
332
+ porte le tenant) : sans elle,
333
+ PostgreSQL écarte aussi une ligne qui prend une valeur qu'une autre ligne détient sous une
334
+ contrainte d'unicité (un e-mail), et l'enregistrement se lit comme une mise à jour perdue.
335
+ Nommée, cette ligne est refusée par la base, et `optimisticUpdate` lève `UniqueViolationError`
336
+ (code `CONFLICT`, 409), comme pour une mise à jour qui prend une telle valeur.
337
+
330
338
  ## Colonnes d'audit, restriction au tenant et pages
331
339
 
332
340
  Les formes de persistance standard du noyau, sur drizzle. `auditColumns()` et `tenantColumn()`
@@ -351,6 +359,7 @@ import {
351
359
  DrizzleContext,
352
360
  optimisticUpdate,
353
361
  ownTenantCondition,
362
+ primaryKeyOf,
354
363
  tenantColumn,
355
364
  tenantCondition,
356
365
  } from '@ddd-ts-kit/drizzle/postgres';
@@ -430,7 +439,9 @@ export class DrizzleInvoiceRepository implements InvoiceRepository {
430
439
  // tenant_id vient du travail en cours, created_* et updated_* de son acteur et de la Clock
431
440
  const row = { ...state, ...tenantScopeOf(this.scope), ...stampCreation(this.scope, this.clock) };
432
441
 
433
- return this.database.executor().insert(invoices).values(row).onConflictDoNothing().returning({ id: invoices.id });
442
+ const insertion = this.database.executor().insert(invoices).values(row);
443
+
444
+ return insertion.onConflictDoNothing({ target: primaryKeyOf(invoices) }).returning({ id: invoices.id });
434
445
  }
435
446
 
436
447
  private update(state: VersionedInvoice, expected: number) {
package/README.md CHANGED
@@ -233,7 +233,7 @@ import { and, eq } from 'drizzle-orm';
233
233
  import { integer, text, type PgQueryResultHKT } from 'drizzle-orm/pg-core';
234
234
  import { AggregateRoot } from '@ddd-ts-kit/kernel';
235
235
  import { Repository } from '@ddd-ts-kit/kernel/declarations';
236
- import { contextSchema, DrizzleContext, optimisticUpdate } from '@ddd-ts-kit/drizzle/postgres';
236
+ import { contextSchema, DrizzleContext, optimisticUpdate, primaryKeyOf } from '@ddd-ts-kit/drizzle/postgres';
237
237
 
238
238
  // The aggregate and its port of the previous sample, reduced to their shape.
239
239
  type OrderStatus = 'open' | 'placed';
@@ -291,7 +291,7 @@ export class DrizzleOrderRepository implements OrderRepository {
291
291
  const row = OrderMapper.toRow(order, next);
292
292
  const written =
293
293
  expected === 0
294
- ? await db.insert(orders).values(row).onConflictDoNothing().returning({ id: orders.id })
294
+ ? await db.insert(orders).values(row).onConflictDoNothing({ target: primaryKeyOf(orders) }).returning({ id: orders.id })
295
295
  : await db
296
296
  .update(orders)
297
297
  .set(row)
@@ -316,9 +316,17 @@ with a relational schema (`drizzle(url, { schema })`), declare `Database` as
316
316
 
317
317
  | `expected` | Statement | No row written means |
318
318
  |---|---|---|
319
- | `0`: never saved | `INSERT ... ON CONFLICT DO NOTHING` | the id is taken |
319
+ | `0`: never saved | `INSERT ... ON CONFLICT (id) DO NOTHING` | the id is taken |
320
320
  | `n` | `UPDATE ... SET version = n + 1 WHERE id = ... AND version = n` | another transaction saved it since it was read |
321
321
 
322
+ The conflict target is the primary key alone, as the table declares it
323
+ (`onConflictDoNothing({ target: primaryKeyOf(orders) })`, the id, or every column of a key that
324
+ holds the tenant): without
325
+ it, PostgreSQL also skips a row that takes a value another row holds under a unique constraint
326
+ (an email), and the save reads as a lost update. Named, that row is refused by the database,
327
+ and `optimisticUpdate` raises `UniqueViolationError` (code `CONFLICT`, 409), as for an update
328
+ that takes such a value.
329
+
322
330
  ## Audit columns, tenant scoping and pages
323
331
 
324
332
  The standard persistence shapes of the kernel, on drizzle. `auditColumns()` and `tenantColumn()`
@@ -342,6 +350,7 @@ import {
342
350
  DrizzleContext,
343
351
  optimisticUpdate,
344
352
  ownTenantCondition,
353
+ primaryKeyOf,
345
354
  tenantColumn,
346
355
  tenantCondition,
347
356
  } from '@ddd-ts-kit/drizzle/postgres';
@@ -421,7 +430,9 @@ export class DrizzleInvoiceRepository implements InvoiceRepository {
421
430
  // tenant_id from the work in progress, created_* and updated_* from its actor and the Clock
422
431
  const row = { ...state, ...tenantScopeOf(this.scope), ...stampCreation(this.scope, this.clock) };
423
432
 
424
- return this.database.executor().insert(invoices).values(row).onConflictDoNothing().returning({ id: invoices.id });
433
+ const insertion = this.database.executor().insert(invoices).values(row);
434
+
435
+ return insertion.onConflictDoNothing({ target: primaryKeyOf(invoices) }).returning({ id: invoices.id });
425
436
  }
426
437
 
427
438
  private update(state: VersionedInvoice, expected: number) {
@@ -0,0 +1,9 @@
1
+ import { type PgColumn, type PgTable } from 'drizzle-orm/pg-core';
2
+ /**
3
+ * The columns of the primary key of `table`, as its definition declares them (`.primaryKey()`
4
+ * on a column, or `primaryKey({ columns })`): the target of the first save of an aggregate,
5
+ * `onConflictDoNothing({ target: primaryKeyOf(table) })`, so that a row that takes another
6
+ * unique value is refused by the database rather than skipped.
7
+ */
8
+ export declare function primaryKeyOf(table: PgTable): PgColumn[];
9
+ //# sourceMappingURL=primary-key-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"primary-key-of.d.ts","sourceRoot":"","sources":["../../../src/postgres/sql/primary-key-of.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,QAAQ,EAAE,KAAK,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAElF;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,QAAQ,EAAE,CASvD"}
@@ -0,0 +1,16 @@
1
+ import { getTableConfig } from 'drizzle-orm/pg-core';
2
+ /**
3
+ * The columns of the primary key of `table`, as its definition declares them (`.primaryKey()`
4
+ * on a column, or `primaryKey({ columns })`): the target of the first save of an aggregate,
5
+ * `onConflictDoNothing({ target: primaryKeyOf(table) })`, so that a row that takes another
6
+ * unique value is refused by the database rather than skipped.
7
+ */
8
+ export function primaryKeyOf(table) {
9
+ const { name, columns, primaryKeys } = getTableConfig(table);
10
+ const declared = primaryKeys[0]?.columns ?? columns.filter((column) => column.primary);
11
+ if (declared.length === 0) {
12
+ throw new TypeError(`The table ${name} declares no primary key: the first save has no key to ignore a conflict on`);
13
+ }
14
+ return declared;
15
+ }
16
+ //# sourceMappingURL=primary-key-of.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"primary-key-of.js","sourceRoot":"","sources":["../../../src/postgres/sql/primary-key-of.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAA+B,MAAM,qBAAqB,CAAC;AAElF;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;IAC7D,MAAM,QAAQ,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAEvF,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,SAAS,CAAC,aAAa,IAAI,6EAA6E,CAAC,CAAC;IACtH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC"}
@@ -9,6 +9,7 @@ export * from './postgres/outbox/tenant-outbox-table.js';
9
9
  export * from './postgres/outbox/processed-events-table.js';
10
10
  export * from './postgres/outbox/drizzle-outbox-store.js';
11
11
  export * from './postgres/outbox/drizzle-processed-event-store.js';
12
+ export * from './postgres/sql/primary-key-of.js';
12
13
  export * from './postgres/columns/audit-columns.js';
13
14
  export * from './postgres/columns/tenant-column.js';
14
15
  export * from './postgres/read-model/read-model-columns.js';
@@ -1 +1 @@
1
- {"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../src/postgres.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,KAAK,WAAW,GACjB,MAAM,oB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A8C,CAAC;AAC7D,cAAc,gCAAgC,CAAC;AAC/C,cAAc,uCAAuC,CAAC;AACtD,cAAc,0CAA0C,CAAC;AACzD,cAAc,wCAAwC,CAAC;AACvD,cAAc,iDAAiD,CAAC;AAChE,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC;AACjD,cAAc,0CAA0C,CAAC;AACzD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,2CAA2C,CAAC;AAC1D,cAAc,kDAAkD,CAAC;AACjE,cAAc,yCAAyC,CAAC;AACxD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,gDAAgD,CAAC;AAC/D,cAAc,iCAAiC,CAAC;AAChD,cAAc,qCAAqC,CAAC;AACpD,cAAc,yCAAyC,CAAC;AACxD,cAAc,oCAAoC,CAAC;AACnD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,mCAAmC,CAAC;AAClD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,mCAAmC,CAAC;AAClD,cAAc,sCAAsC,CAAC;AACrD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,wDAAwD,CAAC;AACvE,cAAc,gEAAgE,CAAC;AAC/E,cAAc,uDAAuD,CAAC;AACtE,cAAc,+DAA+D,CAAC;AAC9E,cAAc,kDAAkD,CAAC"}
package/dist/postgres.js CHANGED
@@ -9,6 +9,7 @@ export * from './postgres/outbox/tenant-outbox-table.js';
9
9
  export * from './postgres/outbox/processed-events-table.js';
10
10
  export * from './postgres/outbox/drizzle-outbox-store.js';
11
11
  export * from './postgres/outbox/drizzle-processed-event-store.js';
12
+ export * from './postgres/sql/primary-key-of.js';
12
13
  export * from './postgres/columns/audit-columns.js';
13
14
  export * from './postgres/columns/tenant-column.js';
14
15
  export * from './postgres/read-model/read-model-columns.js';
@@ -1 +1 @@
1
- {"version":3,"file":"postgres.js","sourceRoot":"","sources":["../src/postgres.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,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AAyD,CAAC;AACxE,cAAc,8CAA8C,CAAC;AAC7D,cAAc,gCAAgC,CAAC;AAC/C,cAAc,uCAAuC,CAAC;AACtD,cAAc,0CAA0C,CAAC;AACzD,cAAc,wCAAwC,CAAC;AACvD,cAAc,iDAAiD,CAAC;AAChE,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC;AACjD,cAAc,0CAA0C,CAAC;AACzD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,2CAA2C,CAAC;AAC1D,cAAc,kDAAkD,CAAC;AACjE,cAAc,yCAAyC,CAAC;AACxD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,gDAAgD,CAAC;AAC/D,cAAc,iCAAiC,CAAC;AAChD,cAAc,qCAAqC,CAAC;AACpD,cAAc,yCAAyC,CAAC;AACxD,cAAc,oCAAoC,CAAC;AACnD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,mCAAmC,CAAC;AAClD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,mCAAmC,CAAC;AAClD,cAAc,sCAAsC,CAAC;AACrD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,wDAAwD,CAAC;AACvE,cAAc,gEAAgE,CAAC;AAC/E,cAAc,uDAAuD,CAAC;AACtE,cAAc,+DAA+D,CAAC;AAC9E,cAAc,kDAAkD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ddd-ts-kit/drizzle",
3
- "version": "0.102.0",
3
+ "version": "0.104.0",
4
4
  "description": "Drizzle adapters for the kernel ports on PostgreSQL: unit of work, transactional outbox store, processed-event store, event store, background job store, process store, one schema per context.",
5
5
  "keywords": [
6
6
  "ddd",
@@ -39,9 +39,9 @@
39
39
  "dist"
40
40
  ],
41
41
  "dependencies": {
42
- "@ddd-ts-kit/kernel": "0.102.0",
43
- "@ddd-ts-kit/events": "0.102.0",
44
- "@ddd-ts-kit/scheduling": "0.102.0"
42
+ "@ddd-ts-kit/kernel": "0.104.0",
43
+ "@ddd-ts-kit/events": "0.104.0",
44
+ "@ddd-ts-kit/scheduling": "0.104.0"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "drizzle-orm": ">=0.40.0"
@@ -49,7 +49,7 @@
49
49
  "devDependencies": {
50
50
  "drizzle-orm": "latest",
51
51
  "@electric-sql/pglite": "0.5.11",
52
- "@ddd-ts-kit/testing": "0.102.0",
52
+ "@ddd-ts-kit/testing": "0.104.0",
53
53
  "pg": "^8.23.1",
54
54
  "@types/pg": "^8.23.1"
55
55
  }