@mescius/js-collaboration-ot-postgres 18.2.5 → 19.0.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.d.ts +4 -5
- package/dist/index.js +12 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { Pool } from 'pg';
|
|
2
|
-
import { Db, IOp, ICommitSnapshot, ISnapshot, IDocument
|
|
2
|
+
import { Db, IOp, ICommitSnapshot, ISnapshot, IDocument } from "@mescius/js-collaboration-ot";
|
|
3
3
|
|
|
4
4
|
export declare class PostgresDb<S = unknown, T = unknown> extends Db<S, T> {
|
|
5
5
|
constructor(pool: Pool);
|
|
6
6
|
|
|
7
|
-
getDocument(roomId: string): Promise<IDocument |
|
|
8
|
-
getSnapshot(roomId: string): Promise<ISnapshot<S> |
|
|
9
|
-
|
|
10
|
-
getFragment(roomId: string, fragmentId: string): Promise<S | undefined>;
|
|
7
|
+
getDocument(roomId: string): Promise<IDocument | null>;
|
|
8
|
+
getSnapshot(roomId: string): Promise<ISnapshot<S> | null>;
|
|
9
|
+
getFragment(roomId: string, fragmentId: string): Promise<{ version: number; data: S | null } | null>;
|
|
11
10
|
getOps(roomId: string, fromVersion: number, toVersion?: number): Promise<IOp<T>[]>;
|
|
12
11
|
commitOp(id: string, op: IOp<T>, document: IDocument): Promise<boolean>;
|
|
13
12
|
commitSnapshot(roomId: string, snapshot: ICommitSnapshot<S>): Promise<boolean>;
|
package/dist/index.js
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
/*!
|
|
2
|
+
*
|
|
3
|
+
* SpreadJS Library 19.0.0
|
|
4
|
+
*
|
|
5
|
+
* Copyright(c) MESCIUS inc. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the SpreadJS Commercial License.
|
|
8
|
+
* us.sales@mescius.com
|
|
9
|
+
* https://developer.mescius.com/spreadjs
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
!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 a="object"==typeof exports?e(require("@mescius/js-collaboration-ot")):e(t["@mescius/js-collaboration-ot"]);for(var s in a)("object"==typeof exports?exports:t)[s]=a[s]}}(this,(t=>(()=>{"use strict";var e={252:e=>{e.exports=t}},a={};function s(t){var n=a[t];if(void 0!==n)return n.exports;var o=a[t]={exports:{}};return e[t](o,o.exports,s),o.exports}var n={};return(()=>{var t=n;Object.defineProperty(t,"__esModule",{value:!0}),t.PostgresDb=void 0;const e=s(252);class a 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.query("SELECT id, type, version, snapshot_version FROM documents WHERE id = $1",[t]);if(0===e.rows.length)return null;const a=e.rows[0];return{id:a.id,type:a.type,version:a.version,snapshotVersion:a.snapshot_version}}async getSnapshot(t){const e=await this.pool.query("SELECT d.id, d.type, d.snapshot_version, sf.fragment_id, sf.data FROM documents d LEFT JOIN snapshot_fragments sf ON d.id = sf.doc_id WHERE d.id = $1",[t]);if(0===e.rows.length)return null;const a=e.rows[0],s=this._parseFragmentRows(e);return{id:a.id,v:a.snapshot_version,type:a.type,fragments:s}}_parseFragmentRows(t){const e={};for(const a of t.rows)a.fragment_id&&(e[a.fragment_id]=JSON.parse(a.data));return e}async getFragment(t,e){const a=await this.pool.query("SELECT d.snapshot_version, sf.data FROM documents d LEFT JOIN snapshot_fragments sf ON d.id = sf.doc_id AND sf.doc_id = $1 AND sf.fragment_id = $2 WHERE d.id = $1",[t,e]);if(0===a.rows.length)return null;const s=a.rows[0];return{version:s.snapshot_version,data:"string"==typeof s.data?JSON.parse(s.data):s.data}}async getOps(t,e,a){let s="SELECT operation FROM operations WHERE doc_id = $1 AND version >= $2";const n=[t,e];void 0!==a&&(s+=" AND version < $3",n.push(a)),s+=" ORDER BY version";return(await this.pool.query(s,n)).rows.map((t=>JSON.parse(t.operation)))}async commitOp(t,e,a){const s=JSON.stringify(e),n=await this.pool.connect();try{await n.query("BEGIN");const o=await n.query("SELECT version FROM documents WHERE id = $1 FOR UPDATE",[t]);return e.create?o.rows.length>0?(await n.query("ROLLBACK"),!1):(await n.query("INSERT INTO documents (id, type, version, snapshot_version) VALUES ($1, $2, $3, $4)",[t,a.type,a.version,a.snapshotVersion]),await n.query("INSERT INTO operations (doc_id, version, operation) VALUES ($1, $2, $3)",[t,e.v,s]),await n.query("COMMIT"),!0):e.del?0===o.rows.length?(await n.query("ROLLBACK"),!1):(await n.query("DELETE FROM documents WHERE id = $1",[t]),await n.query("COMMIT"),!0):0===o.rows.length||e.v!==o.rows[0].version?(await n.query("ROLLBACK"),!1):(await n.query("INSERT INTO operations (doc_id, version, operation) VALUES ($1, $2, $3)",[t,e.v,s]),await n.query("UPDATE documents SET version = $1 WHERE id = $2",[a.version,t]),await n.query("COMMIT"),!0)}catch(t){return await n.query("ROLLBACK"),!1}finally{n.release()}}async commitSnapshot(t,e){const{deleteSnapshot:a,setSnapshotFragments:s,createFragments:n,updateFragments:o,deleteFragments:r}=e.fragmentsChanges,i=s?Object.entries(s).map((([t,e])=>[t,JSON.stringify(e)])):null,E=n?Object.entries(n).map((([t,e])=>[t,JSON.stringify(e)])):null,d=o?Object.entries(o).map((([t,e])=>[t,JSON.stringify(e)])):null,c=await this.pool.connect();try{await c.query("BEGIN");const n=await c.query("SELECT snapshot_version FROM documents WHERE id = $1 FOR UPDATE",[t]);if(0===n.rows.length)return await c.query("ROLLBACK"),!1;const o=n.rows[0].snapshot_version;return e.fromVersion!==o||e.v<=o?(await c.query("ROLLBACK"),!1):(await c.query("UPDATE documents SET snapshot_version = $1 WHERE id = $2",[e.v,t]),a?await c.query("DELETE FROM snapshot_fragments WHERE doc_id = $1",[t]):s?(await c.query("DELETE FROM snapshot_fragments WHERE doc_id = $1",[t]),i&&await Promise.all(i.map((async([e,a])=>{await c.query("INSERT INTO snapshot_fragments (doc_id, fragment_id, data) VALUES ($1, $2, $3)",[t,e,a])})))):(E&&await Promise.all(E.map((async([e,a])=>{await c.query("INSERT INTO snapshot_fragments (doc_id, fragment_id, data) VALUES ($1, $2, $3)",[t,e,a])}))),d&&await Promise.all(d.map((async([e,a])=>{await c.query("UPDATE snapshot_fragments SET data = $1 WHERE doc_id = $2 AND fragment_id = $3",[a,t,e])}))),r&&await Promise.all(r.map((async e=>{await c.query("DELETE FROM snapshot_fragments WHERE doc_id = $1 AND fragment_id = $2",[t,e])})))),await c.query("COMMIT"),!0)}catch(t){return await c.query("ROLLBACK"),!1}finally{c.release()}}async close(){await this.pool.end()}}t.PostgresDb=a})(),n})()));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mescius/js-collaboration-ot-postgres",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "19.0.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": "
|
|
29
|
+
"@mescius/js-collaboration-ot": "19.0.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/pg": "^8.11.10"
|