limbo/bindings/javascript
Jussi Saurio 6559f23f85 Merge 'hack imports of wasm due to the issues in Vite and Next.js build systems' from Nikita Sivukhin
This tries to establish smooth as possible experience for browser
packages in web.
In order to do that this PR do the following tricks:
1. export `./vite` entry-point which should be used like `import {
connect } from "@tursodatabase/database-browser/vite"` for Vite bundler
   * This entrypoint has fix for the issue
https://github.com/vitejs/vite/issues/8427 which breaks package in the
dev-server mode
   * In order to overcome this we do 2 tricks:
     - Inline WASM module in order to avoid loading it from the separate
file
     - Use same file as entry-point for main thread and for the web
worker
   * Note, that we do these tricks only for `development` build and
produce build will be chunked and optimized as usual with Vite and will
treat worker and WASM modules as separate fiels
3. export `./turbopack` entry-point which should be used like `import {
connect } from @tursodatabase/database-browser/turbopack"` for Turbopack
(Next.js) bundler
   * This entrypoint has fix for the issue
https://github.com/vercel/next.js/issues/82520'
   * In order to overcome this for now we always inline WASM for Next.js
4. Bundle browser libraries in order to easily consume them without need
for bundlers:
   * e.g. `import { connect } from
"https://unpkg.com/@tursodatabase/database-browser/bundle/main.es.js";`
5. We vendor `@napi-rs/wasm-runtime` for now in order to fix runtime
errors due to accesses to `process.env.NODE_DEBUG_NATIVE` env var (not
defined in web)
   * This should be very temporary solution because I already fixed
wasm-util dependency which cause this error
(https://github.com/toyobayashi/wasm-util/issues/4) and hope that napi-
rs PR will be merged soon: https://github.com/napi-rs/napi-rs/pull/2921

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3017
2025-09-12 16:18:47 +03:00
..
.yarn/releases bind/js: Switch to napi v3 2025-07-25 11:45:57 -03:00
docs add formatting instructions for js 2025-07-01 11:11:36 -04:00
examples opfs for sync in one commit! 2025-09-10 22:35:57 +04:00
packages newlines 2025-09-12 16:25:39 +04:00
perf rename database-core -> database-common 2025-09-09 14:26:21 +04:00
scripts hack imports of wasm due to the issues in Vite and Next.js build systems 2025-09-12 14:03:31 +04:00
src opfs for sync in one commit! 2025-09-10 22:35:57 +04:00
sync newlines 2025-09-12 16:25:39 +04:00
.gitignore build bundles for browser libs 2025-09-12 14:15:28 +04:00
.npmignore final adjustments 2025-09-09 14:06:10 +04:00
.yarnrc.yml Remove wasm binding 2025-07-28 14:48:51 -03:00
build.rs Initial JavaScript bindings with napi-rs 2025-03-26 13:30:13 +02:00
Cargo.toml Add tracing_release feature for benchmarks to compile tracing macros to noops 2025-09-10 09:56:12 -04:00
Makefile skip optional packages publish as they will be published for native package 2025-09-03 17:26:36 +04:00
package-lock.json bundle browser packages too in order to easily consume them without bundlers 2025-09-12 15:27:40 +04:00
package.json newlines 2025-09-12 16:25:39 +04:00
README.md update readme 2025-09-03 18:21:22 +04:00
replace.sh bundle browser packages too in order to easily consume them without bundlers 2025-09-12 15:27:40 +04:00
yarn.lock bundle browser packages too in order to easily consume them without bundlers 2025-09-12 15:27:40 +04:00

Turso Database for JavaScript

npm

Chat with other users of Turso on Discord


About

This package is the Turso in-memory database library for JavaScript.

⚠️ Warning: This software is ALPHA, only use for development, testing, and experimentation. We are working to make it production ready, but do not use it for critical data right now.

Features

  • SQLite compatible: SQLite query language and file format support (status).
  • In-process: No network overhead, runs directly in your Node.js process
  • TypeScript support: Full TypeScript definitions included
  • Cross-platform: Supports Linux (x86 and arm64), macOS, Windows and browsers (through WebAssembly)

Installation

npm install @tursodatabase/database

Getting Started

In-Memory Database

import { connect } from '@tursodatabase/database';

// Create an in-memory database
const db = await connect(':memory:');

// Create a table
db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)');

// Insert data
const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
insert.run('Alice', 'alice@example.com');
insert.run('Bob', 'bob@example.com');

// Query data
const users = db.prepare('SELECT * FROM users').all();
console.log(users);
// Output: [
//   { id: 1, name: 'Alice', email: 'alice@example.com' },
//   { id: 2, name: 'Bob', email: 'bob@example.com' }
// ]

File-Based Database

import { connect } from '@tursodatabase/database';

// Create or open a database file
const db = await connect('my-database.db');

// Create a table
db.exec(`
  CREATE TABLE IF NOT EXISTS posts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL,
    content TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
  )
`);

// Insert a post
const insertPost = db.prepare('INSERT INTO posts (title, content) VALUES (?, ?)');
const result = insertPost.run('Hello World', 'This is my first blog post!');

console.log(`Inserted post with ID: ${result.lastInsertRowid}`);

Transactions

import { connect } from '@tursodatabase/database';

const db = await connect('transactions.db');

// Using transactions for atomic operations
const transaction = db.transaction((users) => {
  const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
  for (const user of users) {
    insert.run(user.name, user.email);
  }
});

// Execute transaction
transaction([
  { name: 'Alice', email: 'alice@example.com' },
  { name: 'Bob', email: 'bob@example.com' }
]);

WebAssembly Support

Turso Database can run in browsers using WebAssembly. Check the browser.js and WASM artifacts for browser usage.

API Reference

For complete API documentation, see JavaScript API Reference.

License

This project is licensed under the MIT license.

Support