@mescius/js-collaboration-ot-postgres 18.1.4 → 18.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("@mescius/js-collaboration-ot"));else if("function"==typeof define&&define.amd)define(["@mescius/js-collaboration-ot"],e);else{var n="object"==typeof exports?e(require("@mescius/js-collaboration-ot")):e(t["@mescius/js-collaboration-ot"]);for(var
|
|
1
|
+
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("@mescius/js-collaboration-ot"));else if("function"==typeof define&&define.amd)define(["@mescius/js-collaboration-ot"],e);else{var n="object"==typeof exports?e(require("@mescius/js-collaboration-ot")):e(t["@mescius/js-collaboration-ot"]);for(var a in n)("object"==typeof exports?exports:t)[a]=n[a]}}(this,(t=>(()=>{"use strict";var e={252:e=>{e.exports=t}},n={};function a(t){var o=n[t];if(void 0!==o)return o.exports;var r=n[t]={exports:{}};return e[t](r,r.exports,a),r.exports}var o={};return(()=>{var t=o;Object.defineProperty(t,"__esModule",{value:!0}),t.PostgresDb=void 0;const e=a(252);class n extends e.Db{constructor(t){super(),this.pool=t}async init(t=!1){const e=await this.pool.connect();try{await e.query("BEGIN"),t&&(await e.query("DROP TABLE IF EXISTS snapshot_fragments"),await e.query("DROP TABLE IF EXISTS operations"),await e.query("DROP TABLE IF EXISTS documents")),await e.query("\n CREATE TABLE IF NOT EXISTS documents (\n id TEXT PRIMARY KEY,\n type TEXT NOT NULL,\n version INT NOT NULL,\n snapshot_version INT NOT NULL\n )\n "),await e.query("\n CREATE TABLE IF NOT EXISTS operations (\n doc_id TEXT NOT NULL REFERENCES documents(id) ON DELETE CASCADE,\n version INT NOT NULL,\n operation TEXT NOT NULL,\n PRIMARY KEY (doc_id, version)\n )\n "),await e.query("\n CREATE TABLE IF NOT EXISTS snapshot_fragments (\n doc_id TEXT NOT NULL REFERENCES documents(id) ON DELETE CASCADE,\n fragment_id TEXT NOT NULL,\n data TEXT NOT NULL,\n PRIMARY KEY (doc_id, fragment_id)\n )\n "),await e.query("COMMIT")}catch(t){throw await e.query("ROLLBACK"),t}finally{e.release()}}async getDocument(t){const e=await this.pool.connect();try{const n=await e.query("SELECT id, type, version, snapshot_version FROM documents WHERE id = $1",[t]);if(0===n.rows.length)return;const a=n.rows[0];return{id:a.id,type:a.type,version:a.version,snapshotVersion:a.snapshot_version}}finally{e.release()}}async getSnapshot(t){const e=await this.pool.connect();try{const n=await e.query("SELECT id, type, snapshot_version FROM documents WHERE id = $1",[t]);if(0===n.rows.length)return;const a=n.rows[0],o=await this._queryFragments(e,t);return{id:a.id,v:a.snapshot_version,type:a.type,fragments:o}}finally{e.release()}}async getFragments(t){const e=await this.pool.connect();try{return await this._queryFragments(e,t)}finally{e.release()}}async _queryFragments(t,e){const n=await t.query("SELECT fragment_id, data FROM snapshot_fragments WHERE doc_id = $1",[e]),a={};for(const t of n.rows)a[t.fragment_id]=JSON.parse(t.data);return a}async getFragment(t,e){const n=await this.pool.connect();try{const a=await n.query("SELECT data FROM snapshot_fragments WHERE doc_id = $1 AND fragment_id = $2",[t,e]);if(0===a.rows.length)return;return JSON.parse(a.rows[0].data)}finally{n.release()}}async getOps(t,e,n){const a=await this.pool.connect();try{let o="SELECT operation FROM operations WHERE doc_id = $1 AND version >= $2";const r=[t,e];return void 0!==n&&(o+=" AND version < $3",r.push(n)),o+=" ORDER BY version",(await a.query(o,r)).rows.map((t=>JSON.parse(t.operation)))}finally{a.release()}}async commitOp(t,e,n){const a=await this.pool.connect();try{await a.query("BEGIN");const o=await a.query("SELECT version FROM documents WHERE id = $1 FOR UPDATE",[t]);return e.create?o.rows.length>0?(await a.query("ROLLBACK"),!1):(await a.query("INSERT INTO documents (id, type, version, snapshot_version) VALUES ($1, $2, $3, $4)",[t,n.type,n.version,n.snapshotVersion]),await a.query("INSERT INTO operations (doc_id, version, operation) VALUES ($1, $2, $3)",[t,e.v,JSON.stringify(e)]),await a.query("COMMIT"),!0):e.del?0===o.rows.length?(await a.query("ROLLBACK"),!1):(await a.query("DELETE FROM documents WHERE id = $1",[t]),await a.query("COMMIT"),!0):0===o.rows.length||e.v!==o.rows[0].version?(await a.query("ROLLBACK"),!1):(await a.query("INSERT INTO operations (doc_id, version, operation) VALUES ($1, $2, $3)",[t,e.v,JSON.stringify(e)]),await a.query("UPDATE documents SET version = $1 WHERE id = $2",[n.version,t]),await a.query("COMMIT"),!0)}catch(t){return await a.query("ROLLBACK"),!1}finally{a.release()}}async commitSnapshot(t,e){const n=await this.pool.connect();try{await n.query("BEGIN");const a=await n.query("SELECT snapshot_version FROM documents WHERE id = $1 FOR UPDATE",[t]);if(0===a.rows.length)return await n.query("ROLLBACK"),!1;const o=a.rows[0].snapshot_version;if(e.fromVersion!==o||e.v<=o)return await n.query("ROLLBACK"),!1;if(await n.query("UPDATE documents SET snapshot_version = $1 WHERE id = $2",[e.v,t]),e.fragmentsChanges.deleteSnapshot)await n.query("DELETE FROM snapshot_fragments WHERE doc_id = $1",[t]);else{const{createFragments:a,updateFragments:o,deleteFragments:r}=e.fragmentsChanges;if(a)for(const[e,o]of Object.entries(a))await n.query("INSERT INTO snapshot_fragments (doc_id, fragment_id, data) VALUES ($1, $2, $3)",[t,e,JSON.stringify(o)]);if(o)for(const[e,a]of Object.entries(o))await n.query("UPDATE snapshot_fragments SET data = $1 WHERE doc_id = $2 AND fragment_id = $3",[JSON.stringify(a),t,e]);if(r)for(const e of r)await n.query("DELETE FROM snapshot_fragments WHERE doc_id = $1 AND fragment_id = $2",[t,e])}return await n.query("COMMIT"),!0}catch(t){return await n.query("ROLLBACK"),!1}finally{n.release()}}async close(){await this.pool.end()}}t.PostgresDb=n})(),o})()));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mescius/js-collaboration-ot-postgres",
|
|
3
|
-
"version": "18.
|
|
3
|
+
"version": "18.2.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"license": "Commercial",
|
|
27
27
|
"description": "SpreadJS Collaboration plugin",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@mescius/js-collaboration-ot": "18.
|
|
29
|
+
"@mescius/js-collaboration-ot": "18.2.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/pg": "^8.11.10"
|