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
Add DuckDB node.js API #1112
Merged
Add DuckDB node.js API #1112
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fixes #661 |
🥳 |
@hannesmuehleisen I'm super excited to play with this! Can't seem to find the package on npm... am I too early? |
Yes please try again tomorrow |
Got it! Looking forward to it :) |
I'll go ahead and add some TS types when we get a chance to test it too |
Ok |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds a node.js API for DuckDB in
/tools/nodejs
due to popular demand. The API for this client is somewhat compliant to the SQLite node.js client for easier transition (and transition you must eventually). The npm package is called 'duckdb' thanks to @twilson63 who graciously allowed us to take over that package name.Install the node.js API like so (for now, binaries & npm will follow)
(this could take a while)
Then load the package and create a database object:
Then you can run a query:
Other available methods are
each
, where the callback is invoked for each row,run
to execute a single statement without results andexec
, which can execute several SQL commands at once but also does not return results. All those commands can work with prepared statements, taking the values for the parameters as additional arguments. For example like so:However, these are all shorthands for something much more elegant. A database can have multiple
Connection
s, those are created usingdb.connect()
.You can create multiple connections, each with their own transaction context.
Connection
objects also contain shorthands to directly callrun()
,all()
andeach()
with parameters and callbacks, respectively, for example:From connections, you can create prepared statements (and only that) using
con.prepare()
:To execute this statement, you can call for example
all()
on thestmt
object:You can also execute the prepared statement multiple times. This is for example useful to fill a table with data:
prepare()
can also take a callback which gets the prepared statement as an argument: