Usage
Usage
CRUD, relations, queries, React hooks, and read/write boundaries.
Usage covers day-to-day application code: reading and writing records, loading relations, choosing a query surface, and connecting UI with React hooks.
Guides
| Guide | Topics |
|---|---|
| CRUD | insert, update, delete, findById, findMany, count |
| Relations | belongsTo includes, hasMany patterns |
| Queries | AST, fluent builder, Mango, Prisma-style |
| React | Provider, useQuery, useWriter, sync hooks |
| Read & write | db.read, db.write, batch writes, serialized writers |
Mental model
Schema + adapter (setup)
↓
createDatabase → MelonCollection per table
↓
Reads: collection.find* / query(ast).fetch*
Writes: db.write(tx => ...) ← required for mutations
↓
React: MelonDbProvider + useQuery / useWriterAll query syntaxes compile to the same QueryAst before the SQLite adapter runs SQL.
Quick cross-surface example
// Fluent (@melon-db/db-query)
const ast = createQueryFactory(schema)
.from('tasks')
.where('status', 'eq', 'open')
.toAst();
// Mango (@melon-db/db-query-mango)
const mangoPrepared = createMangoCompiler().compile(
{ selector: { status: 'open' }, collection: 'tasks' },
schema,
);
// Prisma-style (@melon-db/db-prisma)
const prisma = createPrismaLikeClient(db);
await prisma.tasks.findMany({ where: { status: 'open' } });
// Direct AST
await db.collection('tasks').findMany({
collection: 'tasks',
mode: 'many',
where: { type: 'predicate', predicate: { field: 'status', op: 'eq', value: 'open' } },
});Pick one authoring surface per feature; they share the same engine and subscriptions.
