@dabble/patches 0.5.9 → 0.5.11

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.
@@ -19,7 +19,7 @@ async function commitChanges(store, docId, changes, sessionTimeoutMillis, option
19
19
  let rev = baseRev + 1;
20
20
  changes.forEach((c) => {
21
21
  if (c.baseRev == null) c.baseRev = baseRev;
22
- else if (c.baseRev !== baseRev) {
22
+ else if (c.baseRev !== baseRev && !options?.historicalImport) {
23
23
  throw new Error(`Client changes must have consistent baseRev in all changes for doc ${docId}.`);
24
24
  }
25
25
  if (c.rev == null) c.rev = rev++;
@@ -3,9 +3,9 @@ import { createVersionMetadata } from "../../data/version.js";
3
3
  import { getISO } from "../../utils/dates.js";
4
4
  async function createVersion(store, docId, state, changes, metadata) {
5
5
  if (changes.length === 0) return;
6
- const startRev = changes[0].baseRev;
6
+ const startRev = changes[0].rev;
7
7
  if (startRev === void 0) {
8
- throw new Error(`Client changes must include baseRev for doc ${docId}.`);
8
+ throw new Error(`Client changes must include rev for doc ${docId}.`);
9
9
  }
10
10
  const sessionMetadata = createVersionMetadata({
11
11
  origin: "main",
@@ -4,8 +4,8 @@ import { getSnapshotAtRevision } from "./getSnapshotAtRevision.js";
4
4
  async function getStateAtRevision(store, docId, rev) {
5
5
  const { state: versionState, rev: snapshotRev, changes } = await getSnapshotAtRevision(store, docId, rev);
6
6
  return {
7
- // Ensure null is passed if versionState or versionState.state is null/undefined
8
- state: applyChanges(versionState?.state ?? null, changes),
7
+ // Ensure null is passed if versionState is null/undefined
8
+ state: applyChanges(versionState ?? null, changes),
9
9
  rev: changes.at(-1)?.rev ?? snapshotRev
10
10
  };
11
11
  }
@@ -1,14 +1,12 @@
1
1
  import "../../chunk-IZ2YBCUP.js";
2
- import { createSortableId } from "crypto-id";
3
2
  import { createVersionMetadata } from "../../data/version.js";
4
3
  import { getISO, timestampDiff } from "../../utils/dates.js";
5
4
  import { applyChanges } from "../shared/applyChanges.js";
6
5
  import { breakChanges } from "../shared/changeBatching.js";
7
6
  import { getStateAtRevision } from "./getStateAtRevision.js";
8
7
  async function handleOfflineSessionsAndBatches(store, sessionTimeoutMillis, docId, changes, baseRev, batchId, origin = "offline-branch", isOffline = true, maxStorageBytes) {
9
- const groupId = batchId ?? createSortableId();
10
8
  const [lastVersion] = await store.listVersions(docId, {
11
- groupId,
9
+ groupId: batchId,
12
10
  reverse: true,
13
11
  limit: 1
14
12
  });
@@ -40,14 +38,14 @@ async function handleOfflineSessionsAndBatches(store, sessionTimeoutMillis, docI
40
38
  offlineBaseState = applyChanges(offlineBaseState, sessionChanges);
41
39
  const sessionMetadata = createVersionMetadata({
42
40
  parentId,
43
- groupId,
41
+ groupId: batchId,
44
42
  origin,
45
43
  isOffline,
46
44
  // Convert client timestamps to UTC for version metadata (enables lexicographic sorting)
47
45
  startedAt: getISO(sessionChanges[0].createdAt),
48
46
  endedAt: getISO(sessionChanges[sessionChanges.length - 1].createdAt),
49
47
  endRev: sessionChanges[sessionChanges.length - 1].rev,
50
- startRev: baseRev
48
+ startRev: sessionChanges[0].rev
51
49
  });
52
50
  await store.createVersion(docId, sessionMetadata, offlineBaseState, sessionChanges);
53
51
  parentId = sessionMetadata.id;
package/dist/types.d.ts CHANGED
@@ -104,7 +104,7 @@ interface VersionMetadata {
104
104
  endedAt: string;
105
105
  /** The ending revision number of this version (the last change's rev). */
106
106
  endRev: number;
107
- /** The starting revision number of this version (the first change's baseRev). If this is an offline/branch version, this is the revision number of the source document where the branch was created. */
107
+ /** The starting revision number of this version (the first change's rev). */
108
108
  startRev: number;
109
109
  /** Optional arbitrary metadata associated with the version. */
110
110
  [metadata: string]: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dabble/patches",
3
- "version": "0.5.9",
3
+ "version": "0.5.11",
4
4
  "description": "Immutable JSON Patch implementation based on RFC 6902 supporting operational transformation and last-writer-wins",
5
5
  "author": "Jacob Wright <jacwright@gmail.com>",
6
6
  "bugs": {
@@ -108,4 +108,4 @@
108
108
  "optional": true
109
109
  }
110
110
  }
111
- }
111
+ }