Node-sqlite3: Is there a way to perform a synchronous all()?

Created on 9 Feb 2017  Â·  3Comments  Â·  Source: mapbox/node-sqlite3

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!

Most helpful comment

@C3realGuy Sqlite's C API is synchronous – this is a very reasonable request.

All 3 comments

There is no synchronous function and there shouldn't be one.
Use a callback (or promises). You need to understand the way javascript works, what async means etc.
Also check out the async/await stuff for ES7.

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

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

@C3realGuy Sqlite's C API is synchronous – this is a very reasonable request.

You can use better-sqlite3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikolasz picture mikolasz  Â·  3Comments

NilSet picture NilSet  Â·  3Comments

raphaelsoul picture raphaelsoul  Â·  4Comments

DenisCarriere picture DenisCarriere  Â·  3Comments

barbalex picture barbalex  Â·  4Comments