Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 31 lines 1.1 kB view raw
1// Shared SQLite database across multiple moroutine calls on a single worker. 2// The db is opened once via a task-arg and cached for all subsequent queries. 3// Requires Node v24+. 4// 5// Run: node examples/sqlite/main.ts 6 7import { workers } from '../../src/index.ts'; 8import { openDb, insertUser, getUser, listUsers, countUsers } from './db.ts'; 9 10// openDb(':memory:') is a task — it runs once on the worker and is cached. 11// Every query receives the cached db instance without re-opening it. 12const db = openDb(':memory:'); 13 14{ 15 using run = workers(1); 16 17 const id1 = await run(insertUser(db, 'Alice', 'alice@example.com')); 18 const id2 = await run(insertUser(db, 'Bob', 'bob@example.com')); 19 const id3 = await run(insertUser(db, 'Charlie', 'charlie@example.com')); 20 21 console.log(`Inserted users with ids: ${id1}, ${id2}, ${id3}`); 22 23 const alice = await run(getUser(db, id1)); 24 console.log('Get Alice:', alice); 25 26 const count = await run(countUsers(db)); 27 console.log(`Total users: ${count}`); 28 29 const all = await run(listUsers(db)); 30 console.log('All users:', all); 31}