@monlite/postgres
v0.2.0
Published
Postgres engine for @monlite/core — the same monlite API, on a networked Postgres (JSONB) instead of a local SQLite file. Swap the engine, not your code.
Maintainers
Readme
@monlite/postgres
The Postgres engine for monlite. The same
@monlite/core API — documents, queries, the full CRUD surface — on a networked Postgres
(documents stored as JSONB) instead of a local SQLite file. Swap the engine, not your code.
npm install @monlite/core @monlite/postgres pgimport { createDb } from "@monlite/postgres";
const db = createDb("postgres://user@host/db");
await db.collection("users").create({ data: { name: "Ada", age: 30, tags: ["admin"] } });
const adults = await db.collection("users").findMany({
where: { age: { gte: 18 }, tags: { has: "admin" } },
orderBy: { age: "desc" },
});Or pass the engine to core's createDb:
import { createDb } from "@monlite/core";
import { postgres } from "@monlite/postgres";
const db = createDb("pg", { driver: postgres("postgres://…", { pool: { max: 10 } }) });Same API, different engine
Documents become jsonb; the query builder emits the Postgres dialect (->/->>/@>/~,
jsonb_array_elements, to_jsonb); transactions run on a pooled client with SAVEPOINTs. The
db.collection(...) API is identical to SQLite-backed monlite — develop on a .db file,
deploy to Postgres, don't rewrite a line.
The whole data surface works:
CRUD —
create,createMany,findMany,findFirst,findById,count,exists,update,updateMany,upsert,delete,deleteMany,findOneAndUpdate,bulkWrite,purgeExpired.Aggregation —
aggregate,groupBy(withhaving+orderBy),distinct.Realtime —
watch()via PostgresLISTEN/NOTIFY(truly cross-process — a write from any connection reaches every watcher).Full-text search —
@monlite/ftson a native generatedtsvectorcolumn + GIN index.Vector search —
@monlite/vectoron a native generatedvectorcolumn + HNSW index (pgvector).Job queue —
@monlite/queue'screatePgQueue(db), claiming withFOR UPDATE SKIP LOCKED.
Not yet: only explain() (Postgres' EXPLAIN output is engine-specific — throws a clear error).
Notes
- Requires PostgreSQL 14+ (vector search needs pgvector; the
monlite/postgresimage bundles it). Placeholders?are rewritten to$1,$2,…. - Each transaction runs on its own pooled connection — concurrent transactions parallelize
(nested ones become
SAVEPOINTs) and retry automatically on serialization failure / deadlock. @monlite/corestays the minimal, zero-dependency local engine — this package is purely additive, opt-in.
License
MIT
