denodb icon indicating copy to clipboard operation
denodb copied to clipboard

return type of Model.delete() and Model.create() is wrong.

Open LoganTann opened this issue 3 years ago • 0 comments

Similar issue to https://github.com/eveningkid/denodb/issues/271

Using sqlite as DBMS.

The delete() method should have a correct return type. Actually, vscode considers it returns Model | Model[]. It should return { affectedRows: number } instead.

async deleteLore(name: string): Promise<number> {
    const result = await Lore.where({ name }).delete();
    console.log(result);
    // @ts-ignore : let me compile, it works
    return result.affectedRows;
}

Without the ts-ignore :

error: TS2339 [ERROR]: Property 'affectedRows' does not exist on type 'Model | Model[]'.
  Property 'affectedRows' does not exist on type 'Model[]'.

With ts-ignore, if field exists and model name is "Lore" :

Lore { affectedRows: 0 }

By the way, other functions such as create haves the similar issue, it returns something like :

{
	"affectedRows": 1,
	"lastInsertId": 5
}

While the return type in denodb's source code (see https://doc.deno.land/https://deno.land/x/[email protected]/mod.ts/~/Model) is Promise<Model>

Finally, I think that other methods such as select() should not return Model type, but the model's final type.

Imagine I have a model called Lore with fields title, description. When I get the result of a select, I'd like my code editor to autocomplete fields that belongs to my model. Actually, it only returns create, delete and update function.

LoganTann avatar Jun 30 '22 19:06 LoganTann