@model-ts/dynamodb 4.0.0 → 4.2.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/CHANGELOG.md +12 -0
- package/dist/cjs/__test__/client-env-guard.test.d.ts +1 -0
- package/dist/cjs/__test__/client-env-guard.test.js +28 -0
- package/dist/cjs/__test__/client-env-guard.test.js.map +1 -0
- package/dist/cjs/__test__/conformance.test.d.ts +1 -0
- package/dist/cjs/__test__/conformance.test.js +1835 -0
- package/dist/cjs/__test__/conformance.test.js.map +1 -0
- package/dist/cjs/__test__/in-memory.spec.test.d.ts +1 -0
- package/dist/cjs/__test__/in-memory.spec.test.js +185 -0
- package/dist/cjs/__test__/in-memory.spec.test.js.map +1 -0
- package/dist/cjs/__test__/rollback.test.d.ts +1 -0
- package/dist/cjs/__test__/rollback.test.js +196 -0
- package/dist/cjs/__test__/rollback.test.js.map +1 -0
- package/dist/cjs/client.d.ts +2 -2
- package/dist/cjs/client.js +14 -6
- package/dist/cjs/client.js.map +1 -1
- package/dist/cjs/errors.d.ts +13 -1
- package/dist/cjs/errors.js +12 -1
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/in-memory/document-client.d.ts +45 -0
- package/dist/cjs/in-memory/document-client.js +795 -0
- package/dist/cjs/in-memory/document-client.js.map +1 -0
- package/dist/cjs/in-memory/expression.d.ts +42 -0
- package/dist/cjs/in-memory/expression.js +585 -0
- package/dist/cjs/in-memory/expression.js.map +1 -0
- package/dist/cjs/in-memory/index.d.ts +2 -0
- package/dist/cjs/in-memory/index.js +6 -0
- package/dist/cjs/in-memory/index.js.map +1 -0
- package/dist/cjs/in-memory/spec.d.ts +23 -0
- package/dist/cjs/in-memory/spec.js +141 -0
- package/dist/cjs/in-memory/spec.js.map +1 -0
- package/dist/cjs/in-memory/store.d.ts +73 -0
- package/dist/cjs/in-memory/store.js +267 -0
- package/dist/cjs/in-memory/store.js.map +1 -0
- package/dist/cjs/in-memory/treap.d.ts +31 -0
- package/dist/cjs/in-memory/treap.js +187 -0
- package/dist/cjs/in-memory/treap.js.map +1 -0
- package/dist/cjs/in-memory/utils.d.ts +10 -0
- package/dist/cjs/in-memory/utils.js +38 -0
- package/dist/cjs/in-memory/utils.js.map +1 -0
- package/dist/cjs/sandbox.d.ts +2 -0
- package/dist/cjs/sandbox.js +172 -1
- package/dist/cjs/sandbox.js.map +1 -1
- package/dist/esm/__test__/client-env-guard.test.d.ts +1 -0
- package/dist/esm/__test__/client-env-guard.test.js +26 -0
- package/dist/esm/__test__/client-env-guard.test.js.map +1 -0
- package/dist/esm/__test__/conformance.test.d.ts +1 -0
- package/dist/esm/__test__/conformance.test.js +1833 -0
- package/dist/esm/__test__/conformance.test.js.map +1 -0
- package/dist/esm/__test__/in-memory.spec.test.d.ts +1 -0
- package/dist/esm/__test__/in-memory.spec.test.js +183 -0
- package/dist/esm/__test__/in-memory.spec.test.js.map +1 -0
- package/dist/esm/__test__/rollback.test.d.ts +1 -0
- package/dist/esm/__test__/rollback.test.js +194 -0
- package/dist/esm/__test__/rollback.test.js.map +1 -0
- package/dist/esm/client.d.ts +2 -2
- package/dist/esm/client.js +14 -6
- package/dist/esm/client.js.map +1 -1
- package/dist/esm/errors.d.ts +13 -1
- package/dist/esm/errors.js +10 -0
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/in-memory/document-client.d.ts +45 -0
- package/dist/esm/in-memory/document-client.js +791 -0
- package/dist/esm/in-memory/document-client.js.map +1 -0
- package/dist/esm/in-memory/expression.d.ts +42 -0
- package/dist/esm/in-memory/expression.js +577 -0
- package/dist/esm/in-memory/expression.js.map +1 -0
- package/dist/esm/in-memory/index.d.ts +2 -0
- package/dist/esm/in-memory/index.js +3 -0
- package/dist/esm/in-memory/index.js.map +1 -0
- package/dist/esm/in-memory/spec.d.ts +23 -0
- package/dist/esm/in-memory/spec.js +138 -0
- package/dist/esm/in-memory/spec.js.map +1 -0
- package/dist/esm/in-memory/store.d.ts +73 -0
- package/dist/esm/in-memory/store.js +258 -0
- package/dist/esm/in-memory/store.js.map +1 -0
- package/dist/esm/in-memory/treap.d.ts +31 -0
- package/dist/esm/in-memory/treap.js +183 -0
- package/dist/esm/in-memory/treap.js.map +1 -0
- package/dist/esm/in-memory/utils.d.ts +10 -0
- package/dist/esm/in-memory/utils.js +28 -0
- package/dist/esm/in-memory/utils.js.map +1 -0
- package/dist/esm/sandbox.d.ts +2 -0
- package/dist/esm/sandbox.js +172 -1
- package/dist/esm/sandbox.js.map +1 -1
- package/package.json +2 -1
- package/src/__test__/client-env-guard.test.ts +31 -0
- package/src/__test__/conformance.test.ts +2042 -0
- package/src/__test__/in-memory.spec.test.ts +230 -0
- package/src/__test__/rollback.test.ts +279 -0
- package/src/client.ts +17 -4
- package/src/errors.ts +24 -0
- package/src/in-memory/document-client.ts +1140 -0
- package/src/in-memory/expression.ts +730 -0
- package/src/in-memory/index.ts +2 -0
- package/src/in-memory/spec.ts +159 -0
- package/src/in-memory/store.ts +360 -0
- package/src/in-memory/treap.ts +239 -0
- package/src/in-memory/utils.ts +45 -0
- package/src/sandbox.ts +227 -1
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import * as t from "io-ts"
|
|
2
|
+
import { model } from "@model-ts/core"
|
|
3
|
+
import { Client } from "../client"
|
|
4
|
+
import { getProvider } from "../provider"
|
|
5
|
+
import { createSandbox, Sandbox } from "../sandbox"
|
|
6
|
+
import { NotSupportedError } from "../errors"
|
|
7
|
+
import { IN_MEMORY_SPEC } from "../in-memory/spec"
|
|
8
|
+
|
|
9
|
+
const CODEC = t.type({
|
|
10
|
+
id: t.string,
|
|
11
|
+
group: t.string,
|
|
12
|
+
value: t.number,
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const withInMemory = async <T>(
|
|
16
|
+
run: (ctx: {
|
|
17
|
+
client: Client
|
|
18
|
+
sandbox: Sandbox
|
|
19
|
+
Item: any
|
|
20
|
+
}) => Promise<T>
|
|
21
|
+
): Promise<T> => {
|
|
22
|
+
const previous = process.env.EXPERIMENTAL_DYNAMODB_IN_MEMORY
|
|
23
|
+
process.env.EXPERIMENTAL_DYNAMODB_IN_MEMORY = "1"
|
|
24
|
+
|
|
25
|
+
const client = new Client({ tableName: "table" })
|
|
26
|
+
const provider = getProvider(client)
|
|
27
|
+
|
|
28
|
+
class Item extends model("Item", CODEC, provider) {
|
|
29
|
+
get PK() {
|
|
30
|
+
return `PK#${this.group}`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get SK() {
|
|
34
|
+
return `SK#${String(this.value).padStart(3, "0")}`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get GSI2PK() {
|
|
38
|
+
return `GSI2PK#${this.group}`
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get GSI2SK() {
|
|
42
|
+
return `GSI2SK#${this.id}`
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const sandbox = await createSandbox(client)
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
return await run({ client, sandbox, Item })
|
|
50
|
+
} finally {
|
|
51
|
+
await sandbox.destroy()
|
|
52
|
+
|
|
53
|
+
if (typeof previous === "undefined")
|
|
54
|
+
delete process.env.EXPERIMENTAL_DYNAMODB_IN_MEMORY
|
|
55
|
+
else process.env.EXPERIMENTAL_DYNAMODB_IN_MEMORY = previous
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
describe("in-memory spec", () => {
|
|
60
|
+
test("locks projection and index scope", () => {
|
|
61
|
+
expect(IN_MEMORY_SPEC.projection).toBe("ALL")
|
|
62
|
+
expect(IN_MEMORY_SPEC.excludedIndexes).toEqual(["GSI1"])
|
|
63
|
+
expect(IN_MEMORY_SPEC.indexes.includes("primary")).toBe(true)
|
|
64
|
+
expect(IN_MEMORY_SPEC.indexes.includes("GSI2")).toBe(true)
|
|
65
|
+
expect(IN_MEMORY_SPEC.indexes.includes("GSI19")).toBe(true)
|
|
66
|
+
expect(IN_MEMORY_SPEC.indexes.includes("GSI1" as any)).toBe(false)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test("throws deterministic NotSupportedError for unsupported method", async () => {
|
|
70
|
+
await withInMemory(async ({ client }) => {
|
|
71
|
+
await expect(
|
|
72
|
+
(client.documentClient as any).transactGet({}).promise()
|
|
73
|
+
).rejects.toMatchObject({
|
|
74
|
+
name: "NotSupportedError",
|
|
75
|
+
code: "NotSupportedError",
|
|
76
|
+
method: "transactGet",
|
|
77
|
+
featurePath: "transactGet",
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test("rejects GSI1 with deterministic NotSupportedError", async () => {
|
|
83
|
+
await withInMemory(async ({ client, Item }) => {
|
|
84
|
+
await new Item({ id: "1", group: "g", value: 1 }).put()
|
|
85
|
+
|
|
86
|
+
await expect(
|
|
87
|
+
client.documentClient
|
|
88
|
+
.query({
|
|
89
|
+
TableName: client.tableName,
|
|
90
|
+
IndexName: "GSI1",
|
|
91
|
+
KeyConditionExpression: "PK = :pk",
|
|
92
|
+
ExpressionAttributeValues: { ":pk": "PK#g" },
|
|
93
|
+
})
|
|
94
|
+
.promise()
|
|
95
|
+
).rejects.toMatchObject({
|
|
96
|
+
name: "NotSupportedError",
|
|
97
|
+
code: "NotSupportedError",
|
|
98
|
+
method: "query",
|
|
99
|
+
featurePath: "query.IndexName",
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test("keeps GSI membership in sync after update/removal", async () => {
|
|
105
|
+
await withInMemory(async ({ client, Item }) => {
|
|
106
|
+
const item = await new Item({ id: "42", group: "test", value: 42 }).put()
|
|
107
|
+
|
|
108
|
+
const before = await client.documentClient
|
|
109
|
+
.query({
|
|
110
|
+
TableName: client.tableName,
|
|
111
|
+
IndexName: "GSI2",
|
|
112
|
+
KeyConditionExpression: "GSI2PK = :pk",
|
|
113
|
+
ExpressionAttributeValues: { ":pk": "GSI2PK#test" },
|
|
114
|
+
})
|
|
115
|
+
.promise()
|
|
116
|
+
|
|
117
|
+
expect(before.Count).toBe(1)
|
|
118
|
+
|
|
119
|
+
await Item.updateRaw(
|
|
120
|
+
{ PK: item.PK, SK: item.SK },
|
|
121
|
+
{ GSI2PK: null, GSI2SK: null } as any
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
const after = await client.documentClient
|
|
125
|
+
.query({
|
|
126
|
+
TableName: client.tableName,
|
|
127
|
+
IndexName: "GSI2",
|
|
128
|
+
KeyConditionExpression: "GSI2PK = :pk",
|
|
129
|
+
ExpressionAttributeValues: { ":pk": "GSI2PK#test" },
|
|
130
|
+
})
|
|
131
|
+
.promise()
|
|
132
|
+
|
|
133
|
+
expect(after.Count).toBe(0)
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
test("supports if_not_exists in update expressions", async () => {
|
|
138
|
+
await withInMemory(async ({ client, Item }) => {
|
|
139
|
+
const item = await new Item({ id: "100", group: "expr", value: 1 }).put()
|
|
140
|
+
|
|
141
|
+
const first = await client.documentClient
|
|
142
|
+
.update({
|
|
143
|
+
TableName: client.tableName,
|
|
144
|
+
Key: { PK: item.PK, SK: item.SK },
|
|
145
|
+
UpdateExpression: "SET #count = if_not_exists(#count, :zero) + :inc",
|
|
146
|
+
ExpressionAttributeNames: { "#count": "count" },
|
|
147
|
+
ExpressionAttributeValues: { ":zero": 0, ":inc": 2 },
|
|
148
|
+
ReturnValues: "ALL_NEW",
|
|
149
|
+
})
|
|
150
|
+
.promise()
|
|
151
|
+
|
|
152
|
+
expect(first.Attributes?.count).toBe(2)
|
|
153
|
+
|
|
154
|
+
const second = await client.documentClient
|
|
155
|
+
.update({
|
|
156
|
+
TableName: client.tableName,
|
|
157
|
+
Key: { PK: item.PK, SK: item.SK },
|
|
158
|
+
UpdateExpression: "SET #count = if_not_exists(#count, :zero) + :inc",
|
|
159
|
+
ExpressionAttributeNames: { "#count": "count" },
|
|
160
|
+
ExpressionAttributeValues: { ":zero": 0, ":inc": 3 },
|
|
161
|
+
ReturnValues: "ALL_NEW",
|
|
162
|
+
})
|
|
163
|
+
.promise()
|
|
164
|
+
|
|
165
|
+
expect(second.Attributes?.count).toBe(5)
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
test("supports list_append and filter functions", async () => {
|
|
170
|
+
await withInMemory(async ({ client, Item }) => {
|
|
171
|
+
const item = await new Item({ id: "200", group: "expr", value: 2 }).put()
|
|
172
|
+
|
|
173
|
+
await client.documentClient
|
|
174
|
+
.update({
|
|
175
|
+
TableName: client.tableName,
|
|
176
|
+
Key: { PK: item.PK, SK: item.SK },
|
|
177
|
+
UpdateExpression:
|
|
178
|
+
"SET #tags = list_append(if_not_exists(#tags, :empty), :more)",
|
|
179
|
+
ExpressionAttributeNames: { "#tags": "tags" },
|
|
180
|
+
ExpressionAttributeValues: { ":empty": [], ":more": ["a", "b"] },
|
|
181
|
+
ReturnValues: "ALL_NEW",
|
|
182
|
+
})
|
|
183
|
+
.promise()
|
|
184
|
+
|
|
185
|
+
const filtered = await client.documentClient
|
|
186
|
+
.scan({
|
|
187
|
+
TableName: client.tableName,
|
|
188
|
+
FilterExpression:
|
|
189
|
+
"contains(#tags, :tag) and attribute_type(#id, :t) and size(#id) > :min",
|
|
190
|
+
ExpressionAttributeNames: { "#tags": "tags", "#id": "id" },
|
|
191
|
+
ExpressionAttributeValues: { ":tag": "a", ":t": "S", ":min": 1 },
|
|
192
|
+
})
|
|
193
|
+
.promise()
|
|
194
|
+
|
|
195
|
+
expect(filtered.Count).toBe(1)
|
|
196
|
+
expect(filtered.Items?.[0]?.id).toBe("200")
|
|
197
|
+
})
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
test("is deterministic for repeated seeded runs", async () => {
|
|
201
|
+
const execute = async () =>
|
|
202
|
+
withInMemory(async ({ client, sandbox, Item }) => {
|
|
203
|
+
const order = [7, 1, 9, 2, 8, 3, 6, 4, 5, 0]
|
|
204
|
+
|
|
205
|
+
await Promise.all(
|
|
206
|
+
order.map((value) =>
|
|
207
|
+
new Item({ id: String(value), group: "det", value }).put()
|
|
208
|
+
)
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
const page = await client.documentClient
|
|
212
|
+
.query({
|
|
213
|
+
TableName: client.tableName,
|
|
214
|
+
KeyConditionExpression: "PK = :pk",
|
|
215
|
+
ExpressionAttributeValues: { ":pk": "PK#det" },
|
|
216
|
+
})
|
|
217
|
+
.promise()
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
snapshot: await sandbox.snapshot(),
|
|
221
|
+
queryKeys: (page.Items ?? []).map((entry) => `${entry.PK}::${entry.SK}`),
|
|
222
|
+
}
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
const first = await execute()
|
|
226
|
+
const second = await execute()
|
|
227
|
+
|
|
228
|
+
expect(second).toEqual(first)
|
|
229
|
+
})
|
|
230
|
+
})
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import * as t from "io-ts"
|
|
2
|
+
import { model } from "@model-ts/core"
|
|
3
|
+
import { Sandbox, createSandbox } from "../sandbox"
|
|
4
|
+
import { Client } from "../client"
|
|
5
|
+
import { getProvider } from "../provider"
|
|
6
|
+
|
|
7
|
+
const client = new Client({ tableName: "table" })
|
|
8
|
+
const provider = getProvider(client)
|
|
9
|
+
|
|
10
|
+
const SIMPLE_CODEC = t.type({
|
|
11
|
+
foo: t.string,
|
|
12
|
+
bar: t.number,
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
class Simple extends model("Simple", SIMPLE_CODEC, provider) {
|
|
16
|
+
get PK() {
|
|
17
|
+
return `PK#${this.foo}`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get SK() {
|
|
21
|
+
return `SK#${this.bar}`
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("rollback", () => {
|
|
26
|
+
let sandbox: Sandbox
|
|
27
|
+
|
|
28
|
+
beforeAll(async () => {
|
|
29
|
+
sandbox = await createSandbox(client)
|
|
30
|
+
await sandbox.seed(
|
|
31
|
+
new Simple({ foo: "seeded1", bar: 1 }),
|
|
32
|
+
new Simple({ foo: "seeded2", bar: 2 })
|
|
33
|
+
)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
afterAll(async () => {
|
|
37
|
+
await sandbox.destroy()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test("rolls back newly added items", async () => {
|
|
41
|
+
sandbox.startTracking()
|
|
42
|
+
|
|
43
|
+
await new Simple({ foo: "new-item", bar: 99 }).put()
|
|
44
|
+
expect(await sandbox.get("PK#new-item", "SK#99")).not.toBeNull()
|
|
45
|
+
|
|
46
|
+
await sandbox.rollback()
|
|
47
|
+
|
|
48
|
+
expect(await sandbox.get("PK#new-item", "SK#99")).toBeNull()
|
|
49
|
+
expect(await sandbox.get("PK#seeded1", "SK#1")).not.toBeNull()
|
|
50
|
+
expect(await sandbox.get("PK#seeded2", "SK#2")).not.toBeNull()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test("rolls back updated items", async () => {
|
|
54
|
+
sandbox.startTracking()
|
|
55
|
+
|
|
56
|
+
await Simple.updateRaw(
|
|
57
|
+
{ PK: "PK#seeded1", SK: "SK#1" },
|
|
58
|
+
{ foo: "modified" }
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
const modified = await sandbox.get("PK#seeded1", "SK#1")
|
|
62
|
+
expect(modified.foo).toBe("modified")
|
|
63
|
+
|
|
64
|
+
await sandbox.rollback()
|
|
65
|
+
|
|
66
|
+
const restored = await sandbox.get("PK#seeded1", "SK#1")
|
|
67
|
+
expect(restored.foo).toBe("seeded1")
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test("rolls back deleted items", async () => {
|
|
71
|
+
sandbox.startTracking()
|
|
72
|
+
|
|
73
|
+
await Simple.delete({ PK: "PK#seeded1", SK: "SK#1" })
|
|
74
|
+
expect(await sandbox.get("PK#seeded1", "SK#1")).toBeNull()
|
|
75
|
+
|
|
76
|
+
await sandbox.rollback()
|
|
77
|
+
|
|
78
|
+
const restored = await sandbox.get("PK#seeded1", "SK#1")
|
|
79
|
+
expect(restored).not.toBeNull()
|
|
80
|
+
expect(restored.foo).toBe("seeded1")
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test("rolls back soft-deleted items", async () => {
|
|
84
|
+
sandbox.startTracking()
|
|
85
|
+
|
|
86
|
+
const item = new Simple({ foo: "seeded1", bar: 1 })
|
|
87
|
+
// @ts-ignore - _docVersion needed for softDelete encoding
|
|
88
|
+
item._docVersion = 0
|
|
89
|
+
await client.softDelete(item)
|
|
90
|
+
|
|
91
|
+
// Original should be gone, deleted version should exist
|
|
92
|
+
expect(await sandbox.get("PK#seeded1", "SK#1")).toBeNull()
|
|
93
|
+
expect(
|
|
94
|
+
await sandbox.get("$$DELETED$$PK#seeded1", "$$DELETED$$SK#1")
|
|
95
|
+
).not.toBeNull()
|
|
96
|
+
|
|
97
|
+
await sandbox.rollback()
|
|
98
|
+
|
|
99
|
+
// Original should be restored, deleted version should be gone
|
|
100
|
+
expect(await sandbox.get("PK#seeded1", "SK#1")).not.toBeNull()
|
|
101
|
+
expect(
|
|
102
|
+
await sandbox.get("$$DELETED$$PK#seeded1", "$$DELETED$$SK#1")
|
|
103
|
+
).toBeNull()
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test("rolls back mixed operations", async () => {
|
|
107
|
+
sandbox.startTracking()
|
|
108
|
+
|
|
109
|
+
// Add a new item
|
|
110
|
+
await new Simple({ foo: "brand-new", bar: 50 }).put()
|
|
111
|
+
|
|
112
|
+
// Update an existing item
|
|
113
|
+
await Simple.updateRaw(
|
|
114
|
+
{ PK: "PK#seeded1", SK: "SK#1" },
|
|
115
|
+
{ foo: "changed" }
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
// Delete an existing item
|
|
119
|
+
await Simple.delete({ PK: "PK#seeded2", SK: "SK#2" })
|
|
120
|
+
|
|
121
|
+
// Verify all changes took effect
|
|
122
|
+
expect(await sandbox.get("PK#brand-new", "SK#50")).not.toBeNull()
|
|
123
|
+
expect((await sandbox.get("PK#seeded1", "SK#1")).foo).toBe("changed")
|
|
124
|
+
expect(await sandbox.get("PK#seeded2", "SK#2")).toBeNull()
|
|
125
|
+
|
|
126
|
+
await sandbox.rollback()
|
|
127
|
+
|
|
128
|
+
// New item should be gone
|
|
129
|
+
expect(await sandbox.get("PK#brand-new", "SK#50")).toBeNull()
|
|
130
|
+
// Updated item should be restored
|
|
131
|
+
expect((await sandbox.get("PK#seeded1", "SK#1")).foo).toBe("seeded1")
|
|
132
|
+
// Deleted item should be back
|
|
133
|
+
expect(await sandbox.get("PK#seeded2", "SK#2")).not.toBeNull()
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
test("supports multiple tracking/rollback cycles", async () => {
|
|
137
|
+
// Cycle 1
|
|
138
|
+
sandbox.startTracking()
|
|
139
|
+
await new Simple({ foo: "cycle1", bar: 10 }).put()
|
|
140
|
+
await sandbox.rollback()
|
|
141
|
+
expect(await sandbox.get("PK#cycle1", "SK#10")).toBeNull()
|
|
142
|
+
|
|
143
|
+
// Cycle 2
|
|
144
|
+
sandbox.startTracking()
|
|
145
|
+
await new Simple({ foo: "cycle2", bar: 20 }).put()
|
|
146
|
+
await sandbox.rollback()
|
|
147
|
+
expect(await sandbox.get("PK#cycle2", "SK#20")).toBeNull()
|
|
148
|
+
|
|
149
|
+
// Cycle 3 - modify seeded data
|
|
150
|
+
sandbox.startTracking()
|
|
151
|
+
await Simple.updateRaw(
|
|
152
|
+
{ PK: "PK#seeded1", SK: "SK#1" },
|
|
153
|
+
{ foo: "cycle3-mod" }
|
|
154
|
+
)
|
|
155
|
+
await sandbox.rollback()
|
|
156
|
+
expect((await sandbox.get("PK#seeded1", "SK#1")).foo).toBe("seeded1")
|
|
157
|
+
|
|
158
|
+
// Seeded data is intact throughout
|
|
159
|
+
expect(await sandbox.get("PK#seeded1", "SK#1")).not.toBeNull()
|
|
160
|
+
expect(await sandbox.get("PK#seeded2", "SK#2")).not.toBeNull()
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
test("does not track writes before startTracking", async () => {
|
|
164
|
+
// Write without tracking
|
|
165
|
+
await new Simple({ foo: "untracked", bar: 77 }).put()
|
|
166
|
+
|
|
167
|
+
// Now start tracking and immediately rollback
|
|
168
|
+
sandbox.startTracking()
|
|
169
|
+
await sandbox.rollback()
|
|
170
|
+
|
|
171
|
+
// The untracked write should still be there
|
|
172
|
+
expect(await sandbox.get("PK#untracked", "SK#77")).not.toBeNull()
|
|
173
|
+
|
|
174
|
+
// Clean up manually for subsequent tests
|
|
175
|
+
sandbox.startTracking()
|
|
176
|
+
await Simple.delete({ PK: "PK#untracked", SK: "SK#77" })
|
|
177
|
+
await sandbox.rollback()
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
test("tracks the same key modified multiple times", async () => {
|
|
181
|
+
sandbox.startTracking()
|
|
182
|
+
|
|
183
|
+
// Update the same item twice
|
|
184
|
+
await Simple.updateRaw(
|
|
185
|
+
{ PK: "PK#seeded1", SK: "SK#1" },
|
|
186
|
+
{ foo: "first-update" }
|
|
187
|
+
)
|
|
188
|
+
await Simple.updateRaw(
|
|
189
|
+
{ PK: "PK#seeded1", SK: "SK#1" },
|
|
190
|
+
{ foo: "second-update" }
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
expect((await sandbox.get("PK#seeded1", "SK#1")).foo).toBe("second-update")
|
|
194
|
+
|
|
195
|
+
await sandbox.rollback()
|
|
196
|
+
|
|
197
|
+
// Should restore to original, not to the intermediate state
|
|
198
|
+
expect((await sandbox.get("PK#seeded1", "SK#1")).foo).toBe("seeded1")
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
describe("rollback with beforeEach/afterEach pattern", () => {
|
|
203
|
+
let sandbox: Sandbox
|
|
204
|
+
|
|
205
|
+
beforeAll(async () => {
|
|
206
|
+
sandbox = await createSandbox(client)
|
|
207
|
+
await sandbox.seed(
|
|
208
|
+
new Simple({ foo: "a", bar: 1 }),
|
|
209
|
+
new Simple({ foo: "b", bar: 2 })
|
|
210
|
+
)
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
beforeEach(() => {
|
|
214
|
+
sandbox.startTracking()
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
afterEach(async () => {
|
|
218
|
+
await sandbox.rollback()
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
afterAll(async () => {
|
|
222
|
+
await sandbox.destroy()
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
test("test A: add an item", async () => {
|
|
226
|
+
await new Simple({ foo: "from-test-a", bar: 100 }).put()
|
|
227
|
+
expect(await sandbox.get("PK#from-test-a", "SK#100")).not.toBeNull()
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
test("test B: item from test A does not exist", async () => {
|
|
231
|
+
expect(await sandbox.get("PK#from-test-a", "SK#100")).toBeNull()
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
test("test C: modify seeded data", async () => {
|
|
235
|
+
await Simple.updateRaw({ PK: "PK#a", SK: "SK#1" }, { foo: "modified-a" })
|
|
236
|
+
expect((await sandbox.get("PK#a", "SK#1")).foo).toBe("modified-a")
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
test("test D: seeded data is back to original", async () => {
|
|
240
|
+
expect((await sandbox.get("PK#a", "SK#1")).foo).toBe("a")
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
test("test E: delete seeded data", async () => {
|
|
244
|
+
await Simple.delete({ PK: "PK#b", SK: "SK#2" })
|
|
245
|
+
expect(await sandbox.get("PK#b", "SK#2")).toBeNull()
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
test("test F: deleted seeded data is back", async () => {
|
|
249
|
+
expect(await sandbox.get("PK#b", "SK#2")).not.toBeNull()
|
|
250
|
+
expect((await sandbox.get("PK#b", "SK#2")).foo).toBe("b")
|
|
251
|
+
})
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
describe("existing sandbox semantics still work", () => {
|
|
255
|
+
let sandbox: Sandbox
|
|
256
|
+
|
|
257
|
+
beforeEach(async () => {
|
|
258
|
+
sandbox = await createSandbox(client)
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
afterEach(async () => {
|
|
262
|
+
await sandbox.destroy()
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
test("create and destroy per test still works", async () => {
|
|
266
|
+
await sandbox.seed(new Simple({ foo: "per-test", bar: 1 }))
|
|
267
|
+
|
|
268
|
+
const item = await sandbox.get("PK#per-test", "SK#1")
|
|
269
|
+
expect(item).not.toBeNull()
|
|
270
|
+
expect(item.foo).toBe("per-test")
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
test("snapshot and diff still work", async () => {
|
|
274
|
+
const before = await sandbox.snapshot()
|
|
275
|
+
await new Simple({ foo: "diffed", bar: 1 }).put()
|
|
276
|
+
const diffResult = await sandbox.diff(before)
|
|
277
|
+
expect(diffResult).toContain("PK#diffed")
|
|
278
|
+
})
|
|
279
|
+
})
|
package/src/client.ts
CHANGED
|
@@ -44,6 +44,7 @@ import {
|
|
|
44
44
|
PaginationResult,
|
|
45
45
|
} from "./pagination"
|
|
46
46
|
import { GSI, GSI_NAMES, GSIPK, GSISK } from "./gsi"
|
|
47
|
+
import { createInMemoryDocumentClient } from "./in-memory"
|
|
47
48
|
|
|
48
49
|
export type QueryParams = Omit<
|
|
49
50
|
DocumentClient.QueryInput,
|
|
@@ -110,7 +111,18 @@ export class Client {
|
|
|
110
111
|
this.tableName = props?.tableName
|
|
111
112
|
this.cursorEncryptionKey = props?.cursorEncryptionKey
|
|
112
113
|
this.paginationOptions = props?.paginationOptions
|
|
113
|
-
|
|
114
|
+
|
|
115
|
+
const inMemoryRequested = process.env.EXPERIMENTAL_DYNAMODB_IN_MEMORY === "1"
|
|
116
|
+
if (inMemoryRequested && process.env.NODE_ENV !== "test") {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`EXPERIMENTAL_DYNAMODB_IN_MEMORY=1 is only allowed when NODE_ENV is "test" (received "${process.env.NODE_ENV ?? "undefined"}").`
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
this.documentClient =
|
|
123
|
+
inMemoryRequested
|
|
124
|
+
? (createInMemoryDocumentClient() as any as DocumentClient)
|
|
125
|
+
: new DocumentClient(props)
|
|
114
126
|
this.dataLoader = new DataLoader<
|
|
115
127
|
GetOperation<Decodable>,
|
|
116
128
|
DynamoDBModelInstance,
|
|
@@ -160,6 +172,7 @@ export class Client {
|
|
|
160
172
|
M extends DynamoDBModelConstructor<T>
|
|
161
173
|
>({
|
|
162
174
|
_model,
|
|
175
|
+
_operation,
|
|
163
176
|
_deleted,
|
|
164
177
|
item,
|
|
165
178
|
IgnoreExistence,
|
|
@@ -200,6 +213,7 @@ export class Client {
|
|
|
200
213
|
|
|
201
214
|
async get<M extends Decodable>({
|
|
202
215
|
_model,
|
|
216
|
+
_operation,
|
|
203
217
|
key,
|
|
204
218
|
...params
|
|
205
219
|
}: GetOperation<M>): Promise<DecodableInstance<M>> {
|
|
@@ -272,7 +286,7 @@ export class Client {
|
|
|
272
286
|
async updateRaw<M extends DynamoDBModelConstructor<any>>(
|
|
273
287
|
operation: UpdateRawOperation<M>
|
|
274
288
|
): Promise<InstanceType<M>> {
|
|
275
|
-
const { _model, key, attributes, ...params } = operation
|
|
289
|
+
const { _model, _operation, key, attributes, ...params } = operation
|
|
276
290
|
|
|
277
291
|
const {
|
|
278
292
|
UpdateExpression,
|
|
@@ -831,8 +845,7 @@ export class Client {
|
|
|
831
845
|
): DocumentClient.TransactWriteItem => {
|
|
832
846
|
switch (operation._operation) {
|
|
833
847
|
case "put": {
|
|
834
|
-
const { _model, _deleted, item, IgnoreExistence, ...params } = operation
|
|
835
|
-
debugger
|
|
848
|
+
const { _model, _operation, _deleted, item, IgnoreExistence, ...params } = operation
|
|
836
849
|
const encoded = (_model as M & DynamoDBInternals<M>).__dynamoDBEncode(
|
|
837
850
|
item
|
|
838
851
|
)
|
package/src/errors.ts
CHANGED
|
@@ -45,6 +45,29 @@ export class PaginationError extends Error {
|
|
|
45
45
|
name = "PaginationError"
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
export class NotSupportedError extends Error {
|
|
49
|
+
name = "NotSupportedError"
|
|
50
|
+
code = "NotSupportedError"
|
|
51
|
+
method: string
|
|
52
|
+
featurePath: string
|
|
53
|
+
reason: string
|
|
54
|
+
|
|
55
|
+
constructor({
|
|
56
|
+
method,
|
|
57
|
+
featurePath,
|
|
58
|
+
reason,
|
|
59
|
+
}: {
|
|
60
|
+
method: string
|
|
61
|
+
featurePath: string
|
|
62
|
+
reason: string
|
|
63
|
+
}) {
|
|
64
|
+
super(reason)
|
|
65
|
+
this.method = method
|
|
66
|
+
this.featurePath = featurePath
|
|
67
|
+
this.reason = reason
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
48
71
|
export type DynamoDBError =
|
|
49
72
|
| KeyExistsError
|
|
50
73
|
| ItemNotFoundError
|
|
@@ -52,3 +75,4 @@ export type DynamoDBError =
|
|
|
52
75
|
| BulkWriteTransactionError
|
|
53
76
|
| BulkWriteRollbackError
|
|
54
77
|
| PaginationError
|
|
78
|
+
| NotSupportedError
|