dynamodb-onetable
dynamodb-onetable copied to clipboard
Dates inside array item are not being saved
Describe the bug
We are saving a date inside an array of objects using one table and they are being saved as empty objects {}
Dates outside of an array are working as expected
To Reproduce
I have tried to reproduce the error with the provided debug test but getting an actual error with the date inside the array:
```Unsupported type passed: Fri Sep 16 2022 17:18:24 GMT+0100 (British Summer Time). Pass options.convertClassInstanceToMap=true to marshall typeof object as map attribute.```
Any pointers on how to get past this error would be useful as I think it's a red herring as we don't see this on our project.
/*
debug.ts - Just for debug
Edit your test case here and invoke via: "jest debug"
Or run VS Code in the top level directory and just run.
*/
import {AWS, Client, Entity, Match, Model, Table, print, dump, delay} from './utils/init'
import { OneSchema } from '../src/index.js'
jest.setTimeout(7200 * 1000)
export const primary = {hash: "partitionKey", sort: "sortKey"};
// Change with your schema
const TestSchema = {
format: "onetable:1.1.0",
version: "0.0.1",
indexes: {
primary,
},
params: {
"isoDates": true,
"timestamps": true,
"createdField": "createdOn",
"updatedField": "updatedOn"
},
models: {
Test: {
partitionKey: {
type: String,
value: `TESTHOLDER-\${holderId}`
},
sortKey: {
type: String,
value: `TEST-\${id}#EFFECTIVEDATE-\${effectiveDate}`
},
id: {
type: String,
required: true
},
holderId: {
type: String,
required: true
},
effectiveDate: {
type: Date,
filter: false
},
additionalPeople: {
type: Array,
required: true,
items: {
type: Object,
schema: {
dateOfBirth: {
type: Date,
required: true
}
}
}
}
}
} as const
};
// Change your table params as required
const table = new Table({
name: 'DebugTable',
client: Client,
partial: false,
schema: TestSchema,
logger: true,
})
export type TestEntity = Entity<typeof TestSchema.models.Test>;
export const Test = table.getModel<TestEntity>("Test");
// This will create a local table
test('Create Table', async() => {
if (!(await table.exists())) {
await table.createTable()
expect(await table.exists()).toBe(true)
}
})
test('Test', async() => {
const test: TestEntity = {
id: "123456",
holderId: "7890",
effectiveDate: new Date(),
additionalPeople: [{
dateOfBirth: new Date()
}]
};
await Test.create(test);
const result = await Test.get({
holderId: test.holderId,
id: test.id
});
expect(result).not.toBeUndefined();
expect(result?.additionalPeople[0].dateOfBirth).toEqual(test.additionalPeople[0].dateOfBirth);
})
test('Destroy Table', async() => {
await table.deleteTable('DeleteTableForever')
expect(await table.exists()).toBe(false)
})
Unsupported type passed: Fri Sep 16 2022 17:18:24 GMT+0100 (British Summer Time). Pass options.convertClassInstanceToMap=true to marshall typeof object as map attribute.
at convertToAttr (node_modules/@aws-sdk/util-dynamodb/dist-cjs/convertToAttr.js:49:11)
at node_modules/@aws-sdk/util-dynamodb/dist-cjs/convertToAttr.js:118:54
at convertToMapAttrFromEnumerableProps (node_modules/@aws-sdk/util-dynamodb/dist-cjs/convertToAttr.js:122:7)
at convertToAttr (node_modules/@aws-sdk/util-dynamodb/dist-cjs/convertToAttr.js:23:16)
at node_modules/@aws-sdk/util-dynamodb/dist-cjs/convertToAttr.js:55:50
at Array.map (<anonymous>)
at convertToListAttr (node_modules/@aws-sdk/util-dynamodb/dist-cjs/convertToAttr.js:55:10)
at convertToAttr (node_modules/@aws-sdk/util-dynamodb/dist-cjs/convertToAttr.js:13:16)
at node_modules/@aws-sdk/util-dynamodb/dist-cjs/convertToAttr.js:118:54
at convertToMapAttrFromEnumerableProps (node_modules/@aws-sdk/util-dynamodb/dist-cjs/convertToAttr.js:122:7)
Expected behavior I expect the date of birth that is used when creating the entity to match what has been retrieved from dynamodb
Screenshots If applicable, add screenshots to help explain your problem.
Environment (please complete the following information):
- MacOS 12.5.1
- Node Version: v18.7.0
- OneTable Version: 2.5.0
- TypeScript Version: 4.6.3
- Any other relevant environment information
Additional context Add any other context about the problem here.
Thank you for the report and the reproduction. Easy to replicate.
Currently, nested items really only has support for the schema in TS typings. Internally, the array is handled as a non-typed array, so date and other type conversions are not implemented.
Unfortunately, not an easy fix or quick fix.
Thanks @mobsense
Would that be why the dates aren't saving at all in our code base?
The error mentioned in the issue is not what we are seeing, we get no error but the dates are empty objects instead.
Yes.
Do a transform in your own code to convert the date to Date.getTime() or Date.toISOString. And Date.parse() on the return.
We'll fix this, but that workaround should get you by.
I can see this has been marked as fixed recently. Does this indicate that a new release has (or will soon have) support for this, or are you considering the workaround a fix? @dev-embedthis
Fix committed to the repo. Dates inside arrays are transformed.