When a document is created with an optional field as undefined, the @nested-object-types metadata does not include that field. If the field is later set to a typed value (e.g., a Date) and the document is saved, the metadata is not updated to include the new type information. On subsequent loads, the field is returned as a string instead of being deserialized to the correct type. Steps to Reproduce:
Define an entity with an optional Date field:
class MyDocument {
id: string;
createdAt: Date; // Required - set at creation
completedAt?: Date; // Optional - set later
}
Create and save a document without setting completedAt:
const doc = new MyDocument();
doc.createdAt = new Date();
// completedAt is undefined
await session.store(doc);
await session.saveChanges();
Resulting metadata:
{
"@metadata": {
"@nested-object-types": {
"createdAt": "date"
}
}
}
Later, load the document, set completedAt, and save:
const doc = await session.load<MyDocument>(id);
doc.completedAt = new Date();
await session.saveChanges();
Metadata after save (unchanged):
{
"@metadata": {
"@nested-object-types": {
"createdAt": "date"
}
}
}
On subsequent load, completedAt is returned as an ISO string instead of a Date object.
Expected Behavior: After step 4, the metadata should be updated to include completedAt:
{
"@metadata": {
"@nested-object-types": {
"createdAt": "date",
"completedAt": "date"
}
}
}
And on subsequent loads, completedAt should be deserialized as a Date object. Workaround: Using z.coerce.date() in Zod schemas to accept both strings and Date objects during validation.
Environment:
ravendb (Node.js client): v7.1.4
Node.js: v20.19.5