@ddd-ts-kit/knex 0.0.0-stage → 0.1.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.
Files changed (122) hide show
  1. package/README.md +545 -2
  2. package/dist/columns/audit-columns.d.ts +7 -0
  3. package/dist/columns/audit-columns.d.ts.map +1 -0
  4. package/dist/columns/audit-columns.js +13 -0
  5. package/dist/columns/audit-columns.js.map +1 -0
  6. package/dist/columns/audit-row.d.ts +16 -0
  7. package/dist/columns/audit-row.d.ts.map +1 -0
  8. package/dist/columns/audit-row.js +5 -0
  9. package/dist/columns/audit-row.js.map +1 -0
  10. package/dist/columns/indexable-length.d.ts +3 -0
  11. package/dist/columns/indexable-length.d.ts.map +1 -0
  12. package/dist/columns/indexable-length.js +3 -0
  13. package/dist/columns/indexable-length.js.map +1 -0
  14. package/dist/columns/instant.d.ts +6 -0
  15. package/dist/columns/instant.d.ts.map +1 -0
  16. package/dist/columns/instant.js +3 -0
  17. package/dist/columns/instant.js.map +1 -0
  18. package/dist/columns/tenant-column.d.ts +4 -0
  19. package/dist/columns/tenant-column.d.ts.map +1 -0
  20. package/dist/columns/tenant-column.js +6 -0
  21. package/dist/columns/tenant-column.js.map +1 -0
  22. package/dist/columns/tenant-row.d.ts +7 -0
  23. package/dist/columns/tenant-row.d.ts.map +1 -0
  24. package/dist/columns/tenant-row.js +5 -0
  25. package/dist/columns/tenant-row.js.map +1 -0
  26. package/dist/index.d.ts +21 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.js +21 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/outbox/builder-in.d.ts +3 -0
  31. package/dist/outbox/builder-in.d.ts.map +1 -0
  32. package/dist/outbox/builder-in.js +4 -0
  33. package/dist/outbox/builder-in.js.map +1 -0
  34. package/dist/outbox/create-outbox-tables.d.ts +9 -0
  35. package/dist/outbox/create-outbox-tables.d.ts.map +1 -0
  36. package/dist/outbox/create-outbox-tables.js +97 -0
  37. package/dist/outbox/create-outbox-tables.js.map +1 -0
  38. package/dist/outbox/drop-outbox-tables.d.ts +5 -0
  39. package/dist/outbox/drop-outbox-tables.d.ts.map +1 -0
  40. package/dist/outbox/drop-outbox-tables.js +8 -0
  41. package/dist/outbox/drop-outbox-tables.js.map +1 -0
  42. package/dist/outbox/knex-outbox-store.d.ts +61 -0
  43. package/dist/outbox/knex-outbox-store.d.ts.map +1 -0
  44. package/dist/outbox/knex-outbox-store.js +184 -0
  45. package/dist/outbox/knex-outbox-store.js.map +1 -0
  46. package/dist/outbox/knex-processed-event-store.d.ts +21 -0
  47. package/dist/outbox/knex-processed-event-store.d.ts.map +1 -0
  48. package/dist/outbox/knex-processed-event-store.js +26 -0
  49. package/dist/outbox/knex-processed-event-store.js.map +1 -0
  50. package/dist/outbox/outbox-tables-options.d.ts +12 -0
  51. package/dist/outbox/outbox-tables-options.d.ts.map +1 -0
  52. package/dist/outbox/outbox-tables-options.js +2 -0
  53. package/dist/outbox/outbox-tables-options.js.map +1 -0
  54. package/dist/outbox/table-names.d.ts +3 -0
  55. package/dist/outbox/table-names.d.ts.map +1 -0
  56. package/dist/outbox/table-names.js +3 -0
  57. package/dist/outbox/table-names.js.map +1 -0
  58. package/dist/read-model/column-of.d.ts +3 -0
  59. package/dist/read-model/column-of.d.ts.map +1 -0
  60. package/dist/read-model/column-of.js +9 -0
  61. package/dist/read-model/column-of.js.map +1 -0
  62. package/dist/read-model/criteria-to-knex.d.ts +11 -0
  63. package/dist/read-model/criteria-to-knex.d.ts.map +1 -0
  64. package/dist/read-model/criteria-to-knex.js +28 -0
  65. package/dist/read-model/criteria-to-knex.js.map +1 -0
  66. package/dist/read-model/knex-read-model-options.d.ts +10 -0
  67. package/dist/read-model/knex-read-model-options.d.ts.map +1 -0
  68. package/dist/read-model/knex-read-model-options.js +2 -0
  69. package/dist/read-model/knex-read-model-options.js.map +1 -0
  70. package/dist/read-model/knex-read-model-store.d.ts +16 -0
  71. package/dist/read-model/knex-read-model-store.d.ts.map +1 -0
  72. package/dist/read-model/knex-read-model-store.js +13 -0
  73. package/dist/read-model/knex-read-model-store.js.map +1 -0
  74. package/dist/read-model/knex-read-model-table.d.ts +32 -0
  75. package/dist/read-model/knex-read-model-table.d.ts.map +1 -0
  76. package/dist/read-model/knex-read-model-table.js +83 -0
  77. package/dist/read-model/knex-read-model-table.js.map +1 -0
  78. package/dist/read-model/knex-stamped-rows.d.ts +19 -0
  79. package/dist/read-model/knex-stamped-rows.d.ts.map +1 -0
  80. package/dist/read-model/knex-stamped-rows.js +68 -0
  81. package/dist/read-model/knex-stamped-rows.js.map +1 -0
  82. package/dist/read-model/like-pattern.d.ts +4 -0
  83. package/dist/read-model/like-pattern.d.ts.map +1 -0
  84. package/dist/read-model/like-pattern.js +6 -0
  85. package/dist/read-model/like-pattern.js.map +1 -0
  86. package/dist/read-model/predicate-to-knex.d.ts +11 -0
  87. package/dist/read-model/predicate-to-knex.d.ts.map +1 -0
  88. package/dist/read-model/predicate-to-knex.js +63 -0
  89. package/dist/read-model/predicate-to-knex.js.map +1 -0
  90. package/dist/read-model/read-model-columns.d.ts +8 -0
  91. package/dist/read-model/read-model-columns.d.ts.map +1 -0
  92. package/dist/read-model/read-model-columns.js +14 -0
  93. package/dist/read-model/read-model-columns.js.map +1 -0
  94. package/dist/read-model/records-of.d.ts +3 -0
  95. package/dist/read-model/records-of.d.ts.map +1 -0
  96. package/dist/read-model/records-of.js +9 -0
  97. package/dist/read-model/records-of.js.map +1 -0
  98. package/dist/read-model/row.mapper.d.ts +8 -0
  99. package/dist/read-model/row.mapper.d.ts.map +1 -0
  100. package/dist/read-model/row.mapper.js +17 -0
  101. package/dist/read-model/row.mapper.js.map +1 -0
  102. package/dist/sql/dialect-of.d.ts +4 -0
  103. package/dist/sql/dialect-of.d.ts.map +1 -0
  104. package/dist/sql/dialect-of.js +17 -0
  105. package/dist/sql/dialect-of.js.map +1 -0
  106. package/dist/sql/insert-ignoring-conflict.d.ts +16 -0
  107. package/dist/sql/insert-ignoring-conflict.d.ts.map +1 -0
  108. package/dist/sql/insert-ignoring-conflict.js +28 -0
  109. package/dist/sql/insert-ignoring-conflict.js.map +1 -0
  110. package/dist/sql/sql-dialect.d.ts +3 -0
  111. package/dist/sql/sql-dialect.d.ts.map +1 -0
  112. package/dist/sql/sql-dialect.js +2 -0
  113. package/dist/sql/sql-dialect.js.map +1 -0
  114. package/dist/unit-of-work/create-knex-unit-of-work.d.ts +14 -0
  115. package/dist/unit-of-work/create-knex-unit-of-work.d.ts.map +1 -0
  116. package/dist/unit-of-work/create-knex-unit-of-work.js +24 -0
  117. package/dist/unit-of-work/create-knex-unit-of-work.js.map +1 -0
  118. package/dist/unit-of-work/knex-context.d.ts +13 -0
  119. package/dist/unit-of-work/knex-context.d.ts.map +1 -0
  120. package/dist/unit-of-work/knex-context.js +10 -0
  121. package/dist/unit-of-work/knex-context.js.map +1 -0
  122. package/package.json +41 -4
@@ -0,0 +1,28 @@
1
+ import { dialectOf } from './dialect-of.js';
2
+ /**
3
+ * Inserts one row unless its key already exists, and says whether it did insert. Unlike
4
+ * catching the unique violation, it leaves the surrounding transaction usable on PostgreSQL.
5
+ * It serves the processed-event store and the first save of an aggregate alike.
6
+ *
7
+ * `table` is the query builder of the target table, built on `executor`:
8
+ * `insertIgnoringConflict(db, db('invoices').withSchema('billing'), { row, conflict: 'id' })`.
9
+ */
10
+ export async function insertIgnoringConflict(executor, table, { row, conflict }) {
11
+ const columns = typeof conflict === 'string' ? [conflict] : [...conflict];
12
+ const insert = table.insert(row).onConflict(columns).ignore();
13
+ if (dialectOf(executor) === 'mysql') {
14
+ return wroteOnMySql(executor, insert);
15
+ }
16
+ // RETURNING yields the row only when it was written.
17
+ const inserted = await insert.returning(columns[0] ?? '*');
18
+ return inserted.length === 1;
19
+ }
20
+ // No RETURNING in MySQL and knex only reports the insert id: the driver's own result header
21
+ // is the one place that says whether INSERT IGNORE wrote the row.
22
+ async function wroteOnMySql(executor, insert) {
23
+ const { sql, bindings } = insert.toSQL().toNative();
24
+ const result = await executor.raw(sql, bindings);
25
+ const header = Array.isArray(result) ? result[0] : result;
26
+ return typeof header === 'object' && header !== null && 'affectedRows' in header && header.affectedRows === 1;
27
+ }
28
+ //# sourceMappingURL=insert-ignoring-conflict.js.map
@@ -0,0 +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,iBAAiB,CAAC;AAQ5C;;;;;;;GAOG;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,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC;IAE9D,IAAI,SAAS,CAAC,QAAQ,CAAC,KAAK,OAAO,EAAE,CAAC;QACpC,OAAO,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IACxC,CAAC;IAED,qDAAqD;IACrD,MAAM,QAAQ,GAAc,MAAM,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC;IAEtE,OAAO,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAC;AAC/B,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"}
@@ -0,0 +1,3 @@
1
+ /** The dialect families the adapters tell apart; everything else takes the portable path. */
2
+ export type SqlDialect = 'postgres' | 'mysql' | 'sqlite' | 'other';
3
+ //# sourceMappingURL=sql-dialect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sql-dialect.d.ts","sourceRoot":"","sources":["../../src/sql/sql-dialect.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,MAAM,MAAM,UAAU,GAAG,UAAU,GAAG,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=sql-dialect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sql-dialect.js","sourceRoot":"","sources":["../../src/sql/sql-dialect.ts"],"names":[],"mappings":""}
@@ -0,0 +1,14 @@
1
+ import type { Knex } from 'knex';
2
+ import { type UnitOfWork } from '@ddd-ts-kit/kernel';
3
+ import type { KnexContext } from './knex-context.js';
4
+ /** One object for both ports: bind it to `UnitOfWork` and to `KnexContext`. */
5
+ export interface KnexUnitOfWork extends UnitOfWork, KnexContext {
6
+ }
7
+ /**
8
+ * A unit of work over `knex.transaction`. The transaction travels in an AsyncLocalStorage
9
+ * owned by this unit of work, so two databases never see each other's transaction. A nested
10
+ * `run` joins the outer transaction: it commits or rolls back as a whole. Domain events are
11
+ * dispatched at the end of the outermost `run`, before its commit (`withinUnitOfWork`).
12
+ */
13
+ export declare function createKnexUnitOfWork(knex: Knex, config?: Knex.TransactionConfig): KnexUnitOfWork;
14
+ //# sourceMappingURL=create-knex-unit-of-work.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-knex-unit-of-work.d.ts","sourceRoot":"","sources":["../../src/unit-of-work/create-knex-unit-of-work.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAoB,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACvE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,+EAA+E;AAC/E,MAAM,WAAW,cAAe,SAAQ,UAAU,EAAE,WAAW;CAAG;AAElE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,IAAI,CAAC,iBAAiB,GAAG,cAAc,CAgBhG"}
@@ -0,0 +1,24 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+ import { withinUnitOfWork } from '@ddd-ts-kit/kernel';
3
+ /**
4
+ * A unit of work over `knex.transaction`. The transaction travels in an AsyncLocalStorage
5
+ * owned by this unit of work, so two databases never see each other's transaction. A nested
6
+ * `run` joins the outer transaction: it commits or rolls back as a whole. Domain events are
7
+ * dispatched at the end of the outermost `run`, before its commit (`withinUnitOfWork`).
8
+ */
9
+ export function createKnexUnitOfWork(knex, config) {
10
+ const current = new AsyncLocalStorage();
11
+ return {
12
+ run(work) {
13
+ return withinUnitOfWork(work, (scoped) => {
14
+ if (current.getStore() !== undefined) {
15
+ return scoped();
16
+ }
17
+ return knex.transaction((transaction) => current.run(transaction, scoped), config);
18
+ });
19
+ },
20
+ executor: () => current.getStore() ?? knex,
21
+ inTransaction: () => current.getStore() !== undefined,
22
+ };
23
+ }
24
+ //# sourceMappingURL=create-knex-unit-of-work.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-knex-unit-of-work.js","sourceRoot":"","sources":["../../src/unit-of-work/create-knex-unit-of-work.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,gBAAgB,EAAmB,MAAM,oBAAoB,CAAC;AAMvE;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,IAAU,EAAE,MAA+B;IAC9E,MAAM,OAAO,GAAG,IAAI,iBAAiB,EAAoB,CAAC;IAE1D,OAAO;QACL,GAAG,CAAI,IAAsB;YAC3B,OAAO,gBAAgB,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,EAAE;gBACvC,IAAI,OAAO,CAAC,QAAQ,EAAE,KAAK,SAAS,EAAE,CAAC;oBACrC,OAAO,MAAM,EAAE,CAAC;gBAClB,CAAC;gBAED,OAAO,IAAI,CAAC,WAAW,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;YACrF,CAAC,CAAC,CAAC;QACL,CAAC;QACD,QAAQ,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,IAAI;QAC1C,aAAa,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,SAAS;KACtD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { Knex } from 'knex';
2
+ /**
3
+ * Gives adapters the right handle to run their queries on: the transaction of the current
4
+ * unit of work when there is one, the root knex instance otherwise. Repositories depend on
5
+ * this and never receive a transaction as a parameter. A knex transaction is a `Knex`, hence
6
+ * the single return type.
7
+ */
8
+ export declare abstract class KnexContext {
9
+ static readonly tokenKey = "knex.KnexContext";
10
+ abstract executor(): Knex;
11
+ abstract inTransaction(): boolean;
12
+ }
13
+ //# sourceMappingURL=knex-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"knex-context.d.ts","sourceRoot":"","sources":["../../src/unit-of-work/knex-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAEjC;;;;;GAKG;AACH,8BAAsB,WAAW;IAC/B,MAAM,CAAC,QAAQ,CAAC,QAAQ,sBAAsB;IAE9C,QAAQ,CAAC,QAAQ,IAAI,IAAI,CAAC;IAE1B,QAAQ,CAAC,aAAa,IAAI,OAAO,CAAC;CACnC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Gives adapters the right handle to run their queries on: the transaction of the current
3
+ * unit of work when there is one, the root knex instance otherwise. Repositories depend on
4
+ * this and never receive a transaction as a parameter. A knex transaction is a `Knex`, hence
5
+ * the single return type.
6
+ */
7
+ export class KnexContext {
8
+ static tokenKey = 'knex.KnexContext';
9
+ }
10
+ //# sourceMappingURL=knex-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"knex-context.js","sourceRoot":"","sources":["../../src/unit-of-work/knex-context.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,OAAgB,WAAW;IAC/B,MAAM,CAAU,QAAQ,GAAG,kBAAkB,CAAC;CAK/C"}
package/package.json CHANGED
@@ -1,6 +1,43 @@
1
1
  {
2
2
  "name": "@ddd-ts-kit/knex",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Knex adapters for the kernel ports: unit of work, transactional outbox store, processed-event store.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/gbourgeat/ddd-ts-kit.git",
9
+ "directory": "packages/knex"
10
+ },
11
+ "homepage": "https://github.com/gbourgeat/ddd-ts-kit/tree/main/packages/knex#readme",
12
+ "bugs": "https://github.com/gbourgeat/ddd-ts-kit/issues",
13
+ "publishConfig": {
14
+ "access": "public",
15
+ "provenance": true
16
+ },
17
+ "type": "module",
18
+ "sideEffects": false,
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "dependencies": {
29
+ "@ddd-ts-kit/kernel": "0.1.0",
30
+ "@ddd-ts-kit/events": "0.1.0"
31
+ },
32
+ "peerDependencies": {
33
+ "knex": "^3.0.0"
34
+ },
35
+ "devDependencies": {
36
+ "knex": "^3.3.0",
37
+ "@ddd-ts-kit/testing": "0.1.0",
38
+ "pg": "^8.23.1",
39
+ "@types/pg": "^8.23.1",
40
+ "@electric-sql/pglite": "^0.5.8",
41
+ "@electric-sql/pglite-socket": "^0.2.11"
42
+ }
43
+ }