// Shared SQLite database across multiple moroutine calls on a single worker. // The db is opened once via a task-arg and cached for all subsequent queries. // Requires Node v24+. // // Run: node examples/sqlite/main.ts import { workers } from '../../src/index.ts'; import { openDb, insertUser, getUser, listUsers, countUsers } from './db.ts'; // openDb(':memory:') is a task — it runs once on the worker and is cached. // Every query receives the cached db instance without re-opening it. const db = openDb(':memory:'); { using run = workers(1); const id1 = await run(insertUser(db, 'Alice', 'alice@example.com')); const id2 = await run(insertUser(db, 'Bob', 'bob@example.com')); const id3 = await run(insertUser(db, 'Charlie', 'charlie@example.com')); console.log(`Inserted users with ids: ${id1}, ${id2}, ${id3}`); const alice = await run(getUser(db, id1)); console.log('Get Alice:', alice); const count = await run(countUsers(db)); console.log(`Total users: ${count}`); const all = await run(listUsers(db)); console.log('All users:', all); }