Skip to content

DuckDBClient #313

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 5 commits into from
Nov 8, 2022
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 .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2018
"ecmaVersion": 2020
Copy link
Member Author

Choose a reason for hiding this comment

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

This is needed for dynamic import. However, this probably means that this should be released as a major version bump since it means older JavaScript environments may not be able to support the new syntax. We could avoid this by reverting to the AMD (require) bundle for duckdb-wasm and apache-arrow, but it might be time to move forward. I still need to think about this a bit.

},
"env": {
"es6": true,
Expand Down
10 changes: 7 additions & 3 deletions bin/resolve-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ const mains = ["unpkg", "jsdelivr", "browser", "main"];
}
{
const package = await resolve("apache-arrow@4");
console.log(`export const arrow = dependency("${package.name}", "${package.version}", "${package.export}");`);
console.log(`export const arrow4 = dependency("${package.name}", "${package.version}", "${package.export}");`);
}
{
const package = await resolve("apache-arrow@9");
console.log(`export const arrow9 = dependency("${package.name}", "${package.version}", "+esm");`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Arrow 10 is out so you can update to that.

Copy link
Member Author

Choose a reason for hiding this comment

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

}
{
const package = await resolve("arquero");
Expand All @@ -87,8 +91,8 @@ const mains = ["unpkg", "jsdelivr", "browser", "main"];
console.log(`export const leaflet = dependency("${package.name}", "${package.version}", "${package.export.replace(/-src\.js$/, ".js")}");`);
}
{
const package = await resolve("@duckdb/duckdb-wasm@1.17.0");
console.log(`export const duckdb = dependency("${package.name}", "${package.version}", "${package.export}");`);
const package = await resolve("@duckdb/duckdb-wasm");
console.log(`export const duckdb = dependency("${package.name}", "${package.version}", "+esm");`);
}
})();

Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default [
reserved: [
"FileAttachment",
"RequireError",
"DuckDBClient",
"SQLiteDatabaseClient",
"Workbook",
"ZipArchive",
Expand Down
58 changes: 58 additions & 0 deletions src/arrow.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Returns true if the vaue is an Apache Arrow table. This uses a “duck” test
// (instead of strict instanceof) because we want it to work with a range of
// Apache Arrow versions at least 7.0.0 or above.
// https://arrow.apache.org/docs/7.0/js/classes/Arrow_dom.Table.html
export function isArrowTable(value) {
return (
value &&
typeof value.getChild === "function" &&
typeof value.toArray === "function" &&
value.schema &&
Array.isArray(value.schema.fields)
);
}

export function getArrowTableSchema(table) {
return table.schema.fields.map(getArrowFieldSchema);
}

function getArrowFieldSchema(field) {
return {
name: field.name,
type: getArrowType(field.type),
nullable: field.nullable,
databaseType: String(field.type)
};
}

// https://github.com/apache/arrow/blob/89f9a0948961f6e94f1ef5e4f310b707d22a3c11/js/src/enum.ts#L140-L141
function getArrowType(type) {
switch (type.typeId) {
case 2: // Int
return "integer";
case 3: // Float
case 7: // Decimal
return "number";
case 4: // Binary
case 15: // FixedSizeBinary
return "buffer";
case 5: // Utf8
return "string";
case 6: // Bool
return "boolean";
case 8: // Date
case 9: // Time
case 10: // Timestamp
return "date";
case 12: // List
case 16: // FixedSizeList
return "array";
case 13: // Struct
case 14: // Union
return "object";
case 11: // Interval
case 17: // Map
default:
return "other";
}
}
12 changes: 3 additions & 9 deletions src/dependencies.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import dependency from "./dependency.mjs";
import * as ddb from "@duckdb/duckdb-wasm";
export const d3 = dependency("d3", "7.6.1", "dist/d3.min.js");
export const inputs = dependency("@observablehq/inputs", "0.10.4", "dist/inputs.min.js");
export const plot = dependency("@observablehq/plot", "0.6.0", "dist/plot.umd.min.js");
Expand All @@ -14,16 +13,11 @@ export const sql = dependency("sql.js", "1.7.0", "dist/sql-wasm.js");
export const vega = dependency("vega", "5.22.1", "build/vega.min.js");
export const vegalite = dependency("vega-lite", "5.5.0", "build/vega-lite.min.js");
export const vegaliteApi = dependency("vega-lite-api", "5.0.0", "build/vega-lite-api.min.js");
export const arrow = dependency("apache-arrow", "^8", "Arrow.es2015.min.js");
export const arrow4 = dependency("apache-arrow", "4.0.1", "Arrow.es2015.min.js");
export const arrow9 = dependency("apache-arrow", "9.0.0", "+esm");
export const arquero = dependency("arquero", "4.8.8", "dist/arquero.min.js");
export const topojson = dependency("topojson-client", "3.1.0", "dist/topojson-client.min.js");
export const exceljs = dependency("exceljs", "4.3.0", "dist/exceljs.min.js");
export const mermaid = dependency("mermaid", "9.1.6", "dist/mermaid.min.js");
export const leaflet = dependency("leaflet", "1.8.0", "dist/leaflet.js");
export const duckdb = ddb;
// export const duckdb = dependency("@duckdb/duckdb-wasm", "1.17.0", "dist/duckdb-browser.cjs");
// export const duckdb = dependency("@duckdb/duckdb-wasm", "1.17.0", "");
// export const duckdb = dependency("@duckdb/duckdb-wasm", "1.17.0", "?min");
// tried: dependency("@duckdb/duckdb-wasm", "1.17.0", "+esm");
// dependency("@duckdb/duckdb-wasm", "1.17.0", "dist/duckdb-browser.cjs");
// dependency("@duckdb/duckdb-wasm", "1.17.0", "dist/duckdb-browser.mjs");
export const duckdb = dependency("@duckdb/duckdb-wasm", "1.17.0", "+esm");
Loading