Approach to get data out of duckdb's callback functions

Viewed 5

I'm pretty new to JS, and struggling to work out the "JS" way to do this.

I've seen some similar issues on SO, but I can't really work out how to apply them to my situation.

Specifically I'm trying to use duckdb's nodejs API to query a csv stored in the filesystem.

We have lots of other DB connectors also, and call the correct connector module based on the user's db selection: runQuery then returns results from a query in a blob.

Eg for our SQLite connector

// THIS WORKS 
const sqlite3 = require('sqlite3');
const { open } = require('sqlite');
const queryString = "SELECT * FROM TABLE"  // actually defined in another file
const filepath = USER_DEFINED_PATH_TO_FILE // defined in ENV VAR

const runQuery = async (queryString) => {
    const db = await open({filename: filepath, driver: sqlite3.Database})
    const result = await db.all(queryString);
    return result;
}

I'm trying to achieve the same thing for duckdb to read a file from a local csv. However the duckdb API only seems to have callback functions, so this doesn't work.

// THIS DOESN'T
var duckdb = require('duckdb');
const queryString = "SELECT * FROM 'my_data.csv'"

const runQuery = async (queryString) => {    
    
    const db = new duckdb.Database(":memory:");
    return await db.all(queryString, function(error, result){
        return result;
    });  

Result:

TypeError: Cannot read properties of undefined (reading 'replace') 

I know that the connector is working as expected, as I can console.log() the data inside the db.all() function.

How should I structure this so I can return the data from the runQuery function?

0 Answers
Related