@model-ts/dynamodb 3.0.0 → 3.0.2

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 (36) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/cjs/__test__/client-with-cursor-encryption.test.js +696 -21
  3. package/dist/cjs/__test__/client-with-cursor-encryption.test.js.map +1 -1
  4. package/dist/cjs/__test__/client.test.js +775 -95
  5. package/dist/cjs/__test__/client.test.js.map +1 -1
  6. package/dist/cjs/__test__/pagination.test.d.ts +1 -0
  7. package/dist/cjs/__test__/pagination.test.js +241 -0
  8. package/dist/cjs/__test__/pagination.test.js.map +1 -0
  9. package/dist/cjs/client.js +1 -1
  10. package/dist/cjs/client.js.map +1 -1
  11. package/dist/cjs/pagination.d.ts +1 -1
  12. package/dist/cjs/pagination.js +4 -6
  13. package/dist/cjs/pagination.js.map +1 -1
  14. package/dist/cjs/sandbox.js +3 -3
  15. package/dist/cjs/sandbox.js.map +1 -1
  16. package/dist/esm/__test__/client-with-cursor-encryption.test.js +696 -21
  17. package/dist/esm/__test__/client-with-cursor-encryption.test.js.map +1 -1
  18. package/dist/esm/__test__/client.test.js +776 -96
  19. package/dist/esm/__test__/client.test.js.map +1 -1
  20. package/dist/esm/__test__/pagination.test.d.ts +1 -0
  21. package/dist/esm/__test__/pagination.test.js +238 -0
  22. package/dist/esm/__test__/pagination.test.js.map +1 -0
  23. package/dist/esm/client.js +1 -1
  24. package/dist/esm/client.js.map +1 -1
  25. package/dist/esm/pagination.d.ts +1 -1
  26. package/dist/esm/pagination.js +4 -6
  27. package/dist/esm/pagination.js.map +1 -1
  28. package/dist/esm/sandbox.js +3 -3
  29. package/dist/esm/sandbox.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/__test__/client-with-cursor-encryption.test.ts +696 -21
  32. package/src/__test__/client.test.ts +782 -97
  33. package/src/__test__/pagination.test.ts +300 -0
  34. package/src/client.ts +1 -1
  35. package/src/pagination.ts +5 -9
  36. package/src/sandbox.ts +19 -17
@@ -0,0 +1,300 @@
1
+ import crypto from "crypto"
2
+ import { encodeDDBCursor, decodeDDBCursor } from "../pagination"
3
+ import { PaginationError } from "../errors"
4
+
5
+ describe("encodeDDBCursor", () => {
6
+ describe("basic encoding without encryption", () => {
7
+ it("should encode basic PK and SK", () => {
8
+ const result = encodeDDBCursor({
9
+ PK: "USER#123",
10
+ SK: "PROFILE#456",
11
+ })
12
+
13
+ expect(result).toMatchInlineSnapshot(
14
+ `"eyJQSyI6IlVTRVIjMTIzIiwiU0siOiJQUk9GSUxFIzQ1NiJ9"`
15
+ )
16
+ })
17
+
18
+ it("should encode with GSI2 values", () => {
19
+ const result = encodeDDBCursor({
20
+ PK: "USER#123",
21
+ SK: "PROFILE#456",
22
+ GSI2PK: "GSI2PK#user123",
23
+ GSI2SK: "GSI2SK#profile456",
24
+ })
25
+
26
+ expect(result).toMatchInlineSnapshot(
27
+ `"eyJQSyI6IlVTRVIjMTIzIiwiU0siOiJQUk9GSUxFIzQ1NiIsIkdTSTJQSyI6IkdTSTJQSyN1c2VyMTIzIiwiR1NJMlNLIjoiR1NJMlNLI3Byb2ZpbGU0NTYifQ=="`
28
+ )
29
+ })
30
+
31
+ it("should encode with multiple GSI values", () => {
32
+ const result = encodeDDBCursor({
33
+ PK: "USER#123",
34
+ SK: "PROFILE#456",
35
+ GSI2PK: "GSI2PK#user123",
36
+ GSI2SK: "GSI2SK#profile456",
37
+ GSI3PK: "GSI3PK#fixed",
38
+ GSI3SK: "GSI3SK#value",
39
+ })
40
+
41
+ expect(result).toMatchInlineSnapshot(
42
+ `"eyJQSyI6IlVTRVIjMTIzIiwiU0siOiJQUk9GSUxFIzQ1NiIsIkdTSTJQSyI6IkdTSTJQSyN1c2VyMTIzIiwiR1NJMlNLIjoiR1NJMlNLI3Byb2ZpbGU0NTYiLCJHU0kzUEsiOiJHU0kzUEsjZml4ZWQiLCJHU0kzU0siOiJHU0kzU0sjdmFsdWUifQ=="`
43
+ )
44
+ })
45
+ })
46
+
47
+ describe("encoding with encryption", () => {
48
+ const encryptionKey = crypto.randomBytes(32)
49
+
50
+ it("should encrypt the cursor when encryption key is provided", () => {
51
+ const result = encodeDDBCursor(
52
+ {
53
+ PK: "USER#123",
54
+ SK: "PROFILE#456",
55
+ },
56
+ encryptionKey
57
+ )
58
+
59
+ // The result should be encrypted and different from the unencrypted version
60
+ expect(result).not.toBe(
61
+ "eyJQSyI6IlVTRVIjMTIzIiwiU0siOiJQUk9GSUxFIzQ1NiJ9"
62
+ )
63
+ expect(result).toMatch(/^[A-Za-z0-9+/=]+$/) // Base64 format
64
+ })
65
+
66
+ it("should produce consistent encrypted results for same input", () => {
67
+ const input = {
68
+ PK: "USER#123",
69
+ SK: "PROFILE#456",
70
+ }
71
+
72
+ const result1 = encodeDDBCursor(input, encryptionKey)
73
+ const result2 = encodeDDBCursor(input, encryptionKey)
74
+
75
+ expect(result1).toBe(result2)
76
+ })
77
+
78
+ it("should encrypt with GSI values", () => {
79
+ const result = encodeDDBCursor(
80
+ {
81
+ PK: "USER#123",
82
+ SK: "PROFILE#456",
83
+ GSI2PK: "GSI2PK#user123",
84
+ GSI2SK: "GSI2SK#profile456",
85
+ },
86
+ encryptionKey
87
+ )
88
+
89
+ expect(result).toMatch(/^[A-Za-z0-9+/=]+$/)
90
+ })
91
+ })
92
+ })
93
+
94
+ describe("decodeDDBCursor", () => {
95
+ describe("basic decoding without encryption", () => {
96
+ it("should decode basic PK and SK", () => {
97
+ const encoded = "eyJQSyI6IlVTRVIjMTIzIiwiU0siOiJQUk9GSUxFIzQ1NiJ9"
98
+ const result = decodeDDBCursor(encoded)
99
+
100
+ expect(result).toMatchInlineSnapshot(`
101
+ Object {
102
+ "PK": "USER#123",
103
+ "SK": "PROFILE#456",
104
+ }
105
+ `)
106
+ })
107
+
108
+ it("should decode with GSI2 values", () => {
109
+ const encoded =
110
+ "eyJQSyI6IlVTRVIjMTIzIiwiU0siOiJQUk9GSUxFIzQ1NiIsIkdTSTJQSyI6IkdTSTJQSyN1c2VyMTIzIiwiR1NJMlNLIjoiR1NJMlNLI3Byb2ZpbGU0NTYifQ=="
111
+ const result = decodeDDBCursor(encoded, "GSI2")
112
+
113
+ expect(result).toMatchInlineSnapshot(`
114
+ Object {
115
+ "GSI2PK": "GSI2PK#user123",
116
+ "GSI2SK": "GSI2SK#profile456",
117
+ "PK": "USER#123",
118
+ "SK": "PROFILE#456",
119
+ }
120
+ `)
121
+ })
122
+
123
+ it("should decode with GSI3 values", () => {
124
+ const encoded =
125
+ "eyJQSyI6IlVTRVIjMTIzIiwiU0siOiJQUk9GSUxFIzQ1NiIsIkdTSTJQSyI6IkdTSTJQSyN1c2VyMTIzIiwiR1NJMlNLIjoiR1NJMlNLI3Byb2ZpbGU0NTYiLCJHU0kzUEsiOiJHU0kzUEsjZml4ZWQiLCJHU0kzU0siOiJHU0kzU0sjdmFsdWUifQ=="
126
+ const result = decodeDDBCursor(encoded, "GSI3")
127
+
128
+ expect(result).toMatchInlineSnapshot(`
129
+ Object {
130
+ "GSI3PK": "GSI3PK#fixed",
131
+ "GSI3SK": "GSI3SK#value",
132
+ "PK": "USER#123",
133
+ "SK": "PROFILE#456",
134
+ }
135
+ `)
136
+ })
137
+ })
138
+
139
+ describe("decoding with encryption", () => {
140
+ const encryptionKey = crypto.randomBytes(32)
141
+
142
+ it("should decode encrypted cursor", () => {
143
+ const input = {
144
+ PK: "USER#123",
145
+ SK: "PROFILE#456",
146
+ }
147
+ const encoded = encodeDDBCursor(input, encryptionKey)
148
+ const result = decodeDDBCursor(encoded, undefined, encryptionKey)
149
+
150
+ expect(result).toMatchInlineSnapshot(`
151
+ Object {
152
+ "PK": "USER#123",
153
+ "SK": "PROFILE#456",
154
+ }
155
+ `)
156
+ })
157
+
158
+ it("should decode encrypted cursor with GSI values", () => {
159
+ const input = {
160
+ PK: "USER#123",
161
+ SK: "PROFILE#456",
162
+ GSI2PK: "GSI2PK#user123",
163
+ GSI2SK: "GSI2SK#profile456",
164
+ }
165
+ const encoded = encodeDDBCursor(input, encryptionKey)
166
+ const result = decodeDDBCursor(encoded, "GSI2", encryptionKey)
167
+
168
+ expect(result).toMatchInlineSnapshot(`
169
+ Object {
170
+ "GSI2PK": "GSI2PK#user123",
171
+ "GSI2SK": "GSI2SK#profile456",
172
+ "PK": "USER#123",
173
+ "SK": "PROFILE#456",
174
+ }
175
+ `)
176
+ })
177
+
178
+ it("should throw PaginationError when decryption fails", () => {
179
+ const wrongKey = crypto.randomBytes(32)
180
+ const input = {
181
+ PK: "USER#123",
182
+ SK: "PROFILE#456",
183
+ }
184
+ const encoded = encodeDDBCursor(input, encryptionKey)
185
+
186
+ expect(() => {
187
+ decodeDDBCursor(encoded, undefined, wrongKey)
188
+ }).toThrow(PaginationError)
189
+ expect(() => {
190
+ decodeDDBCursor(encoded, undefined, wrongKey)
191
+ }).toThrow("Couldn't decode cursor")
192
+ })
193
+ })
194
+
195
+ describe("error handling", () => {
196
+ it("should throw PaginationError for invalid base64", () => {
197
+ expect(() => {
198
+ decodeDDBCursor("invalid-base64")
199
+ }).toThrow(PaginationError)
200
+ expect(() => {
201
+ decodeDDBCursor("invalid-base64")
202
+ }).toThrow("Couldn't decode cursor")
203
+ })
204
+
205
+ it("should throw PaginationError for invalid JSON", () => {
206
+ const invalidJson = Buffer.from("invalid json").toString("base64")
207
+ expect(() => {
208
+ decodeDDBCursor(invalidJson)
209
+ }).toThrow(PaginationError)
210
+ })
211
+
212
+ it("should throw PaginationError when PK is missing", () => {
213
+ const invalidData = Buffer.from(
214
+ JSON.stringify({ SK: "PROFILE#456" })
215
+ ).toString("base64")
216
+ expect(() => {
217
+ decodeDDBCursor(invalidData)
218
+ }).toThrow(PaginationError)
219
+ })
220
+
221
+ it("should throw PaginationError when SK is missing", () => {
222
+ const invalidData = Buffer.from(
223
+ JSON.stringify({ PK: "USER#123" })
224
+ ).toString("base64")
225
+ expect(() => {
226
+ decodeDDBCursor(invalidData)
227
+ }).toThrow(PaginationError)
228
+ })
229
+
230
+ it("should throw PaginationError when PK is not a string", () => {
231
+ const invalidData = Buffer.from(
232
+ JSON.stringify({ PK: 123, SK: "PROFILE#456" })
233
+ ).toString("base64")
234
+ expect(() => {
235
+ decodeDDBCursor(invalidData)
236
+ }).toThrow(PaginationError)
237
+ })
238
+
239
+ it("should throw PaginationError when SK is not a string", () => {
240
+ const invalidData = Buffer.from(
241
+ JSON.stringify({ PK: "USER#123", SK: 456 })
242
+ ).toString("base64")
243
+ expect(() => {
244
+ decodeDDBCursor(invalidData)
245
+ }).toThrow(PaginationError)
246
+ })
247
+ })
248
+
249
+ describe("round-trip encoding and decoding", () => {
250
+ it("should round-trip basic values", () => {
251
+ const input = {
252
+ PK: "USER#123",
253
+ SK: "PROFILE#456",
254
+ }
255
+ const encoded = encodeDDBCursor(input)
256
+ const decoded = decodeDDBCursor(encoded)
257
+
258
+ expect(decoded).toEqual(input)
259
+ })
260
+
261
+ it("should round-trip with GSI values", () => {
262
+ const input = {
263
+ PK: "USER#123",
264
+ SK: "PROFILE#456",
265
+ GSI2PK: "GSI2PK#user123",
266
+ GSI2SK: "GSI2SK#profile456",
267
+ GSI3PK: "GSI3PK#fixed",
268
+ GSI3SK: "GSI3SK#value",
269
+ }
270
+ const encoded = encodeDDBCursor(input)
271
+ const decoded = decodeDDBCursor(encoded, "GSI2")
272
+
273
+ expect(decoded).toEqual({
274
+ PK: input.PK,
275
+ SK: input.SK,
276
+ GSI2PK: input.GSI2PK,
277
+ GSI2SK: input.GSI2SK,
278
+ })
279
+ })
280
+
281
+ it("should round-trip with encryption", () => {
282
+ const encryptionKey = crypto.randomBytes(32)
283
+ const input = {
284
+ PK: "USER#123",
285
+ SK: "PROFILE#456",
286
+ GSI2PK: "GSI2PK#user123",
287
+ GSI2SK: "GSI2SK#profile456",
288
+ }
289
+ const encoded = encodeDDBCursor(input, encryptionKey)
290
+ const decoded = decodeDDBCursor(encoded, "GSI2", encryptionKey)
291
+
292
+ expect(decoded).toEqual({
293
+ PK: input.PK,
294
+ SK: input.SK,
295
+ GSI2PK: input.GSI2PK,
296
+ GSI2SK: input.GSI2SK,
297
+ })
298
+ })
299
+ })
300
+ })
package/src/client.ts CHANGED
@@ -741,7 +741,7 @@ export class Client {
741
741
  ? E.left({ ...state, rollbackSuccessful: true })
742
742
  : E.right(state)
743
743
 
744
- const [currentBatch, remaining] = A.splitAt(25)(operations)
744
+ const [currentBatch, remaining] = A.splitAt(100)(operations)
745
745
 
746
746
  try {
747
747
  const transactItems = currentBatch
package/src/pagination.ts CHANGED
@@ -132,11 +132,7 @@ const decryptCursor = (key: Buffer, encryptedCursor: string) => {
132
132
  }
133
133
 
134
134
  export const encodeDDBCursor = (
135
- {
136
- PK,
137
- SK,
138
- ...values
139
- }: {
135
+ item: {
140
136
  PK: string
141
137
  SK: string
142
138
  } & { [key in GSIPK]?: string } &
@@ -145,11 +141,11 @@ export const encodeDDBCursor = (
145
141
  ) => {
146
142
  const cursor = Buffer.from(
147
143
  JSON.stringify({
148
- PK,
149
- SK,
144
+ PK: item.PK,
145
+ SK: item.SK,
150
146
  ...GSI_NAMES.map((GSI) => ({
151
- [`${GSI}PK`]: values[`${GSI}PK` as const],
152
- [`${GSI}SK`]: values[`${GSI}SK` as const],
147
+ [`${GSI}PK`]: item[`${GSI}PK` as const],
148
+ [`${GSI}SK`]: item[`${GSI}SK` as const],
153
149
  })).reduce((acc, cur) => Object.assign(acc, cur), {}),
154
150
  })
155
151
  ).toString("base64")
package/src/sandbox.ts CHANGED
@@ -125,23 +125,25 @@ export const createSandbox = async (client: Client): Promise<Sandbox> => {
125
125
  seed: async (...args: Array<{ [key: string]: any }>) => {
126
126
  const chunks = chunksOf(25)(args)
127
127
 
128
- for (const chunk of chunks) {
129
- const items = chunk.map((i) =>
130
- typeof i?._model?.__dynamoDBEncode === "function"
131
- ? i._model.__dynamoDBEncode(i)
132
- : typeof i.encode === "function"
133
- ? i.encode()
134
- : i
135
- )
136
-
137
- await client.documentClient
138
- .batchWrite({
139
- RequestItems: {
140
- [tableName]: items.map((i) => ({ PutRequest: { Item: i } })),
141
- },
142
- })
143
- .promise()
144
- }
128
+ await Promise.all(
129
+ chunks.map(async (chunk) => {
130
+ const items = chunk.map((i) =>
131
+ typeof i?._model?.__dynamoDBEncode === "function"
132
+ ? i._model.__dynamoDBEncode(i)
133
+ : typeof i.encode === "function"
134
+ ? i.encode()
135
+ : i
136
+ )
137
+
138
+ return client.documentClient
139
+ .batchWrite({
140
+ RequestItems: {
141
+ [tableName]: items.map((i) => ({ PutRequest: { Item: i } })),
142
+ },
143
+ })
144
+ .promise()
145
+ })
146
+ )
145
147
  },
146
148
  get: (pk: string, sk: string) =>
147
149
  client.documentClient