@remit/drizzle-service 0.0.29 → 0.0.30

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/drizzle-service",
3
- "version": "0.0.29",
3
+ "version": "0.0.30",
4
4
  "description": "Drizzle ORM service parameterized by dialect (Postgres / SQLite)",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ export {
10
10
  export { DrizzleEnvelopeRepository } from "./repos/envelope.js";
11
11
  export { FilterRepo } from "./repos/filter.js";
12
12
  export { FilterAnchorRepo } from "./repos/filter-anchor.js";
13
+ export { DrizzleFilterAnchorTransaction } from "./repos/filter-anchor-transaction.js";
13
14
  export * from "./repos/i4-account.js";
14
15
  export * from "./repos/i4-account-config.js";
15
16
  export * from "./repos/i4-account-export-request.js";
@@ -0,0 +1,136 @@
1
+ import assert from "node:assert";
2
+ import { after, before, describe, test } from "node:test";
3
+ import { FilterScope } from "@remit/domain-enums";
4
+ import { NotFoundError } from "../error.js";
5
+ import { createTestDb, randomId, type TestDb } from "../test-db.js";
6
+ import { FilterRepo } from "./filter.js";
7
+ import { FilterAnchorRepo } from "./filter-anchor.js";
8
+ import { DrizzleFilterAnchorTransaction } from "./filter-anchor-transaction.js";
9
+
10
+ describe("DrizzleFilterAnchorTransaction", () => {
11
+ let db: TestDb;
12
+ let close: () => Promise<void>;
13
+ let transaction: DrizzleFilterAnchorTransaction;
14
+ let filterRepo: FilterRepo;
15
+ let filterAnchorRepo: FilterAnchorRepo;
16
+
17
+ before(async () => {
18
+ ({ db, close } = await createTestDb());
19
+ transaction = new DrizzleFilterAnchorTransaction(db as never);
20
+ filterRepo = new FilterRepo(db as never);
21
+ filterAnchorRepo = new FilterAnchorRepo(db as never);
22
+ });
23
+
24
+ after(async () => {
25
+ await close();
26
+ });
27
+
28
+ test("creates the Filter and its FilterAnchor together", async () => {
29
+ const accountConfigId = randomId();
30
+
31
+ const filter = await transaction.createWithAnchor(
32
+ {
33
+ accountConfigId,
34
+ name: "Booking confirmations",
35
+ scope: FilterScope.Standing,
36
+ hasAnchor: true,
37
+ },
38
+ {
39
+ accountConfigId,
40
+ anchorMessageId: randomId(),
41
+ anchorEmbedding: [0.1, 0.2, 0.3],
42
+ anchorEmbeddingId: "amazon.titan-embed-text-v2:0@1024",
43
+ anchorSourceText: "Your booking is confirmed",
44
+ },
45
+ );
46
+
47
+ assert.equal(filter.hasAnchor, true);
48
+ const anchor = await filterAnchorRepo.get(accountConfigId, filter.filterId);
49
+ assert.ok(anchor, "the FilterAnchor row must exist");
50
+ assert.equal(anchor?.anchorSourceText, "Your booking is confirmed");
51
+ });
52
+
53
+ test("creates a purely-literal Filter when anchor is null", async () => {
54
+ const accountConfigId = randomId();
55
+
56
+ const filter = await transaction.createWithAnchor(
57
+ {
58
+ accountConfigId,
59
+ name: "From billing",
60
+ scope: FilterScope.Standing,
61
+ hasAnchor: false,
62
+ },
63
+ null,
64
+ );
65
+
66
+ assert.equal(filter.hasAnchor, false);
67
+ const anchor = await filterAnchorRepo.get(accountConfigId, filter.filterId);
68
+ assert.equal(anchor, null);
69
+ });
70
+
71
+ test("rolls back the Filter row when the FilterAnchor write fails (#351)", async () => {
72
+ const accountConfigId = randomId();
73
+
74
+ await assert.rejects(() =>
75
+ transaction.createWithAnchor(
76
+ {
77
+ accountConfigId,
78
+ name: "Broken anchor",
79
+ scope: FilterScope.Standing,
80
+ hasAnchor: true,
81
+ },
82
+ {
83
+ accountConfigId,
84
+ anchorMessageId: randomId(),
85
+ anchorEmbedding: [0.1, 0.2, 0.3],
86
+ anchorEmbeddingId: "amazon.titan-embed-text-v2:0@1024",
87
+ // A NOT NULL column at the DB level — simulates a real write
88
+ // failure on the second half of the pair (network blip,
89
+ // transient error), the exact case #351 leaves broken today.
90
+ anchorSourceText: null as unknown as string,
91
+ },
92
+ ),
93
+ );
94
+
95
+ const filters = await filterRepo.listByAccountConfig(accountConfigId);
96
+ assert.equal(
97
+ filters.length,
98
+ 0,
99
+ "the Filter row must not survive when its anchor write fails",
100
+ );
101
+ });
102
+
103
+ test("the caller never sees a Filter row with hasAnchor: true and no anchor (#351)", async () => {
104
+ const accountConfigId = randomId();
105
+
106
+ await assert.rejects(() =>
107
+ transaction.createWithAnchor(
108
+ {
109
+ accountConfigId,
110
+ name: "Broken anchor 2",
111
+ scope: FilterScope.Standing,
112
+ hasAnchor: true,
113
+ },
114
+ {
115
+ accountConfigId,
116
+ anchorMessageId: randomId(),
117
+ anchorEmbedding: [0.1, 0.2, 0.3],
118
+ anchorEmbeddingId: "amazon.titan-embed-text-v2:0@1024",
119
+ anchorSourceText: null as unknown as string,
120
+ },
121
+ ),
122
+ );
123
+
124
+ const filters = await filterRepo.listByAccountConfig(accountConfigId);
125
+ const orphan = filters.find((f) => f.hasAnchor);
126
+ assert.equal(
127
+ orphan,
128
+ undefined,
129
+ "no Filter with hasAnchor: true may exist without a FilterAnchor row",
130
+ );
131
+ });
132
+
133
+ test("get on a never-created filter still throws NotFoundError (sanity)", async () => {
134
+ await assert.rejects(filterRepo.get(randomId(), randomId()), NotFoundError);
135
+ });
136
+ });
@@ -0,0 +1,45 @@
1
+ import type {
2
+ CreateFilterAnchorInput,
3
+ CreateFilterInput,
4
+ FilterItem,
5
+ IFilterAnchorTransaction,
6
+ } from "@remit/data-ports";
7
+ import type { Db } from "../db.js";
8
+ import { runInTransaction } from "../tx.js";
9
+ import { FilterRepo } from "./filter.js";
10
+ import { FilterAnchorRepo } from "./filter-anchor.js";
11
+
12
+ type DB = Db<Record<string, unknown>>;
13
+
14
+ /**
15
+ * Runs the `Filter` create and its optional `FilterAnchor` write in one
16
+ * transaction (#351): a failure on either side rolls back both, so a
17
+ * `Filter` row can never persist with `hasAnchor: true` and no matching
18
+ * `FilterAnchor` row, and a `FilterAnchor` write failure surfaces as a
19
+ * failed create request rather than a silently-broken filter.
20
+ */
21
+ export class DrizzleFilterAnchorTransaction
22
+ implements IFilterAnchorTransaction
23
+ {
24
+ constructor(private db: DB) {}
25
+
26
+ createWithAnchor(
27
+ filterInput: CreateFilterInput,
28
+ anchorInput: Omit<CreateFilterAnchorInput, "filterId"> | null,
29
+ ): Promise<FilterItem> {
30
+ return runInTransaction(this.db, async (tx) => {
31
+ const filterRepo = new FilterRepo(tx as never);
32
+ const filter = await filterRepo.create(filterInput);
33
+
34
+ if (anchorInput) {
35
+ const filterAnchorRepo = new FilterAnchorRepo(tx as never);
36
+ await filterAnchorRepo.put({
37
+ ...anchorInput,
38
+ filterId: filter.filterId,
39
+ });
40
+ }
41
+
42
+ return filter;
43
+ });
44
+ }
45
+ }