{"version":3,"file":"hocuspocus-sqlite.cjs","names":["Database","BetterSqlite3"],"sources":["../src/SQLite.ts"],"sourcesContent":["import type { DatabaseConfiguration } from \"@hocuspocus/extension-database\";\nimport { Database } from \"@hocuspocus/extension-database\";\nimport BetterSqlite3 from \"better-sqlite3\";\nimport kleur from \"kleur\";\n\nexport const schema = `CREATE TABLE IF NOT EXISTS \"documents\" (\n  \"name\" varchar(255) NOT NULL,\n  \"data\" blob NOT NULL,\n  UNIQUE(name)\n)`;\n\nexport const selectQuery = `\n  SELECT data FROM \"documents\" WHERE name = $name ORDER BY rowid DESC\n`;\n\nexport const upsertQuery = `\n  INSERT INTO \"documents\" (\"name\", \"data\") VALUES ($name, $data)\n    ON CONFLICT(name) DO UPDATE SET data = $data\n`;\n\nconst SQLITE_INMEMORY = \":memory:\";\n\nexport interface SQLiteConfiguration extends DatabaseConfiguration {\n\t/**\n\t * Valid values are filenames, \":memory:\" for an anonymous in-memory database and an empty\n\t * string for an anonymous disk-based database. Anonymous databases are not persisted and\n\t * when closing the database handle, their contents are lost.\n\t *\n\t * https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#new-daboratabasepath-options\n\t */\n\tdatabase: string;\n\t/**\n\t * The database schema to create.\n\t */\n\tschema: string;\n}\n\nexport class SQLite extends Database {\n\tdb?: BetterSqlite3.Database;\n\n\tconfiguration: SQLiteConfiguration = {\n\t\tdatabase: SQLITE_INMEMORY,\n\t\tschema,\n\t\tfetch: async ({ documentName }) => {\n\t\t\tconst row = this.db\n\t\t\t\t?.prepare(selectQuery)\n\t\t\t\t.get({ name: documentName }) as { data: Buffer } | undefined;\n\n\t\t\treturn row?.data ?? null;\n\t\t},\n\t\tstore: async ({ documentName, state }) => {\n\t\t\tthis.db?.prepare(upsertQuery).run({\n\t\t\t\tname: documentName,\n\t\t\t\tdata: state,\n\t\t\t});\n\t\t},\n\t};\n\n\tconstructor(configuration?: Partial<SQLiteConfiguration>) {\n\t\tsuper({});\n\n\t\tthis.configuration = {\n\t\t\t...this.configuration,\n\t\t\t...configuration,\n\t\t};\n\t}\n\n\tasync onConfigure() {\n\t\tthis.db = new BetterSqlite3(this.configuration.database);\n\t\tthis.db.exec(this.configuration.schema);\n\t}\n\n\tasync onListen() {\n\t\tif (this.configuration.database === SQLITE_INMEMORY) {\n\t\t\tconsole.warn(\n\t\t\t\t`  ${kleur.yellow(\"The SQLite extension is configured as an in-memory database. All changes will be lost on restart!\")}`,\n\t\t\t);\n\t\t\tconsole.log();\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,MAAa,SAAS;;;;;AAMtB,MAAa,cAAc;;;AAI3B,MAAa,cAAc;;;;AAK3B,MAAM,kBAAkB;AAiBxB,IAAa,SAAb,cAA4BA,wCAAS;CAqBpC,YAAY,eAA8C;AACzD,QAAM,EAAE,CAAC;uBAnB2B;GACpC,UAAU;GACV;GACA,OAAO,OAAO,EAAE,mBAAmB;AAKlC,YAJY,KAAK,IACd,QAAQ,YAAY,CACrB,IAAI,EAAE,MAAM,cAAc,CAAC,GAEjB,QAAQ;;GAErB,OAAO,OAAO,EAAE,cAAc,YAAY;AACzC,SAAK,IAAI,QAAQ,YAAY,CAAC,IAAI;KACjC,MAAM;KACN,MAAM;KACN,CAAC;;GAEH;AAKA,OAAK,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAG;GACH;;CAGF,MAAM,cAAc;AACnB,OAAK,KAAK,IAAIC,uBAAc,KAAK,cAAc,SAAS;AACxD,OAAK,GAAG,KAAK,KAAK,cAAc,OAAO;;CAGxC,MAAM,WAAW;AAChB,MAAI,KAAK,cAAc,aAAa,iBAAiB;AACpD,WAAQ,KACP,KAAK,cAAM,OAAO,oGAAoG,GACtH;AACD,WAAQ,KAAK"}