mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-24 19:44:21 +01:00
Before, db generated incorrect plan in case when offset parameter were introduced as variable: ``` turso> EXPLAIN SELECT * FROM users LIMIT ? OFFSET ?; addr opcode p1 p2 p3 p4 p5 comment ---- ----------------- ---- ---- ---- ------------- -- ------- 0 Init 0 16 0 0 Start at 16 1 Variable 1 1 0 0 r[1]=parameter(1); OFFSET expr 2 MustBeInt 1 0 0 0 3 Variable 2 2 0 0 r[2]=parameter(2); OFFSET expr 4 MustBeInt 2 0 0 0 5 OffsetLimit 1 3 2 0 if r[1]>0 then r[3]=r[1]+max(0,r[2]) else r[3]=(-1) 6 OpenRead 0 2 0 0 table=users, root=2, iDb=0 7 Rewind 0 15 0 0 Rewind table users 8 Variable 2 2 0 0 r[2]=parameter(2); OFFSET expr 9 MustBeInt 2 0 0 0 10 IfPos 2 14 1 0 r[2]>0 -> r[2]-=1, goto 14 11 Column 0 0 4 0 r[4]=users.x 12 ResultRow 4 1 0 0 output=r[4] 13 DecrJumpZero 1 15 0 0 if (--r[1]==0) goto 15 14 Next 0 8 0 0 15 Halt 0 0 0 0 16 Transaction 0 1 1 0 iDb=0 tx_mode=Read 17 Goto 0 1 0 0 ``` Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com> Closes #3360
Turso Database for JavaScript in Node
About
This package is the Turso embedded database library for JavaScript in Node.
⚠️ 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 (browser is supported in the separate package
@tursodatabase/database-browserpackage)
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
await 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 (?, ?)');
await insert.run('Alice', 'alice@example.com');
await insert.run('Bob', 'bob@example.com');
// Query data
const users = await 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
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
)
`);
// Insert a post
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');
// Using transactions for atomic operations
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);
}
});
// Execute transaction
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
- The @tursodatabase/serverless package provides a serverless driver with the same API.
- The @tursodatabase/sync package provides bidirectional sync between a local Turso database and Turso Cloud.
License
This project is licensed under the MIT license.