@univerjs-pro/collaboration-database-memory 1.4.0 → 1.5.2

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/README.md CHANGED
@@ -28,8 +28,16 @@ await database.dispose();
28
28
 
29
29
  Pass the Adapter to `UniverCollabService` as `dbAdapter`.
30
30
 
31
+ ## Storage contract
32
+
31
33
  It follows the same collaboration persistence contract as the SQLite and PostgreSQL Adapters, including revision CAS, revisioned snapshots, Sheet blocks, and atomic Unit lifecycle batches. Submission idempotency is resolved by the Service from continuous changeset history, not by the Adapter.
32
34
 
35
+ Unit records and confirmed changesets carry application `metadata`. A successful `commitChangeset()`
36
+ atomically saves the complete `unitMetadata`, `changeset.metadata` and new head. The Service supplies
37
+ both metadata objects on every commit; defaults and drafts belong to the Service. Metadata must be
38
+ JSON-serializable objects.
39
+ `getUnit()` and `getChangesets()` return isolated copies of the saved metadata.
40
+
33
41
  ## Limitations
34
42
 
35
43
  - All data is lost when the process exits.
package/README.zh-CN.md CHANGED
@@ -27,10 +27,17 @@ await database.dispose();
27
27
 
28
28
  将 Adapter 作为 `dbAdapter` 传给 `UniverCollabService`。
29
29
 
30
+ ## 存储契约
31
+
30
32
  它与 SQLite、PostgreSQL Adapter 遵守相同的协同持久化契约,包括 revision CAS、revisioned
31
33
  snapshot、Sheet blocks 和原子 Unit 生命周期批次。提交幂等由 Service 从连续
32
34
  changeset history 中判断,不由 Adapter 维护 submission 索引。
33
35
 
36
+ Unit 记录与 confirmed changeset 包含应用 `metadata`。成功的 `commitChangeset()` 将
37
+ `changeset.metadata`、新 head 和完整的 `unitMetadata` 原子保存。每次提交必须提供两个完整
38
+ metadata 对象,默认值和草稿由 Service 提供。metadata 必须是可 JSON 序列化的对象。
39
+ `getUnit()` 和 `getChangesets()` 返回已保存 metadata 的独立副本。
40
+
34
41
  ## 限制
35
42
 
36
43
  - 进程退出后全部数据丢失。
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@univerjs-pro/collaboration-service");var t=class{_units=new Map;_hardDeletedUnitIDs=new Set;async getUnit(e,t){return structuredClone(this._getActiveUnit(t)?.record??null)}async getSnapshot(e,t,n){return structuredClone(this._findSnapshot(t,n))}async getSnapshotInfo(e,t,n){let r=this._findSnapshot(t,n);return r?{unitID:r.unitID,type:r.type,rev:r.rev}:null}async getChangesets(t,n,r){if(r.from<0||r.to!==void 0&&r.to<0)throw new e.CollabError(`INVALID_REQUEST`,`Changeset range revisions cannot be negative`);let i=this._getActiveUnit(n);return i?structuredClone(i.changesets.filter(({revision:e})=>e>r.from&&(r.to===void 0||e<=r.to))):null}async getSheetBlock(e,t,n){return structuredClone(this._getActiveUnit(t)?.sheetBlocks.get(n)??null)}async createUnit(t,r){n(r);let{record:i,snapshot:a,sheetBlocks:o=[]}=structuredClone(r);if(this._hardDeletedUnitIDs.has(i.unitID))throw new e.CollabError(`INVALID_REQUEST`,`A hard-deleted unit ID cannot be reused`);let s=this._units.get(i.unitID);if(s){if(s.status!==`active`)throw new e.CollabError(`INVALID_REQUEST`,`A deleted unit ID cannot be reused`);return{status:`already-exists`,record:structuredClone(s.record)}}return this._units.set(i.unitID,{record:i,status:`active`,snapshots:new Map([[1,a]]),changesets:[],sheetBlocks:new Map(o.map(e=>[e.id,e]))}),{status:`created`,record:structuredClone(i)}}async commitChangeset(e,t){let n=this._requireActiveUnit(t.changeset.unitID);if(n.record.headRevision!==t.changeset.revision-1)return{status:`revision-mismatch`,actualHeadRevision:n.record.headRevision};let r=structuredClone(t.changeset);return r.createTime=Math.floor(Date.now()/1e3),n.changesets.push(r),n.record={...n.record,headRevision:r.revision},{status:`committed`,changeset:structuredClone(r),headRevision:r.revision}}async saveSnapshot(t,n){let r=this._requireActiveUnit(n.snapshot.unitID),{snapshot:i,sheetBlocks:a=[]}=structuredClone(n);if(i.type!==r.record.type||i.rev<1||i.rev>r.record.headRevision)throw new e.CollabError(`INVALID_REQUEST`,`Snapshot does not match the stored unit head`);for(let e of a)r.sheetBlocks.set(e.id,e);r.snapshots.set(i.rev,i)}async deleteUnits(t,n){let r=[],i=[];for(let t of n.unitIDs){let a=this._units.get(t);if(!a){if(n.hardDelete&&this._hardDeletedUnitIDs.has(t)){i.push({unitID:t,status:`already-hard-deleted`});continue}throw new e.CollabError(`UNIT_NOT_FOUND`,`Cannot delete a missing unit`)}let o;o=n.hardDelete?`hard-deleted`:a.status===`soft-deleted`?`already-soft-deleted`:`soft-deleted`,i.push({unitID:t,status:o}),r.push(a)}for(let e of r)n.hardDelete?(this._units.delete(e.record.unitID),this._hardDeletedUnitIDs.add(e.record.unitID)):e.status=`soft-deleted`;return{units:i}}async recoverUnits(t,n){let r=[];for(let t of n.unitIDs){if(this._hardDeletedUnitIDs.has(t))throw new e.CollabError(`INVALID_REQUEST`,`A hard-deleted unit cannot be recovered`);let n=this._units.get(t);if(!n)throw new e.CollabError(`UNIT_NOT_FOUND`,`Cannot recover a missing unit`);r.push(n)}let i=[];for(let e of r)i.push({unitID:e.record.unitID,status:e.status===`active`?`already-active`:`recovered`}),e.status=`active`;return{units:i}}async dispose(){this._units.clear(),this._hardDeletedUnitIDs.clear()}_findSnapshot(t,n){if(n?.revision!==void 0&&n.revision<0)throw new e.CollabError(`INVALID_REQUEST`,`Snapshot revision cannot be negative`);let r=this._getActiveUnit(t);if(!r)return null;let i=n?.revision??r.record.headRevision,a=-1;for(let e of r.snapshots.keys())e<=i&&e>a&&(a=e);return r.snapshots.get(a)??null}_getActiveUnit(e){let t=this._units.get(e);return t?.status===`active`?t:void 0}_requireActiveUnit(t){let n=this._getActiveUnit(t);if(!n)throw new e.CollabError(`UNIT_NOT_FOUND`,`Unit is not active`);return n}};function n({record:t,snapshot:n}){if(t.headRevision!==1||n.rev!==1||n.unitID!==t.unitID||n.type!==t.type)throw new e.CollabError(`INVALID_REQUEST`,`Initial record and snapshot must match at revision 1`);if(typeof t.creatorID!=`string`||t.creatorID.length===0||!Number.isSafeInteger(t.createdAt)||t.createdAt<0)throw new e.CollabError(`INVALID_REQUEST`,`Unit creation requires creatorID and createdAt in Unix milliseconds`)}exports.MemoryDatabaseAdapter=t;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@univerjs-pro/collaboration-service");var t=class{_units=new Map;_hardDeletedUnitIDs=new Set;async getUnit(e,t){return structuredClone(this._getActiveUnit(t)?.record??null)}async getSnapshot(e,t,n){return structuredClone(this._findSnapshot(t,n))}async getSnapshotInfo(e,t,n){let r=this._findSnapshot(t,n);return r?{unitID:r.unitID,type:r.type,rev:r.rev}:null}async getChangesets(t,n,r){if(r.from<0||r.to!==void 0&&r.to<0)throw new e.CollabError(`INVALID_REQUEST`,`Changeset range revisions cannot be negative`);let i=this._getActiveUnit(n);return i?structuredClone(i.changesets.filter(({revision:e})=>e>r.from&&(r.to===void 0||e<=r.to))):null}async getSheetBlock(e,t,n){return structuredClone(this._getActiveUnit(t)?.sheetBlocks.get(n)??null)}async createUnit(t,r){n(r);let{record:i,snapshot:a,sheetBlocks:o=[]}=structuredClone(r);if(this._hardDeletedUnitIDs.has(i.unitID))throw new e.CollabError(`INVALID_REQUEST`,`A hard-deleted unit ID cannot be reused`);let s=this._units.get(i.unitID);if(s){if(s.status!==`active`)throw new e.CollabError(`INVALID_REQUEST`,`A deleted unit ID cannot be reused`);return{status:`already-exists`,record:structuredClone(s.record)}}return this._units.set(i.unitID,{record:i,status:`active`,snapshots:new Map([[1,a]]),changesets:[],sheetBlocks:new Map(o.map(e=>[e.id,e]))}),{status:`created`,record:structuredClone(i)}}async commitChangeset(e,t){let n=this._requireActiveUnit(t.changeset.unitID);if(n.record.headRevision!==t.changeset.revision-1)return{status:`revision-mismatch`,actualHeadRevision:n.record.headRevision};let r=structuredClone(t.changeset),i=structuredClone(t.unitMetadata);return r.createTime=Math.floor(Date.now()/1e3),n.changesets.push(r),n.record={...n.record,headRevision:r.revision,metadata:i},{status:`committed`,changeset:structuredClone(r),headRevision:r.revision}}async saveSnapshot(t,n){let r=this._requireActiveUnit(n.snapshot.unitID),{snapshot:i,sheetBlocks:a=[]}=structuredClone(n);if(i.type!==r.record.type||i.rev<1||i.rev>r.record.headRevision)throw new e.CollabError(`INVALID_REQUEST`,`Snapshot does not match the stored unit head`);for(let e of a)r.sheetBlocks.has(e.id)||r.sheetBlocks.set(e.id,e);r.snapshots.set(i.rev,i)}async deleteUnits(t,n){let r=[],i=[];for(let t of n.unitIDs){let a=this._units.get(t);if(!a){if(n.hardDelete&&this._hardDeletedUnitIDs.has(t)){i.push({unitID:t,status:`already-hard-deleted`});continue}throw new e.CollabError(`UNIT_NOT_FOUND`,`Cannot delete a missing unit`)}let o;o=n.hardDelete?`hard-deleted`:a.status===`soft-deleted`?`already-soft-deleted`:`soft-deleted`,i.push({unitID:t,status:o}),r.push(a)}for(let e of r)n.hardDelete?(this._units.delete(e.record.unitID),this._hardDeletedUnitIDs.add(e.record.unitID)):e.status=`soft-deleted`;return{units:i}}async recoverUnits(t,n){let r=[];for(let t of n.unitIDs){if(this._hardDeletedUnitIDs.has(t))throw new e.CollabError(`INVALID_REQUEST`,`A hard-deleted unit cannot be recovered`);let n=this._units.get(t);if(!n)throw new e.CollabError(`UNIT_NOT_FOUND`,`Cannot recover a missing unit`);r.push(n)}let i=[];for(let e of r)i.push({unitID:e.record.unitID,status:e.status===`active`?`already-active`:`recovered`}),e.status=`active`;return{units:i}}async dispose(){this._units.clear(),this._hardDeletedUnitIDs.clear()}_findSnapshot(t,n){if(n?.revision!==void 0&&n.revision<0)throw new e.CollabError(`INVALID_REQUEST`,`Snapshot revision cannot be negative`);let r=this._getActiveUnit(t);if(!r)return null;let i=n?.revision??r.record.headRevision,a=-1;for(let e of r.snapshots.keys())e<=i&&e>a&&(a=e);return r.snapshots.get(a)??null}_getActiveUnit(e){let t=this._units.get(e);return t?.status===`active`?t:void 0}_requireActiveUnit(t){let n=this._getActiveUnit(t);if(!n)throw new e.CollabError(`UNIT_NOT_FOUND`,`Unit is not active`);return n}};function n({record:t,snapshot:n}){if(t.headRevision!==1||n.rev!==1||n.unitID!==t.unitID||n.type!==t.type)throw new e.CollabError(`INVALID_REQUEST`,`Initial record and snapshot must match at revision 1`);if(typeof t.creatorID!=`string`||t.creatorID.length===0||!Number.isSafeInteger(t.createdAt)||t.createdAt<0)throw new e.CollabError(`INVALID_REQUEST`,`Unit creation requires creatorID and createdAt in Unix milliseconds`)}exports.MemoryDatabaseAdapter=t;
package/dist/index.d.ts CHANGED
@@ -1,13 +1,14 @@
1
- import { CommitChangesetInput, CommitChangesetResult, CreateUnitDatabaseInput, CreateUnitDatabaseResult, DatabaseContext, DeleteUnitsDatabaseInput, DeleteUnitsDatabaseResult, IDatabaseAdapter, RecoverUnitsDatabaseInput, RecoverUnitsDatabaseResult, SaveSnapshotInput, SnapshotInfo, SubmitDatabaseContext, UnitRecord } from "@univerjs-pro/collaboration-service";
2
- import { IChangeset, ISheetBlock, ISnapshot } from "@univerjs/protocol";
1
+ import { ChangesetRecord, CommitChangesetInput, CommitChangesetResult, CreateUnitDatabaseInput, CreateUnitDatabaseResult, DatabaseContext, DeleteUnitsDatabaseInput, DeleteUnitsDatabaseResult, IDatabaseAdapter, RecoverUnitsDatabaseInput, RecoverUnitsDatabaseResult, SaveSnapshotInput, SnapshotInfo, SubmitDatabaseContext, UnitRecord } from "@univerjs-pro/collaboration-service";
2
+ import { ISheetBlock, ISnapshot } from "@univerjs/protocol";
3
3
 
4
- //#region src/memory-database-adapter.d.ts
4
+ // src/memory-database-adapter.d.ts
5
5
  /**
6
6
  * An in-memory `IDatabaseAdapter`. Data is scoped to this instance and lost on process exit.
7
7
  *
8
8
  * Reads and writes copy protocol objects so stored data stays isolated from caller mutations.
9
9
  * Every method runs synchronously inside its `async` body, which gives each call the atomicity
10
10
  * a database implementation must provide with a transaction.
11
+ * @public
11
12
  */
12
13
  declare class MemoryDatabaseAdapter implements IDatabaseAdapter {
13
14
  private readonly _units;
@@ -22,7 +23,7 @@ declare class MemoryDatabaseAdapter implements IDatabaseAdapter {
22
23
  getChangesets(_ctx: DatabaseContext, unitID: string, range: {
23
24
  readonly from: number;
24
25
  readonly to?: number;
25
- }): Promise<readonly IChangeset[] | null>;
26
+ }): Promise<readonly ChangesetRecord[] | null>;
26
27
  getSheetBlock(_ctx: DatabaseContext, unitID: string, blockID: string): Promise<ISheetBlock | null>;
27
28
  /** Atomically creates a Unit at revision 1 with its snapshot and blocks; an existing active Unit returns its record. */
28
29
  createUnit(_ctx: DatabaseContext, input: CreateUnitDatabaseInput): Promise<CreateUnitDatabaseResult>;
@@ -39,5 +40,4 @@ declare class MemoryDatabaseAdapter implements IDatabaseAdapter {
39
40
  private _getActiveUnit;
40
41
  private _requireActiveUnit;
41
42
  }
42
- //#endregion
43
43
  export { MemoryDatabaseAdapter };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "engines": {
12
12
  "node": ">=22"
13
13
  },
14
- "version": "1.4.0",
14
+ "version": "1.5.2",
15
15
  "main": "./dist/index.cjs",
16
16
  "module": "./dist/index.mjs",
17
17
  "types": "./dist/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  "dist"
27
27
  ],
28
28
  "peerDependencies": {
29
- "@univerjs-pro/collaboration-service": "1.4.0",
30
- "@univerjs/protocol": "1.4.0"
29
+ "@univerjs-pro/collaboration-service": "1.5.2",
30
+ "@univerjs/protocol": "1.5.2"
31
31
  }
32
32
  }