@routier/pouchdb-plugin 0.1.0-rc.0 → 0.3.0
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/README.md +119 -0
- package/dist/PouchDbPlugin.d.ts +68 -8
- package/dist/index.cjs +1072 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +1050 -5
- package/dist/index.js.map +1 -1
- package/package.json +68 -57
- package/dist/PouchDbPlugin.js +0 -715
- package/dist/PouchDbPlugin.js.map +0 -1
- package/dist/PouchDbTranslator.js +0 -78
- package/dist/PouchDbTranslator.js.map +0 -1
- package/readme.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 James DeMeuse
|
|
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,119 @@
|
|
|
1
|
+
# @routier/pouchdb-plugin
|
|
2
|
+
|
|
3
|
+
Routier storage backed by PouchDB, with optional replication to CouchDB.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { DataStore } from "@routier/datastore";
|
|
7
|
+
import { PouchDbPlugin } from "@routier/pouchdb-plugin";
|
|
8
|
+
|
|
9
|
+
const plugin = new PouchDbPlugin("my-database", {
|
|
10
|
+
sync: {
|
|
11
|
+
remoteDb: "http://127.0.0.1:5984/my-database",
|
|
12
|
+
live: true,
|
|
13
|
+
retry: true,
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
class AppStore extends DataStore {
|
|
18
|
+
constructor() {
|
|
19
|
+
super(plugin);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const store = new AppStore();
|
|
24
|
+
|
|
25
|
+
// Replication does not start on its own.
|
|
26
|
+
plugin.sync(store.schemas);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Contracts
|
|
30
|
+
|
|
31
|
+
### Durability
|
|
32
|
+
|
|
33
|
+
PouchDB's adapter decides. IndexedDB in a browser, LevelDB in Node, memory in tests. A
|
|
34
|
+
committed write is durable to whatever the adapter promises.
|
|
35
|
+
|
|
36
|
+
### One database per plugin
|
|
37
|
+
|
|
38
|
+
Each plugin instance owns one database name and holds one connection to it. Every operation
|
|
39
|
+
and the replication share that handle.
|
|
40
|
+
|
|
41
|
+
This matters for replication: two PouchDB objects over one name behave as a single database
|
|
42
|
+
only when the adapter broadcasts changes between them. IndexedDB does. The memory adapter does
|
|
43
|
+
not. One handle removes the question.
|
|
44
|
+
|
|
45
|
+
### Document shape
|
|
46
|
+
|
|
47
|
+
PouchDB stores documents, not tables, so one database holds every collection. Declare
|
|
48
|
+
`_id` and `_rev` on the schema and scope each collection to itself:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const schema = s.define("products", {
|
|
52
|
+
_id: s.string().key().identity(),
|
|
53
|
+
_rev: s.string().identity(),
|
|
54
|
+
name: s.string(),
|
|
55
|
+
}).modify(x => ({
|
|
56
|
+
documentType: x.computed((_, collectionName) => collectionName).tracked()
|
|
57
|
+
})).compile();
|
|
58
|
+
|
|
59
|
+
class AppStore extends DataStore {
|
|
60
|
+
products = this.collection(schema)
|
|
61
|
+
.scope(([x, p]) => x.documentType === p.collectionName, { ...schema })
|
|
62
|
+
.proxy()
|
|
63
|
+
.create();
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Without the scope, every collection reads every other collection's documents.
|
|
68
|
+
|
|
69
|
+
### Replication
|
|
70
|
+
|
|
71
|
+
Call `plugin.sync(store.schemas)` to start it. It returns PouchDB's `Sync` handle.
|
|
72
|
+
|
|
73
|
+
Supply `onChange`, `onError`, `onDenied`, `onPaused`, `onActive`, and `onComplete` in the
|
|
74
|
+
`sync` options to observe it. Each receives the schemas you passed to `sync()`.
|
|
75
|
+
|
|
76
|
+
The plugin starts replication **once** per instance. A second `sync()` call returns the
|
|
77
|
+
existing handle rather than opening a second replication.
|
|
78
|
+
|
|
79
|
+
`destroy()` cancels the replication before it deletes the database. A live sync that is never
|
|
80
|
+
cancelled keeps polling the remote and holds both databases open.
|
|
81
|
+
|
|
82
|
+
Authentication goes in the remote URL (`http://user:password@host:5984/db`) or in the `auth`
|
|
83
|
+
and `headers` sync options.
|
|
84
|
+
|
|
85
|
+
### Concurrency
|
|
86
|
+
|
|
87
|
+
Work is serialized through one queue per plugin instance, so operations on one database run in
|
|
88
|
+
order. Two plugin instances over two databases do not block each other.
|
|
89
|
+
|
|
90
|
+
Conflicts across replicas are PouchDB's to resolve. It keeps both revisions and picks a
|
|
91
|
+
deterministic winner; the plugin does not merge them for you.
|
|
92
|
+
|
|
93
|
+
**Optimistic concurrency through `ConcurrencyDbPlugin` is not supported.** PouchDB's `_rev`
|
|
94
|
+
already rejects a stale write at the document level.
|
|
95
|
+
|
|
96
|
+
### Schema migration
|
|
97
|
+
|
|
98
|
+
None at the storage level. Documents are stored whole. A renamed or removed property does not
|
|
99
|
+
rewrite what is stored, and old documents keep their old shape.
|
|
100
|
+
|
|
101
|
+
### Disposal
|
|
102
|
+
|
|
103
|
+
Call `store.destroyAsync()`. It cancels replication, clears the index cache, deletes the
|
|
104
|
+
database, and closes the handle.
|
|
105
|
+
|
|
106
|
+
### Failure semantics
|
|
107
|
+
|
|
108
|
+
- A failed bulk write reports the first document error and fails the save.
|
|
109
|
+
- A replication error reaches `onError`. With `retry: true`, PouchDB keeps retrying.
|
|
110
|
+
- Rejected credentials surface through `onError` or `onDenied`. Nothing reaches the remote.
|
|
111
|
+
|
|
112
|
+
## Supported versions
|
|
113
|
+
|
|
114
|
+
Node 18 or later, and any browser. PouchDB 9. CouchDB 3 for replication.
|
|
115
|
+
|
|
116
|
+
## See also
|
|
117
|
+
|
|
118
|
+
- [PouchDB plugin guide](../../docs/integrations/plugins/built-in-plugins/pouchdb/README.md)
|
|
119
|
+
- `e2e/src/couchdbReplication.test.ts` — replication against a real CouchDB
|
package/dist/PouchDbPlugin.d.ts
CHANGED
|
@@ -2,17 +2,17 @@ import { PropertyInfo } from '@routier/core/schema';
|
|
|
2
2
|
import { DbPluginBulkPersistEvent, DbPluginEvent, DbPluginQueryEvent, IDbPlugin, IQuery, ITranslatedValue } from '@routier/core/plugins';
|
|
3
3
|
import { PluginEventCallbackPartialResult, PluginEventCallbackResult } from '@routier/core/results';
|
|
4
4
|
import { Expression } from '@routier/core/expressions';
|
|
5
|
-
import { BulkPersistResult,
|
|
5
|
+
import { BulkPersistResult, ReadonlySchemaCollection } from '@routier/core';
|
|
6
6
|
type PouchDBPluginOptions = PouchDB.Configuration.DatabaseConfiguration & {
|
|
7
7
|
queryType?: "default" | "memory-optimized" | "experimental";
|
|
8
8
|
sync?: PouchDB.Replication.SyncOptions & {
|
|
9
9
|
remoteDb: string;
|
|
10
|
-
onChange?: (schemas:
|
|
11
|
-
onError?: (schemas:
|
|
12
|
-
onComplete?: (schemas:
|
|
13
|
-
onPaused?: (schemas:
|
|
14
|
-
onActive?: (schemas:
|
|
15
|
-
onDenied?: (schemas:
|
|
10
|
+
onChange?: (schemas: ReadonlySchemaCollection, event: PouchDB.Replication.SyncResult<{}>) => void;
|
|
11
|
+
onError?: (schemas: ReadonlySchemaCollection, error?: any) => void;
|
|
12
|
+
onComplete?: (schemas: ReadonlySchemaCollection, event: PouchDB.Replication.SyncResultComplete<{}>) => void;
|
|
13
|
+
onPaused?: (schemas: ReadonlySchemaCollection, event?: any) => void;
|
|
14
|
+
onActive?: (schemas: ReadonlySchemaCollection) => void;
|
|
15
|
+
onDenied?: (schemas: ReadonlySchemaCollection, event?: any) => void;
|
|
16
16
|
auth?: {
|
|
17
17
|
username: string;
|
|
18
18
|
password: string;
|
|
@@ -41,8 +41,68 @@ export type PouchDbDesignDoc = {
|
|
|
41
41
|
export declare class PouchDbPlugin implements IDbPlugin {
|
|
42
42
|
private readonly _name;
|
|
43
43
|
private readonly _options?;
|
|
44
|
+
/**
|
|
45
|
+
* Per INSTANCE, all three of these. They used to be module-level, which made every
|
|
46
|
+
* PouchDbPlugin in the process share one database's state regardless of its name:
|
|
47
|
+
*
|
|
48
|
+
* - the **queue** serialized work across unrelated databases, so a slow save against one
|
|
49
|
+
* database blocked a read against another;
|
|
50
|
+
* - the **index cache** was keyed by nothing at all, so the first database's design
|
|
51
|
+
* document was reported as every database's;
|
|
52
|
+
* - the **sync handle** lived under the literal key `"sync"`, so only the FIRST plugin in
|
|
53
|
+
* a process could establish replication — every later one got back the first one's
|
|
54
|
+
* handle, pointed at a different remote.
|
|
55
|
+
*
|
|
56
|
+
* A plugin instance is the natural scope: it owns exactly one database name.
|
|
57
|
+
*/
|
|
58
|
+
private readonly queue;
|
|
59
|
+
private indexCache;
|
|
60
|
+
private syncHandle;
|
|
61
|
+
/**
|
|
62
|
+
* ONE local database handle for this plugin, shared by every operation and by `sync()`.
|
|
63
|
+
*
|
|
64
|
+
* This used to be a fresh `new PouchDB(name)` per operation, with `sync()` constructing
|
|
65
|
+
* yet another one of its own. Two PouchDB objects over the same name only behave as one
|
|
66
|
+
* database when the ADAPTER broadcasts changes between them: IndexedDB does, so this
|
|
67
|
+
* works in a browser, and the in-memory adapter does not, so under it a live replication
|
|
68
|
+
* never observes the plugin's own writes and the plugin never observes what replication
|
|
69
|
+
* pulls in. Replication and data access were effectively wired to two different databases
|
|
70
|
+
* on any adapter without that broadcast.
|
|
71
|
+
*
|
|
72
|
+
* One handle removes the question, and removes the per-operation construction churn with
|
|
73
|
+
* it. It is closed and cleared by `destroy()`.
|
|
74
|
+
*/
|
|
75
|
+
private localDb;
|
|
76
|
+
private database;
|
|
44
77
|
constructor(name: string, options?: PouchDBPluginOptions);
|
|
45
|
-
|
|
78
|
+
/**
|
|
79
|
+
* See `IDbPlugin.databaseName`. The local database name — the same value two contexts
|
|
80
|
+
* opening one PouchDB database supply, so they share subscription channels. Remote sync
|
|
81
|
+
* targets are deliberately not part of it: two local databases replicating to one remote
|
|
82
|
+
* are still two databases.
|
|
83
|
+
*/
|
|
84
|
+
get databaseName(): string;
|
|
85
|
+
sync(schemas: ReadonlySchemaCollection): PouchDB.Replication.Sync<{}>;
|
|
86
|
+
/**
|
|
87
|
+
* Fills in the `_rev` of any document about to be updated or removed that arrives without
|
|
88
|
+
* one, in a single lookup.
|
|
89
|
+
*
|
|
90
|
+
* PouchDB is the only backend in this repository that needs a revision to change a row, and
|
|
91
|
+
* it used to make that the CALLER's problem: a schema had to declare `_rev` and every
|
|
92
|
+
* entity had to carry the current value. That is this plugin's write protocol leaking into
|
|
93
|
+
* every schema written for it, and it made whole features unusable here — a generated
|
|
94
|
+
* search-index row is built from the document being saved, so it has an id and never a
|
|
95
|
+
* revision, and every edit to an indexed document failed with a conflict whose only
|
|
96
|
+
* detail was `true`.
|
|
97
|
+
*
|
|
98
|
+
* A revision is a fact this database owns, so this is the layer that should look it up.
|
|
99
|
+
* One `allDocs` for every id that needs one, only when at least one does, and never on the
|
|
100
|
+
* path where every entity already carries its own. Nothing above has to know.
|
|
101
|
+
*
|
|
102
|
+
* A missing row resolves to no revision and is left alone: an update to a document that is
|
|
103
|
+
* not there must still fail, and it fails the way it always did.
|
|
104
|
+
*/
|
|
105
|
+
private _withRevisions;
|
|
46
106
|
private _identityBulkOperations;
|
|
47
107
|
private _defaultBulkOperations;
|
|
48
108
|
private _prepareProperties;
|