@athenox/talos
Version:
Talos: a backend you describe instead of write. Declare a schema, write the rules in plain language, map URLs to sentences; a small built-in model turns them into query plans.
30 lines (29 loc) • 1.11 kB
JavaScript
/**
* A Store over a SQLite handle — better-sqlite3's `Database` or Node's
* `DatabaseSync`, which share `prepare(sql).all/run`.
*/
export function sqliteStore(db) {
// Drivers refuse undefined and Date; SQLite has no booleans.
const arg = (v) => v === undefined ? null : v instanceof Date ? v.toISOString() : typeof v === 'boolean' ? Number(v) : v;
return {
dialect: 'sqlite',
all: (sql, ...p) => db.prepare(sql).all(...p.map(arg)),
run: (sql, ...p) => {
const r = db.prepare(sql).run(...p.map(arg));
return { changes: Number(r.changes), lastInsertRowid: Number(r.lastInsertRowid) };
},
lookup: () => null,
};
}
/** A Store over a mysql2/promise pool (MySQL, MariaDB). */
export function mysqlStore(pool) {
return {
dialect: 'mysql',
async all(sql, ...p) { return (await pool.query(sql, p))[0]; },
async run(sql, ...p) {
const [r] = await pool.query(sql, p);
return { changes: r.affectedRows, lastInsertRowid: r.insertId };
},
lookup: () => null,
};
}