Skip to content

@duckdb/duckdb-wasm 1.24 #356

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

Merged
merged 7 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ export const topojson = dependency("topojson-client", "3.1.0", "dist/topojson-cl
export const exceljs = dependency("exceljs", "4.3.0", "dist/exceljs.min.js");
export const mermaid = dependency("mermaid", "9.2.2", "dist/mermaid.min.js");
export const leaflet = dependency("leaflet", "1.9.3", "dist/leaflet.js");
export const duckdb = dependency("@duckdb/duckdb-wasm", "1.17.0", "+esm");
export const duckdb = dependency("@duckdb/duckdb-wasm", "1.24.0", "+esm");
27 changes: 19 additions & 8 deletions src/duckdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {cdn} from "./require.js";
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

let promise;

export class DuckDBClient {
constructor(db) {
Object.defineProperties(this, {
Expand Down Expand Up @@ -126,6 +128,9 @@ export class DuckDBClient {
if (config.query?.castTimestampToDate === undefined) {
config = {...config, query: {...config.query, castTimestampToDate: true}};
}
if (config.query?.castBigIntToDouble === undefined) {
config = {...config, query: {...config.query, castBigIntToDouble: true}};
}
await db.open(config);
await Promise.all(
Object.entries(sources).map(async ([name, source]) => {
Expand Down Expand Up @@ -166,7 +171,7 @@ async function insertFile(database, name, file, options) {
const buffer = await file.arrayBuffer();
await database.registerFileBuffer(file.name, new Uint8Array(buffer));
} else {
await database.registerFileURL(file.name, url);
await database.registerFileURL(file.name, url, 4); // duckdb.DuckDBDataProtocol.HTTP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be an issue if the enum ever changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Something to check when we upgrade.

Copy link
Contributor

@domoritz domoritz Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason not to use the value from the DuckDB lib?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just plumbing. The duckdb module isn’t available globally; it’s loaded in loadDuckDB here:

const module = await import(`${cdn}${duckdb.resolve()}`);

So, I’d need to call loadDuckDB again here, or I’d need to somehow pass it through the call to createDuckDB above and then pass it down into insertFile.

const db = await createDuckDB();

await insertFile(db, name, source);

All of those felt like a lot more work than typing the number 4.

}
const connection = await database.connect();
try {
Expand All @@ -179,7 +184,7 @@ async function insertFile(database, name, file, options) {
...options
}).catch(async (error) => {
// If initial attempt to insert CSV resulted in a conversion
// error, try again, this time treating all columns as strings.
// error, try again, this time treating all columns as strings.
if (error.toString().includes("Could not convert")) {
return await insertUntypedCSV(connection, file, name);
}
Expand Down Expand Up @@ -247,9 +252,9 @@ async function insertArray(database, name, array, options) {
return await insertArrowTable(database, name, table, options);
}

async function createDuckDB() {
const duck = await import(`${cdn}${duckdb.resolve()}`);
const bundle = await duck.selectBundle({
async function loadDuckDB() {
const module = await import(`${cdn}${duckdb.resolve()}`);
const bundle = await module.selectBundle({
mvp: {
mainModule: `${cdn}${duckdb.resolve("dist/duckdb-mvp.wasm")}`,
mainWorker: `${cdn}${duckdb.resolve("dist/duckdb-browser-mvp.worker.js")}`
Expand All @@ -259,9 +264,15 @@ async function createDuckDB() {
mainWorker: `${cdn}${duckdb.resolve("dist/duckdb-browser-eh.worker.js")}`
}
});
const logger = new duck.ConsoleLogger();
const worker = await duck.createWorker(bundle.mainWorker);
const db = new duck.AsyncDuckDB(logger, worker);
const logger = new module.ConsoleLogger();
return {module, bundle, logger};
}

async function createDuckDB() {
if (promise === undefined) promise = loadDuckDB();
const {module, bundle, logger} = await promise;
const worker = await module.createWorker(bundle.mainWorker);
const db = new module.AsyncDuckDB(logger, worker);
await db.instantiate(bundle.mainModule);
return db;
}
Expand Down