@mongrov/db 0.2.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/README.md +332 -0
- package/dist/database.d.ts +42 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +83 -0
- package/dist/database.js.map +1 -0
- package/dist/hooks.d.ts +136 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +221 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/kv-backends/index.d.ts +6 -0
- package/dist/kv-backends/index.d.ts.map +1 -0
- package/dist/kv-backends/index.js +6 -0
- package/dist/kv-backends/index.js.map +1 -0
- package/dist/kv-backends/mmkv-backend.d.ts +21 -0
- package/dist/kv-backends/mmkv-backend.d.ts.map +1 -0
- package/dist/kv-backends/mmkv-backend.js +59 -0
- package/dist/kv-backends/mmkv-backend.js.map +1 -0
- package/dist/kv-backends/secure-backend.d.ts +27 -0
- package/dist/kv-backends/secure-backend.d.ts.map +1 -0
- package/dist/kv-backends/secure-backend.js +97 -0
- package/dist/kv-backends/secure-backend.js.map +1 -0
- package/dist/kv-store.d.ts +27 -0
- package/dist/kv-store.d.ts.map +1 -0
- package/dist/kv-store.js +34 -0
- package/dist/kv-store.js.map +1 -0
- package/dist/replication.d.ts +69 -0
- package/dist/replication.d.ts.map +1 -0
- package/dist/replication.js +147 -0
- package/dist/replication.js.map +1 -0
- package/dist/token-store.d.ts +38 -0
- package/dist/token-store.d.ts.map +1 -0
- package/dist/token-store.js +47 -0
- package/dist/token-store.js.map +1 -0
- package/dist/types.d.ts +175 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replication.d.ts","sourceRoot":"","sources":["../src/replication.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EACV,iBAAiB,EACjB,sBAAsB,EAEvB,MAAM,SAAS,CAAA;AAYhB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,EACtC,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAC3B,sBAAsB,CA+ExB;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CACrC,gBAAgB,EAAE,sBAAsB,GACvC,OAAO,CAAC,IAAI,CAAC,CAIf;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CACrC,gBAAgB,EAAE,sBAAsB,GACvC,OAAO,CAAC,IAAI,CAAC,CAIf"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RxDB Replication Helper
|
|
3
|
+
*
|
|
4
|
+
* Thin wrapper over rxdb/plugins/replication for creating replication states.
|
|
5
|
+
* App provides push/pull handlers that connect to their backend (e.g., collab adapter).
|
|
6
|
+
*/
|
|
7
|
+
import { replicateRxCollection } from 'rxdb/plugins/replication';
|
|
8
|
+
/**
|
|
9
|
+
* Default no-op logger for when none is provided.
|
|
10
|
+
*/
|
|
11
|
+
const noopLogger = {
|
|
12
|
+
debug: () => { },
|
|
13
|
+
info: () => { },
|
|
14
|
+
warn: () => { },
|
|
15
|
+
error: () => { },
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Creates a replication state for syncing a collection with a remote backend.
|
|
19
|
+
*
|
|
20
|
+
* This is a thin wrapper over RxDB's replication plugin. The app provides
|
|
21
|
+
* push/pull handlers that connect to their backend (e.g., @mongrov/collab adapter).
|
|
22
|
+
*
|
|
23
|
+
* @param config - Replication configuration
|
|
24
|
+
* @returns RxDB replication state instance
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { createReplicationState } from '@mongrov/db'
|
|
29
|
+
*
|
|
30
|
+
* // Create replication with push/pull handlers
|
|
31
|
+
* const replication = createReplicationState({
|
|
32
|
+
* replicationIdentifier: 'messages-sync',
|
|
33
|
+
* collection: db.messages,
|
|
34
|
+
* push: {
|
|
35
|
+
* handler: async (docs) => {
|
|
36
|
+
* // Send changes to backend via collab adapter
|
|
37
|
+
* await collabAdapter.pushMessages(docs)
|
|
38
|
+
* },
|
|
39
|
+
* },
|
|
40
|
+
* pull: {
|
|
41
|
+
* handler: async (checkpoint, batchSize) => {
|
|
42
|
+
* // Fetch changes from backend
|
|
43
|
+
* const result = await collabAdapter.pullMessages(checkpoint, batchSize)
|
|
44
|
+
* return {
|
|
45
|
+
* documents: result.messages,
|
|
46
|
+
* checkpoint: result.checkpoint,
|
|
47
|
+
* }
|
|
48
|
+
* },
|
|
49
|
+
* },
|
|
50
|
+
* live: true, // Enable real-time sync
|
|
51
|
+
* })
|
|
52
|
+
*
|
|
53
|
+
* // Listen for sync events
|
|
54
|
+
* replication.error$.subscribe((error) => {
|
|
55
|
+
* console.error('Sync error:', error)
|
|
56
|
+
* })
|
|
57
|
+
*
|
|
58
|
+
* // Manual sync trigger
|
|
59
|
+
* await replication.reSync()
|
|
60
|
+
*
|
|
61
|
+
* // Stop replication
|
|
62
|
+
* await replication.cancel()
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export function createReplicationState(config) {
|
|
66
|
+
const { replicationIdentifier, collection, push, pull, autoStart = true, retryTime = 0, live = false, logger = noopLogger, } = config;
|
|
67
|
+
logger.info('Creating replication state', {
|
|
68
|
+
replicationIdentifier,
|
|
69
|
+
hasPush: !!push,
|
|
70
|
+
hasPull: !!pull,
|
|
71
|
+
live,
|
|
72
|
+
});
|
|
73
|
+
// Build RxDB replication options
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
75
|
+
const replicationOptions = {
|
|
76
|
+
replicationIdentifier,
|
|
77
|
+
collection,
|
|
78
|
+
autoStart,
|
|
79
|
+
retryTime,
|
|
80
|
+
live,
|
|
81
|
+
};
|
|
82
|
+
// Add push handler if provided
|
|
83
|
+
if (push) {
|
|
84
|
+
replicationOptions.push = {
|
|
85
|
+
batchSize: push.batchSize ?? 100,
|
|
86
|
+
handler: async (docs) => {
|
|
87
|
+
logger.debug('Pushing documents', { count: docs.length });
|
|
88
|
+
try {
|
|
89
|
+
await push.handler(docs);
|
|
90
|
+
logger.debug('Push complete', { count: docs.length });
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
logger.error('Push failed', { error });
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
// Add pull handler if provided
|
|
100
|
+
if (pull) {
|
|
101
|
+
replicationOptions.pull = {
|
|
102
|
+
batchSize: pull.batchSize ?? 100,
|
|
103
|
+
handler: async (lastCheckpoint, batchSize) => {
|
|
104
|
+
logger.debug('Pulling documents', { checkpoint: lastCheckpoint, batchSize });
|
|
105
|
+
try {
|
|
106
|
+
const result = await pull.handler(lastCheckpoint, batchSize);
|
|
107
|
+
logger.debug('Pull complete', { count: result.documents.length });
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
logger.error('Pull failed', { error });
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
const replicationState = replicateRxCollection(replicationOptions);
|
|
118
|
+
// Log replication events
|
|
119
|
+
replicationState.error$.subscribe((error) => {
|
|
120
|
+
logger.error('Replication error', { error: error.message });
|
|
121
|
+
});
|
|
122
|
+
replicationState.active$.subscribe((active) => {
|
|
123
|
+
logger.debug('Replication active state changed', { active });
|
|
124
|
+
});
|
|
125
|
+
return replicationState;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Cancels a replication state and cleans up resources.
|
|
129
|
+
*
|
|
130
|
+
* @param replicationState - The replication state to cancel
|
|
131
|
+
*/
|
|
132
|
+
export async function cancelReplication(replicationState) {
|
|
133
|
+
if (replicationState && typeof replicationState.cancel === 'function') {
|
|
134
|
+
await replicationState.cancel();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Triggers a manual sync cycle for a replication state.
|
|
139
|
+
*
|
|
140
|
+
* @param replicationState - The replication state to sync
|
|
141
|
+
*/
|
|
142
|
+
export async function resyncReplication(replicationState) {
|
|
143
|
+
if (replicationState && typeof replicationState.reSync === 'function') {
|
|
144
|
+
await replicationState.reSync();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=replication.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replication.js","sourceRoot":"","sources":["../src/replication.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAA;AAQhE;;GAEG;AACH,MAAM,UAAU,GAAa;IAC3B,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;IACf,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;IACd,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;IACd,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;CAChB,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,UAAU,sBAAsB,CACpC,MAA4B;IAE5B,MAAM,EACJ,qBAAqB,EACrB,UAAU,EACV,IAAI,EACJ,IAAI,EACJ,SAAS,GAAG,IAAI,EAChB,SAAS,GAAG,CAAC,EACb,IAAI,GAAG,KAAK,EACZ,MAAM,GAAG,UAAU,GACpB,GAAG,MAAM,CAAA;IAEV,MAAM,CAAC,IAAI,CAAC,4BAA4B,EAAE;QACxC,qBAAqB;QACrB,OAAO,EAAE,CAAC,CAAC,IAAI;QACf,OAAO,EAAE,CAAC,CAAC,IAAI;QACf,IAAI;KACL,CAAC,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
+
* Bridges KVStore to auth's TokenStore interface.
|
|
5
|
+
* Allows @mongrov/auth to use KVStore for token persistence.
|
|
6
|
+
*/
|
|
7
|
+
import type { KVStore } from './types';
|
|
8
|
+
/**
|
|
9
|
+
* TokenStore interface (matches @mongrov/auth TokenStore)
|
|
10
|
+
* Re-defined here to avoid circular dependency.
|
|
11
|
+
*/
|
|
12
|
+
export interface TokenStore {
|
|
13
|
+
getAccessToken(): Promise<string | null>;
|
|
14
|
+
setAccessToken(token: string): Promise<void>;
|
|
15
|
+
getRefreshToken(): Promise<string | null>;
|
|
16
|
+
setRefreshToken(token: string): Promise<void>;
|
|
17
|
+
clear(): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create a TokenStore backed by a KVStore.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { createKVStore, createTokenStore } from '@mongrov/db'
|
|
25
|
+
* import { createAuthClient } from '@mongrov/auth'
|
|
26
|
+
*
|
|
27
|
+
* // Use secure KVStore for tokens
|
|
28
|
+
* const secureStore = createKVStore({ secure: true })
|
|
29
|
+
* const tokenStore = createTokenStore(secureStore)
|
|
30
|
+
*
|
|
31
|
+
* const authClient = createAuthClient({
|
|
32
|
+
* adapter: myAdapter,
|
|
33
|
+
* tokenStore, // Inject the KVStore-backed token store
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function createTokenStore(kvStore: KVStore): TokenStore;
|
|
38
|
+
//# sourceMappingURL=token-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-store.d.ts","sourceRoot":"","sources":["../src/token-store.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAKtC;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,cAAc,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;IACxC,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5C,eAAe,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;IACzC,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC7C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACvB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,UAAU,CAuB7D"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token store adapter
|
|
3
|
+
*
|
|
4
|
+
* Bridges KVStore to auth's TokenStore interface.
|
|
5
|
+
* Allows @mongrov/auth to use KVStore for token persistence.
|
|
6
|
+
*/
|
|
7
|
+
const ACCESS_KEY = 'mongrov.auth-access';
|
|
8
|
+
const REFRESH_KEY = 'mongrov.auth-refresh';
|
|
9
|
+
/**
|
|
10
|
+
* Create a TokenStore backed by a KVStore.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { createKVStore, createTokenStore } from '@mongrov/db'
|
|
15
|
+
* import { createAuthClient } from '@mongrov/auth'
|
|
16
|
+
*
|
|
17
|
+
* // Use secure KVStore for tokens
|
|
18
|
+
* const secureStore = createKVStore({ secure: true })
|
|
19
|
+
* const tokenStore = createTokenStore(secureStore)
|
|
20
|
+
*
|
|
21
|
+
* const authClient = createAuthClient({
|
|
22
|
+
* adapter: myAdapter,
|
|
23
|
+
* tokenStore, // Inject the KVStore-backed token store
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function createTokenStore(kvStore) {
|
|
28
|
+
return {
|
|
29
|
+
async getAccessToken() {
|
|
30
|
+
return kvStore.get(ACCESS_KEY);
|
|
31
|
+
},
|
|
32
|
+
async setAccessToken(token) {
|
|
33
|
+
await kvStore.set(ACCESS_KEY, token);
|
|
34
|
+
},
|
|
35
|
+
async getRefreshToken() {
|
|
36
|
+
return kvStore.get(REFRESH_KEY);
|
|
37
|
+
},
|
|
38
|
+
async setRefreshToken(token) {
|
|
39
|
+
await kvStore.set(REFRESH_KEY, token);
|
|
40
|
+
},
|
|
41
|
+
async clear() {
|
|
42
|
+
await kvStore.delete(ACCESS_KEY);
|
|
43
|
+
await kvStore.delete(REFRESH_KEY);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=token-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-store.js","sourceRoot":"","sources":["../src/token-store.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,MAAM,UAAU,GAAG,qBAAqB,CAAA;AACxC,MAAM,WAAW,GAAG,sBAAsB,CAAA;AAc1C;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAgB;IAC/C,OAAO;QACL,KAAK,CAAC,cAAc;YAClB,OAAO,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;QAChC,CAAC;QAED,KAAK,CAAC,cAAc,CAAC,KAAa;YAChC,MAAM,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAA;QACtC,CAAC;QAED,KAAK,CAAC,eAAe;YACnB,OAAO,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;QACjC,CAAC;QAED,KAAK,CAAC,eAAe,CAAC,KAAa;YACjC,MAAM,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,CAAC,CAAA;QACvC,CAAC;QAED,KAAK,CAAC,KAAK;YACT,MAAM,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;YAChC,MAAM,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAA;QACnC,CAAC;KACF,CAAA;AACH,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for @mongrov/db
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Unified async key-value store interface.
|
|
6
|
+
* Abstracts over MMKV (fast) and SecureStore (secure).
|
|
7
|
+
*/
|
|
8
|
+
export interface KVStore {
|
|
9
|
+
/** Get a string value by key */
|
|
10
|
+
get(key: string): Promise<string | null>;
|
|
11
|
+
/** Set a string value */
|
|
12
|
+
set(key: string, value: string): Promise<void>;
|
|
13
|
+
/** Delete a key */
|
|
14
|
+
delete(key: string): Promise<void>;
|
|
15
|
+
/** Get and parse a JSON object */
|
|
16
|
+
getObject<T>(key: string): Promise<T | null>;
|
|
17
|
+
/** Stringify and set a JSON object */
|
|
18
|
+
setObject<T>(key: string, value: T): Promise<void>;
|
|
19
|
+
/** Clear all keys in this store */
|
|
20
|
+
clear(): Promise<void>;
|
|
21
|
+
/** Get all keys (for migration/debugging) */
|
|
22
|
+
getAllKeys(): Promise<string[]>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Configuration for createKVStore factory.
|
|
26
|
+
*/
|
|
27
|
+
export interface KVStoreConfig {
|
|
28
|
+
/**
|
|
29
|
+
* Use secure storage (expo-secure-store) instead of MMKV.
|
|
30
|
+
* Use for tokens, secrets, and sensitive data.
|
|
31
|
+
* @default false
|
|
32
|
+
*/
|
|
33
|
+
secure?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* MMKV instance ID for isolation between stores.
|
|
36
|
+
* Ignored when secure=true.
|
|
37
|
+
* @default 'mongrov-kv'
|
|
38
|
+
*/
|
|
39
|
+
instanceId?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Logger interface for database operations.
|
|
43
|
+
*/
|
|
44
|
+
export interface DBLogger {
|
|
45
|
+
debug(msg: string, data?: Record<string, unknown>): void;
|
|
46
|
+
info(msg: string, data?: Record<string, unknown>): void;
|
|
47
|
+
warn(msg: string, data?: Record<string, unknown>): void;
|
|
48
|
+
error(msg: string, data?: Record<string, unknown>): void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Configuration for createDatabase factory.
|
|
52
|
+
*/
|
|
53
|
+
export interface DatabaseConfig {
|
|
54
|
+
/** Database name (used for storage identification) */
|
|
55
|
+
name: string;
|
|
56
|
+
/**
|
|
57
|
+
* RxDB storage adapter — required, app provides.
|
|
58
|
+
* Examples: getRxStorageSQLite(), getRxStorageMemory()
|
|
59
|
+
*/
|
|
60
|
+
storage: RxStorageType;
|
|
61
|
+
/** Collection configurations */
|
|
62
|
+
collections: CollectionConfig[];
|
|
63
|
+
/** Optional logger for database operations */
|
|
64
|
+
logger?: DBLogger;
|
|
65
|
+
/**
|
|
66
|
+
* Enable multi-instance mode (multiple tabs/processes).
|
|
67
|
+
* Usually false for React Native (single process).
|
|
68
|
+
* @default false
|
|
69
|
+
*/
|
|
70
|
+
multiInstance?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Ignore duplicate database creation errors.
|
|
73
|
+
* Useful during hot reload in development.
|
|
74
|
+
* @default true
|
|
75
|
+
*/
|
|
76
|
+
ignoreDuplicate?: boolean;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Configuration for a single collection.
|
|
80
|
+
*/
|
|
81
|
+
export interface CollectionConfig {
|
|
82
|
+
/** Collection name */
|
|
83
|
+
name: string;
|
|
84
|
+
/** RxDB JSON schema for the collection */
|
|
85
|
+
schema: RxJsonSchemaType;
|
|
86
|
+
/** Migration strategies for schema version upgrades */
|
|
87
|
+
migrationStrategies?: MigrationStrategies;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Migration strategies keyed by target version number.
|
|
91
|
+
*/
|
|
92
|
+
export type MigrationStrategies = Record<number, MigrationStrategy>;
|
|
93
|
+
/**
|
|
94
|
+
* Migration function for a schema version upgrade.
|
|
95
|
+
*/
|
|
96
|
+
export type MigrationStrategy = (oldDoc: Record<string, unknown>) => Record<string, unknown> | Promise<Record<string, unknown>>;
|
|
97
|
+
/** RxDB storage adapter type (from rxdb) */
|
|
98
|
+
export type RxStorageType = any;
|
|
99
|
+
/** RxDB JSON schema type (from rxdb) */
|
|
100
|
+
export type RxJsonSchemaType = any;
|
|
101
|
+
/** RxDB database instance type (from rxdb) */
|
|
102
|
+
export type RxDatabaseType = any;
|
|
103
|
+
/** RxDB collection instance type (from rxdb) */
|
|
104
|
+
export type RxCollectionType = any;
|
|
105
|
+
/** RxDB document instance type (from rxdb) */
|
|
106
|
+
export type RxDocumentType = any;
|
|
107
|
+
/** RxDB query type (from rxdb) */
|
|
108
|
+
export type MangoQueryType = any;
|
|
109
|
+
/** RxDB replication state type (from rxdb) */
|
|
110
|
+
export type RxReplicationStateType = any;
|
|
111
|
+
/**
|
|
112
|
+
* Checkpoint for tracking sync progress.
|
|
113
|
+
* App defines structure based on backend requirements.
|
|
114
|
+
*/
|
|
115
|
+
export type ReplicationCheckpoint = Record<string, unknown>;
|
|
116
|
+
/**
|
|
117
|
+
* Push handler for sending local changes to remote.
|
|
118
|
+
* Called by RxDB when local documents change.
|
|
119
|
+
*/
|
|
120
|
+
export type ReplicationPushHandler<T = RxDocumentType> = (docs: T[]) => Promise<void>;
|
|
121
|
+
/**
|
|
122
|
+
* Pull handler for fetching remote changes.
|
|
123
|
+
* Called by RxDB to sync from remote.
|
|
124
|
+
*/
|
|
125
|
+
export type ReplicationPullHandler<T = RxDocumentType> = (checkpoint: ReplicationCheckpoint | null, batchSize: number) => Promise<{
|
|
126
|
+
documents: T[];
|
|
127
|
+
checkpoint: ReplicationCheckpoint | null;
|
|
128
|
+
}>;
|
|
129
|
+
/**
|
|
130
|
+
* Configuration for createReplicationState.
|
|
131
|
+
*/
|
|
132
|
+
export interface ReplicationConfig<T = RxDocumentType> {
|
|
133
|
+
/** Unique identifier for this replication */
|
|
134
|
+
replicationIdentifier: string;
|
|
135
|
+
/** The RxDB collection to replicate */
|
|
136
|
+
collection: RxCollectionType;
|
|
137
|
+
/**
|
|
138
|
+
* Push handler — sends local changes to remote.
|
|
139
|
+
* If not provided, push is disabled (pull-only sync).
|
|
140
|
+
*/
|
|
141
|
+
push?: {
|
|
142
|
+
handler: ReplicationPushHandler<T>;
|
|
143
|
+
/** Batch size for push operations @default 100 */
|
|
144
|
+
batchSize?: number;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Pull handler — fetches remote changes.
|
|
148
|
+
* If not provided, pull is disabled (push-only sync).
|
|
149
|
+
*/
|
|
150
|
+
pull?: {
|
|
151
|
+
handler: ReplicationPullHandler<T>;
|
|
152
|
+
/** Batch size for pull operations @default 100 */
|
|
153
|
+
batchSize?: number;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Whether to start replication immediately.
|
|
157
|
+
* @default true
|
|
158
|
+
*/
|
|
159
|
+
autoStart?: boolean;
|
|
160
|
+
/**
|
|
161
|
+
* Interval in milliseconds between pull cycles.
|
|
162
|
+
* Set to 0 for manual-only sync (use with live streams).
|
|
163
|
+
* @default 0
|
|
164
|
+
*/
|
|
165
|
+
retryTime?: number;
|
|
166
|
+
/**
|
|
167
|
+
* Enable live replication (real-time sync).
|
|
168
|
+
* When true, replication listens for remote changes via streams.
|
|
169
|
+
* @default false
|
|
170
|
+
*/
|
|
171
|
+
live?: boolean;
|
|
172
|
+
/** Optional logger for replication events */
|
|
173
|
+
logger?: DBLogger;
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;;GAGG;AACH,MAAM,WAAW,OAAO;IACtB,gCAAgC;IAChC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;IAExC,yBAAyB;IACzB,GAAG,CAAC,GAAG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package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mongrov/db",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Database utilities for @mongrov apps: KVStore + RxDB",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/mongrov/mongrov-packages.git",
|
|
9
|
+
"directory": "packages/db"
|
|
10
|
+
},
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=18"
|
|
13
|
+
},
|
|
14
|
+
"main": "dist/index.js",
|
|
15
|
+
"types": "dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.build.json",
|
|
27
|
+
"clean": "rm -rf dist",
|
|
28
|
+
"prebuild": "pnpm run clean",
|
|
29
|
+
"test": "jest --coverage",
|
|
30
|
+
"test:watch": "jest --watch",
|
|
31
|
+
"typecheck": "tsc --noEmit",
|
|
32
|
+
"prepublishOnly": "pnpm run build"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": ">=18",
|
|
36
|
+
"react-native": ">=0.74",
|
|
37
|
+
"react-native-mmkv": ">=3"
|
|
38
|
+
},
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"expo-secure-store": {
|
|
41
|
+
"optional": true
|
|
42
|
+
},
|
|
43
|
+
"rxdb": {
|
|
44
|
+
"optional": true
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@testing-library/react": "^14.0.0",
|
|
49
|
+
"@types/jest": "^29.5.12",
|
|
50
|
+
"@types/react": "^18.2.0",
|
|
51
|
+
"expo-secure-store": "^55.0.11",
|
|
52
|
+
"jest": "^29.7.0",
|
|
53
|
+
"react": "^18.2.0",
|
|
54
|
+
"react-native": "^0.74.0",
|
|
55
|
+
"react-native-mmkv": "^3.0.0",
|
|
56
|
+
"rxdb": "^15.0.0",
|
|
57
|
+
"ts-jest": "^29.1.2",
|
|
58
|
+
"typescript": "^5.9.3"
|
|
59
|
+
}
|
|
60
|
+
}
|