tinybase 6.4.1 → 6.5.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.
@@ -35,6 +35,7 @@ export * from '../persisters/persister-partykit-server/index.d.ts';
35
35
  export * from '../persisters/persister-pglite/index.d.ts';
36
36
  export * from '../persisters/persister-postgres/index.d.ts';
37
37
  export * from '../persisters/persister-powersync/index.d.ts';
38
+ export * from '../persisters/persister-react-native-mmkv/index.d.ts';
38
39
  export * from '../persisters/persister-react-native-sqlite/index.d.ts';
39
40
  export * from '../persisters/persister-remote/index.d.ts';
40
41
  export * from '../persisters/persister-sqlite-bun/index.d.ts';
@@ -34,6 +34,7 @@ export * from '../../persisters/persister-partykit-server/with-schemas/index.d.t
34
34
  export * from '../../persisters/persister-pglite/with-schemas/index.d.ts';
35
35
  export * from '../../persisters/persister-postgres/with-schemas/index.d.ts';
36
36
  export * from '../../persisters/persister-powersync/with-schemas/index.d.ts';
37
+ export * from '../../persisters/persister-react-native-mmkv/with-schemas/index.d.ts';
37
38
  export * from '../../persisters/persister-react-native-sqlite/with-schemas/index.d.ts';
38
39
  export * from '../../persisters/persister-remote/with-schemas/index.d.ts';
39
40
  export * from '../../persisters/persister-sqlite-bun/with-schemas/index.d.ts';
@@ -16,6 +16,7 @@
16
16
  * |FilePersister|Local file (where possible)|Yes|Yes
17
17
  * |IndexedDbPersister|Browser IndexedDB|Yes|No
18
18
  * |RemotePersister|Remote server|Yes|No
19
+ * |ReactNativeMmkvPersister|MMKV in React Native, via [react-native-mmkv](https://github.com/mrousavy/react-native-mmkv)|Yes|Yes
19
20
  * |DurableObjectStoragePersister|Cloudflare Durable Object (KV)|No|Yes
20
21
  * |DurableObjectSqlStoragePersister|Cloudflare Durable Object (SQLite)|No|Yes
21
22
  * |Sqlite3Persister|SQLite in Node, via [sqlite3](https://github.com/TryGhost/node-sqlite3)|Yes|Yes*
@@ -0,0 +1,118 @@
1
+ /**
2
+ * The persister-react-native-mmkv module of the TinyBase project lets you save
3
+ * and load Store data to and from a MMKV storage using the
4
+ * [`react-native-mmkv`](https://github.com/mrousavy/react-native-mmkv) module
5
+ * (in an appropriate React Native environment).
6
+ * @see Database Persistence guide
7
+ * @packageDocumentation
8
+ * @module persister-react-native-mmkv
9
+ * @since v6.5.0
10
+ */
11
+ import type {MMKV} from 'react-native-mmkv';
12
+ import type {MergeableStore} from '../../mergeable-store/index.d.ts';
13
+ import type {Store} from '../../store/index.d.ts';
14
+ import type {Persister, Persists} from '../index.d.ts';
15
+
16
+ /**
17
+ * The ReactNativeMmkvPersister interface represents a Persister that lets you
18
+ * save and load Store data to and from a `react-native-mmkv` storage.
19
+ *
20
+ * You should use the createReactNativeMmkvPersister function to create a
21
+ * ReactNativeMmkvPersister object.
22
+ *
23
+ * It is a minor extension to the Persister interface and simply provides an
24
+ * extra getStorageName method for accessing the unique key of the storage
25
+ * location the Store is being persisted to.
26
+ * @category Persister
27
+ * @since v6.5.0
28
+ */
29
+ export interface ReactNativeMmkvPersister
30
+ extends Persister<Persists.StoreOrMergeableStore> {
31
+ /**
32
+ * The getStorageName method returns the unique key of the storage location the
33
+ * Store is being persisted to.
34
+ * @returns The unique key of the storage location.
35
+ * @example
36
+ * This example creates a Persister object against a newly-created Store and
37
+ * then gets the unique key of the storage location back out again.
38
+ *
39
+ * ```js yolo
40
+ * import {MMKV} from 'react-native-mmkv';
41
+ * import {createStore} from 'tinybase';
42
+ * import {createReactNativeMmkvPersister} from 'tinybase/persisters/persister-react-native-mmkv';
43
+ *
44
+ * const storage = new MMKV();
45
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
46
+ * const persister = createReactNativeMmkvPersister(
47
+ * store,
48
+ * storage,
49
+ * 'my_tinybase',
50
+ * );
51
+ * console.log(persister.getStorageName() == 'my_tinybase');
52
+ * // -> true
53
+ *
54
+ * await persister.destroy();
55
+ * ```
56
+ * @category Getter
57
+ * @since v6.5.0
58
+ */
59
+ getStorageName(): string;
60
+ }
61
+
62
+ /**
63
+ * The createReactNativeMmkvPersister function creates a
64
+ * ReactNativeMmkvPersister object that can persist the Store to a local
65
+ * `react-native-mmkv` storage.
66
+ *
67
+ * A ReactNativeMmkvPersister supports both regular Store and MergeableStore
68
+ * objects.
69
+ *
70
+ * As well as providing a reference to the Store to persist, you must provide a
71
+ * `storage` parameter which identifies the MMKV storage instance.
72
+ *
73
+ * The third argument is a `storageName` string that configures which key to use
74
+ * for the storage key.
75
+ * @param store The Store or MergeableStore to persist.
76
+ * @param storage The MMKV storage instance.
77
+ * @param storageName The unique key to identify the storage location.
78
+ * @param onIgnoredError An optional handler for the errors that the Persister
79
+ * would otherwise ignore when trying to save or load data. This is suitable for
80
+ * debugging persistence issues in a development environment.
81
+ * @returns A reference to the new ReactNativeMmkvPersister object.
82
+ * @example
83
+ * This example creates a ReactNativeMmkvPersister object and persists the
84
+ * Store to a MMKV storage instance as a JSON serialization into the
85
+ * `my_tinybase` key. It makes a change to the storage directly and then
86
+ * reloads it back into the Store.
87
+ *
88
+ * ```js yolo
89
+ * import {MMKV} from 'react-native-mmkv';
90
+ * import {createStore} from 'tinybase';
91
+ * import {createReactNativeMmkvPersister} from 'tinybase/persisters/persister-react-native-mmkv';
92
+ *
93
+ * const storage = new MMKV();
94
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
95
+ * const persister = createReactNativeMmkvPersister(
96
+ * store,
97
+ * storage,
98
+ * 'my_tinybase',
99
+ * );
100
+ *
101
+ * await persister.save();
102
+ * // Store will be saved to the MMKV storage.
103
+ *
104
+ * console.log(storage.getString('my_tinybase'));
105
+ * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
106
+ *
107
+ * await persister.destroy();
108
+ * storage.delete('my_tinybase');
109
+ * ```
110
+ * @category Creation
111
+ * @since v6.5.0
112
+ */
113
+ export function createReactNativeMmkvPersister(
114
+ store: Store | MergeableStore,
115
+ storage: MMKV,
116
+ storageName?: string,
117
+ onIgnoredError?: (error: any) => void,
118
+ ): ReactNativeMmkvPersister;
@@ -0,0 +1,132 @@
1
+ /**
2
+ * The persister-react-native-mmkv module of the TinyBase project lets you save
3
+ * and load Store data to and from a MMKV storage using the
4
+ * [`react-native-mmkv`](https://github.com/mrousavy/react-native-mmkv) module
5
+ * (in an appropriate React Native environment).
6
+ * @see Database Persistence guide
7
+ * @packageDocumentation
8
+ * @module persister-react-native-mmkv
9
+ * @since v6.5.0
10
+ */
11
+ import type {MMKV} from 'react-native-mmkv';
12
+ import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.ts';
13
+ import type {
14
+ OptionalSchemas,
15
+ Store,
16
+ } from '../../../store/with-schemas/index.d.ts';
17
+ import type {Persister, Persists} from '../../with-schemas/index.d.ts';
18
+
19
+ /**
20
+ * The ReactNativeMmkvPersister interface represents a Persister that lets you
21
+ * save and load Store data to and from a `react-native-mmkv` storage.
22
+ *
23
+ * You should use the createReactNativeMmkvPersister function to create a
24
+ * ReactNativeMmkvPersister object.
25
+ *
26
+ * It is a minor extension to the Persister interface and simply provides an
27
+ * extra getStorageName method for accessing the unique key of the storage
28
+ * location the Store is being persisted to.
29
+ * @category Persister
30
+ * @since v6.5.0
31
+ */
32
+ export interface ReactNativeMmkvPersister<Schemas extends OptionalSchemas>
33
+ extends Persister<Schemas, Persists.StoreOrMergeableStore> {
34
+ /**
35
+ * The getStorageName method returns the unique key of the storage location the
36
+ * Store is being persisted to.
37
+ * @returns The unique key of the storage location.
38
+ * @example
39
+ * This example creates a Persister object against a newly-created Store and
40
+ * then gets the unique key of the storage location back out again.
41
+ *
42
+ * ```js yolo
43
+ * import {MMKV} from 'react-native-mmkv';
44
+ * import {createStore} from 'tinybase';
45
+ * import {createReactNativeMmkvPersister} from 'tinybase/persisters/persister-react-native-mmkv';
46
+ *
47
+ * const storage = new MMKV();
48
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
49
+ * const persister = createReactNativeMmkvPersister(
50
+ * store,
51
+ * storage,
52
+ * 'my_tinybase',
53
+ * );
54
+ * console.log(persister.getStorageName() == 'my_tinybase');
55
+ * // -> true
56
+ *
57
+ * await persister.destroy();
58
+ * ```
59
+ * @category Getter
60
+ * @since v6.5.0
61
+ */
62
+ getStorageName(): string;
63
+ }
64
+
65
+ /**
66
+ * The createReactNativeMmkvPersister function creates a
67
+ * ReactNativeMmkvPersister object that can persist the Store to a local
68
+ * `react-native-mmkv` storage.
69
+ *
70
+ * This has schema-based typing. The following is a simplified representation:
71
+ *
72
+ * ```ts override
73
+ * createReactNativeMmkvPersister(
74
+ * store: Store | MergeableStore,
75
+ * storage: MMKV,
76
+ * storageName?: string,
77
+ * onIgnoredError?: (error: any) => void,
78
+ * ): ReactNativeMmkvPersister;
79
+ * ```
80
+ *
81
+ * A ReactNativeMmkvPersister supports both regular Store and MergeableStore
82
+ * objects.
83
+ *
84
+ * As well as providing a reference to the Store to persist, you must provide a
85
+ * `storage` parameter which identifies the MMKV storage instance.
86
+ *
87
+ * The third argument is a `storageName` string that configures which key to use
88
+ * for the storage key.
89
+ * @param store The Store or MergeableStore to persist.
90
+ * @param storage The MMKV storage instance.
91
+ * @param storageName The unique key to identify the storage location.
92
+ * @param onIgnoredError An optional handler for the errors that the Persister
93
+ * would otherwise ignore when trying to save or load data. This is suitable for
94
+ * debugging persistence issues in a development environment.
95
+ * @returns A reference to the new ReactNativeMmkvPersister object.
96
+ * @example
97
+ * This example creates a ReactNativeMmkvPersister object and persists the
98
+ * Store to a MMKV storage instance as a JSON serialization into the
99
+ * `my_tinybase` key. It makes a change to the storage directly and then
100
+ * reloads it back into the Store.
101
+ *
102
+ * ```js yolo
103
+ * import {MMKV} from 'react-native-mmkv';
104
+ * import {createStore} from 'tinybase';
105
+ * import {createReactNativeMmkvPersister} from 'tinybase/persisters/persister-react-native-mmkv';
106
+ *
107
+ * const storage = new MMKV();
108
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
109
+ * const persister = createReactNativeMmkvPersister(
110
+ * store,
111
+ * storage,
112
+ * 'my_tinybase',
113
+ * );
114
+ *
115
+ * await persister.save();
116
+ * // Store will be saved to the MMKV storage.
117
+ *
118
+ * console.log(storage.getString('my_tinybase'));
119
+ * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
120
+ *
121
+ * await persister.destroy();
122
+ * storage.delete('my_tinybase');
123
+ * ```
124
+ * @category Creation
125
+ * @since v6.5.0
126
+ */
127
+ export function createReactNativeMmkvPersister<Schemas extends OptionalSchemas>(
128
+ store: Store<Schemas> | MergeableStore<Schemas>,
129
+ storage: MMKV,
130
+ storageName?: string,
131
+ onIgnoredError?: (error: any) => void,
132
+ ): ReactNativeMmkvPersister<Schemas>;
@@ -16,6 +16,7 @@
16
16
  * |FilePersister|Local file (where possible)|Yes|Yes
17
17
  * |IndexedDbPersister|Browser IndexedDB|Yes|No
18
18
  * |RemotePersister|Remote server|Yes|No
19
+ * |ReactNativeMmkvPersister|MMKV in React Native, via [react-native-mmkv](https://github.com/mrousavy/react-native-mmkv)|Yes|Yes
19
20
  * |DurableObjectStoragePersister|Cloudflare Durable Object (KV)|No|Yes
20
21
  * |DurableObjectSqlStoragePersister|Cloudflare Durable Object (SQLite)|No|Yes
21
22
  * |Sqlite3Persister|SQLite in Node, via [sqlite3](https://github.com/TryGhost/node-sqlite3)|Yes|Yes*