'use strict'; var Y = require('yjs'); var pgAdapter = require('./pg-adapter.cjs'); var utils = require('./utils.cjs'); function _interopNamespaceDefault(e) { var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var Y__namespace = /*#__PURE__*/_interopNamespaceDefault(Y); const DEFAULT_FLUSH_SIZE = 200; const DEFAULT_TABLE_NAME = 'yjs-writings'; const DEFAULT_USE_INDEX = false; class PostgresqlPersistence { constructor(flushSize, db) { Object.defineProperty(this, "flushSize", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "tr", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_transact", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.flushSize = flushSize; this.tr = {}; this._transact = (docName, f) => { if (!this.tr[docName]) { this.tr[docName] = Promise.resolve(); } const currTr = this.tr[docName]; let nextTr = null; nextTr = (async () => { await currTr; let res = null; try { res = await f(db); } catch (err) { console.warn('Error during saving transaction', err); } if (this.tr[docName] === nextTr) { delete this.tr[docName]; } return res; })(); this.tr[docName] = nextTr; return this.tr[docName]; }; } static async build(connectionOptions, postgresqlPersistenceOptions = {}) { const { flushSize = DEFAULT_FLUSH_SIZE, useIndex = DEFAULT_USE_INDEX, tableName = DEFAULT_TABLE_NAME, } = postgresqlPersistenceOptions; if (typeof tableName !== 'string' || !tableName) { throw new Error('Constructor option "tableName" is not a valid string. Either dont use this option (default is "yjs-writings") or use a valid string! Take a look into the Readme for more information: https://github.com/MaxNoetzold/y-mongodb-provider#persistence--mongodbpersistenceconnectionlink-string-options-object'); } if (typeof useIndex !== 'boolean') { throw new Error('Constructor option "useIndex" is not a boolean. Either dont use this option (default is "false") or use a valid boolean! Take a look into the Readme for more information: https://github.com/MaxNoetzold/y-mongodb-provider#persistence--mongodbpersistenceconnectionlink-string-options-object'); } if (typeof flushSize !== 'number' || flushSize <= 0) { throw new Error('Constructor option "flushSize" is not a valid number. Either dont use this option (default is "200") or use a valid number larger than 0! Take a look into the Readme for more information: https://github.com/MaxNoetzold/y-mongodb-provider#persistence--mongodbpersistenceconnectionlink-string-options-object'); } const db = await pgAdapter.PgAdapter.connect(connectionOptions, { tableName, useIndex }); return new PostgresqlPersistence(flushSize, db); } getYDoc(docName) { return this._transact(docName, async (db) => { return utils.getYDocFromDb(db, docName, this.flushSize); }); } storeUpdate(docName, update) { return this._transact(docName, (db) => utils.storeUpdate(db, docName, update)); } getStateVector(docName) { return this._transact(docName, async (db) => { const { clock, sv } = await utils.readStateVector(db, docName); let curClock = -1; if (sv !== null) { curClock = await utils.getCurrentUpdateClock(db, docName); } if (sv !== null && clock === curClock) { return sv; } else { const ydoc = await utils.getYDocFromDb(db, docName, this.flushSize); const newSv = Y__namespace.encodeStateVector(ydoc); await utils.flushDocument(db, docName, Y__namespace.encodeStateAsUpdate(ydoc), newSv); return newSv; } }); } async getDiff(docName, stateVector) { const ydoc = await this.getYDoc(docName); return Y__namespace.encodeStateAsUpdate(ydoc, stateVector); } clearDocument(docName) { return this._transact(docName, async (db) => db.deleteDocument(docName)); } destroy() { return this._transact('global', async (db) => { await db.close(); }); } } exports.PostgresqlPersistence = PostgresqlPersistence; //# sourceMappingURL=y-postgresql.cjs.map