@docstack/pouchdb-adapter-googledrive 0.0.6 → 0.0.8
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/.env.example +3 -0
- package/.test-drive-integration/1xssnxhy3akj +1 -0
- package/.test-drive-integration/8p8eymf8afm +1 -0
- package/.test-drive-integration/d1a4udbbuh +1 -0
- package/.test-drive-integration/usx93j46xtl +1 -0
- package/.test-drive-integration/vc0pq3g8vp +1 -0
- package/.test-drive-integration/zenra4v6i +1 -0
- package/.test-drive-root/05wzn5mtlrzt +1 -0
- package/.test-drive-root/0axnnh1gmn2d +1 -0
- package/.test-drive-root/0l2pw73vk4i +1 -0
- package/.test-drive-root/0tzush8sunaj +1 -0
- package/.test-drive-root/143bh2d29rnl +1 -0
- package/.test-drive-root/1synnvu59il +1 -0
- package/.test-drive-root/239cd47eh23i +1 -0
- package/.test-drive-root/275ocoaj6nq +1 -0
- package/.test-drive-root/29d78af16s4 +1 -0
- package/.test-drive-root/2gzvpzclnwu +1 -0
- package/.test-drive-root/2pmk0leotvp +1 -0
- package/.test-drive-root/38ijn3s7p33 +1 -0
- package/.test-drive-root/3jm64la13rp +1 -0
- package/.test-drive-root/3l4zrzi5bm2 +1 -0
- package/.test-drive-root/3of67xqpsfh +1 -0
- package/.test-drive-root/3pi1rk70we5 +1 -0
- package/.test-drive-root/3w3n7tpdn4n +1 -0
- package/.test-drive-root/3xg31irodim +1 -0
- package/.test-drive-root/50asm455hsa +1 -0
- package/.test-drive-root/51ldbder4dm +1 -0
- package/.test-drive-root/5l7dt9r491r +1 -0
- package/.test-drive-root/65mfhb9yuq5 +1 -0
- package/.test-drive-root/6iocm2rs118 +1 -0
- package/.test-drive-root/6o6vlpa6r6j +1 -0
- package/.test-drive-root/6rrcl4sdhoi +1 -0
- package/.test-drive-root/6ucq10yu9bx +1 -0
- package/.test-drive-root/7de4gtsch5b +1 -0
- package/.test-drive-root/7j4j3eq54q +1 -0
- package/.test-drive-root/7n3ai8wgsvy +1 -0
- package/.test-drive-root/7v4b3eyitw3 +1 -0
- package/.test-drive-root/8e15eu4xksq +1 -0
- package/.test-drive-root/8pu2c63yh1j +1 -0
- package/.test-drive-root/8ryxg058q8g +1 -0
- package/.test-drive-root/99l00ber08g +1 -0
- package/.test-drive-root/9gxcumnvepb +1 -0
- package/.test-drive-root/a4xxhwpe2cp +1 -0
- package/.test-drive-root/a57knn0ufon +1 -0
- package/.test-drive-root/a7gszl8y5ir +1 -0
- package/.test-drive-root/b0nddw4fdbu +1 -0
- package/.test-drive-root/b3b1d1yuolh +1 -0
- package/.test-drive-root/b4syq7nig1f +1 -0
- package/.test-drive-root/bcc8mhlffkg +1 -0
- package/.test-drive-root/bmngbpsk34 +1 -0
- package/.test-drive-root/c9i4hvcll2s +1 -0
- package/.test-drive-root/cao688yanw +1 -0
- package/.test-drive-root/cdtduwukrek +1 -0
- package/.test-drive-root/ckzeli8w7ej +1 -0
- package/.test-drive-root/ctywezu2fi5 +1 -0
- package/.test-drive-root/cyxiv5vgtle +1 -0
- package/.test-drive-root/d83g0suoz08 +1 -0
- package/.test-drive-root/d9tefhps5m6 +1 -0
- package/.test-drive-root/dac156l068 +1 -0
- package/.test-drive-root/daqsvyj5cu9 +1 -0
- package/.test-drive-root/df0wbnhhz1 +1 -0
- package/.test-drive-root/difkbp1duv +1 -0
- package/.test-drive-root/djrry1wk98v +1 -0
- package/.test-drive-root/dx42jrz7ty9 +1 -0
- package/.test-drive-root/e0s76kp3ena +1 -0
- package/.test-drive-root/e9xvdv0mqig +1 -0
- package/.test-drive-root/f3xo9hy1956 +1 -0
- package/.test-drive-root/f4scgrpgejw +1 -0
- package/.test-drive-root/f9kbt63p7xp +1 -0
- package/.test-drive-root/fe07yvs6p14 +1 -0
- package/.test-drive-root/ff5suiec8xv +1 -0
- package/.test-drive-root/fwc1qm37as +1 -0
- package/.test-drive-root/gh39v4lzxjp +1 -0
- package/.test-drive-root/gxvkr1qq21b +1 -0
- package/.test-drive-root/h4p64z7djk5 +1 -0
- package/.test-drive-root/habe7525tnl +1 -0
- package/.test-drive-root/hhzddxj31ar +1 -0
- package/.test-drive-root/hyj9crn9sb +1 -0
- package/.test-drive-root/igey5y5f3mi +1 -0
- package/.test-drive-root/isvxwzkgym +1 -0
- package/.test-drive-root/javyqnt07ws +1 -0
- package/.test-drive-root/jevlhmyaczl +1 -0
- package/.test-drive-root/jve7ypin7bb +1 -0
- package/.test-drive-root/koycie6jmyr +1 -0
- package/.test-drive-root/kvlad8ps1l +1 -0
- package/.test-drive-root/l13wzs51qi +1 -0
- package/.test-drive-root/lbwwk07et6i +1 -0
- package/.test-drive-root/m3uoxcsft2g +1 -0
- package/.test-drive-root/m7l8z61tnei +1 -0
- package/.test-drive-root/mmwpyz6aa2e +1 -0
- package/.test-drive-root/mqbyniatwu +1 -0
- package/.test-drive-root/mz3jbwwr12 +1 -0
- package/.test-drive-root/mza04oxze7 +1 -0
- package/.test-drive-root/ndqlafk0c0c +1 -0
- package/.test-drive-root/noojz8gst2 +1 -0
- package/.test-drive-root/o8ujk9otq7 +1 -0
- package/.test-drive-root/oa54zbip13m +1 -0
- package/.test-drive-root/ore7c15j64a +1 -0
- package/.test-drive-root/oxj3tt9q16 +1 -0
- package/.test-drive-root/pac4f4x1r9 +1 -0
- package/.test-drive-root/pqawj7k06i +1 -0
- package/.test-drive-root/qujzt0hpj6i +1 -0
- package/.test-drive-root/r5sdb4w0d3 +1 -0
- package/.test-drive-root/r8pybcnme1r +1 -0
- package/.test-drive-root/rpht4r0nk9 +1 -0
- package/.test-drive-root/rru3wcrfved +1 -0
- package/.test-drive-root/rtq2tifpvgt +1 -0
- package/.test-drive-root/ru00ubalaar +1 -0
- package/.test-drive-root/rx65wqybqlm +1 -0
- package/.test-drive-root/s585nksqc5 +1 -0
- package/.test-drive-root/scvyemdb9gh +1 -0
- package/.test-drive-root/si2b3ac0lve +1 -0
- package/.test-drive-root/sjbanmc5v5k +1 -0
- package/.test-drive-root/sl2coirs8zo +1 -0
- package/.test-drive-root/so3w2nrxm7 +1 -0
- package/.test-drive-root/t3y2vj69hz9 +1 -0
- package/.test-drive-root/t95zgk2fl8f +1 -0
- package/.test-drive-root/t9f08z6ayj +1 -0
- package/.test-drive-root/ta501uh71xi +1 -0
- package/.test-drive-root/taqhm9suyu +1 -0
- package/.test-drive-root/tya2ijynx5k +1 -0
- package/.test-drive-root/u4llyu4o7en +1 -0
- package/.test-drive-root/u5wco26a6lb +1 -0
- package/.test-drive-root/u6sricrto +1 -0
- package/.test-drive-root/ukcmmzglxa +1 -0
- package/.test-drive-root/uvsiktw52p +1 -0
- package/.test-drive-root/uylv1bn9swb +1 -0
- package/.test-drive-root/uzkzgxo3k5b +1 -0
- package/.test-drive-root/vf68w1pfrv +1 -0
- package/.test-drive-root/vp4g881bu7 +1 -0
- package/.test-drive-root/vuv8aavujb +1 -0
- package/.test-drive-root/wg24h3o6df +1 -0
- package/.test-drive-root/wyi5oftc8nh +1 -0
- package/.test-drive-root/x5zl3x2eh +1 -0
- package/.test-drive-root/xb3bpq9xtk +1 -0
- package/.test-drive-root/xfn089pthf9 +1 -0
- package/.test-drive-root/xrs7jul0mdl +1 -0
- package/.test-drive-root/y5qtilrbuzq +1 -0
- package/.test-drive-root/ysxlnyjuav +1 -0
- package/.test-drive-root/z2loakinuyk +1 -0
- package/.test-drive-root/z6lxmw8opjo +1 -0
- package/.test-drive-root/z74cnyignf9 +1 -0
- package/.test-drive-root/zi9agjhrfw +1 -0
- package/.test-drive-root/zspmm1wmo5l +1 -0
- package/README.md +72 -72
- package/jest.config.js +8 -8
- package/package.json +47 -40
- package/DOCUMENTATION.md +0 -54
- package/lib/adapter.d.ts +0 -17
- package/lib/adapter.js +0 -440
- package/lib/cache.d.ts +0 -12
- package/lib/cache.js +0 -42
- package/lib/client.d.ts +0 -32
- package/lib/client.js +0 -166
- package/lib/drive.d.ts +0 -72
- package/lib/drive.js +0 -553
- package/lib/index.d.ts +0 -10
- package/lib/index.js +0 -55
- package/lib/types.d.ts +0 -86
- package/lib/types.js +0 -2
package/README.md
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
# PouchDB Adapter for Google Drive
|
|
2
|
-
|
|
3
|
-
A persistent, serverless PouchDB adapter that uses Google Drive as a backend storage. Designed for high concurrency, large datasets (via lazy loading), and offline resilience.
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- **🚀 Append-Only Log**: Uses an efficient append-only log pattern for fast, conflict-free writes.
|
|
8
|
-
- **⚡ Lazy Loading**: Optimizes memory and bandwidth by loading only the **Index** into memory. Document bodies are fetched on-demand.
|
|
9
|
-
- **🛡️ Optimistic Concurrency Control**: Uses ETag-based locking on metadata to prevent race conditions.
|
|
10
|
-
- **📦 Auto-Compaction**: Automatically merges logs for performance.
|
|
11
|
-
- **🌍 Universal**: Works natively in Node.js 18+, Browsers, and Edge environments (no `googleapis` dependency).
|
|
12
|
-
|
|
13
|
-
## Requirements
|
|
14
|
-
|
|
15
|
-
- **Node.js 18+** (for global `fetch` support) or a modern browser.
|
|
16
|
-
|
|
17
|
-
## Installation
|
|
18
|
-
|
|
19
|
-
```bash
|
|
20
|
-
npm install @docstack/pouchdb-adapter-googledrive
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## Usage
|
|
24
|
-
|
|
25
|
-
The adapter is initialized as a plugin with your Google Drive access token.
|
|
26
|
-
|
|
27
|
-
```typescript
|
|
28
|
-
import PouchDB from 'pouchdb-core';
|
|
29
|
-
import GoogleDriveAdapter from '@docstack/pouchdb-adapter-googledrive';
|
|
30
|
-
|
|
31
|
-
// 1. Initialize the Adapter Plugin Factory
|
|
32
|
-
const adapterPlugin = GoogleDriveAdapter({
|
|
33
|
-
accessToken: 'YOUR_GOOGLE_ACCESS_TOKEN',
|
|
34
|
-
folderName: 'my-app-db-folder', // Root folder in Drive
|
|
35
|
-
pollingIntervalMs: 2000 // Optional: check for remote changes
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
// 2. Register Plugin
|
|
39
|
-
PouchDB.plugin(adapterPlugin);
|
|
40
|
-
|
|
41
|
-
// 3. Create Database
|
|
42
|
-
const db = new PouchDB('user_db', {
|
|
43
|
-
adapter: 'googledrive'
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
await db.post({ title: 'Hello World' });
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### Dynamic Tokens
|
|
50
|
-
|
|
51
|
-
If your token expires, you can provide an async function that returns a valid token:
|
|
52
|
-
|
|
53
|
-
```typescript
|
|
54
|
-
const adapterPlugin = GoogleDriveAdapter({
|
|
55
|
-
accessToken: async () => {
|
|
56
|
-
const session = await getMySession();
|
|
57
|
-
return session.accessToken;
|
|
58
|
-
},
|
|
59
|
-
folderName: 'my-app-db'
|
|
60
|
-
});
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Architecture
|
|
64
|
-
|
|
65
|
-
The adapter implements a **"Remote-First"** architecture:
|
|
66
|
-
- **Lazy Loading**: `db.get(id)` fetches data on-demand from Drive.
|
|
67
|
-
- **Caching**: Changes are indexed locally but bodies are cached in an LRU cache.
|
|
68
|
-
- **Resilience**: Writes use optimistic locking to handle multi-client concurrency safer.
|
|
69
|
-
|
|
70
|
-
## License
|
|
71
|
-
|
|
72
|
-
CC-BY-SA-4.0
|
|
1
|
+
# PouchDB Adapter for Google Drive
|
|
2
|
+
|
|
3
|
+
A persistent, serverless PouchDB adapter that uses Google Drive as a backend storage. Designed for high concurrency, large datasets (via lazy loading), and offline resilience.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **🚀 Append-Only Log**: Uses an efficient append-only log pattern for fast, conflict-free writes.
|
|
8
|
+
- **⚡ Lazy Loading**: Optimizes memory and bandwidth by loading only the **Index** into memory. Document bodies are fetched on-demand.
|
|
9
|
+
- **🛡️ Optimistic Concurrency Control**: Uses ETag-based locking on metadata to prevent race conditions.
|
|
10
|
+
- **📦 Auto-Compaction**: Automatically merges logs for performance.
|
|
11
|
+
- **🌍 Universal**: Works natively in Node.js 18+, Browsers, and Edge environments (no `googleapis` dependency).
|
|
12
|
+
|
|
13
|
+
## Requirements
|
|
14
|
+
|
|
15
|
+
- **Node.js 18+** (for global `fetch` support) or a modern browser.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @docstack/pouchdb-adapter-googledrive
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
The adapter is initialized as a plugin with your Google Drive access token.
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import PouchDB from 'pouchdb-core';
|
|
29
|
+
import GoogleDriveAdapter from '@docstack/pouchdb-adapter-googledrive';
|
|
30
|
+
|
|
31
|
+
// 1. Initialize the Adapter Plugin Factory
|
|
32
|
+
const adapterPlugin = GoogleDriveAdapter({
|
|
33
|
+
accessToken: 'YOUR_GOOGLE_ACCESS_TOKEN',
|
|
34
|
+
folderName: 'my-app-db-folder', // Root folder in Drive
|
|
35
|
+
pollingIntervalMs: 2000 // Optional: check for remote changes
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// 2. Register Plugin
|
|
39
|
+
PouchDB.plugin(adapterPlugin);
|
|
40
|
+
|
|
41
|
+
// 3. Create Database
|
|
42
|
+
const db = new PouchDB('user_db', {
|
|
43
|
+
adapter: 'googledrive'
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
await db.post({ title: 'Hello World' });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Dynamic Tokens
|
|
50
|
+
|
|
51
|
+
If your token expires, you can provide an async function that returns a valid token:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
const adapterPlugin = GoogleDriveAdapter({
|
|
55
|
+
accessToken: async () => {
|
|
56
|
+
const session = await getMySession();
|
|
57
|
+
return session.accessToken;
|
|
58
|
+
},
|
|
59
|
+
folderName: 'my-app-db'
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Architecture
|
|
64
|
+
|
|
65
|
+
The adapter implements a **"Remote-First"** architecture:
|
|
66
|
+
- **Lazy Loading**: `db.get(id)` fetches data on-demand from Drive.
|
|
67
|
+
- **Caching**: Changes are indexed locally but bodies are cached in an LRU cache.
|
|
68
|
+
- **Resilience**: Writes use optimistic locking to handle multi-client concurrency safer.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
CC-BY-SA-4.0
|
package/jest.config.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
preset: 'ts-jest',
|
|
3
|
-
testEnvironment: 'node',
|
|
4
|
-
testMatch: ['**/tests/**/*.test.ts'],
|
|
5
|
-
transform: {
|
|
6
|
-
'^.+\\.ts$': 'ts-jest',
|
|
7
|
-
},
|
|
8
|
-
};
|
|
1
|
+
module.exports = {
|
|
2
|
+
preset: 'ts-jest',
|
|
3
|
+
testEnvironment: 'node',
|
|
4
|
+
testMatch: ['**/tests/**/*.test.ts'],
|
|
5
|
+
transform: {
|
|
6
|
+
'^.+\\.ts$': 'ts-jest',
|
|
7
|
+
},
|
|
8
|
+
};
|
package/package.json
CHANGED
|
@@ -1,41 +1,48 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@docstack/pouchdb-adapter-googledrive",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "PouchDB adapter for Google Drive",
|
|
5
|
-
"main": "lib/index.js",
|
|
6
|
-
"types": "lib/index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"build": "tsc",
|
|
9
|
-
"test": "jest"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"@types/
|
|
35
|
-
"@types/
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@docstack/pouchdb-adapter-googledrive",
|
|
3
|
+
"version": "0.0.8",
|
|
4
|
+
"description": "PouchDB adapter for Google Drive",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "jest",
|
|
10
|
+
"test:prod": "TEST_ENV=production jest"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"pouchdb",
|
|
14
|
+
"adapter",
|
|
15
|
+
"google-drive"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/onyx-ac/docstack-pouchdb-adapter-gdrive.git"
|
|
20
|
+
},
|
|
21
|
+
"author": "Onyx <hello@onyx.ac> (https://onyx.ac)",
|
|
22
|
+
"license": "CC-BY-SA-4.0",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/onyx-ac/docstack-pouchdb-adapter-gdrive/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://onyx.ac/docstack",
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=18"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"pouchdb-core": "^7.3.1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/express": "^5.0.6",
|
|
35
|
+
"@types/jest": "^30.0.0",
|
|
36
|
+
"@types/node": "^20.0.0",
|
|
37
|
+
"@types/pouchdb-adapter-memory": "^6.1.6",
|
|
38
|
+
"@types/pouchdb-core": "^7.0.15",
|
|
39
|
+
"@types/pouchdb-replication": "^6.4.7",
|
|
40
|
+
"dotenv": "^17.2.3",
|
|
41
|
+
"express": "^5.2.1",
|
|
42
|
+
"jest": "^30.2.0",
|
|
43
|
+
"pouchdb-adapter-memory": "^9.0.0",
|
|
44
|
+
"pouchdb-replication": "^9.0.0",
|
|
45
|
+
"ts-jest": "^29.4.6",
|
|
46
|
+
"typescript": "^5.0.0"
|
|
47
|
+
}
|
|
41
48
|
}
|
package/DOCUMENTATION.md
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# Architecture & Design Documentation
|
|
2
|
-
|
|
3
|
-
## 1. Core Principles
|
|
4
|
-
The `pouchdb-adapter-googledrive` implementation is built on three core pillars to ensure data integrity and performance on a file-based remote storage system.
|
|
5
|
-
|
|
6
|
-
### A. Append-Only Log (Storage)
|
|
7
|
-
Instead of modifying a single database file (which is prone to conflicts), we use an **Append-Only** strategy.
|
|
8
|
-
- **Changes**: Every write operation (or batch of writes) creates a **new, immutable file** (e.g., `changes-{seq}-{uuid}.ndjson`).
|
|
9
|
-
- **Snapshots**: Periodically, the log is compacted into a `snapshot` file.
|
|
10
|
-
- **Benefit**: Historical data is preserved until compaction, and file-write conflicts are minimized.
|
|
11
|
-
|
|
12
|
-
### B. Optimistic Concurrency Control (OCC)
|
|
13
|
-
To prevent race conditions (two clients writing simultaneously), we use **ETag-based locking** on a single entry point: `_meta.json`.
|
|
14
|
-
- **The Lock**: `_meta.json` holds the current Sequence Number and the list of active log files.
|
|
15
|
-
- **The Protocol**:
|
|
16
|
-
1. Reader fetches `_meta.json` and its `ETag`.
|
|
17
|
-
2. Writer prepares a new change file and uploads it (orphaned initially).
|
|
18
|
-
3. Writer attempts to update `_meta.json` with the new file reference, sending `If-Match: <Old-ETag>`.
|
|
19
|
-
4. **Success**: The change is now officially part of the DB.
|
|
20
|
-
5. **Failure (412/409)**: Another client updated the DB. The writer deletes its orphaned file, pulls the new state, and retries the logical operation.
|
|
21
|
-
|
|
22
|
-
### C. Remote-First "Lazy" Loading (Memory Optimization)
|
|
23
|
-
To support large databases without exhausting client memory, we separate **Metadata** from **Content**.
|
|
24
|
-
|
|
25
|
-
#### Storage Structure
|
|
26
|
-
- `_meta.json`: Root pointer. Small.
|
|
27
|
-
- `snapshot-index.json`: A map of `{ docId: { rev, filePointer } }`. Medium size (~100 bytes/doc). Loaded at startup.
|
|
28
|
-
- `snapshot-data.json`: The actual document bodies. Large. **Never fully loaded.**
|
|
29
|
-
- `changes-*.ndjson`: Recent updates.
|
|
30
|
-
|
|
31
|
-
#### Client Startup Sequence
|
|
32
|
-
1. **Fetch Meta**: Download `_meta.json` and get the `snapshotIndexId`.
|
|
33
|
-
2. **Fetch Index**: Download `snapshot-index.json`. This builds the "Revision Tree" in memory.
|
|
34
|
-
3. **Replay Logs**: Download and parse only the small `changes-*.ndjson` files created since the snapshot to update the in-memory Index.
|
|
35
|
-
4. **Ready**: The client is now ready to query keys. No document content has been downloaded yet.
|
|
36
|
-
|
|
37
|
-
#### On-Demand Usage
|
|
38
|
-
- **`db.get(id)`**:
|
|
39
|
-
1. Look up `id` in the **Memory Index** to find the `filePointer`.
|
|
40
|
-
2. Check **LRU Cache**.
|
|
41
|
-
3. If missing, fetch the specific file/range from Google Drive.
|
|
42
|
-
- **`db.allDocs({ keys: [...] })`**: Efficiently looks up pointers and fetches only requested docs.
|
|
43
|
-
|
|
44
|
-
## 2. Technical Patterns
|
|
45
|
-
|
|
46
|
-
### Atomic Compaction
|
|
47
|
-
Compaction is a critical maintenance task that merges the `snapshot-data` with recent `changes` to create a new baseline.
|
|
48
|
-
- **Safe**: Limits memory usage by streaming/batching.
|
|
49
|
-
- **Atomic**: Uploads the new snapshot as a new file. Swaps the pointer in `_meta.json` using OCC.
|
|
50
|
-
- **Zero-Downtime**: Clients can continue reading/writing to the old logs while compaction runs. Writes that happen *during* compaction are detected via the ETag check, causing the compaction to abort/retry safeley.
|
|
51
|
-
|
|
52
|
-
### Conflict Handling
|
|
53
|
-
- **PouchDB Level**: Standard CouchDB revision conflicts (409) are preserved. A "winner" is chosen deterministically, but conflicting revisions are kept in the tree (requires `snapshot-index` to store the full revision tree, not just the winner).
|
|
54
|
-
- **Adapter Level**: Drive API 409s handling (retry logic) ensures the transport layer is reliable.
|
package/lib/adapter.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { GoogleDriveAdapterOptions } from './types';
|
|
2
|
-
/** Combined options type for PouchDB adapter */
|
|
3
|
-
interface AdapterOptions extends GoogleDriveAdapterOptions {
|
|
4
|
-
name: string;
|
|
5
|
-
}
|
|
6
|
-
/** Callback type for adapter initialization */
|
|
7
|
-
type AdapterCallback = (err: Error | null, api?: any) => void;
|
|
8
|
-
/**
|
|
9
|
-
* GoogleDriveAdapter - PouchDB adapter for Google Drive storage.
|
|
10
|
-
* Updated for Lazy Loading (Async Access).
|
|
11
|
-
*/
|
|
12
|
-
export declare function GoogleDriveAdapter(PouchDB: any): {
|
|
13
|
-
(this: any, opts: AdapterOptions, callback: AdapterCallback): void;
|
|
14
|
-
valid(): boolean;
|
|
15
|
-
use_prefix: boolean;
|
|
16
|
-
};
|
|
17
|
-
export default GoogleDriveAdapter;
|