Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert return type mismatch #99

Open
rrd108 opened this issue Jun 20, 2024 · 2 comments · May be fixed by #118
Open

Insert return type mismatch #99

rrd108 opened this issue Jun 20, 2024 · 2 comments · May be fixed by #118

Comments

@rrd108
Copy link

rrd108 commented Jun 20, 2024

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

@amandesai01
Copy link
Contributor

amandesai01 commented Oct 25, 2024

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

@amandesai01
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants