diff --git a/src/index.test.ts b/src/index.test.ts index 485dc06..1d0abe8 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -325,6 +325,48 @@ describe("omanyd", () => { expect(readThing?.a.b.date).toBeInstanceOf(Date); expect(readThing?.a.b.date).toEqual(new Date("2023-03-02T11:49:00.000Z")); }); + + it("should save and return dates nested within arrays", async () => { + interface Thing { + id: string; + list: { + date: Date; + }[]; + } + const ThingStore = Omanyd.define({ + name: "arrayNestedDate", + hashKey: "id", + schema: Joi.object({ + id: Omanyd.types.id(), + list: Joi.array() + .items( + Joi.object({ + date: Joi.date().required(), + }).required() + ) + .required(), + }), + }); + + await Omanyd.createTables(); + + const savedThing = await ThingStore.create({ + list: [ + { + date: new Date("2023-03-02T11:49:00.000Z"), + }, + ], + }); + const readThing = await ThingStore.getByHashKey(savedThing.id); + expect(savedThing.list[0].date).toBeInstanceOf(Date); + expect(savedThing.list[0].date).toEqual( + new Date("2023-03-02T11:49:00.000Z") + ); + expect(readThing?.list[0].date).toBeInstanceOf(Date); + expect(readThing?.list[0].date).toEqual( + new Date("2023-03-02T11:49:00.000Z") + ); + }); }); describe("range key", () => { diff --git a/src/serializer.ts b/src/serializer.ts index 3c8641b..f9b7859 100644 --- a/src/serializer.ts +++ b/src/serializer.ts @@ -52,17 +52,26 @@ class Serializer { return undefined; } - const meta = schema.describe().metas?.[0]?.type; - switch (meta) { - case "SS": - return { SS: a }; - default: - return { - L: a - .map((item) => this.any(item)) - .filter(Boolean) as AWSDDB.AttributeValue[], - }; + const description = schema.describe(); + const meta = description.metas?.[0]?.type; + if (meta === "SS") { + return { SS: a }; } + + const itemsSchema = schema.$_terms.items[0]; + if (itemsSchema) { + return { + L: a + .map((item) => this.toDynamoValue(item, itemsSchema)) + .filter(Boolean) as AWSDDB.AttributeValue[], + }; + } + + return { + L: a + .map((item) => this.any(item)) + .filter(Boolean) as AWSDDB.AttributeValue[], + }; } private any(value: any): AWSDDB.AttributeValue | undefined {