Turso Database for JavaScript in Node
About
This package is the Turso embedded database library for JavaScript in Node.
Turso Database runs in production today at multiple organizations. It has not yet reached 1.0, so as with any database we recommend keeping backups — see the FAQ for where the project stands.
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 (browser is supported in the separate package
@tursodatabase/database-wasm package)
Installation
npm install @tursodatabase/database
Getting Started
In-Memory Database
import { connect } from '@tursodatabase/database';
const db = await connect(':memory:');
await db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)');
const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
await insert.run('Alice', 'alice@example.com');
await insert.run('Bob', 'bob@example.com');
const users = await db.prepare('SELECT * FROM users').all();
console.log(users);
File-Based Database
import { connect } from '@tursodatabase/database';
const db = await connect('my-database.db');
await 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
)
`);
const insertPost = db.prepare('INSERT INTO posts (title, content) VALUES (?, ?)');
const result = await 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');
const transaction = db.transaction(async (users) => {
const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
for (const user of users) {
await insert.run(user.name, user.email);
}
});
await transaction([
{ name: 'Alice', email: 'alice@example.com' },
{ name: 'Bob', email: 'bob@example.com' }
]);
API Reference
For complete API documentation, see JavaScript API Reference.
Related Packages
License
This project is licensed under the MIT license
Support