unjs/db0

Insert return type mismatch

Closed this issue · 2 comments

Environment

Nuxt 3.13
Node 18

Reproduction

export default defineEventHandler(async event => {
  const db = useDatabase()
  const res = await db.sql`INSERT INTO orders VALUES (null, ${data.name})`
  return res
})

Describe the bug

If the INSERT was successful res will be {success: true}

Typescript infer the type as DefaultSQLResult

Additional context

No response

Logs

No response

The issue is, it depend on runtime as to what will be returned.
If it is a lookup query, i.e.

  • if starts with select, it runs db.prepare(sql).all(primitives)
  • else, it runs db.prepare(sql).run(primitives)

So to fix typings, you can run

db.prepare('INSERT INTO orders VALUES (null, ?)').run(data.name)

to get correct typing

But the issue is correct. For now, I have opened a PR which adds optional success field in default type, essentially, making required type a subset, and updated success flag when select query runs.
cc: @pi0