@wxn0brp/db-crdt 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 wxn0brP
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # @wxn0brp/db-crdt
2
+
3
+ CRDT integration for @wxn0brp/db-core.
4
+
5
+ This library provides a CRDT (Conflict-free Replicated Data Type) implementation on top of the `@wxn0brp/db-core` library. It allows you to have eventually consistent data in a distributed environment.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ yarn add github:wxn0brP/ValtheraDB-crdt#dist
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import { crdtValthera } from "@wxn0brp/db-crdt";
17
+ import { Valthera } from "@wxn0brp/db";
18
+
19
+ const db = crdtValthera(new Valthera("dir"));
20
+
21
+ // All operations are now CRDT-enabled
22
+ await db.add("my-collection", { name: "John Doe" });
23
+ ```
24
+
25
+ ## API
26
+
27
+ ### `crdtValthera(target: ValtheraCompatible): ValtheraCRDT`
28
+
29
+ This is the main function that wraps a `@wxn0brp/db-core` compatible database instance and returns a `ValtheraCRDT` instance.
30
+
31
+ ### `ValtheraCRDT`
32
+
33
+ The `ValtheraCRDT` interface extends the `ValtheraCompatible` interface and adds the following methods:
34
+
35
+ - `rebuild(collection: string): Promise<void>`: Rebuilds a collection from its operation log.
36
+ - `sync(other: ValtheraCRDT, collection: string, rebuild?: boolean): Promise<void>`: Syncs a collection from another `ValtheraCRDT` instance.
37
+ - `makeSnapshot(collection: string): Promise<void>`: Creates a snapshot of a collection, effectively replacing the operation log with the current state of the data.
38
+
39
+ ### `reverseSync(my: ValtheraCRDT, other: ValtheraCRDT, collection: string)`
40
+
41
+ Sync a collection from `my` to `other`.
42
+
43
+ ## License
44
+
45
+ This project is licensed under the MIT License.
@@ -0,0 +1,10 @@
1
+ import { ValtheraCompatible } from "@wxn0brp/db-core";
2
+ import { ValtheraCRDT } from "./types.js";
3
+ export declare function createCrdtValthera(target: ValtheraCompatible): ValtheraCRDT;
4
+ /**
5
+ * Sync a collection from my to other.
6
+ * @param my The target database that should be synced.
7
+ * @param other The source database that should be synced from.
8
+ * @param collection The collection to sync.
9
+ */
10
+ export declare function reverseSync(my: ValtheraCRDT, other: ValtheraCRDT, collection: string): Promise<void>;
package/dist/index.js ADDED
@@ -0,0 +1,70 @@
1
+ import { rebuild } from "./rebuild.js";
2
+ import { makeSnapshot } from "./snapshot.js";
3
+ import { collectionPrefix } from "./static.js";
4
+ import { sync } from "./sync.js";
5
+ import { VEE } from "@wxn0brp/event-emitter";
6
+ const proxyList = [
7
+ "add",
8
+ "update",
9
+ "updateOne",
10
+ "updateOneOrAdd",
11
+ "remove",
12
+ "removeOne",
13
+ "toggleOne"
14
+ ];
15
+ async function processOperation(target, op, result, args) {
16
+ const collection = collectionPrefix + "/" + args[0];
17
+ const opLow = op.toLowerCase();
18
+ let res = null;
19
+ if (opLow === "add") {
20
+ res = await target._target().add(collection, { a: result }, true);
21
+ }
22
+ else if (!opLow.includes("find") && !opLow.includes("collection")) {
23
+ res = await target._target().add(collection, { d: args.slice(1), op }, true);
24
+ }
25
+ return res?._id || null;
26
+ }
27
+ export function createCrdtValthera(target) {
28
+ const proxy = new Proxy(target, {
29
+ get(target, prop, receiver) {
30
+ const original = Reflect.get(target, prop, receiver);
31
+ if (proxyList.includes(prop) && typeof original === "function") {
32
+ return async function (...args) {
33
+ const result = await original.apply(target, args);
34
+ const opId = await processOperation(proxy, prop, result, args);
35
+ if ("emiter" in target && opId) {
36
+ const emiter = target.emiter;
37
+ if (emiter instanceof VEE) {
38
+ emiter.emit("crdt", opId);
39
+ }
40
+ }
41
+ return result;
42
+ };
43
+ }
44
+ return original;
45
+ },
46
+ set(target, prop, value, receiver) {
47
+ return Reflect.set(target, prop, value, receiver);
48
+ }
49
+ });
50
+ proxy.rebuild = async (collection) => {
51
+ return await rebuild(proxy, collection);
52
+ };
53
+ proxy.sync = async (other, collection, rebuild = false) => {
54
+ return await sync(proxy, other, collection, rebuild);
55
+ };
56
+ proxy.makeSnapshot = async (collection) => {
57
+ return await makeSnapshot(proxy, collection);
58
+ };
59
+ proxy._target = () => target;
60
+ return proxy;
61
+ }
62
+ /**
63
+ * Sync a collection from my to other.
64
+ * @param my The target database that should be synced.
65
+ * @param other The source database that should be synced from.
66
+ * @param collection The collection to sync.
67
+ */
68
+ export async function reverseSync(my, other, collection) {
69
+ return await sync(other, my, collection);
70
+ }
@@ -0,0 +1,2 @@
1
+ import { ValtheraCRDT } from "./types.js";
2
+ export declare function rebuild(db: ValtheraCRDT, collection: string): Promise<void>;
@@ -0,0 +1,15 @@
1
+ import { sortByIds } from "@wxn0brp/db-core";
2
+ import { collectionPrefix } from "./static.js";
3
+ export async function rebuild(db, collection) {
4
+ const operations = await db.find(collectionPrefix + "/" + collection, {}).then(res => sortByIds(res));
5
+ await db.removeCollection(collection);
6
+ const _db = db._target();
7
+ for (const op of operations) {
8
+ if (op.a) {
9
+ await _db.add(collection, op.a, false);
10
+ }
11
+ else if (op.d) {
12
+ await _db[op.op](collection, ...op.d);
13
+ }
14
+ }
15
+ }
@@ -0,0 +1,2 @@
1
+ import { ValtheraCRDT } from "./types.js";
2
+ export declare function makeSnapshot(db: ValtheraCRDT, collection: string): Promise<void>;
@@ -0,0 +1,9 @@
1
+ import { collectionPrefix } from "./static.js";
2
+ export async function makeSnapshot(db, collection) {
3
+ const data = await db.find(collection, {});
4
+ await db.removeCollection(collectionPrefix + "/" + collection);
5
+ await db._target().updateOneOrAdd(collectionPrefix, { _id: collection }, { time: Date.now() });
6
+ for (const d of data) {
7
+ await db._target().add(collectionPrefix + "/" + collection, d, false);
8
+ }
9
+ }
@@ -0,0 +1 @@
1
+ export declare const collectionPrefix = "__vcrdt__";
package/dist/static.js ADDED
@@ -0,0 +1 @@
1
+ export const collectionPrefix = "__vcrdt__";
package/dist/sync.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { ValtheraCRDT } from "./types.js";
2
+ export declare function sync(my: ValtheraCRDT, other: ValtheraCRDT, collection: string, rebuild?: boolean): Promise<void>;
package/dist/sync.js ADDED
@@ -0,0 +1,17 @@
1
+ import { collectionPrefix } from "./static.js";
2
+ async function getIds(db, collection) {
3
+ const data = await db.find(collection, {}, {}, {}, { select: ["_id"] });
4
+ return data.map((d) => d._id);
5
+ }
6
+ export async function sync(my, other, collection, rebuild = false) {
7
+ const crdtCol = collectionPrefix + "/" + collection;
8
+ const myIds = await getIds(my, crdtCol);
9
+ const otherIds = await getIds(other, crdtCol);
10
+ const missing = otherIds.filter(id => !myIds.includes(id));
11
+ const getData = await other.find(crdtCol, { $in: { _id: missing } });
12
+ for (const data of getData) {
13
+ await my._target().add(crdtCol, data, false);
14
+ }
15
+ if (rebuild)
16
+ await my.rebuild(collection);
17
+ }
@@ -0,0 +1,7 @@
1
+ import { ValtheraCompatible } from "@wxn0brp/db-core";
2
+ export interface ValtheraCRDT extends ValtheraCompatible {
3
+ _target: () => ValtheraCompatible;
4
+ rebuild: (collection: string) => Promise<void>;
5
+ sync: (other: ValtheraCRDT, collection: string, rebuild?: boolean) => Promise<void>;
6
+ makeSnapshot: (collection: string) => Promise<void>;
7
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@wxn0brp/db-crdt",
3
+ "version": "0.0.1",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "description": "CRDT integration for ValtheraDB",
7
+ "author": "wxn0brP",
8
+ "license": "MIT",
9
+ "type": "module",
10
+ "homepage": "https://github.com/wxn0brP/ValtheraDB-crdt",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/wxn0brP/ValtheraDB-crdt.git"
14
+ },
15
+ "scripts": {
16
+ "build": "tsc && tsc-alias"
17
+ },
18
+ "devDependencies": {
19
+ "@types/node": "*",
20
+ "@wxn0brp/db": "^0.41.0",
21
+ "tsc-alias": "*",
22
+ "typescript": "*"
23
+ },
24
+ "peerDependencies": {
25
+ "@wxn0brp/db-core": ">=0.3.0"
26
+ },
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "default": "./dist/index.js",
34
+ "import": "./dist/index.js"
35
+ },
36
+ "./*": {
37
+ "types": "./dist/*.d.ts",
38
+ "default": "./dist/*.js",
39
+ "import": "./dist/*.js"
40
+ }
41
+ }
42
+ }