@korajs/merge 0.1.0 → 0.1.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 +77 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# @korajs/merge
|
|
2
|
+
|
|
3
|
+
Three-tier conflict resolution engine for Kora.js. Handles concurrent modifications across offline devices and produces deterministic, commutative merge results.
|
|
4
|
+
|
|
5
|
+
> Most developers don't install this directly. Use [`korajs`](https://www.npmjs.com/package/korajs) instead.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @korajs/merge
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## How It Works
|
|
14
|
+
|
|
15
|
+
The merge engine resolves conflicts in three tiers:
|
|
16
|
+
|
|
17
|
+
**Tier 1 -- Auto-Merge (default for all fields):**
|
|
18
|
+
- `string`, `number`, `boolean`, `enum`, `timestamp` -- Last-Write-Wins via HLC
|
|
19
|
+
- `array` -- Add-wins set (union of elements)
|
|
20
|
+
- `richtext` -- Yjs CRDT (character-level merge)
|
|
21
|
+
|
|
22
|
+
**Tier 2 -- Constraint Validation:**
|
|
23
|
+
After auto-merge, constraints (unique, capacity, referential) are checked. Violations trigger the configured `onConflict` strategy.
|
|
24
|
+
|
|
25
|
+
**Tier 3 -- Custom Resolvers:**
|
|
26
|
+
For domain-specific logic that neither auto-merge nor constraints can handle.
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { MergeEngine } from '@korajs/merge'
|
|
32
|
+
|
|
33
|
+
const engine = new MergeEngine({ schema })
|
|
34
|
+
|
|
35
|
+
// Merge two concurrent operations
|
|
36
|
+
const result = engine.merge(localOperation, remoteOperation)
|
|
37
|
+
|
|
38
|
+
// result.value -- the resolved value
|
|
39
|
+
// result.trace -- full MergeTrace for debugging/DevTools
|
|
40
|
+
// result.tier -- which tier resolved it (1, 2, or 3)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Custom Resolver
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
const schema = defineSchema({
|
|
47
|
+
collections: {
|
|
48
|
+
inventory: {
|
|
49
|
+
fields: {
|
|
50
|
+
productId: t.string(),
|
|
51
|
+
quantity: t.number(),
|
|
52
|
+
},
|
|
53
|
+
resolve: {
|
|
54
|
+
quantity: (local, remote, base) => {
|
|
55
|
+
// Additive merge: apply both deltas
|
|
56
|
+
const localDelta = local - base
|
|
57
|
+
const remoteDelta = remote - base
|
|
58
|
+
return Math.max(0, base + localDelta + remoteDelta)
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Guarantees
|
|
67
|
+
|
|
68
|
+
- **Deterministic** -- same operations always produce the same result
|
|
69
|
+
- **Commutative** -- `merge(A, B)` equals `merge(B, A)`
|
|
70
|
+
- **Idempotent** -- applying the same operation twice has no additional effect
|
|
71
|
+
- **Traceable** -- every decision produces a `MergeTrace` for inspection
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT
|
|
76
|
+
|
|
77
|
+
See the [full documentation](https://github.com/ehoneahobed/kora) for guides, API reference, and examples.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@korajs/merge",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Kora.js merge package",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"yjs": "^13.6.30",
|
|
26
|
-
"@korajs/core": "0.1.
|
|
26
|
+
"@korajs/core": "0.1.2"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@fast-check/vitest": "0.2.0",
|