|
| 1 | +import type { DocumentClient } from "aws-sdk/clients/dynamodb"; |
| 2 | +import type { V2DocumentClient } from "../../index"; |
| 3 | + |
| 4 | +export type StoredItem = Record<string, any>; |
| 5 | + |
| 6 | +export type MockableMethod = keyof V2DocumentClient; |
| 7 | + |
| 8 | +const MockableMethods: Record<MockableMethod, MockableMethod> = { |
| 9 | + get: "get", |
| 10 | + put: "put", |
| 11 | + update: "update", |
| 12 | + delete: "delete", |
| 13 | + batchWrite: "batchWrite", |
| 14 | + batchGet: "batchGet", |
| 15 | + scan: "scan", |
| 16 | + query: "query", |
| 17 | + transactWrite: "transactWrite", |
| 18 | + transactGet: "transactGet", |
| 19 | +} |
| 20 | + |
| 21 | +export type CanceledTransactionSimulation = { |
| 22 | + cancellationReasons: Array<{ |
| 23 | + Code?: string; |
| 24 | + Item?: StoredItem; |
| 25 | + Message?: string; |
| 26 | + }>; |
| 27 | +}; |
| 28 | + |
| 29 | +type MethodInput = { |
| 30 | + get: DocumentClient.GetItemInput; |
| 31 | + put: DocumentClient.PutItemInput; |
| 32 | + update: DocumentClient.UpdateItemInput; |
| 33 | + delete: DocumentClient.DeleteItemInput; |
| 34 | + batchWrite: DocumentClient.BatchWriteItemInput; |
| 35 | + batchGet: DocumentClient.BatchGetItemInput; |
| 36 | + scan: DocumentClient.ScanInput; |
| 37 | + query: DocumentClient.QueryInput; |
| 38 | + transactWrite: DocumentClient.TransactWriteItemsInput; |
| 39 | + transactGet: DocumentClient.TransactGetItemsInput; |
| 40 | +}; |
| 41 | + |
| 42 | +type MethodOutput = { |
| 43 | + get: DocumentClient.GetItemOutput; |
| 44 | + put: DocumentClient.PutItemOutput; |
| 45 | + update: DocumentClient.UpdateItemOutput; |
| 46 | + delete: DocumentClient.DeleteItemOutput; |
| 47 | + batchWrite: DocumentClient.BatchWriteItemOutput; |
| 48 | + batchGet: DocumentClient.BatchGetItemOutput; |
| 49 | + scan: DocumentClient.ScanOutput; |
| 50 | + query: DocumentClient.QueryOutput; |
| 51 | + transactWrite: |
| 52 | + | DocumentClient.TransactWriteItemsOutput |
| 53 | + | CanceledTransactionSimulation; |
| 54 | + transactGet: |
| 55 | + | DocumentClient.TransactGetItemsOutput |
| 56 | + | CanceledTransactionSimulation; |
| 57 | +}; |
| 58 | + |
| 59 | +export type MockHandler<Method extends MockableMethod = MockableMethod> = |
| 60 | + | MethodOutput[Method] |
| 61 | + | ((params: MethodInput[Method]) => MethodOutput[Method]); |
| 62 | + |
| 63 | +/** method-name-keyed map: unknown method names are compile errors */ |
| 64 | +export type MockHandlers = { |
| 65 | + [Method in MockableMethod]?: MockHandler<Method>; |
| 66 | +}; |
| 67 | + |
| 68 | +export type MockClientCall = { |
| 69 | + method: MockableMethod; |
| 70 | + params: Record<string, any>; |
| 71 | +}; |
| 72 | + |
| 73 | +export type MockedV2DocumentClient = V2DocumentClient & { |
| 74 | + createSet: (value: any) => Set<any>; |
| 75 | +}; |
| 76 | + |
| 77 | +export type MockV2Client = { |
| 78 | + client: MockedV2DocumentClient; |
| 79 | + calls: MockClientCall[]; |
| 80 | +}; |
| 81 | + |
| 82 | +export function makeMockV2Client(handlers: MockHandlers = {}): MockV2Client { |
| 83 | + const calls: MockClientCall[] = []; |
| 84 | + const transactMethods = new Set<MockableMethod>([ |
| 85 | + "transactWrite", |
| 86 | + "transactGet", |
| 87 | + ]); |
| 88 | + const client: any = { |
| 89 | + createSet: (value: any) => new Set([].concat(value)), |
| 90 | + }; |
| 91 | + for (const method of Object.values(MockableMethods)) { |
| 92 | + client[method] = (params: Record<string, any>) => { |
| 93 | + calls.push({ method, params }); |
| 94 | + const handler = handlers[method]; |
| 95 | + const value: any = |
| 96 | + typeof handler === "function" |
| 97 | + ? (handler as (input: Record<string, any>) => unknown)(params) |
| 98 | + : handler; |
| 99 | + if (transactMethods.has(method)) { |
| 100 | + const stored: Record<string, (input: any) => void> = {}; |
| 101 | + return { |
| 102 | + on: (event: string, callback: (input: any) => void) => { |
| 103 | + stored[event] = callback; |
| 104 | + }, |
| 105 | + abort: () => {}, |
| 106 | + promise: () => { |
| 107 | + if (value && value.cancellationReasons) { |
| 108 | + if (stored.extractError) { |
| 109 | + stored.extractError({ |
| 110 | + httpResponse: { |
| 111 | + body: { |
| 112 | + toString: () => |
| 113 | + JSON.stringify({ |
| 114 | + CancellationReasons: value.cancellationReasons, |
| 115 | + }), |
| 116 | + }, |
| 117 | + }, |
| 118 | + }); |
| 119 | + } |
| 120 | + return Promise.reject(new Error("TransactionCanceledException")); |
| 121 | + } |
| 122 | + return Promise.resolve(value === undefined ? {} : value); |
| 123 | + }, |
| 124 | + }; |
| 125 | + } |
| 126 | + return { |
| 127 | + promise: () => Promise.resolve(value === undefined ? {} : value), |
| 128 | + }; |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + return { client, calls }; |
| 133 | +} |
| 134 | + |
| 135 | +export type MakePagingQueryHandlerOptions = { |
| 136 | + pages: number; |
| 137 | + perPage: number; |
| 138 | + /** produces the i-th stored item (from-DynamoDB shape, including key fields) */ |
| 139 | + makeItem: (i: number) => StoredItem; |
| 140 | +}; |
| 141 | + |
| 142 | +export type PagingQueryResponse = { |
| 143 | + Items: StoredItem[]; |
| 144 | + Count: number; |
| 145 | + LastEvaluatedKey?: { pk: any; sk: any }; |
| 146 | +}; |
| 147 | + |
| 148 | +export type MakePagingQueryHandlerResponse = ( |
| 149 | + params: Record<string, any>, |
| 150 | +) => PagingQueryResponse; |
| 151 | + |
| 152 | +// Builds a `query`/`scan` handler that pages through `pages` responses of |
| 153 | +// `perPage` items each, emitting a `LastEvaluatedKey` on every page but the |
| 154 | +// last. Sort keys must be unique across items. Like DynamoDB, the handler |
| 155 | +// resumes from the params' `ExclusiveStartKey` by locating the item whose |
| 156 | +// pk/sk match, so it also honors cursors ElectroDB synthesizes mid-page |
| 157 | +// (e.g. when a `count` limit lands inside a page). |
| 158 | +export function makePagingQueryHandler( |
| 159 | + options: MakePagingQueryHandlerOptions, |
| 160 | +): MakePagingQueryHandlerResponse { |
| 161 | + const { pages, perPage, makeItem } = options; |
| 162 | + const items: StoredItem[] = []; |
| 163 | + const indexBySortKey = new Map<string, number>(); |
| 164 | + for (let i = 0; i < pages * perPage; i++) { |
| 165 | + const item = makeItem(i); |
| 166 | + items.push(item); |
| 167 | + indexBySortKey.set(`${item.pk}|${item.sk}`, i); |
| 168 | + } |
| 169 | + return (params: Record<string, any>) => { |
| 170 | + let start = 0; |
| 171 | + const exclusiveStartKey = params.ExclusiveStartKey; |
| 172 | + if (exclusiveStartKey !== undefined) { |
| 173 | + const index = indexBySortKey.get( |
| 174 | + `${exclusiveStartKey.pk}|${exclusiveStartKey.sk}`, |
| 175 | + ); |
| 176 | + if (index === undefined) { |
| 177 | + throw new Error("Unknown ExclusiveStartKey provided to paging mock"); |
| 178 | + } |
| 179 | + start = index + 1; |
| 180 | + } |
| 181 | + const Items = items.slice(start, start + perPage); |
| 182 | + const response: PagingQueryResponse = { Items, Count: Items.length }; |
| 183 | + const lastReturned = start + Items.length - 1; |
| 184 | + if (lastReturned < items.length - 1 && Items.length > 0) { |
| 185 | + const lastItem = Items[Items.length - 1]; |
| 186 | + response.LastEvaluatedKey = { pk: lastItem.pk, sk: lastItem.sk }; |
| 187 | + } |
| 188 | + return response; |
| 189 | + }; |
| 190 | +} |
0 commit comments