The ArangoJS collection() function can take a typescript definition of the document type inside the collection (collection()).
This type is extended to the save() function. This is great if the type is just the saved document object plus Partial<> arango document metadata.
However, I'd like to be able to explicitly type the values passed to the save() function, because they may differ significantly from the stored document - for example, if the collection uses computed values, the save function would still expect these values to be passed into the save() function, which doesn't make sense. The alternative that I'm aware of is to mark those computed values in Typescript as optional, but if the Database computed function ensures that the value is never undefined, the clients should also be able to know the values are never undefined.
My current solution is this:
const doc = await db
.collection<Arango.Vertices.CollaborativeDocument.Document>(configs.arango.collections.vertices.collaborativeDocuments.config.key)
.save({
_createdAt: new Date().toISOString(),
_updatedAt: new Date().toISOString(),
data: "",
} as Arango.Vertices.CollaborativeDocument.Document);
But I don't like to use Type Assertion, and this loses out on the type definition of the create type in question.
Something like this would be great:
const doc = await db
.collection<Arango.Vertices.CollaborativeDocument.Document>(configs.arango.collections.vertices.collaborativeDocuments.config.key)
.save<Arango.Vertices.CollaborativeDocument.Create>({
_createdAt: new Date().toISOString(),
_updatedAt: new Date().toISOString(),
data: "",
});
Alternatively, if there is a better way to achieve this, please let me know!
The ArangoJS collection() function can take a typescript definition of the document type inside the collection (collection()).
This type is extended to the save() function. This is great if the type is just the saved document object plus Partial<> arango document metadata.
However, I'd like to be able to explicitly type the values passed to the save() function, because they may differ significantly from the stored document - for example, if the collection uses computed values, the save function would still expect these values to be passed into the save() function, which doesn't make sense. The alternative that I'm aware of is to mark those computed values in Typescript as optional, but if the Database computed function ensures that the value is never undefined, the clients should also be able to know the values are never undefined.
My current solution is this:
But I don't like to use Type Assertion, and this loses out on the type definition of the create type in question.
Something like this would be great:
Alternatively, if there is a better way to achieve this, please let me know!