|
| 1 | +process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED = "1"; |
| 2 | + |
| 3 | +import { Entity } from "../index"; |
| 4 | +import { v4 as uuid } from "uuid"; |
| 5 | +import { expect } from "chai"; |
| 6 | +import DynamoDB from "aws-sdk/clients/dynamodb"; |
| 7 | + |
| 8 | +const client = new DynamoDB.DocumentClient({ |
| 9 | + region: "us-east-1", |
| 10 | + endpoint: process.env.LOCAL_DYNAMO_ENDPOINT ?? "http://localhost:8000", |
| 11 | + credentials: { |
| 12 | + accessKeyId: "test", |
| 13 | + secretAccessKey: "test", |
| 14 | + }, |
| 15 | +}); |
| 16 | + |
| 17 | +function sortBy<T>(arr: T[], key: keyof T): T[] { |
| 18 | + return [...arr].sort((a, z) => (a[key] > z[key] ? 1 : -1)); |
| 19 | +} |
| 20 | + |
| 21 | +describe("composite index hydration", () => { |
| 22 | + const table = "multi-attribute-projections"; |
| 23 | + |
| 24 | + function createEntity() { |
| 25 | + return new Entity( |
| 26 | + { |
| 27 | + model: { |
| 28 | + entity: uuid(), |
| 29 | + version: "1", |
| 30 | + service: uuid(), |
| 31 | + }, |
| 32 | + attributes: { |
| 33 | + id: { |
| 34 | + type: "string", |
| 35 | + }, |
| 36 | + organizationId: { |
| 37 | + type: "string", |
| 38 | + field: "attr1", |
| 39 | + }, |
| 40 | + teamId: { |
| 41 | + type: "string", |
| 42 | + field: "attr2", |
| 43 | + }, |
| 44 | + city: { |
| 45 | + type: "string", |
| 46 | + field: "attr3", |
| 47 | + }, |
| 48 | + state: { |
| 49 | + type: "string", |
| 50 | + field: "attr4", |
| 51 | + }, |
| 52 | + zip: { |
| 53 | + type: "string", |
| 54 | + field: "attr5", |
| 55 | + }, |
| 56 | + description: { |
| 57 | + type: "string", |
| 58 | + }, |
| 59 | + }, |
| 60 | + indexes: { |
| 61 | + primary: { |
| 62 | + pk: { |
| 63 | + field: "pk", |
| 64 | + composite: ["organizationId"], |
| 65 | + }, |
| 66 | + sk: { |
| 67 | + field: "sk", |
| 68 | + composite: ["id"], |
| 69 | + }, |
| 70 | + }, |
| 71 | + locationAll: { |
| 72 | + index: "gsi1", |
| 73 | + type: "composite", |
| 74 | + pk: { |
| 75 | + composite: ["organizationId", "teamId", "city"], |
| 76 | + }, |
| 77 | + sk: { |
| 78 | + composite: ["state", "zip"], |
| 79 | + }, |
| 80 | + }, |
| 81 | + locationKeysOnly: { |
| 82 | + index: "gsi2", |
| 83 | + type: "composite", |
| 84 | + projection: "keys_only", |
| 85 | + pk: { |
| 86 | + composite: ["organizationId", "teamId"], |
| 87 | + }, |
| 88 | + sk: { |
| 89 | + composite: ["city"], |
| 90 | + }, |
| 91 | + }, |
| 92 | + locationInclude: { |
| 93 | + index: "gsi3", |
| 94 | + type: "composite", |
| 95 | + projection: ["city"], |
| 96 | + pk: { |
| 97 | + composite: ["organizationId"], |
| 98 | + }, |
| 99 | + sk: { |
| 100 | + composite: ["state", "zip"], |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + { table, client }, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + function makeItems(count: number, overrides: Partial<Record<string, string>>) { |
| 110 | + return Array.from({ length: count }, () => ({ |
| 111 | + id: uuid(), |
| 112 | + organizationId: uuid(), |
| 113 | + teamId: uuid(), |
| 114 | + city: uuid(), |
| 115 | + state: uuid(), |
| 116 | + zip: uuid(), |
| 117 | + description: uuid(), |
| 118 | + ...overrides, |
| 119 | + })); |
| 120 | + } |
| 121 | + |
| 122 | + it("should hydrate composite index with ALL projection", async () => { |
| 123 | + const entity = createEntity(); |
| 124 | + const organizationId = uuid(); |
| 125 | + const teamId = uuid(); |
| 126 | + const city = uuid(); |
| 127 | + const items = makeItems(10, { organizationId, teamId, city }); |
| 128 | + |
| 129 | + await entity.put(items).go(); |
| 130 | + const { data } = await entity.query |
| 131 | + .locationAll({ organizationId, teamId, city }) |
| 132 | + .go({ hydrate: true }); |
| 133 | + |
| 134 | + expect(sortBy(data, "id")).to.deep.equal(sortBy(items, "id")); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should hydrate composite index with KEYS_ONLY projection", async () => { |
| 138 | + const entity = createEntity(); |
| 139 | + const organizationId = uuid(); |
| 140 | + const teamId = uuid(); |
| 141 | + const items = makeItems(10, { organizationId, teamId }); |
| 142 | + |
| 143 | + await entity.put(items).go(); |
| 144 | + const { data } = await entity.query |
| 145 | + .locationKeysOnly({ organizationId, teamId }) |
| 146 | + .go({ hydrate: true }); |
| 147 | + |
| 148 | + expect(sortBy(data, "id")).to.deep.equal(sortBy(items, "id")); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should hydrate composite index with INCLUDE projection and return non-projected attributes", async () => { |
| 152 | + const entity = createEntity(); |
| 153 | + const organizationId = uuid(); |
| 154 | + const items = makeItems(10, { organizationId }); |
| 155 | + |
| 156 | + await entity.put(items).go(); |
| 157 | + const { data } = await entity.query |
| 158 | + .locationInclude({ organizationId }) |
| 159 | + .go({ hydrate: true, attributes: ["city", "description"] }); |
| 160 | + |
| 161 | + // "description" is not in the GSI projection, so it should only appear via hydration |
| 162 | + const expected = sortBy(items, "city").map((item) => ({ |
| 163 | + city: item.city, |
| 164 | + description: item.description, |
| 165 | + })); |
| 166 | + const actual = sortBy(data, "city").map((item) => ({ |
| 167 | + city: item.city, |
| 168 | + description: item.description, |
| 169 | + })); |
| 170 | + expect(actual).to.deep.equal(expected); |
| 171 | + }); |
| 172 | +}); |
0 commit comments