diff --git a/src/emitter.ts b/src/emitter.ts index 1ea14d8..b3439e8 100644 --- a/src/emitter.ts +++ b/src/emitter.ts @@ -59,9 +59,22 @@ function emitScalar(type: Scalar): Attribute { } function emitArrayModel(type: ArrayModelType): Attribute { + const elementType = type.indexer.value; + + // Enum arrays should be emitted as DynamoDB/ElectroDB "set" type + if (elementType.kind === "Enum") { + const items = Array.from(elementType.members).map( + ([key, member]) => `${member.value ?? key}`, + ); + return { + type: "set", + items, + }; + } + return { type: "list", - items: emitType(type.indexer.value) as CustomAttribute, + items: emitType(elementType) as CustomAttribute, }; } diff --git a/test/entities.test.js b/test/entities.test.js index a63ba70..9c07dee 100644 --- a/test/entities.test.js +++ b/test/entities.test.js @@ -205,6 +205,29 @@ suite("Person Entity", () => { }); }); + suite("Set type (CoffeePreferences[] - enum array)", () => { + test("coffeePreferences is a set type with required: true", () => { + assert.equal(Person.attributes.coffeePreferences.type, "set"); + assert.equal(Person.attributes.coffeePreferences.required, true); + }); + + test("coffeePreferences items contain enum values", () => { + assert.deepEqual(Person.attributes.coffeePreferences.items, [ + "01", + "02", + "03", + ]); + }); + + test("coffeePreferences has correct full structure", () => { + assert.deepEqual(Person.attributes.coffeePreferences, { + type: "set", + items: ["01", "02", "03"], + required: true, + }); + }); + }); + suite("Index configurations", () => { test("persons index (primary, pk only)", () => { const personsIndex = Person.indexes.persons; diff --git a/test/main.tsp b/test/main.tsp index 94e0b6c..84b6a39 100644 --- a/test/main.tsp +++ b/test/main.tsp @@ -7,6 +7,18 @@ scalar String64 extends string; @minLength(25) scalar UUID extends string; +enum PersonStatus { + ACTIVE: "01", + INACTIVE: "02", + DELETED: "03", +} + +enum CoffeePreferences { + MILK: "01", + SUGAR: "02", + SWEETENER: "03", +} + enum CountryCode { NL, US, @@ -91,4 +103,8 @@ model Person { address: Address; contact: Contact[]; nickName?: string; + + status: PersonStatus; + + coffeePreferences: CoffeePreferences[]; }