Path: blob/master/src/packages/conat/persist/context.ts
1453 views
/*1Define functions for using sqlite, the filesystem, compression, etc.2These are functions that typically get set via nodejs on the backend,3not from a browser. Making this explicit helps clarify the dependence4on the backend and make the code more unit testable.5*/67import type BetterSqlite3 from "better-sqlite3";8type Database = BetterSqlite3.Database;9export { type Database };1011let betterSqlite3: any = null;1213export let compress: (data: Buffer) => Buffer = () => {14throw Error("must initialize persist context");15};1617export let decompress: (data: Buffer) => Buffer = () => {18throw Error("must initialize persist context");19};2021export let syncFiles = { local: "", archive: "" };2223export let ensureContainingDirectoryExists: (path: string) => Promise<void> = (24_path,25) => {26throw Error("must initialize persist context");27};2829export function initContext(opts: {30betterSqlite3;31compress: (Buffer) => Buffer;32decompress: (Buffer) => Buffer;33syncFiles: { local: string; archive: string };34ensureContainingDirectoryExists: (path: string) => Promise<void>;35}) {36betterSqlite3 = opts.betterSqlite3;37compress = opts.compress;38decompress = opts.decompress;39syncFiles = opts.syncFiles;40ensureContainingDirectoryExists = opts.ensureContainingDirectoryExists;41}4243export function createDatabase(...args): Database {44if (betterSqlite3 == null) {45throw Error(46"conat/persist must be initialized with the better-sqlite3 module -- import from backend/conat/persist instead",47);48}49return new betterSqlite3(...args);50}515253