@dabble/patches 0.5.21 → 0.5.22

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.
@@ -10,10 +10,10 @@ import '../json-patch/types.js';
10
10
  * - snapshots<{ docId: string; rev: number; state: any }> (primary key: docId)
11
11
  * - committedChanges<Change & { docId: string; }> (primary key: [docId, rev])
12
12
  * - pendingChanges<Change & { docId: string; }> (primary key: [docId, rev])
13
- * - docs<{ docId: string; committedRev: number; deleted?: boolean }> (primary key: docId)
13
+ * - docs<{ docId: string; committedRev: number; lastAttemptedSubmissionRev?: number; deleted?: boolean }> (primary key: docId)
14
14
  *
15
15
  * Under the hood, this class will store snapshots of the document only for committed state. It will not update the
16
- * committed state on *every* received committed change as this can cause issues with IndexedDB with many large updates.
16
+ * committed state on *every* received committed change as this can cause issues with IndexedDB with many small updates.
17
17
  * After every 200 committed changes, the class will save the current state to the snapshot store and delete the committed changes that went into it.
18
18
  * A snapshot will not be created if there are pending changes based on revisions older than the 200th committed change until those pending changes are committed.
19
19
  */
@@ -3,6 +3,7 @@ import { add } from './json-patch/ops/add.js';
3
3
  import { bit } from './json-patch/ops/bitmask.js';
4
4
  import { copy } from './json-patch/ops/copy.js';
5
5
  import { increment } from './json-patch/ops/increment.js';
6
+ import { max, min } from './json-patch/ops/minmax.js';
6
7
  import { move } from './json-patch/ops/move.js';
7
8
  import { remove } from './json-patch/ops/remove.js';
8
9
  import { replace } from './json-patch/ops/replace.js';
@@ -18,6 +19,8 @@ declare function getTypes(custom?: JSONPatchOpHandlerMap): {
18
19
  '@inc': JSONPatchOpHandler;
19
20
  '@bit': JSONPatchOpHandler;
20
21
  '@txt': JSONPatchOpHandler;
22
+ '@max': JSONPatchOpHandler;
23
+ '@min': JSONPatchOpHandler;
21
24
  };
22
25
 
23
26
  declare const index_add: typeof add;
@@ -25,12 +28,14 @@ declare const index_bit: typeof bit;
25
28
  declare const index_copy: typeof copy;
26
29
  declare const index_getTypes: typeof getTypes;
27
30
  declare const index_increment: typeof increment;
31
+ declare const index_max: typeof max;
32
+ declare const index_min: typeof min;
28
33
  declare const index_move: typeof move;
29
34
  declare const index_remove: typeof remove;
30
35
  declare const index_replace: typeof replace;
31
36
  declare const index_test: typeof test;
32
37
  declare namespace index {
33
- export { index_add as add, index_bit as bit, index_copy as copy, index_getTypes as getTypes, index_increment as increment, index_move as move, index_remove as remove, index_replace as replace, index_test as test };
38
+ export { index_add as add, index_bit as bit, index_copy as copy, index_getTypes as getTypes, index_increment as increment, index_max as max, index_min as min, index_move as move, index_remove as remove, index_replace as replace, index_test as test };
34
39
  }
35
40
 
36
41
  export { getTypes as g, index as i };
package/dist/index.d.ts CHANGED
@@ -12,7 +12,7 @@ export { applyPatch } from './json-patch/applyPatch.js';
12
12
  export { composePatch } from './json-patch/composePatch.js';
13
13
  export { invertPatch } from './json-patch/invertPatch.js';
14
14
  export { applyBitmask, bit, bitmask, combineBitmasks } from './json-patch/ops/bitmask.js';
15
- export { i as defaultOps, g as getTypes } from './index-CvQws3AB.js';
15
+ export { i as defaultOps, g as getTypes } from './index-C7ZhU2kS.js';
16
16
  export { createPathProxy, pathProxy } from './json-patch/pathProxy.js';
17
17
  export { transformPatch } from './json-patch/transformPatch.js';
18
18
  export { JSONPatch, PathLike, WriteOptions } from './json-patch/JSONPatch.js';
@@ -22,6 +22,7 @@ export { clampTimestamp, extractTimezoneOffset, getISO, getLocalISO, getLocalTim
22
22
  export { add } from './json-patch/ops/add.js';
23
23
  export { copy } from './json-patch/ops/copy.js';
24
24
  export { increment } from './json-patch/ops/increment.js';
25
+ export { max, min } from './json-patch/ops/minmax.js';
25
26
  export { move } from './json-patch/ops/move.js';
26
27
  export { remove } from './json-patch/ops/remove.js';
27
28
  export { replace } from './json-patch/ops/replace.js';
@@ -70,6 +70,16 @@ declare class JSONPatch {
70
70
  * Decrements a numeric value by 1 or the given amount.
71
71
  */
72
72
  decrement(path: PathLike, value?: number): this;
73
+ /**
74
+ * Sets a value only if it's greater than the current value.
75
+ * Works with numbers or strings (ISO dates compare correctly).
76
+ */
77
+ max(path: PathLike, value: number | string): this;
78
+ /**
79
+ * Sets a value only if it's less than the current value.
80
+ * Works with numbers or strings (ISO dates compare correctly).
81
+ */
82
+ min(path: PathLike, value: number | string): this;
73
83
  /**
74
84
  * Flips a bit at the given index in a bitmask to the given value.
75
85
  */
@@ -96,6 +96,22 @@ class JSONPatch {
96
96
  decrement(path, value = 1) {
97
97
  return this.op("@inc", path, -value);
98
98
  }
99
+ /**
100
+ * Sets a value only if it's greater than the current value.
101
+ * Works with numbers or strings (ISO dates compare correctly).
102
+ */
103
+ max(path, value) {
104
+ if (value && value.toJSON) value = value.toJSON();
105
+ return this.op("@max", path, value);
106
+ }
107
+ /**
108
+ * Sets a value only if it's less than the current value.
109
+ * Works with numbers or strings (ISO dates compare correctly).
110
+ */
111
+ min(path, value) {
112
+ if (value && value.toJSON) value = value.toJSON();
113
+ return this.op("@min", path, value);
114
+ }
99
115
  /**
100
116
  * Flips a bit at the given index in a bitmask to the given value.
101
117
  */
@@ -2,7 +2,7 @@ export { applyPatch } from './applyPatch.js';
2
2
  export { composePatch } from './composePatch.js';
3
3
  export { invertPatch } from './invertPatch.js';
4
4
  export { applyBitmask, bit, bitmask, combineBitmasks } from './ops/bitmask.js';
5
- export { i as defaultOps, g as getTypes } from '../index-CvQws3AB.js';
5
+ export { i as defaultOps, g as getTypes } from '../index-C7ZhU2kS.js';
6
6
  export { createPathProxy, pathProxy } from './pathProxy.js';
7
7
  export { transformPatch } from './transformPatch.js';
8
8
  export { JSONPatch, PathLike, WriteOptions } from './JSONPatch.js';
@@ -10,6 +10,7 @@ export { ApplyJSONPatchOptions, JSONPatchOpHandlerMap as JSONPatchCustomTypes, J
10
10
  export { add } from './ops/add.js';
11
11
  export { copy } from './ops/copy.js';
12
12
  export { increment } from './ops/increment.js';
13
+ export { max, min } from './ops/minmax.js';
13
14
  export { move } from './ops/move.js';
14
15
  export { remove } from './ops/remove.js';
15
16
  export { replace } from './ops/replace.js';
@@ -3,8 +3,9 @@ export { add } from './add.js';
3
3
  export { bit } from './bitmask.js';
4
4
  export { copy } from './copy.js';
5
5
  export { increment } from './increment.js';
6
+ export { max, min } from './minmax.js';
6
7
  export { move } from './move.js';
7
8
  export { remove } from './remove.js';
8
9
  export { replace } from './replace.js';
9
10
  export { test } from './test.js';
10
- export { g as getTypes } from '../../index-CvQws3AB.js';
11
+ export { g as getTypes } from '../../index-C7ZhU2kS.js';
@@ -3,6 +3,7 @@ import { add } from "./add.js";
3
3
  import { bit } from "./bitmask.js";
4
4
  import { copy } from "./copy.js";
5
5
  import { increment } from "./increment.js";
6
+ import { max, min } from "./minmax.js";
6
7
  import { move } from "./move.js";
7
8
  import { remove } from "./remove.js";
8
9
  import { replace } from "./replace.js";
@@ -19,6 +20,8 @@ function getTypes(custom) {
19
20
  "@inc": increment,
20
21
  "@bit": bit,
21
22
  "@txt": text,
23
+ "@max": max,
24
+ "@min": min,
22
25
  ...custom
23
26
  };
24
27
  }
@@ -28,6 +31,8 @@ export {
28
31
  copy,
29
32
  getTypes,
30
33
  increment,
34
+ max,
35
+ min,
31
36
  move,
32
37
  remove,
33
38
  replace,
@@ -0,0 +1,14 @@
1
+ import { JSONPatchOpHandler } from '../types.js';
2
+
3
+ /**
4
+ * Set a value only if it's greater than the current value. Works with numbers or strings
5
+ * (ISO dates compare correctly). Useful for tracking "latest" values like lastModifiedAt.
6
+ */
7
+ declare const max: JSONPatchOpHandler;
8
+ /**
9
+ * Set a value only if it's less than the current value. Works with numbers or strings
10
+ * (ISO dates compare correctly). Useful for tracking "earliest" values like createdAt.
11
+ */
12
+ declare const min: JSONPatchOpHandler;
13
+
14
+ export { max, min };
@@ -0,0 +1,44 @@
1
+ import "../../chunk-IZ2YBCUP.js";
2
+ import { get } from "../utils/get.js";
3
+ import { updateRemovedOps } from "../utils/ops.js";
4
+ import { replace } from "./replace.js";
5
+ const max = {
6
+ like: "replace",
7
+ apply(state, path, value) {
8
+ const current = get(state, path);
9
+ if (current == null || value > current) {
10
+ return replace.apply(state, path, value);
11
+ }
12
+ },
13
+ transform(state, thisOp, otherOps) {
14
+ return updateRemovedOps(state, thisOp.path, otherOps, false, true);
15
+ },
16
+ invert(state, op, value, changedObj, isIndex) {
17
+ return replace.invert(state, op, value, changedObj, isIndex);
18
+ },
19
+ compose(_state, value1, value2) {
20
+ return value1 > value2 ? value1 : value2;
21
+ }
22
+ };
23
+ const min = {
24
+ like: "replace",
25
+ apply(state, path, value) {
26
+ const current = get(state, path);
27
+ if (current == null || value < current) {
28
+ return replace.apply(state, path, value);
29
+ }
30
+ },
31
+ transform(state, thisOp, otherOps) {
32
+ return updateRemovedOps(state, thisOp.path, otherOps, false, true);
33
+ },
34
+ invert(state, op, value, changedObj, isIndex) {
35
+ return replace.invert(state, op, value, changedObj, isIndex);
36
+ },
37
+ compose(_state, value1, value2) {
38
+ return value1 < value2 ? value1 : value2;
39
+ }
40
+ };
41
+ export {
42
+ max,
43
+ min
44
+ };
@@ -99,7 +99,7 @@ declare class RPCServer {
99
99
  createVersion(params: {
100
100
  docId: string;
101
101
  metadata: EditableVersionMetadata;
102
- }, ctx?: AuthContext): Promise<string>;
102
+ }, ctx?: AuthContext): Promise<string | null>;
103
103
  updateVersion(params: {
104
104
  docId: string;
105
105
  versionId: string;
@@ -27,9 +27,9 @@ declare class PatchesHistoryManager {
27
27
  * Create a new named version snapshot of a document's current state.
28
28
  * @param docId The document ID.
29
29
  * @param name The name of the version.
30
- * @returns The ID of the created version.
30
+ * @returns The ID of the created version, or null if no changes to capture.
31
31
  */
32
- createVersion(docId: string, metadata?: EditableVersionMetadata): Promise<string>;
32
+ createVersion(docId: string, metadata?: EditableVersionMetadata): Promise<string | null>;
33
33
  /**
34
34
  * Updates the name of a specific version.
35
35
  * @param docId - The ID of the document.
@@ -22,7 +22,7 @@ class PatchesHistoryManager {
22
22
  * Create a new named version snapshot of a document's current state.
23
23
  * @param docId The document ID.
24
24
  * @param name The name of the version.
25
- * @returns The ID of the created version.
25
+ * @returns The ID of the created version, or null if no changes to capture.
26
26
  */
27
27
  async createVersion(docId, metadata) {
28
28
  assertVersionMetadata(metadata);
@@ -104,7 +104,7 @@ declare class PatchesServer {
104
104
  * @param metadata Optional metadata for the version.
105
105
  * @returns The ID of the created version.
106
106
  */
107
- captureCurrentVersion(docId: string, metadata?: EditableVersionMetadata): Promise<string>;
107
+ captureCurrentVersion(docId: string, metadata?: EditableVersionMetadata): Promise<string | null>;
108
108
  }
109
109
  declare function assertVersionMetadata(metadata?: EditableVersionMetadata): void;
110
110
 
@@ -146,7 +146,7 @@ class PatchesServer {
146
146
  state = applyChanges(state, changes);
147
147
  const version = await createVersion(this.store, docId, state, changes, metadata);
148
148
  if (!version) {
149
- throw new Error(`No changes to create a version for doc ${docId}.`);
149
+ return null;
150
150
  }
151
151
  return version.id;
152
152
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dabble/patches",
3
- "version": "0.5.21",
3
+ "version": "0.5.22",
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": {