atc-db-helper
v0.1.0
Published
SurrealDB v2 wrapper with Mango→SurrealQL translation, retry loops, and post-write hooks.
Maintainers
Readme
@atc-group/surreal-db-helper
Thin wrapper around surrealdb v2 with Mango-style query translation, retry loops, and factory-level post-write hooks.
Install
npm i @atc-group/surreal-db-helper surrealdb lodashRequires SurrealDB server ≥ 2.1.0.
Usage
import { createTable } from '@atc-group/surreal-db-helper';
import Surreal from 'surrealdb';
// app owns the connection
async function connect(dbName: string): Promise<Surreal> {
const db = new Surreal();
await db.connect(process.env.SURREAL_URL!, {
authentication: process.env.SURREAL_TOKEN!,
namespace: 'cloud',
database: dbName,
versionCheck: false,
});
return db;
}
const Order = createTable<{ _id?: string; total: number }>({
dbName: 'n123',
tableName: 'Order',
connect: () => connect('n123'),
idGenerator: () => crypto.randomUUID(),
afterWrite: ({ op, fullName }) => {
console.log(`${op} on ${fullName}`);
// e.g. publish to NATS
},
});
await Order.upsert({ total: 99 });
const rows = await Order.find({ selector: { total: { $gt: 50 } } });API
See the generated .d.ts. Public exports: createTable, buildCondition, buildParam, and all types.
