UNPKG

y-cap-sqlite

Version:

y-cap-sqlite is a persistence provider for [Yjs](https://github.com/yjs/yjs) that uses [@capacitor-community/sqlite](https://github.com/capacitor-community/sqlite) to store document changes in a SQLite database. This is useful for Capacitor projects that

120 lines (118 loc) 4.48 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { CapSQLitePersistence: () => CapSQLitePersistence }); module.exports = __toCommonJS(index_exports); var Y = __toESM(require("yjs"), 1); var promise = __toESM(require("lib0/promise"), 1); var import_observable = require("lib0/observable"); var import_buffer = require("buffer"); var DB_PREFIX = "cap-sqlite-persistence-"; var CapSQLitePersistence = class _CapSQLitePersistence extends import_observable.ObservableV2 { doc; config; whenSynced; name; db; con; synced = false; _dbref = 0; _dbsize = 0; _destroyed = false; constructor(config, doc, name, sqlite) { super(); this.doc = doc; this.config = config; this.name = name; this.con = sqlite; this.whenSynced = promise.create( (resolve) => this.on("synced", () => resolve(this)) ); this.destroy = this.destroy.bind(this); this._storeUpdate.bind(this); doc.on("update", this._storeUpdate.bind(this)); doc.on("destroy", this.destroy.bind(this)); } async initDb() { try { const dbName = DB_PREFIX + this.name; console.log((await this.con.isConnection(dbName, this.config.readonly ?? false)).result); if ((await this.con.isConnection(dbName, this.config.readonly ?? false)).result) { this.db = await this.con.retrieveConnection(dbName, this.config.readonly ?? false); } else { this.db = await this.con.createConnection(dbName, this.config.encrypted ?? false, this.config.mode ?? "", this.config.version ?? 1, this.config.readonly ?? false); } await this.db.open(); await this.db.execute("CREATE TABLE IF NOT EXISTS updates (id INTEGER PRIMARY KEY, content BLOB)"); const s = Y.encodeStateAsUpdate(this.doc); if (s.length > 0) { this._storeUpdate(s, null); } const { values } = await this.db.query("SELECT content FROM updates"); if (values != void 0) { for (const row of values) { Y.applyUpdate(this.doc, new Uint8Array(row.content)); } } this.synced = true; this.emit("synced", [this]); console.log("initialisation finished"); } catch (e) { console.log("error loading update", e); throw e; } } static async create(config, doc, name, sqlite) { const c = new _CapSQLitePersistence(config, doc, name, sqlite); await c.initDb(); return c; } _storeUpdate(update, origin) { if (!this.db || origin == this) { !this.db && console.error("trying to store update without db"); return; } return this.db.run(`INSERT INTO updates (content) VALUES (?)`, [import_buffer.Buffer.from(update)]).catch((e) => console.error("error storing update", e)); } destroy() { this.doc.off("update", this._storeUpdate); this.doc.off("destroy", this.destroy); this._destroyed = true; return this.db?.close().catch((e) => { console.log("can't close", e); }); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CapSQLitePersistence });