@taladb/sync-mongodb 0.8.4
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 +21 -0
- package/dist/index.d.mts +56 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.js +97 -0
- package/dist/index.mjs +62 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 thinkgrid-labs
|
|
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/dist/index.d.mts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { SyncAdapter, SerializedChangeset } from 'taladb';
|
|
2
|
+
import { Collection } from 'mongodb';
|
|
3
|
+
|
|
4
|
+
interface MongoSyncAdapterOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The MongoDB collection to use as the change store. Create it from your own
|
|
7
|
+
* `MongoClient` so the app owns the connection lifecycle. Index `changed_at`
|
|
8
|
+
* for pull performance: `await collection.createIndex({ changed_at: 1 })`.
|
|
9
|
+
*/
|
|
10
|
+
collection: Collection;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A {@link SyncAdapter} backed by MongoDB. Pair with `db.sync()`:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { MongoClient } from 'mongodb';
|
|
17
|
+
* import { MongoSyncAdapter } from '@taladb/sync-mongodb';
|
|
18
|
+
*
|
|
19
|
+
* const client = new MongoClient(process.env.MONGO_URI!);
|
|
20
|
+
* await client.connect();
|
|
21
|
+
* const store = client.db('sync').collection('taladb_changes');
|
|
22
|
+
* const adapter = new MongoSyncAdapter({ collection: store });
|
|
23
|
+
*
|
|
24
|
+
* await db.sync(adapter, { collections: ['notes'] });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* Or let the adapter open the connection for you with {@link MongoSyncAdapter.connect}.
|
|
28
|
+
*/
|
|
29
|
+
declare class MongoSyncAdapter implements SyncAdapter {
|
|
30
|
+
private readonly store;
|
|
31
|
+
constructor(options: MongoSyncAdapterOptions);
|
|
32
|
+
/**
|
|
33
|
+
* Convenience: open a MongoDB connection from a URI and return a ready
|
|
34
|
+
* adapter plus a `close()` to release it. Creates the `changed_at` index.
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* const { adapter, close } = await MongoSyncAdapter.connect({
|
|
38
|
+
* uri: process.env.MONGO_URI!, db: 'sync',
|
|
39
|
+
* });
|
|
40
|
+
* await db.sync(adapter, { collections: ['notes'] });
|
|
41
|
+
* await close();
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
static connect(opts: {
|
|
45
|
+
uri: string;
|
|
46
|
+
db: string;
|
|
47
|
+
collection?: string;
|
|
48
|
+
}): Promise<{
|
|
49
|
+
adapter: MongoSyncAdapter;
|
|
50
|
+
close: () => Promise<void>;
|
|
51
|
+
}>;
|
|
52
|
+
push(changeset: SerializedChangeset): Promise<void>;
|
|
53
|
+
pull(sinceMs: number): Promise<SerializedChangeset>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { MongoSyncAdapter, type MongoSyncAdapterOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { SyncAdapter, SerializedChangeset } from 'taladb';
|
|
2
|
+
import { Collection } from 'mongodb';
|
|
3
|
+
|
|
4
|
+
interface MongoSyncAdapterOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The MongoDB collection to use as the change store. Create it from your own
|
|
7
|
+
* `MongoClient` so the app owns the connection lifecycle. Index `changed_at`
|
|
8
|
+
* for pull performance: `await collection.createIndex({ changed_at: 1 })`.
|
|
9
|
+
*/
|
|
10
|
+
collection: Collection;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A {@link SyncAdapter} backed by MongoDB. Pair with `db.sync()`:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { MongoClient } from 'mongodb';
|
|
17
|
+
* import { MongoSyncAdapter } from '@taladb/sync-mongodb';
|
|
18
|
+
*
|
|
19
|
+
* const client = new MongoClient(process.env.MONGO_URI!);
|
|
20
|
+
* await client.connect();
|
|
21
|
+
* const store = client.db('sync').collection('taladb_changes');
|
|
22
|
+
* const adapter = new MongoSyncAdapter({ collection: store });
|
|
23
|
+
*
|
|
24
|
+
* await db.sync(adapter, { collections: ['notes'] });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* Or let the adapter open the connection for you with {@link MongoSyncAdapter.connect}.
|
|
28
|
+
*/
|
|
29
|
+
declare class MongoSyncAdapter implements SyncAdapter {
|
|
30
|
+
private readonly store;
|
|
31
|
+
constructor(options: MongoSyncAdapterOptions);
|
|
32
|
+
/**
|
|
33
|
+
* Convenience: open a MongoDB connection from a URI and return a ready
|
|
34
|
+
* adapter plus a `close()` to release it. Creates the `changed_at` index.
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* const { adapter, close } = await MongoSyncAdapter.connect({
|
|
38
|
+
* uri: process.env.MONGO_URI!, db: 'sync',
|
|
39
|
+
* });
|
|
40
|
+
* await db.sync(adapter, { collections: ['notes'] });
|
|
41
|
+
* await close();
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
static connect(opts: {
|
|
45
|
+
uri: string;
|
|
46
|
+
db: string;
|
|
47
|
+
collection?: string;
|
|
48
|
+
}): Promise<{
|
|
49
|
+
adapter: MongoSyncAdapter;
|
|
50
|
+
close: () => Promise<void>;
|
|
51
|
+
}>;
|
|
52
|
+
push(changeset: SerializedChangeset): Promise<void>;
|
|
53
|
+
pull(sinceMs: number): Promise<SerializedChangeset>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { MongoSyncAdapter, type MongoSyncAdapterOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
MongoSyncAdapter: () => MongoSyncAdapter
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var MongoSyncAdapter = class _MongoSyncAdapter {
|
|
37
|
+
constructor(options) {
|
|
38
|
+
this.store = options.collection;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Convenience: open a MongoDB connection from a URI and return a ready
|
|
42
|
+
* adapter plus a `close()` to release it. Creates the `changed_at` index.
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* const { adapter, close } = await MongoSyncAdapter.connect({
|
|
46
|
+
* uri: process.env.MONGO_URI!, db: 'sync',
|
|
47
|
+
* });
|
|
48
|
+
* await db.sync(adapter, { collections: ['notes'] });
|
|
49
|
+
* await close();
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
static async connect(opts) {
|
|
53
|
+
const { MongoClient } = await import("mongodb");
|
|
54
|
+
const client = new MongoClient(opts.uri);
|
|
55
|
+
await client.connect();
|
|
56
|
+
const store = client.db(opts.db).collection(opts.collection ?? "taladb_changes");
|
|
57
|
+
await store.createIndex({ changed_at: 1 });
|
|
58
|
+
return {
|
|
59
|
+
adapter: new _MongoSyncAdapter({ collection: store }),
|
|
60
|
+
close: () => client.close()
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
async push(changeset) {
|
|
64
|
+
const changes = JSON.parse(changeset);
|
|
65
|
+
if (changes.length === 0) return;
|
|
66
|
+
const ops = changes.map((chg) => {
|
|
67
|
+
const key = `${chg.collection}::${chg.id}`;
|
|
68
|
+
return {
|
|
69
|
+
updateOne: {
|
|
70
|
+
filter: { _id: key },
|
|
71
|
+
update: [
|
|
72
|
+
{
|
|
73
|
+
$replaceWith: {
|
|
74
|
+
$cond: [
|
|
75
|
+
{ $gt: [chg.changed_at, { $ifNull: ["$changed_at", -1] }] },
|
|
76
|
+
{ _id: key, changed_at: chg.changed_at, change: JSON.stringify(chg) },
|
|
77
|
+
"$$ROOT"
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
],
|
|
82
|
+
upsert: true
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
await this.store.bulkWrite(ops, { ordered: false });
|
|
87
|
+
}
|
|
88
|
+
async pull(sinceMs) {
|
|
89
|
+
const rows = await this.store.find({ changed_at: { $gt: sinceMs } }).sort({ changed_at: 1 }).toArray();
|
|
90
|
+
if (rows.length === 0) return "[]";
|
|
91
|
+
return `[${rows.map((r) => r.change).join(",")}]`;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
95
|
+
0 && (module.exports = {
|
|
96
|
+
MongoSyncAdapter
|
|
97
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var MongoSyncAdapter = class _MongoSyncAdapter {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
this.store = options.collection;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Convenience: open a MongoDB connection from a URI and return a ready
|
|
8
|
+
* adapter plus a `close()` to release it. Creates the `changed_at` index.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const { adapter, close } = await MongoSyncAdapter.connect({
|
|
12
|
+
* uri: process.env.MONGO_URI!, db: 'sync',
|
|
13
|
+
* });
|
|
14
|
+
* await db.sync(adapter, { collections: ['notes'] });
|
|
15
|
+
* await close();
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
static async connect(opts) {
|
|
19
|
+
const { MongoClient } = await import("mongodb");
|
|
20
|
+
const client = new MongoClient(opts.uri);
|
|
21
|
+
await client.connect();
|
|
22
|
+
const store = client.db(opts.db).collection(opts.collection ?? "taladb_changes");
|
|
23
|
+
await store.createIndex({ changed_at: 1 });
|
|
24
|
+
return {
|
|
25
|
+
adapter: new _MongoSyncAdapter({ collection: store }),
|
|
26
|
+
close: () => client.close()
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async push(changeset) {
|
|
30
|
+
const changes = JSON.parse(changeset);
|
|
31
|
+
if (changes.length === 0) return;
|
|
32
|
+
const ops = changes.map((chg) => {
|
|
33
|
+
const key = `${chg.collection}::${chg.id}`;
|
|
34
|
+
return {
|
|
35
|
+
updateOne: {
|
|
36
|
+
filter: { _id: key },
|
|
37
|
+
update: [
|
|
38
|
+
{
|
|
39
|
+
$replaceWith: {
|
|
40
|
+
$cond: [
|
|
41
|
+
{ $gt: [chg.changed_at, { $ifNull: ["$changed_at", -1] }] },
|
|
42
|
+
{ _id: key, changed_at: chg.changed_at, change: JSON.stringify(chg) },
|
|
43
|
+
"$$ROOT"
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
],
|
|
48
|
+
upsert: true
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
await this.store.bulkWrite(ops, { ordered: false });
|
|
53
|
+
}
|
|
54
|
+
async pull(sinceMs) {
|
|
55
|
+
const rows = await this.store.find({ changed_at: { $gt: sinceMs } }).sort({ changed_at: 1 }).toArray();
|
|
56
|
+
if (rows.length === 0) return "[]";
|
|
57
|
+
return `[${rows.map((r) => r.change).join(",")}]`;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
export {
|
|
61
|
+
MongoSyncAdapter
|
|
62
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@taladb/sync-mongodb",
|
|
3
|
+
"version": "0.8.4",
|
|
4
|
+
"description": "MongoDB bidirectional sync adapter for TalaDB — sync a local TalaDB with a MongoDB collection, no intermediate API",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"taladb",
|
|
21
|
+
"mongodb",
|
|
22
|
+
"sync",
|
|
23
|
+
"replication",
|
|
24
|
+
"local-first",
|
|
25
|
+
"offline"
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/thinkgrid-labs/taladb.git",
|
|
31
|
+
"directory": "packages/adapters/mongodb"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://thinkgrid-labs.github.io/taladb/guide/bidirectional-sync",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/thinkgrid-labs/taladb/issues"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=18"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"mongodb": ">=5.0.0",
|
|
42
|
+
"taladb": "^0.8.4"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^22.0.0",
|
|
46
|
+
"mongodb": "^6.0.0",
|
|
47
|
+
"tsup": "^8.0.0",
|
|
48
|
+
"typescript": "^5.9.3",
|
|
49
|
+
"vitest": "^3.2.0",
|
|
50
|
+
"taladb": "0.8.4"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsup",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"test:watch": "vitest"
|
|
57
|
+
}
|
|
58
|
+
}
|