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

Is there a way to perform a synchronous all()? #17

Closed
gabrielfgularte opened this issue Feb 9, 2017 · 2 comments
Closed

Is there a way to perform a synchronous all()? #17

gabrielfgularte opened this issue Feb 9, 2017 · 2 comments

Comments

@gabrielfgularte
Copy link

Is it possible to wait until the query is finished? I have something like:

function query(fields, table) {
  db.on('open', () => {
    return db.prepare(`SELECT ${fields} FROM ${table}`).all()
  })
}

let rows = query('*', 'user')
console.log(rows) // is returning undefined. I need this to wait until all() is finished.

thanks!

@JoshuaWise
Copy link
Member

The database opens asynchronously, but after the database is opened, all queries are done synchronously.

db.on('open', () => {
  function query(fields, table) {
    return db.prepare(`SELECT ${fields} FROM ${table}`).all()
  }
  let rows = query('*', 'user')
  console.log(rows)
})

@JoshuaWise
Copy link
Member

Version 2 has been released! If you update better-sqlite3 to version 2, you'll be able to do this synchronously:

var Database = require('better-sqlite3')
var db = new Database('foobar.db', options)

function query(fields, table) {
  return db.prepare(`SELECT ${fields} FROM ${table}`).all()
}

let rows = query('*', 'user')
console.log(rows)

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

No branches or pull requests

2 participants