couchbaselabs / node-ottoman

Node.js ODM for Couchbase
https://ottomanjs.com/
Apache License 2.0
287 stars 98 forks source link

Infer document type returned from findByID #706

Closed Angusross111 closed 1 year ago

Angusross111 commented 1 year ago

What is the best way to work with Typescript with this library? I want to pass a type to the model creation, and then would expect any documents I am retrieving in the code would have the inferred types? Is that possible? currently I just get any as the type for plannerTask when i hover over it:

    type TaskType = {
        id: string;
        status: string;
    };
    const PlannerTask = model<TaskType>("PlannerTask", {
        id: { type: String, required: true },
        status: { type: String, required: true },
    });
    const taskID = "a124e17f-a12e-4f26-8ead-de611fd2c09b";
    const task = new PlannerTask({ id: taskID, status: "running" });
    await task.save();
    const plannerTask = await PlannerTask.findById(taskID);

Do I need to pass the type to findById each time?

const plannerTask = await PlannerTask.findById<TaskType>(taskID);

Is there also an option to not have findById throw an error if the document doesnt exist and just return undefined

gsi-alejandro commented 1 year ago

hi @Angusross111

You will not need to use it this way: const plannerTask = await PlannerTask.findById<TaskType>(taskID);

You only need to provide:

const PlannerTask = model<TaskType>("PlannerTask", {
    id: { type: String, required: true },
    status: { type: String, required: true },
});

Right now it's not working, the fix will be included in the next release.

gsi-alejandro commented 1 year ago

@Angusross111, Thank you for your feedback, we appreciate it so much.

If you have any ideas, suggestions, or feature requests feel free to create a ticket.

Thank you so much, happy coding!

gsi-alejandro commented 1 year ago

Hi @Angusross111

These changes were released in version 2.3.2