Database
MiniWork uses SQLite with WAL mode for high-performance embedded database operations.
Schema Definition
schema.sql
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT,
author_id INTEGER REFERENCES users(id)
);
Querying Data
// Select multiple rows
const users = ctx.db.query('SELECT * FROM users');
// Select with parameters
const user = ctx.db.query(
'SELECT * FROM users WHERE id = ?',
[userId]
);
Mutations
// Insert
ctx.db.run(
'INSERT INTO posts (title, content) VALUES (?, ?)',
[title, content]
);
// Update
ctx.db.run('UPDATE posts SET title = ? WHERE id = ?', [title, id]);
// Delete
ctx.db.run('DELETE FROM posts WHERE id = ?', [id]);
Transactions
ctx.db.transaction(() => {
ctx.db.run('INSERT INTO posts (title) VALUES (?)', ['Post 1']);
ctx.db.run('INSERT INTO posts (title) VALUES (?)', ['Post 2']);
// If any query fails, all changes are rolled back
});
Change Tracking
MiniWork automatically tracks database changes for real-time updates. Subscribed clients receive updates automatically.