arblant
panel-gateway / test / sqlite-adapter.mjs
sqlite-adapter.mjs22 lines · 746 B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* node:sqlite adapter matching the Workers `state.storage.sql` API surface
   used by the Accounting DO (exec with ? bindings; .one()/.toArray()/rowsWritten). */
import { DatabaseSync } from 'node:sqlite';

export function makeSqliteState() {
  const db = new DatabaseSync(':memory:');
  return {
    storage: {
      sql: {
        exec(sql, ...bindings) {
          const stmt = db.prepare(sql);
          if (/^\s*select/i.test(sql)) {
            const rows = stmt.all(...bindings);
            return { one: () => (rows[0] === undefined ? null : rows[0]), toArray: () => rows };
          }
          const info = stmt.run(...bindings);
          return { rowsWritten: Number(info.changes), rowsRead: 0 };
        },
      },
    },
  };
}