@ddd-ts-kit/knex 0.110.1 → 0.112.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 +57 -7
- package/README.md +53 -7
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/locks/advisory-lock.d.ts +16 -0
- package/dist/locks/advisory-lock.d.ts.map +1 -0
- package/dist/locks/advisory-lock.js +18 -0
- package/dist/locks/advisory-lock.js.map +1 -0
- package/dist/locks/try-advisory-lock.d.ts +10 -0
- package/dist/locks/try-advisory-lock.d.ts.map +1 -0
- package/dist/locks/try-advisory-lock.js +15 -0
- package/dist/locks/try-advisory-lock.js.map +1 -0
- package/dist/read-model/knex-read-model-table.d.ts +4 -1
- package/dist/read-model/knex-read-model-table.d.ts.map +1 -1
- package/dist/read-model/knex-read-model-table.js +17 -3
- package/dist/read-model/knex-read-model-table.js.map +1 -1
- package/dist/read-model/knex-stamped-rows.d.ts +8 -2
- package/dist/read-model/knex-stamped-rows.d.ts.map +1 -1
- package/dist/read-model/knex-stamped-rows.js +25 -7
- package/dist/read-model/knex-stamped-rows.js.map +1 -1
- package/dist/read-model/like-pattern.d.ts +2 -2
- package/dist/read-model/like-pattern.d.ts.map +1 -1
- package/dist/read-model/like-pattern.js +1 -1
- package/dist/read-model/like-pattern.js.map +1 -1
- package/dist/read-model/predicate-to-knex.d.ts +1 -1
- package/dist/read-model/predicate-to-knex.d.ts.map +1 -1
- package/dist/read-model/predicate-to-knex.js +12 -4
- package/dist/read-model/predicate-to-knex.js.map +1 -1
- package/dist/read-model/row.mapper.d.ts +2 -0
- package/dist/read-model/row.mapper.d.ts.map +1 -1
- package/dist/read-model/row.mapper.js +4 -0
- package/dist/read-model/row.mapper.js.map +1 -1
- package/dist/sql/insert-ignoring-conflict.d.ts +6 -0
- package/dist/sql/insert-ignoring-conflict.d.ts.map +1 -1
- package/dist/sql/insert-ignoring-conflict.js +17 -3
- package/dist/sql/insert-ignoring-conflict.js.map +1 -1
- package/package.json +5 -5
package/README.fr.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
[English](README.md) · **Français**
|
|
2
|
-
<!-- translation-of: README.md sha256:
|
|
2
|
+
<!-- translation-of: README.md sha256:d723d12bcfe5ca31 -->
|
|
3
3
|
|
|
4
4
|
# @ddd-ts-kit/knex
|
|
5
5
|
|
|
@@ -395,13 +395,56 @@ dialectes diffèrent (`RETURNING` sur PostgreSQL et SQLite, le nombre de lignes
|
|
|
395
395
|
MySQL). Il n'ignore qu'un conflit sur `conflict` (l'id) : une ligne qui prend une valeur qu'une
|
|
396
396
|
autre ligne détient sous une contrainte d'unicité (un e-mail) est refusée par la base, et
|
|
397
397
|
`optimisticUpdate` lève `UniqueViolationError` (code `CONFLICT`, 409), pas une mise à jour
|
|
398
|
-
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.
|
|
398
|
+
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. Deux premiers enregistrements d'un même agrégat faits en même temps (une requête livrée deux fois) ne sont pas refusés pour la valeur unique qu'ils écrivent tous les deux : sur PostgreSQL, une cible de conflit sur la clé seule les laisse se rencontrer d'abord sur l'autre index unique, si bien que l'utilitaire insère par une instruction qui passe sur toute valeur unique, et seulement quand elle n'a rien écrit par une seconde qui nomme la clé. Celui qui a perdu n'écrit aucune ligne, une `ConcurrencyError`.
|
|
399
399
|
|
|
400
400
|

|
|
401
401
|
|
|
402
402
|
Le second écrivain est refusé au lieu d'écraser le premier en silence. Rien ne le retente pour
|
|
403
403
|
vous : l'appelant reçoit la `ConcurrencyError` et décide s'il recharge la facture.
|
|
404
404
|
|
|
405
|
+
### Un verrou sur une clé métier
|
|
406
|
+
|
|
407
|
+
La concurrence optimiste protège une ligne qui existe. Une décision prise sur des lignes qui
|
|
408
|
+
n'existent pas encore (compter les réservations d'un créneau puis en insérer une, donner le
|
|
409
|
+
numéro suivant d'une séquence par *tenant*) n'a pas de version à comparer : deux unités de travail
|
|
410
|
+
lisent le même compte et écrivent toutes les deux. `advisoryLock(context, key, { tenant })` les
|
|
411
|
+
met en file : il attend qu'aucune autre transaction ne tienne le verrou sur `key`, puis le tient
|
|
412
|
+
jusqu'à ce que l'unité de travail valide ou annule (`pg_advisory_xact_lock`). Rien ne le relâche
|
|
413
|
+
à la main.
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
// infrastructure/persistence/knex-invoice-numbers.repository.ts
|
|
417
|
+
import { advisoryLock } from '@ddd-ts-kit/knex';
|
|
418
|
+
|
|
419
|
+
async next(): Promise<number> {
|
|
420
|
+
// First in the unit of work, before the read it protects.
|
|
421
|
+
await advisoryLock(this.context, 'invoice-number', { tenant: this.scope });
|
|
422
|
+
|
|
423
|
+
return (await this.lastNumber()) + 1;
|
|
424
|
+
}
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
- **La clé est un texte**, que PostgreSQL hache vers le nombre de 64 bits qu'il verrouille
|
|
428
|
+
(`hashtextextended(text, 0)`). Avec `tenant`, un `ExecutionScope`, le texte est
|
|
429
|
+
`tenant:<id>:<key>` (`tenantKey` du kernel) : deux *tenants* ne s'attendent jamais, et un
|
|
430
|
+
travail qui ne nomme aucun *tenant* est refusé. Deux textes de même hachage, environ une paire
|
|
431
|
+
sur 2^64, s'attendent comme le ferait une seule clé : une attente, jamais une réponse fausse.
|
|
432
|
+
- **Dans une unité de travail seulement.** Hors d'une unité de travail, le verrou serait relâché
|
|
433
|
+
à la fin de son instruction : `AdvisoryLockOutsideUnitOfWorkError`. Garder l'isolation par
|
|
434
|
+
défaut, `read committed` : la lecture qui suit le verrou voit alors ce que le détenteur
|
|
435
|
+
précédent a validé, là où `repeatable read` et `serializable` lisent à l'instant où le verrou
|
|
436
|
+
a été demandé.
|
|
437
|
+
- **`tryAdvisoryLock(context, key, { tenant })`** n'attend pas : `true` quand il a pris le
|
|
438
|
+
verrou, `false` quand une autre transaction le tient (`pg_try_advisory_xact_lock`).
|
|
439
|
+
- **Un cas d'usage n'importe aucun *package* d'ORM.** Le verrou se place dans la méthode du
|
|
440
|
+
*repository* qui en a besoin, ou derrière un port du contexte (`InvoiceNumbering.reserve()`)
|
|
441
|
+
dont l'adaptateur est ce seul appel et dont le *fake* ne fait rien.
|
|
442
|
+
- PostgreSQL seulement. Dans un projet multitenant, `tenancy/scoped-statement` accepte le
|
|
443
|
+
*helper* et signale un `pg_advisory_xact_lock` écrit à la main, comme tout SQL brut.
|
|
444
|
+
- Tenu au contrat `advisoryLockContract` de `@ddd-ts-kit/testing/contracts`. L'attente d'une
|
|
445
|
+
seconde unité de travail est prouvée sur un vrai serveur : PGlite est une seule session, où
|
|
446
|
+
la seconde attend la connexion elle-même.
|
|
447
|
+
|
|
405
448
|
## Colonnes d'audit, restriction au tenant et pages
|
|
406
449
|
|
|
407
450
|
Les formes de persistance standard du noyau, sur knex. Quatre petits *helpers*, pour que les
|
|
@@ -701,7 +744,7 @@ les filtres et le périmètre de l'acteur, le jeu de clés (keyset) d'une page p
|
|
|
701
744
|
total terminé par le départage, la fenêtre. `predicateToKnex(builder, predicate, columns)` ajoute
|
|
702
745
|
les filtres seuls, pour un comptage. `columns` associe chaque champ du modèle de lecture à sa
|
|
703
746
|
colonne, et c'est la seule source d'identifiants : chaque valeur est un paramètre lié, et une
|
|
704
|
-
recherche textuelle est `like ? escape ?`, avec `%`, `_` et `\` échappés.
|
|
747
|
+
recherche textuelle est `like ? escape ?`, avec `%`, `_` et `\` échappés. `containsIgnoringCase` et `startsWithIgnoringCase` sont la même recherche en `ILIKE`, que PostgreSQL replie avec la locale de la base (la règle et ses limites sont dans le README du kernel, « Ce que signifient des critères ») ; aucun index b-tree ne la sert, un index GIN de `pg_trgm` sur la colonne le fait.
|
|
705
748
|
|
|
706
749
|
```ts
|
|
707
750
|
// infrastructure/persistence/knex-appointment-queries.reader.ts
|
|
@@ -737,9 +780,10 @@ de chaîne.
|
|
|
737
780
|
// la migration
|
|
738
781
|
await knex.schema.withSchema('scheduling').createTable('appointment_list', (table) => {
|
|
739
782
|
table.text('id').primary();
|
|
740
|
-
table.text('status').
|
|
741
|
-
table.datetime('starts_at', { useTz: true, precision: 3 }).
|
|
783
|
+
table.text('status').nullable(); // NULL dans une ligne supprimée, comme chaque colonne de la ligne
|
|
784
|
+
table.datetime('starts_at', { useTz: true, precision: 3 }).nullable();
|
|
742
785
|
readModelColumns(table); // last_event_id, last_event_at, last_event_version, deleted
|
|
786
|
+
table.check('deleted or (status is not null and starts_at is not null)', [], 'appointment_list_live_rows_whole');
|
|
743
787
|
});
|
|
744
788
|
|
|
745
789
|
// infrastructure/persistence/knex-appointment-list.repository.ts
|
|
@@ -759,8 +803,14 @@ export class KnexAppointmentList extends KnexReadModelStore<AppointmentRow> impl
|
|
|
759
803
|
Chaque écriture passe par le `TableReadModelStore` du noyau : une ligne n'est écrite que si son
|
|
760
804
|
événement est plus récent que celui qu'elle détient, sur l'exécuteur de l'unité de travail, si
|
|
761
805
|
bien que la ligne et la marque d'événement traité sont validées ensemble. Une ligne supprimée
|
|
762
|
-
reste dans la table avec `deleted` positionné :
|
|
763
|
-
|
|
806
|
+
reste dans la table comme une trace, avec `deleted` positionné : sa clé et son tampon, et NULL
|
|
807
|
+
dans toutes les autres colonnes, ce pour quoi chaque colonne de la ligne accepte NULL. La
|
|
808
|
+
suppression d'une clé que la table ne tient pas encore laisse la même trace. Une contrainte de
|
|
809
|
+
vérification garde entières les lignes vivantes (`check (deleted or (status is not null and
|
|
810
|
+
starts_at is not null))`), une requête à vous sur la table écarte les lignes supprimées
|
|
811
|
+
(`deleted = false`), et une table dont des colonnes refusent NULL est refusée par
|
|
812
|
+
`ReadModelTableNotReadyError` avant l'écriture de la première trace, par une lecture des colonnes de la table
|
|
813
|
+
([Ce que laisse une ligne supprimée](../../docs/concepts/cqrs.fr.md#la-table-dun-modèle-de-lecture-et-ce-que-laisse-une-ligne-supprimée)). Une application n'attend pas cette première trace : `tableState()` d'un `KnexReadModelStore` lit sa table comme le registre le fait avant sa première trace, pour les colonnes qu'il associe et aucune autre, et `ReadModelTables` du noyau le demande à chaque registre que l'application liste, puis refuse avec `ReadModelTablesNotReadyError`, qui nomme chaque table qui n'est pas prête, ce qui l'en empêche et la migration à écrire en deux étapes, ainsi que chaque table qu'il n'a pas pu lire ; un projet généré garde la liste, vérifie à son démarrage les tables des contextes qu'il héberge, et `npm run db:read-models` la vérifie avant un déploiement ([Chaque table vérifiée au démarrage, et avant un déploiement](../../docs/concepts/cqrs.fr.md#chaque-table-vérifiée-au-démarrage-et-avant-un-déploiement)). `store.table` est ce que prend
|
|
764
814
|
`ProjectionRebuild` ; la reconstruction est réservée à PostgreSQL (une copie `<table>__rebuild`
|
|
765
815
|
faite par `LIKE ... INCLUDING ALL`, les clés écrites entre-temps dans `<table>__touched`,
|
|
766
816
|
l'échange par renommage dans une seule transaction), et ses garanties et ses limites sont dans le
|
package/README.md
CHANGED
|
@@ -379,13 +379,54 @@ leaves the surrounding PostgreSQL transaction usable, and it hides the one place
|
|
|
379
379
|
dialects differ (`RETURNING` on PostgreSQL and SQLite, the affected-row count on MySQL). It
|
|
380
380
|
ignores a conflict on `conflict` alone (the id): a row that takes a value another row holds
|
|
381
381
|
under a unique constraint (an email) is refused by the database, and `optimisticUpdate` raises
|
|
382
|
-
`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.
|
|
382
|
+
`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. Two first saves of one aggregate made at once (a request delivered twice) are not refused for the unique value they both write: on PostgreSQL a conflict target on the key alone lets them meet on the other unique index first, so the helper inserts with a statement that skips on any unique value, and only when it wrote nothing with a second one that names the key. The one that lost writes no row, a `ConcurrencyError`.
|
|
383
383
|
|
|
384
384
|

|
|
385
385
|
|
|
386
386
|
The second writer is refused instead of silently overwriting the first. Nothing retries it for
|
|
387
387
|
you: the caller receives the `ConcurrencyError` and decides whether to load the invoice again.
|
|
388
388
|
|
|
389
|
+
### A lock on a business key
|
|
390
|
+
|
|
391
|
+
Optimistic concurrency protects a row that exists. A decision taken on rows that do not exist
|
|
392
|
+
yet (count the bookings of a slot then insert one, give the next number of a sequence per
|
|
393
|
+
tenant) has no version to compare: two units of work read the same count and both write.
|
|
394
|
+
`advisoryLock(context, key, { tenant })` serialises them: it waits until no other transaction
|
|
395
|
+
holds the lock on `key`, then holds it until the unit of work commits or rolls back
|
|
396
|
+
(`pg_advisory_xact_lock`). Nothing releases it by hand.
|
|
397
|
+
|
|
398
|
+
```ts
|
|
399
|
+
// infrastructure/persistence/knex-invoice-numbers.repository.ts
|
|
400
|
+
import { advisoryLock } from '@ddd-ts-kit/knex';
|
|
401
|
+
|
|
402
|
+
async next(): Promise<number> {
|
|
403
|
+
// First in the unit of work, before the read it protects.
|
|
404
|
+
await advisoryLock(this.context, 'invoice-number', { tenant: this.scope });
|
|
405
|
+
|
|
406
|
+
return (await this.lastNumber()) + 1;
|
|
407
|
+
}
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
- **The key is a text**, hashed by PostgreSQL into the 64-bit number it locks on
|
|
411
|
+
(`hashtextextended(text, 0)`). With `tenant`, an `ExecutionScope`, the text is
|
|
412
|
+
`tenant:<id>:<key>` (`tenantKey` of the kernel): two tenants never wait for each other, and
|
|
413
|
+
work that names no tenant is refused. Two texts of the same hash, about one pair in 2^64,
|
|
414
|
+
wait for each other as one key would: a wait, never a wrong answer.
|
|
415
|
+
- **Inside a unit of work only.** Outside of one the lock would be released as its statement
|
|
416
|
+
ends: `AdvisoryLockOutsideUnitOfWorkError`. Keep the default isolation, `read committed`:
|
|
417
|
+
the read that follows the lock then sees what the previous holder committed, where
|
|
418
|
+
`repeatable read` and `serializable` read as of the moment the lock was asked.
|
|
419
|
+
- **`tryAdvisoryLock(context, key, { tenant })`** does not wait: `true` when it took the lock,
|
|
420
|
+
`false` when another transaction holds it (`pg_try_advisory_xact_lock`).
|
|
421
|
+
- **A use case imports no ORM package.** The lock sits in the method of the repository that
|
|
422
|
+
needs it, or behind a port of the context (`InvoiceNumbering.reserve()`) whose adapter is
|
|
423
|
+
this one call and whose fake does nothing.
|
|
424
|
+
- PostgreSQL only. In a tenant-scoped project, `tenancy/scoped-statement` accepts the helper
|
|
425
|
+
and reports a raw `pg_advisory_xact_lock`, as any raw SQL.
|
|
426
|
+
- Held to `advisoryLockContract` of `@ddd-ts-kit/testing/contracts`. The wait of a second unit
|
|
427
|
+
of work is proven on a real server: PGlite is one session, where the second one waits for
|
|
428
|
+
the connection itself.
|
|
429
|
+
|
|
389
430
|
## Audit columns, tenant scoping and pages
|
|
390
431
|
|
|
391
432
|
The standard persistence shapes of the kernel, on knex. Four small helpers, so that the columns
|
|
@@ -671,7 +712,7 @@ filters and the scope of the actor, the keyset of a cursor page, the total order
|
|
|
671
712
|
tie-breaker, the window. `predicateToKnex(builder, predicate, columns)` adds the filters alone,
|
|
672
713
|
for a count. `columns` maps each field of the read model to its column, and is the only source
|
|
673
714
|
of identifiers: every value is a binding, a text search is `like ? escape ?` with `%`, `_` and
|
|
674
|
-
`\` escaped.
|
|
715
|
+
`\` escaped. `containsIgnoringCase` and `startsWithIgnoringCase` are the same search as an `ILIKE`, which PostgreSQL folds with the locale of the database (the rule and its limits are in the README of the kernel, "What a criteria means"); no b-tree index serves it, a GIN index of `pg_trgm` on the column does.
|
|
675
716
|
|
|
676
717
|
```ts
|
|
677
718
|
// infrastructure/persistence/knex-appointment-queries.reader.ts
|
|
@@ -706,9 +747,10 @@ An integer is `integer`: a `bigint` comes back as a string from the driver.
|
|
|
706
747
|
// the migration
|
|
707
748
|
await knex.schema.withSchema('scheduling').createTable('appointment_list', (table) => {
|
|
708
749
|
table.text('id').primary();
|
|
709
|
-
table.text('status').
|
|
710
|
-
table.datetime('starts_at', { useTz: true, precision: 3 }).
|
|
750
|
+
table.text('status').nullable(); // NULL in a deleted row, as every column of the row
|
|
751
|
+
table.datetime('starts_at', { useTz: true, precision: 3 }).nullable();
|
|
711
752
|
readModelColumns(table); // last_event_id, last_event_at, last_event_version, deleted
|
|
753
|
+
table.check('deleted or (status is not null and starts_at is not null)', [], 'appointment_list_live_rows_whole');
|
|
712
754
|
});
|
|
713
755
|
|
|
714
756
|
// infrastructure/persistence/knex-appointment-list.repository.ts
|
|
@@ -727,9 +769,13 @@ export class KnexAppointmentList extends KnexReadModelStore<AppointmentRow> impl
|
|
|
727
769
|
|
|
728
770
|
Every write goes through `TableReadModelStore` of the kernel: a row is written only if its
|
|
729
771
|
event is newer than the one it holds, on the executor of the unit of work, so the row and the
|
|
730
|
-
processed-event mark commit together. A removed row stays in the table with `deleted`
|
|
731
|
-
|
|
732
|
-
|
|
772
|
+
processed-event mark commit together. A removed row stays in the table as a trace, with `deleted`
|
|
773
|
+
set: its key and its stamp, and NULL in every other column, which is why every column of the row
|
|
774
|
+
is nullable. A deletion of a key the table does not hold yet leaves the same trace. A check
|
|
775
|
+
constraint keeps the live rows whole (`check (deleted or (status is not null and starts_at is
|
|
776
|
+
not null))`), a query of your own on the table leaves the deleted rows out (`deleted = false`),
|
|
777
|
+
and a table whose columns refuse NULL is refused with `ReadModelTableNotReadyError` before the first trace is written, from one read of the columns of the table
|
|
778
|
+
([What a deleted row leaves](../../docs/concepts/cqrs.md#the-table-of-a-read-model-and-what-a-deleted-row-leaves)). An application does not wait for that first trace: `tableState()` of a `KnexReadModelStore` reads its table as the store does before its first trace, for the columns it maps and no other, and `ReadModelTables` of the kernel asks it of every store the application lists, then refuses with `ReadModelTablesNotReadyError`, which names each table that is not ready, what keeps it from it and the migration to write in two steps, and each table it could not read; a generated project keeps the list, checks at its start the tables of the contexts it hosts, and `npm run db:read-models` checks it before a deployment ([Every table checked at the start, and before a deployment](../../docs/concepts/cqrs.md#every-table-checked-at-the-start-and-before-a-deployment)). `store.table` is what `ProjectionRebuild` takes; the rebuild is
|
|
733
779
|
PostgreSQL only (a copy `<table>__rebuild` made `LIKE ... INCLUDING ALL`, the keys written
|
|
734
780
|
meanwhile in `<table>__touched`, the swap by rename in one transaction) and its guarantees
|
|
735
781
|
and limits are in the README of `@ddd-ts-kit/kernel` ("Rebuild"). The store passes
|
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export * from './columns/tenant-row.js';
|
|
|
19
19
|
export * from './columns/add-tenant-column-options.js';
|
|
20
20
|
export * from './columns/add-tenant-column.js';
|
|
21
21
|
export * from './sql/insert-ignoring-conflict.js';
|
|
22
|
+
export * from './locks/advisory-lock.js';
|
|
23
|
+
export * from './locks/try-advisory-lock.js';
|
|
22
24
|
export * from './read-model/read-model-columns.js';
|
|
23
25
|
export * from './read-model/predicate-to-knex.js';
|
|
24
26
|
export * from './read-model/criteria-to-knex.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,KAAK,WAAW,GACjB,MAAM,oBAAoB,CAAC;AAC5B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yCAAyC,CAAC;AACxD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,4CAA4C,CAAC;AAC3D,cAAc,6CAA6C,CAAC;AAC5D,cAAc,sBAAsB,CAAC;AACrC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9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0CAA0C,CAAC;AACzD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,yBAAyB,CAAC;AACxC,cAAc,uCAAuC,CAAC;AACtD,cAAc,mCAAmC,CAAC;AAClD,cAAc,0BAA0B,CAAC;AACzC,cAAc,2CAA2C,CAAC;AAC1D,cAAc,4BAA4B,CAAC;AAC3C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iCAAiC,CAAC;AAChD,cAAc,oCAAoC,CAAC;AACnD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,2CAA2C,CAAC;AAC1D,cAAc,gDAAgD,CAAC;AAC/D,cAAc,6CAA6C,CAAC;AAC5D,cAAc,gCAAgC,CAAC;AAC/C,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,kCAAkC,CAAC;AACjD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4CAA4C,CAAC;AAC3D,cAAc,oDAAoD,CAAC;AACnE,cAAc,2CAA2C,CAAC;AAC1D,cAAc,mDAAmD,CAAC;AAClE,cAAc,sCAAsC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -19,6 +19,8 @@ export * from './columns/tenant-row.js';
|
|
|
19
19
|
export * from './columns/add-tenant-column-options.js';
|
|
20
20
|
export * from './columns/add-tenant-column.js';
|
|
21
21
|
export * from './sql/insert-ignoring-conflict.js';
|
|
22
|
+
export * from './locks/advisory-lock.js';
|
|
23
|
+
export * from './locks/try-advisory-lock.js';
|
|
22
24
|
export * from './read-model/read-model-columns.js';
|
|
23
25
|
export * from './read-model/predicate-to-knex.js';
|
|
24
26
|
export * from './read-model/criteria-to-knex.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,GAEb,MAAM,oBAAoB,CAAC;AAC5B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yCAAyC,CAAC;AACxD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,4CAA4C,CAAC;AAC3D,cAAc,6CAA6C,CAAC;AAC5D,cAAc,sBAAsB,CAAC;AACrC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mCAAmC,CAAC;AAClD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qCAAqC,CAAC;AACpD,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qCAAqC,CAAC;AACpD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,sCAAsC,CAAC;AACrD,cAAc,oCAAoC,CAAC;AACnD,cAAc,oCAAoC,CAAC;AACnD,cAAc,0CAA0C,CAAC;AACzD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,yBAAyB,CAAC;AACx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ACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,kCAAkC,CAAC;AACjD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4CAA4C,CAAC;AAC3D,cAAc,oDAAoD,CAAC;AACnE,cAAc,2CAA2C,CAAC;AAC1D,cAAc,mDAAmD,CAAC;AAClE,cAAc,sCAAsC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type AdvisoryLockOptions } from '@ddd-ts-kit/kernel';
|
|
2
|
+
import type { KnexContext } from '../unit-of-work/knex-context.js';
|
|
3
|
+
/**
|
|
4
|
+
* Serialises the units of work that take the same key: waits until no other transaction holds
|
|
5
|
+
* the lock on `key`, then holds it until the unit of work commits or rolls back
|
|
6
|
+
* (`pg_advisory_xact_lock`). For a decision no row can carry a version for: count then insert, the
|
|
7
|
+
* next number of a sequence per tenant. Taken first in the unit of work, before what it protects
|
|
8
|
+
* is read; the same key taken twice in one unit of work is held once.
|
|
9
|
+
*
|
|
10
|
+
* The key is hashed by PostgreSQL (`advisoryLockKey` of the kernel says how, and what a collision
|
|
11
|
+
* costs); with `tenant`, two tenants never wait for each other. Refused outside a unit of work
|
|
12
|
+
* (`AdvisoryLockOutsideUnitOfWorkError`). PostgreSQL only. It is the one way an adapter of a
|
|
13
|
+
* tenant-scoped project takes such a lock (`tenancy/scoped-statement`).
|
|
14
|
+
*/
|
|
15
|
+
export declare function advisoryLock(context: KnexContext, key: string, options?: AdvisoryLockOptions): Promise<void>;
|
|
16
|
+
//# sourceMappingURL=advisory-lock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"advisory-lock.d.ts","sourceRoot":"","sources":["../../src/locks/advisory-lock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC/E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAEhE;;;;;;;;;;;GAWG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE,WAAW,EACpB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,mBAAwB,GAChC,OAAO,CAAC,IAAI,CAAC,CAIf"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { advisoryLockKey } from '@ddd-ts-kit/kernel';
|
|
2
|
+
/**
|
|
3
|
+
* Serialises the units of work that take the same key: waits until no other transaction holds
|
|
4
|
+
* the lock on `key`, then holds it until the unit of work commits or rolls back
|
|
5
|
+
* (`pg_advisory_xact_lock`). For a decision no row can carry a version for: count then insert, the
|
|
6
|
+
* next number of a sequence per tenant. Taken first in the unit of work, before what it protects
|
|
7
|
+
* is read; the same key taken twice in one unit of work is held once.
|
|
8
|
+
*
|
|
9
|
+
* The key is hashed by PostgreSQL (`advisoryLockKey` of the kernel says how, and what a collision
|
|
10
|
+
* costs); with `tenant`, two tenants never wait for each other. Refused outside a unit of work
|
|
11
|
+
* (`AdvisoryLockOutsideUnitOfWorkError`). PostgreSQL only. It is the one way an adapter of a
|
|
12
|
+
* tenant-scoped project takes such a lock (`tenancy/scoped-statement`).
|
|
13
|
+
*/
|
|
14
|
+
export async function advisoryLock(context, key, options = {}) {
|
|
15
|
+
const text = advisoryLockKey(context, key, options);
|
|
16
|
+
await context.executor().raw('select pg_advisory_xact_lock(hashtextextended(?, 0))', [text]);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=advisory-lock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"advisory-lock.js","sourceRoot":"","sources":["../../src/locks/advisory-lock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAA4B,MAAM,oBAAoB,CAAC;AAG/E;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,OAAoB,EACpB,GAAW,EACX,OAAO,GAAwB,EAAE;IAEjC,MAAM,IAAI,GAAG,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;IAEpD,MAAM,OAAO,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,sDAAsD,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;AAC/F,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type AdvisoryLockOptions } from '@ddd-ts-kit/kernel';
|
|
2
|
+
import type { KnexContext } from '../unit-of-work/knex-context.js';
|
|
3
|
+
/**
|
|
4
|
+
* `advisoryLock` without the wait: takes the lock on `key` for the unit of work if no other
|
|
5
|
+
* transaction holds it and answers `true`, answers `false` at once otherwise
|
|
6
|
+
* (`pg_try_advisory_xact_lock`). For work that is skipped, not queued, when somebody else is
|
|
7
|
+
* already at it.
|
|
8
|
+
*/
|
|
9
|
+
export declare function tryAdvisoryLock(context: KnexContext, key: string, options?: AdvisoryLockOptions): Promise<boolean>;
|
|
10
|
+
//# sourceMappingURL=try-advisory-lock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"try-advisory-lock.d.ts","sourceRoot":"","sources":["../../src/locks/try-advisory-lock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAC/E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAMhE;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,OAAO,EAAE,WAAW,EACpB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,mBAAwB,GAChC,OAAO,CAAC,OAAO,CAAC,CAQlB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { advisoryLockKey } from '@ddd-ts-kit/kernel';
|
|
2
|
+
/**
|
|
3
|
+
* `advisoryLock` without the wait: takes the lock on `key` for the unit of work if no other
|
|
4
|
+
* transaction holds it and answers `true`, answers `false` at once otherwise
|
|
5
|
+
* (`pg_try_advisory_xact_lock`). For work that is skipped, not queued, when somebody else is
|
|
6
|
+
* already at it.
|
|
7
|
+
*/
|
|
8
|
+
export async function tryAdvisoryLock(context, key, options = {}) {
|
|
9
|
+
const text = advisoryLockKey(context, key, options);
|
|
10
|
+
const { rows } = await context
|
|
11
|
+
.executor()
|
|
12
|
+
.raw('select pg_try_advisory_xact_lock(hashtextextended(?, 0)) as acquired', [text]);
|
|
13
|
+
return rows[0]?.acquired === true;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=try-advisory-lock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"try-advisory-lock.js","sourceRoot":"","sources":["../../src/locks/try-advisory-lock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAA4B,MAAM,oBAAoB,CAAC;AAO/E;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,OAAoB,EACpB,GAAW,EACX,OAAO,GAAwB,EAAE;IAEjC,MAAM,IAAI,GAAG,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;IAEpD,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,OAAO;SAC3B,QAAQ,EAAE;SACV,GAAG,CAAW,sEAAsE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEjG,OAAO,IAAI,CAAC,CAAC,CAAC,EAAE,QAAQ,KAAK,IAAI,CAAC;AACpC,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CriteriaPlan, type EventStamp, type SearchedRows, type ReadModelRows, type ReadModelTable, type StampedRow, type TouchedKey } from '@ddd-ts-kit/kernel';
|
|
1
|
+
import { type CriteriaPlan, type EventStamp, type SearchedRows, type ReadModelRows, type ReadModelTable, type ReadModelTableState, type StampedRow, type TouchedKey } from '@ddd-ts-kit/kernel';
|
|
2
2
|
import type { KnexContext } from '../unit-of-work/knex-context.js';
|
|
3
3
|
import type { KnexReadModelOptions } from './knex-read-model-options.js';
|
|
4
4
|
/**
|
|
@@ -12,11 +12,14 @@ export declare class KnexReadModelTable<Row extends object> implements ReadModel
|
|
|
12
12
|
private readonly rows;
|
|
13
13
|
private readonly copy;
|
|
14
14
|
private readonly rebuild;
|
|
15
|
+
private readonly traces;
|
|
15
16
|
constructor(context: KnexContext, options: KnexReadModelOptions<Row>);
|
|
16
17
|
find(key: string): Promise<StampedRow<Row> | undefined>;
|
|
17
18
|
insert(entry: StampedRow<Row>): Promise<boolean>;
|
|
18
19
|
replace(entry: StampedRow<Row>, held: EventStamp): Promise<boolean>;
|
|
19
20
|
search(plan: CriteriaPlan): Promise<SearchedRows<Row>>;
|
|
21
|
+
/** What the table can hold of the trace of a deleted row, read as before the first trace: what `ReadModelTables` asks. */
|
|
22
|
+
tableState(): Promise<ReadModelTableState>;
|
|
20
23
|
rebuilding(): Promise<boolean>;
|
|
21
24
|
touch(key: string): Promise<void>;
|
|
22
25
|
beginRebuild(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"knex-read-model-table.d.ts","sourceRoot":"","sources":["../../src/read-model/knex-read-model-table.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"knex-read-model-table.d.ts","sourceRoot":"","sources":["../../src/read-model/knex-read-model-table.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,mBAAmB,EACxB,KAAK,UAAU,EACf,KAAK,UAAU,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAEhE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAOtE;;;;GAIG;AACH,qBAAa,kBAAkB,CAAC,GAAG,SAAS,MAAM,CAAE,YAAW,cAAc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CAAC,IAAI,CAAC,CAE3B;IAED,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAEvB;IAED,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAE9B;IAED,sGAAsG;YACxF,GAAG;CAQlB"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { PostgresReadModelRebuild, } from '@ddd-ts-kit/kernel';
|
|
1
|
+
import { PostgresReadModelRebuild, PostgresTraceColumns, } from '@ddd-ts-kit/kernel';
|
|
2
2
|
import { criteriaToKnex } from './criteria-to-knex.js';
|
|
3
|
+
import { TENANT_COLUMN } from '../tenancy/where-tenant.js';
|
|
3
4
|
import { KnexStampedRows } from './knex-stamped-rows.js';
|
|
4
5
|
import { predicateToKnex } from './predicate-to-knex.js';
|
|
5
6
|
import { recordsOf } from './records-of.js';
|
|
@@ -15,12 +16,15 @@ export class KnexReadModelTable {
|
|
|
15
16
|
rows;
|
|
16
17
|
copy;
|
|
17
18
|
rebuild;
|
|
19
|
+
traces;
|
|
18
20
|
constructor(context, options) {
|
|
19
21
|
this.context = context;
|
|
20
22
|
this.options = options;
|
|
21
23
|
const { table, schema } = options;
|
|
22
|
-
this.
|
|
23
|
-
|
|
24
|
+
this.traces = new PostgresTraceColumns((statement, parameters) => this.run(statement, parameters), () => tracedTable(options));
|
|
25
|
+
const copied = { ...options, table: PostgresReadModelRebuild.shadowOf(table) };
|
|
26
|
+
this.rows = new KnexStampedRows(context, options, this.traces);
|
|
27
|
+
this.copy = new KnexStampedRows(context, copied, this.traces);
|
|
24
28
|
this.rebuild = new PostgresReadModelRebuild((statement, parameters) => this.run(statement, parameters), {
|
|
25
29
|
table,
|
|
26
30
|
...(schema === undefined ? {} : { schema }),
|
|
@@ -47,6 +51,10 @@ export class KnexReadModelTable {
|
|
|
47
51
|
const counted = await predicateToKnex(kept(), plan.filter, columns).count({ total: '*' });
|
|
48
52
|
return { rows, total: Number(recordsOf(counted)[0]?.['total'] ?? 0) };
|
|
49
53
|
}
|
|
54
|
+
/** What the table can hold of the trace of a deleted row, read as before the first trace: what `ReadModelTables` asks. */
|
|
55
|
+
tableState() {
|
|
56
|
+
return this.traces.state();
|
|
57
|
+
}
|
|
50
58
|
rebuilding() {
|
|
51
59
|
return this.rebuild.rebuilding();
|
|
52
60
|
}
|
|
@@ -80,4 +88,10 @@ export class KnexReadModelTable {
|
|
|
80
88
|
return recordsOf(typeof result === 'object' && result !== null && 'rows' in result ? result.rows : []);
|
|
81
89
|
}
|
|
82
90
|
}
|
|
91
|
+
/** The table and the columns of the row, in which a trace holds NULL: every one but the key and the tenant. */
|
|
92
|
+
function tracedTable({ table, schema, columns, keyColumn = 'id', tenant, }) {
|
|
93
|
+
const kept = new Set([keyColumn, ...(tenant === undefined ? [] : [TENANT_COLUMN])]);
|
|
94
|
+
const data = Object.values(columns).filter((column) => !kept.has(column));
|
|
95
|
+
return { table, data, ...(schema === undefined ? {} : { schema }) };
|
|
96
|
+
}
|
|
83
97
|
//# sourceMappingURL=knex-read-model-table.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"knex-read-model-table.js","sourceRoot":"","sources":["../../src/read-model/knex-read-model-table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,
|
|
1
|
+
{"version":3,"file":"knex-read-model-table.js","sourceRoot":"","sources":["../../src/read-model/knex-read-model-table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,oBAAoB,GAUrB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC;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,SAAS,WAAW,CAAM,EACxB,KAAK,EACL,MAAM,EACN,OAAO,EACP,SAAS,GAAG,IAAI,EAChB,MAAM,GACoB;IAC1B,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,CAAC,SAAS,EAAE,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;IACpF,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAS,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAElF,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC;AACtE,CAAC"}
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import type { Knex } from 'knex';
|
|
2
|
-
import { type EventStamp, type ReadModelRows, type StampedRow } from '@ddd-ts-kit/kernel';
|
|
2
|
+
import { type EventStamp, type PostgresTraceColumns, type ReadModelRows, type StampedRow } from '@ddd-ts-kit/kernel';
|
|
3
3
|
import type { KnexContext } from '../unit-of-work/knex-context.js';
|
|
4
4
|
import type { KnexReadModelOptions } from './knex-read-model-options.js';
|
|
5
5
|
/** The stamped rows of one table, the copy of a rebuild as well as the table itself. */
|
|
6
6
|
export declare class KnexStampedRows<Row extends object> implements ReadModelRows<Row> {
|
|
7
7
|
private readonly context;
|
|
8
8
|
private readonly options;
|
|
9
|
+
/** Of the table itself, which the copy of a rebuild is made like. */
|
|
10
|
+
private readonly traces;
|
|
9
11
|
private readonly mapper;
|
|
10
|
-
constructor(context: KnexContext, options: KnexReadModelOptions<Row
|
|
12
|
+
constructor(context: KnexContext, options: KnexReadModelOptions<Row>,
|
|
13
|
+
/** Of the table itself, which the copy of a rebuild is made like. */
|
|
14
|
+
traces: PostgresTraceColumns);
|
|
11
15
|
find(key: string): Promise<StampedRow<Row> | undefined>;
|
|
12
16
|
insert(entry: StampedRow<Row>): Promise<boolean>;
|
|
13
17
|
replace(entry: StampedRow<Row>, held: EventStamp): Promise<boolean>;
|
|
@@ -16,6 +20,8 @@ export declare class KnexStampedRows<Row extends object> implements ReadModelRow
|
|
|
16
20
|
/** What a read may reach: the table, confined to the tenant when the read model is of tenant data. */
|
|
17
21
|
reading(): Knex.QueryBuilder;
|
|
18
22
|
private writing;
|
|
23
|
+
/** Before the trace of a deleted row is written: the columns of the table accept it. */
|
|
24
|
+
private holdsTrace;
|
|
19
25
|
private recordOf;
|
|
20
26
|
private keyColumn;
|
|
21
27
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"knex-stamped-rows.d.ts","sourceRoot":"","sources":["../../src/read-model/knex-stamped-rows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,
|
|
1
|
+
{"version":3,"file":"knex-stamped-rows.d.ts","sourceRoot":"","sources":["../../src/read-model/knex-stamped-rows.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,UAAU,EAChB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAItE,wFAAwF;AACxF,qBAAa,eAAe,CAAC,GAAG,SAAS,MAAM,CAAE,YAAW,aAAa,CAAC,GAAG,CAAC;IAI1E,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,qEAAqE;IACrE,OAAO,CAAC,QAAQ,CAAC,MAAM;IANzB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;IAExC,YACmB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE,oBAAoB,CAAC,GAAG,CAAC;IACnD,qEAAqE;IACpD,MAAM,EAAE,oBAAoB,EAG9C;IAEK,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,CAmB5D;IAEK,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CASrD;IAEK,OAAO,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAYxE;IAED,8DAA8D;IAC9D,KAAK,IAAI,IAAI,CAAC,YAAY,CAKzB;IAED,sGAAsG;IACtG,OAAO,IAAI,IAAI,CAAC,YAAY,CAI3B;IAED,OAAO,CAAC,OAAO;IAMf,wFAAwF;YAC1E,UAAU;IAMxB,OAAO,CAAC,QAAQ;IAehB,OAAO,CAAC,SAAS;CAGlB"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { tenantScopeOf } from '@ddd-ts-kit/kernel';
|
|
1
|
+
import { tenantScopeOf, } from '@ddd-ts-kit/kernel';
|
|
2
2
|
import { insertIgnoringConflict } from '../sql/insert-ignoring-conflict.js';
|
|
3
3
|
import { TENANT_COLUMN, whereTenant } from '../tenancy/where-tenant.js';
|
|
4
4
|
import { recordsOf } from './records-of.js';
|
|
@@ -7,10 +7,14 @@ import { RowMapper } from './row.mapper.js';
|
|
|
7
7
|
export class KnexStampedRows {
|
|
8
8
|
context;
|
|
9
9
|
options;
|
|
10
|
+
traces;
|
|
10
11
|
mapper;
|
|
11
|
-
constructor(context, options
|
|
12
|
+
constructor(context, options,
|
|
13
|
+
/** Of the table itself, which the copy of a rebuild is made like. */
|
|
14
|
+
traces) {
|
|
12
15
|
this.context = context;
|
|
13
16
|
this.options = options;
|
|
17
|
+
this.traces = traces;
|
|
14
18
|
this.mapper = new RowMapper(options.columns);
|
|
15
19
|
}
|
|
16
20
|
async find(key) {
|
|
@@ -25,25 +29,28 @@ export class KnexStampedRows {
|
|
|
25
29
|
row: record['deleted'] === true ? undefined : this.mapper.toRow(record),
|
|
26
30
|
stamp: {
|
|
27
31
|
eventId: String(record['last_event_id']),
|
|
28
|
-
occurredAt:
|
|
32
|
+
occurredAt: instantOf(record['last_event_at']),
|
|
29
33
|
...(version === null || version === undefined ? {} : { version: Number(version) }),
|
|
30
34
|
},
|
|
31
35
|
};
|
|
32
36
|
}
|
|
33
37
|
async insert(entry) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
await this.holdsTrace(entry);
|
|
39
|
+
// Two writers of one key inserting at once are not refused for another unique value of the row
|
|
40
|
+
// they both write: `insertIgnoringConflict` names the key only once a first statement skipped.
|
|
37
41
|
return insertIgnoringConflict(this.context.executor(), this.table(), {
|
|
38
42
|
row: this.recordOf(entry),
|
|
39
43
|
conflict: this.keyColumn(),
|
|
40
44
|
});
|
|
41
45
|
}
|
|
42
46
|
async replace(entry, held) {
|
|
47
|
+
await this.holdsTrace(entry);
|
|
43
48
|
const replaced = await this.writing()
|
|
44
49
|
.where(this.keyColumn(), entry.key)
|
|
45
50
|
.where('last_event_id', held.eventId)
|
|
46
51
|
.where('last_event_at', held.occurredAt)
|
|
52
|
+
// The whole stamp: null is written `is null`, so a row whose version alone moved is not replaced.
|
|
53
|
+
.where('last_event_version', held.version ?? null)
|
|
47
54
|
.update(this.recordOf(entry));
|
|
48
55
|
return Number(replaced) === 1;
|
|
49
56
|
}
|
|
@@ -62,10 +69,17 @@ export class KnexStampedRows {
|
|
|
62
69
|
const { tenant } = this.options;
|
|
63
70
|
return tenant === undefined ? this.table() : this.table().where(TENANT_COLUMN, tenantScopeOf(tenant).tenantId);
|
|
64
71
|
}
|
|
72
|
+
/** Before the trace of a deleted row is written: the columns of the table accept it. */
|
|
73
|
+
async holdsTrace({ row }) {
|
|
74
|
+
if (row === undefined) {
|
|
75
|
+
await this.traces.require();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
65
78
|
recordOf({ key, row, stamp }) {
|
|
66
79
|
const { tenant } = this.options;
|
|
67
80
|
return {
|
|
68
|
-
|
|
81
|
+
// A deleted row keeps no data: every column of the row is written NULL, not left to its default.
|
|
82
|
+
...(row === undefined ? this.mapper.toNulls() : this.mapper.toRecord(row)),
|
|
69
83
|
...(tenant === undefined ? {} : { [TENANT_COLUMN]: tenantScopeOf(tenant).tenantId }),
|
|
70
84
|
[this.keyColumn()]: key,
|
|
71
85
|
last_event_id: stamp.eventId,
|
|
@@ -78,4 +92,8 @@ export class KnexStampedRows {
|
|
|
78
92
|
return this.options.keyColumn ?? 'id';
|
|
79
93
|
}
|
|
80
94
|
}
|
|
95
|
+
/** The instant as the driver gave it: the text of a `Date` has no milliseconds, so a date is never read through it. */
|
|
96
|
+
function instantOf(value) {
|
|
97
|
+
return value instanceof Date ? value : new Date(String(value));
|
|
98
|
+
}
|
|
81
99
|
//# sourceMappingURL=knex-stamped-rows.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"knex-stamped-rows.js","sourceRoot":"","sources":["../../src/read-model/knex-stamped-rows.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"knex-stamped-rows.js","sourceRoot":"","sources":["../../src/read-model/knex-stamped-rows.ts"],"names":[],"mappings":"AACA,OAAO,EACL,aAAa,GAKd,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AACzE,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAGrE,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC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U;YAC/B,kBAAkB,EAAE,KAAK,CAAC,OAAO,IAAI,IAAI;YACzC,OAAO,EAAE,GAAG,KAAK,SAAS;SAC3B,CAAC;IACJ,CAAC;IAEO,SAAS;QACf,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,IAAI,CAAC;IACxC,CAAC;CACF;AAED,uHAAuH;AACvH,SAAS,SAAS,CAAC,KAAc;IAC/B,OAAO,KAAK,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AACjE,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { TextSearch } from '@ddd-ts-kit/kernel';
|
|
2
2
|
/** The `LIKE` pattern of a text search, the text itself taken literally: `%`, `_` and `\` are escaped by `\`. */
|
|
3
|
-
export declare function likePattern(operator:
|
|
3
|
+
export declare function likePattern(operator: TextSearch, text: string): string;
|
|
4
4
|
//# sourceMappingURL=like-pattern.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"like-pattern.d.ts","sourceRoot":"","sources":["../../src/read-model/like-pattern.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"like-pattern.d.ts","sourceRoot":"","sources":["../../src/read-model/like-pattern.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD,iHAAiH;AACjH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAItE"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** The `LIKE` pattern of a text search, the text itself taken literally: `%`, `_` and `\` are escaped by `\`. */
|
|
2
2
|
export function likePattern(operator, text) {
|
|
3
3
|
const literal = text.replace(/[\\%_]/g, (special) => `\\${special}`);
|
|
4
|
-
return operator === 'startsWith' ? `${literal}%` : `%${literal}%`;
|
|
4
|
+
return operator === 'startsWith' || operator === 'startsWithIgnoringCase' ? `${literal}%` : `%${literal}%`;
|
|
5
5
|
}
|
|
6
6
|
//# sourceMappingURL=like-pattern.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"like-pattern.js","sourceRoot":"","sources":["../../src/read-model/like-pattern.ts"],"names":[],"mappings":"AAEA,iHAAiH;AACjH,MAAM,UAAU,WAAW,CAAC,
|
|
1
|
+
{"version":3,"file":"like-pattern.js","sourceRoot":"","sources":["../../src/read-model/like-pattern.ts"],"names":[],"mappings":"AAEA,iHAAiH;AACjH,MAAM,UAAU,WAAW,CAAC,QAAoB,EAAE,IAAY;IAC5D,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,KAAK,OAAO,EAAE,CAAC,CAAC;IAErE,OAAO,QAAQ,KAAK,YAAY,IAAI,QAAQ,KAAK,wBAAwB,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,CAAC,CAAC,CAAC,IAAI,OAAO,GAAG,CAAC;AAC7G,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Knex } from 'knex';
|
|
2
|
-
import type
|
|
2
|
+
import { type Predicate } from '@ddd-ts-kit/kernel';
|
|
3
3
|
type Columns = Readonly<Record<string, string>>;
|
|
4
4
|
/**
|
|
5
5
|
* Adds a predicate of a criteria plan to a query, as one parenthesised condition. Every
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"predicate-to-knex.d.ts","sourceRoot":"","sources":["../../src/read-model/predicate-to-knex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"predicate-to-knex.d.ts","sourceRoot":"","sources":["../../src/read-model/predicate-to-knex.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAKL,KAAK,SAAS,EAEf,MAAM,oBAAoB,CAAC;AAI5B,KAAK,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAkBhD;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,OAAO,SAAS,IAAI,CAAC,YAAY,EAC/D,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,SAAS,GAAG,SAAS,EAChC,OAAO,EAAE,OAAO,GACf,OAAO,CAUT"}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
import { isTextSearch, } from '@ddd-ts-kit/kernel';
|
|
1
2
|
import { columnOf } from './column-of.js';
|
|
2
3
|
import { likePattern } from './like-pattern.js';
|
|
4
|
+
/** A search that ignores case is an `ilike`: PostgreSQL folds it with the locale of the database, see `foldCase` of the kernel. */
|
|
5
|
+
const LIKE = {
|
|
6
|
+
contains: '?? like ? escape ?',
|
|
7
|
+
startsWith: '?? like ? escape ?',
|
|
8
|
+
containsIgnoringCase: '?? ilike ? escape ?',
|
|
9
|
+
startsWithIgnoringCase: '?? ilike ? escape ?',
|
|
10
|
+
};
|
|
3
11
|
const COMPARISONS = { eq: '=', ne: '<>', gt: '>', gte: '>=', lt: '<', lte: '<=' };
|
|
4
12
|
/**
|
|
5
13
|
* Adds a predicate of a criteria plan to a query, as one parenthesised condition. Every
|
|
@@ -45,6 +53,10 @@ function tested(builder, test, column) {
|
|
|
45
53
|
function compared(builder, { operator, value }, column) {
|
|
46
54
|
const values = typeof value === 'object' && !(value instanceof Date) ? [...value] : [value];
|
|
47
55
|
const [first = null, second = null] = values;
|
|
56
|
+
if (isTextSearch(operator)) {
|
|
57
|
+
void builder.whereRaw(LIKE[operator], [column, likePattern(operator, String(first)), '\\']);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
48
60
|
switch (operator) {
|
|
49
61
|
case 'in':
|
|
50
62
|
void builder.whereIn(column, values);
|
|
@@ -52,10 +64,6 @@ function compared(builder, { operator, value }, column) {
|
|
|
52
64
|
case 'between':
|
|
53
65
|
void builder.whereBetween(column, [first, second]);
|
|
54
66
|
break;
|
|
55
|
-
case 'contains':
|
|
56
|
-
case 'startsWith':
|
|
57
|
-
void builder.whereRaw('?? like ? escape ?', [column, likePattern(operator, String(first)), '\\']);
|
|
58
|
-
break;
|
|
59
67
|
default:
|
|
60
68
|
void builder.where(column, COMPARISONS[operator], first);
|
|
61
69
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"predicate-to-knex.js","sourceRoot":"","sources":["../../src/read-model/predicate-to-knex.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"predicate-to-knex.js","sourceRoot":"","sources":["../../src/read-model/predicate-to-knex.ts"],"names":[],"mappings":"AACA,OAAO,EACL,YAAY,GAMb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAU7C,mIAAmI;AACnI,MAAM,IAAI,GAAG;IACX,QAAQ,EAAE,oBAAoB;IAC9B,UAAU,EAAE,oBAAoB;IAChC,oBAAoB,EAAE,qBAAqB;IAC3C,sBAAsB,EAAE,qBAAqB;CACrC,CAAC;AAEX,MAAM,WAAW,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAW,CAAC;AAE3F;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAC7B,OAAgB,EAChB,SAAgC,EAChC,OAAgB;IAEhB,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC5B,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC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declare class RowMapper<Row extends object> {
|
|
|
3
3
|
private readonly columns;
|
|
4
4
|
constructor(columns: Readonly<Record<string, string>>);
|
|
5
5
|
toRow(record: Readonly<Record<string, unknown>>): Row;
|
|
6
|
+
/** NULL in every column of the row: what a deleted row holds. */
|
|
7
|
+
toNulls(): Record<string, null>;
|
|
6
8
|
toRecord(row: Row): Record<string, unknown>;
|
|
7
9
|
}
|
|
8
10
|
//# sourceMappingURL=row.mapper.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"row.mapper.d.ts","sourceRoot":"","sources":["../../src/read-model/row.mapper.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,qBAAa,SAAS,CAAC,GAAG,SAAS,MAAM;IAC3B,OAAO,CAAC,QAAQ,CAAC,OAAO;IAApC,YAA6B,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAI;IAE1E,KAAK,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,GAAG,CAIpD;IAED,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAQ1C;CACF"}
|
|
1
|
+
{"version":3,"file":"row.mapper.d.ts","sourceRoot":"","sources":["../../src/read-model/row.mapper.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,qBAAa,SAAS,CAAC,GAAG,SAAS,MAAM;IAC3B,OAAO,CAAC,QAAQ,CAAC,OAAO;IAApC,YAA6B,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAI;IAE1E,KAAK,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,GAAG,CAIpD;IAED,iEAAiE;IACjE,OAAO,IAAI,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAE9B;IAED,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAQ1C;CACF"}
|
|
@@ -7,6 +7,10 @@ export class RowMapper {
|
|
|
7
7
|
toRow(record) {
|
|
8
8
|
return Object.fromEntries(Object.entries(this.columns).map(([field, column]) => [field, record[column] ?? null]));
|
|
9
9
|
}
|
|
10
|
+
/** NULL in every column of the row: what a deleted row holds. */
|
|
11
|
+
toNulls() {
|
|
12
|
+
return Object.fromEntries(Object.values(this.columns).map((column) => [column, null]));
|
|
13
|
+
}
|
|
10
14
|
toRecord(row) {
|
|
11
15
|
return Object.fromEntries(Object.entries(this.columns).map(([field, column]) => {
|
|
12
16
|
const value = Reflect.get(row, field);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"row.mapper.js","sourceRoot":"","sources":["../../src/read-model/row.mapper.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,MAAM,OAAO,SAAS;IACS,OAAO;IAApC,YAA6B,OAAyC;uBAAzC,OAAO;IAAqC,CAAC;IAE1E,KAAK,CAAC,MAAyC;QAC7C,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,CAChF,CAAC;IACX,CAAC;IAED,QAAQ,CAAC,GAAQ;QACf,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,EAAE;YACnD,MAAM,KAAK,GAAY,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAE/C,OAAO,CAAC,MAAM,EAAE,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QACtD,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;CACF"}
|
|
1
|
+
{"version":3,"file":"row.mapper.js","sourceRoot":"","sources":["../../src/read-model/row.mapper.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,MAAM,OAAO,SAAS;IACS,OAAO;IAApC,YAA6B,OAAyC;uBAAzC,OAAO;IAAqC,CAAC;IAE1E,KAAK,CAAC,MAAyC;QAC7C,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,CAChF,CAAC;IACX,CAAC;IAED,iEAAiE;IACjE,OAAO;QACL,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;IACzF,CAAC;IAED,QAAQ,CAAC,GAAQ;QACf,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,EAAE;YACnD,MAAM,KAAK,GAAY,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAE/C,OAAO,CAAC,MAAM,EAAE,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QACtD,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -15,6 +15,12 @@ export interface IgnoringInsert {
|
|
|
15
15
|
* too: when it wrote nothing, one query asks whether a row holds the key, and when none does,
|
|
16
16
|
* a `UniqueViolationError` is raised, as the other databases answer.
|
|
17
17
|
*
|
|
18
|
+
* On PostgreSQL that takes two statements at most. A conflict target on the key alone lets two
|
|
19
|
+
* inserts of one key made at once meet on the other unique index first, and one of them is then
|
|
20
|
+
* refused for a value no other row holds. The first statement skips on any unique value; when it
|
|
21
|
+
* wrote nothing, the second names the key: `false` for a key that is held, and the refusal of
|
|
22
|
+
* the database for a value another row holds.
|
|
23
|
+
*
|
|
18
24
|
* `table` is the query builder of the target table, built on `executor`:
|
|
19
25
|
* `insertIgnoringConflict(db, db('invoices').withSchema('billing'), { row, conflict: 'id' })`.
|
|
20
26
|
*/
|
|
@@ -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;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
|
|
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;;;;;;;;;;;;;;;;;;;GAmBG;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,CAoBlB"}
|
|
@@ -11,6 +11,12 @@ import { dialectOf } from './dialect-of.js';
|
|
|
11
11
|
* too: when it wrote nothing, one query asks whether a row holds the key, and when none does,
|
|
12
12
|
* a `UniqueViolationError` is raised, as the other databases answer.
|
|
13
13
|
*
|
|
14
|
+
* On PostgreSQL that takes two statements at most. A conflict target on the key alone lets two
|
|
15
|
+
* inserts of one key made at once meet on the other unique index first, and one of them is then
|
|
16
|
+
* refused for a value no other row holds. The first statement skips on any unique value; when it
|
|
17
|
+
* wrote nothing, the second names the key: `false` for a key that is held, and the refusal of
|
|
18
|
+
* the database for a value another row holds.
|
|
19
|
+
*
|
|
14
20
|
* `table` is the query builder of the target table, built on `executor`:
|
|
15
21
|
* `insertIgnoringConflict(db, db('invoices').withSchema('billing'), { row, conflict: 'id' })`.
|
|
16
22
|
*/
|
|
@@ -18,13 +24,21 @@ export async function insertIgnoringConflict(executor, table, { row, conflict })
|
|
|
18
24
|
const columns = typeof conflict === 'string' ? [conflict] : [...conflict];
|
|
19
25
|
// Before the insert, which changes the builder: the lookup of the key on MySQL reads the same table.
|
|
20
26
|
const lookup = table.clone();
|
|
27
|
+
const skipping = table.clone();
|
|
21
28
|
const insert = table.insert(row).onConflict(columns).ignore();
|
|
22
|
-
|
|
29
|
+
const dialect = dialectOf(executor);
|
|
30
|
+
if (dialect === 'mysql') {
|
|
23
31
|
return (await wroteOnMySql(executor, insert)) || keyTaken(lookup, row, columns);
|
|
24
32
|
}
|
|
33
|
+
const key = columns[0] ?? '*';
|
|
25
34
|
// RETURNING yields the row only when it was written.
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
if (dialect === 'postgres' && (await wrote(skipping.insert(row).onConflict().ignore().returning(key)))) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
return wrote(insert.returning(key));
|
|
39
|
+
}
|
|
40
|
+
async function wrote(insert) {
|
|
41
|
+
return (await insert).length === 1;
|
|
28
42
|
}
|
|
29
43
|
/** Whether a row holds the key of `row`; when none does, the row was skipped for another unique value. */
|
|
30
44
|
async function keyTaken(lookup, row, columns) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insert-ignoring-conflict.js","sourceRoot":"","sources":["../../src/sql/insert-ignoring-conflict.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAQzC
|
|
1
|
+
{"version":3,"file":"insert-ignoring-conflict.js","sourceRoot":"","sources":["../../src/sql/insert-ignoring-conflict.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAQzC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,QAAc,EACd,KAAwB,EACxB,EAAE,GAAG,EAAE,QAAQ,EAAkB;IAEjC,MAAM,OAAO,GAAG,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC;IAC1E,qGAAqG;IACrG,MAAM,MAAM,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IAC7B,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;IAC/B,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC;IAC9D,MAAM,OAAO,GAAG,SAAS,CAAC,QAAQ,C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C,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.
|
|
3
|
+
"version": "0.112.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",
|
|
@@ -41,16 +41,16 @@
|
|
|
41
41
|
"dist"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@ddd-ts-kit/kernel": "0.
|
|
45
|
-
"@ddd-ts-kit/events": "0.
|
|
46
|
-
"@ddd-ts-kit/scheduling": "0.
|
|
44
|
+
"@ddd-ts-kit/kernel": "0.112.0",
|
|
45
|
+
"@ddd-ts-kit/events": "0.112.0",
|
|
46
|
+
"@ddd-ts-kit/scheduling": "0.112.0"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"knex": "^3.0.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"knex": "^3.3.0",
|
|
53
|
-
"@ddd-ts-kit/testing": "0.
|
|
53
|
+
"@ddd-ts-kit/testing": "0.112.0",
|
|
54
54
|
"pg": "^8.23.1",
|
|
55
55
|
"@types/pg": "^8.23.1",
|
|
56
56
|
"@electric-sql/pglite": "0.5.15",
|