tinybase 9.5.1 → 9.6.0-beta.1

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.
Files changed (147) hide show
  1. package/@types/omni/index.d.ts +4 -0
  2. package/@types/omni/with-schemas/index.d.ts +4 -0
  3. package/@types/persisters/index.d.ts +8 -1
  4. package/@types/persisters/persister-better-sqlite3/index.d.ts +178 -0
  5. package/@types/persisters/persister-better-sqlite3/with-schemas/index.d.ts +202 -0
  6. package/@types/persisters/persister-capacitor-sqlite/index.d.ts +181 -0
  7. package/@types/persisters/persister-capacitor-sqlite/with-schemas/index.d.ts +205 -0
  8. package/@types/persisters/persister-indexed-db/index.d.ts +17 -9
  9. package/@types/persisters/persister-indexed-db/with-schemas/index.d.ts +18 -10
  10. package/@types/persisters/persister-libsql/index.d.ts +27 -0
  11. package/@types/persisters/persister-libsql/with-schemas/index.d.ts +27 -0
  12. package/@types/persisters/persister-pg/index.d.ts +197 -0
  13. package/@types/persisters/persister-pg/with-schemas/index.d.ts +222 -0
  14. package/@types/persisters/persister-supabase/index.d.ts +201 -0
  15. package/@types/persisters/persister-supabase/with-schemas/index.d.ts +217 -0
  16. package/@types/persisters/with-schemas/index.d.ts +8 -1
  17. package/@types/ui-react-dom/index.d.ts +10 -10
  18. package/@types/ui-react-dom/with-schemas/index.d.ts +10 -10
  19. package/@types/ui-react-dom-charts/index.d.ts +7 -7
  20. package/@types/ui-react-dom-charts/with-schemas/index.d.ts +7 -7
  21. package/@types/ui-react-inspector/index.d.ts +1 -1
  22. package/@types/ui-react-inspector/with-schemas/index.d.ts +1 -1
  23. package/@types/ui-solid-dom/index.d.ts +10 -10
  24. package/@types/ui-solid-dom/with-schemas/index.d.ts +10 -10
  25. package/@types/ui-solid-inspector/index.d.ts +1 -1
  26. package/@types/ui-solid-inspector/with-schemas/index.d.ts +1 -1
  27. package/@types/ui-svelte-dom/index.d.ts +10 -10
  28. package/@types/ui-svelte-dom/with-schemas/index.d.ts +10 -10
  29. package/@types/ui-svelte-inspector/index.d.ts +1 -1
  30. package/@types/ui-svelte-inspector/with-schemas/index.d.ts +1 -1
  31. package/min/omni/index.js +1 -1
  32. package/min/omni/index.js.gz +0 -0
  33. package/min/omni/with-schemas/index.js +1 -1
  34. package/min/omni/with-schemas/index.js.gz +0 -0
  35. package/min/persisters/index.js +1 -1
  36. package/min/persisters/index.js.gz +0 -0
  37. package/min/persisters/persister-better-sqlite3/index.js +1 -0
  38. package/min/persisters/persister-better-sqlite3/index.js.gz +0 -0
  39. package/min/persisters/persister-better-sqlite3/with-schemas/index.js +1 -0
  40. package/min/persisters/persister-better-sqlite3/with-schemas/index.js.gz +0 -0
  41. package/min/persisters/persister-capacitor-sqlite/index.js +1 -0
  42. package/min/persisters/persister-capacitor-sqlite/index.js.gz +0 -0
  43. package/min/persisters/persister-capacitor-sqlite/with-schemas/index.js +1 -0
  44. package/min/persisters/persister-capacitor-sqlite/with-schemas/index.js.gz +0 -0
  45. package/min/persisters/persister-cr-sqlite-wasm/index.js +1 -1
  46. package/min/persisters/persister-cr-sqlite-wasm/index.js.gz +0 -0
  47. package/min/persisters/persister-cr-sqlite-wasm/with-schemas/index.js +1 -1
  48. package/min/persisters/persister-cr-sqlite-wasm/with-schemas/index.js.gz +0 -0
  49. package/min/persisters/persister-durable-object-sql-storage/index.js +1 -1
  50. package/min/persisters/persister-durable-object-sql-storage/index.js.gz +0 -0
  51. package/min/persisters/persister-durable-object-sql-storage/with-schemas/index.js +1 -1
  52. package/min/persisters/persister-durable-object-sql-storage/with-schemas/index.js.gz +0 -0
  53. package/min/persisters/persister-electric-sql/index.js +1 -1
  54. package/min/persisters/persister-electric-sql/index.js.gz +0 -0
  55. package/min/persisters/persister-electric-sql/with-schemas/index.js +1 -1
  56. package/min/persisters/persister-electric-sql/with-schemas/index.js.gz +0 -0
  57. package/min/persisters/persister-expo-sqlite/index.js +1 -1
  58. package/min/persisters/persister-expo-sqlite/index.js.gz +0 -0
  59. package/min/persisters/persister-expo-sqlite/with-schemas/index.js +1 -1
  60. package/min/persisters/persister-expo-sqlite/with-schemas/index.js.gz +0 -0
  61. package/min/persisters/persister-indexed-db/index.js +1 -1
  62. package/min/persisters/persister-indexed-db/index.js.gz +0 -0
  63. package/min/persisters/persister-indexed-db/with-schemas/index.js +1 -1
  64. package/min/persisters/persister-indexed-db/with-schemas/index.js.gz +0 -0
  65. package/min/persisters/persister-libsql/index.js +1 -1
  66. package/min/persisters/persister-libsql/index.js.gz +0 -0
  67. package/min/persisters/persister-libsql/with-schemas/index.js +1 -1
  68. package/min/persisters/persister-libsql/with-schemas/index.js.gz +0 -0
  69. package/min/persisters/persister-pg/index.js +1 -0
  70. package/min/persisters/persister-pg/index.js.gz +0 -0
  71. package/min/persisters/persister-pg/with-schemas/index.js +1 -0
  72. package/min/persisters/persister-pg/with-schemas/index.js.gz +0 -0
  73. package/min/persisters/persister-pglite/index.js +1 -1
  74. package/min/persisters/persister-pglite/index.js.gz +0 -0
  75. package/min/persisters/persister-pglite/with-schemas/index.js +1 -1
  76. package/min/persisters/persister-pglite/with-schemas/index.js.gz +0 -0
  77. package/min/persisters/persister-postgres/index.js +1 -1
  78. package/min/persisters/persister-postgres/index.js.gz +0 -0
  79. package/min/persisters/persister-postgres/with-schemas/index.js +1 -1
  80. package/min/persisters/persister-postgres/with-schemas/index.js.gz +0 -0
  81. package/min/persisters/persister-powersync/index.js +1 -1
  82. package/min/persisters/persister-powersync/index.js.gz +0 -0
  83. package/min/persisters/persister-powersync/with-schemas/index.js +1 -1
  84. package/min/persisters/persister-powersync/with-schemas/index.js.gz +0 -0
  85. package/min/persisters/persister-react-native-sqlite/index.js +1 -1
  86. package/min/persisters/persister-react-native-sqlite/index.js.gz +0 -0
  87. package/min/persisters/persister-react-native-sqlite/with-schemas/index.js +1 -1
  88. package/min/persisters/persister-react-native-sqlite/with-schemas/index.js.gz +0 -0
  89. package/min/persisters/persister-sqlite-bun/index.js +1 -1
  90. package/min/persisters/persister-sqlite-bun/index.js.gz +0 -0
  91. package/min/persisters/persister-sqlite-bun/with-schemas/index.js +1 -1
  92. package/min/persisters/persister-sqlite-bun/with-schemas/index.js.gz +0 -0
  93. package/min/persisters/persister-sqlite-wasm/index.js +1 -1
  94. package/min/persisters/persister-sqlite-wasm/index.js.gz +0 -0
  95. package/min/persisters/persister-sqlite-wasm/with-schemas/index.js +1 -1
  96. package/min/persisters/persister-sqlite-wasm/with-schemas/index.js.gz +0 -0
  97. package/min/persisters/persister-sqlite3/index.js +1 -1
  98. package/min/persisters/persister-sqlite3/index.js.gz +0 -0
  99. package/min/persisters/persister-sqlite3/with-schemas/index.js +1 -1
  100. package/min/persisters/persister-sqlite3/with-schemas/index.js.gz +0 -0
  101. package/min/persisters/persister-supabase/index.js +1 -0
  102. package/min/persisters/persister-supabase/index.js.gz +0 -0
  103. package/min/persisters/persister-supabase/with-schemas/index.js +1 -0
  104. package/min/persisters/persister-supabase/with-schemas/index.js.gz +0 -0
  105. package/min/persisters/with-schemas/index.js +1 -1
  106. package/min/persisters/with-schemas/index.js.gz +0 -0
  107. package/omni/index.js +269 -34
  108. package/omni/with-schemas/index.js +269 -34
  109. package/package.json +164 -4
  110. package/persisters/index.js +23 -7
  111. package/persisters/persister-better-sqlite3/index.js +1767 -0
  112. package/persisters/persister-better-sqlite3/with-schemas/index.js +1767 -0
  113. package/persisters/persister-capacitor-sqlite/index.js +1769 -0
  114. package/persisters/persister-capacitor-sqlite/with-schemas/index.js +1769 -0
  115. package/persisters/persister-cr-sqlite-wasm/index.js +20 -6
  116. package/persisters/persister-cr-sqlite-wasm/with-schemas/index.js +20 -6
  117. package/persisters/persister-durable-object-sql-storage/index.js +20 -7
  118. package/persisters/persister-durable-object-sql-storage/with-schemas/index.js +20 -7
  119. package/persisters/persister-electric-sql/index.js +20 -6
  120. package/persisters/persister-electric-sql/with-schemas/index.js +20 -6
  121. package/persisters/persister-expo-sqlite/index.js +20 -6
  122. package/persisters/persister-expo-sqlite/with-schemas/index.js +20 -6
  123. package/persisters/persister-indexed-db/index.js +48 -20
  124. package/persisters/persister-indexed-db/with-schemas/index.js +48 -20
  125. package/persisters/persister-libsql/index.js +20 -6
  126. package/persisters/persister-libsql/with-schemas/index.js +20 -6
  127. package/persisters/persister-pg/index.js +1905 -0
  128. package/persisters/persister-pg/with-schemas/index.js +1905 -0
  129. package/persisters/persister-pglite/index.js +20 -6
  130. package/persisters/persister-pglite/with-schemas/index.js +20 -6
  131. package/persisters/persister-postgres/index.js +20 -6
  132. package/persisters/persister-postgres/with-schemas/index.js +20 -6
  133. package/persisters/persister-powersync/index.js +27 -10
  134. package/persisters/persister-powersync/with-schemas/index.js +27 -10
  135. package/persisters/persister-react-native-sqlite/index.js +20 -6
  136. package/persisters/persister-react-native-sqlite/with-schemas/index.js +20 -6
  137. package/persisters/persister-sqlite-bun/index.js +20 -6
  138. package/persisters/persister-sqlite-bun/with-schemas/index.js +20 -6
  139. package/persisters/persister-sqlite-wasm/index.js +20 -6
  140. package/persisters/persister-sqlite-wasm/with-schemas/index.js +20 -6
  141. package/persisters/persister-sqlite3/index.js +20 -6
  142. package/persisters/persister-sqlite3/with-schemas/index.js +20 -6
  143. package/persisters/persister-supabase/index.js +883 -0
  144. package/persisters/persister-supabase/with-schemas/index.js +883 -0
  145. package/persisters/with-schemas/index.js +23 -7
  146. package/readme.md +14 -14
  147. package/releases.md +163 -66
@@ -22,7 +22,9 @@ export * from '../metrics/index.d.ts';
22
22
  export * from '../middleware/index.d.ts';
23
23
  export * from '../persisters/index.d.ts';
24
24
  export * from '../persisters/persister-automerge/index.d.ts';
25
+ export * from '../persisters/persister-better-sqlite3/index.d.ts';
25
26
  export * from '../persisters/persister-browser/index.d.ts';
27
+ export * from '../persisters/persister-capacitor-sqlite/index.d.ts';
26
28
  export * from '../persisters/persister-cr-sqlite-wasm/index.d.ts';
27
29
  export * from '../persisters/persister-durable-object-sql-storage/index.d.ts';
28
30
  export * from '../persisters/persister-durable-object-storage/index.d.ts';
@@ -33,6 +35,7 @@ export * from '../persisters/persister-indexed-db/index.d.ts';
33
35
  export * from '../persisters/persister-libsql/index.d.ts';
34
36
  export * from '../persisters/persister-partykit-client/index.d.ts';
35
37
  export * from '../persisters/persister-partykit-server/index.d.ts';
38
+ export * from '../persisters/persister-pg/index.d.ts';
36
39
  export * from '../persisters/persister-pglite/index.d.ts';
37
40
  export * from '../persisters/persister-postgres/index.d.ts';
38
41
  export * from '../persisters/persister-powersync/index.d.ts';
@@ -42,6 +45,7 @@ export * from '../persisters/persister-remote/index.d.ts';
42
45
  export * from '../persisters/persister-sqlite-bun/index.d.ts';
43
46
  export * from '../persisters/persister-sqlite-wasm/index.d.ts';
44
47
  export * from '../persisters/persister-sqlite3/index.d.ts';
48
+ export * from '../persisters/persister-supabase/index.d.ts';
45
49
  export * from '../persisters/persister-yjs/index.d.ts';
46
50
  export * from '../queries/index.d.ts';
47
51
  export * from '../relationships/index.d.ts';
@@ -21,7 +21,9 @@ export * from '../../mergeable-store/with-schemas/index.d.ts';
21
21
  export * from '../../metrics/with-schemas/index.d.ts';
22
22
  export * from '../../middleware/with-schemas/index.d.ts';
23
23
  export * from '../../persisters/persister-automerge/with-schemas/index.d.ts';
24
+ export * from '../../persisters/persister-better-sqlite3/with-schemas/index.d.ts';
24
25
  export * from '../../persisters/persister-browser/with-schemas/index.d.ts';
26
+ export * from '../../persisters/persister-capacitor-sqlite/with-schemas/index.d.ts';
25
27
  export * from '../../persisters/persister-cr-sqlite-wasm/with-schemas/index.d.ts';
26
28
  export * from '../../persisters/persister-durable-object-sql-storage/with-schemas/index.d.ts';
27
29
  export * from '../../persisters/persister-durable-object-storage/with-schemas/index.d.ts';
@@ -32,6 +34,7 @@ export * from '../../persisters/persister-indexed-db/with-schemas/index.d.ts';
32
34
  export * from '../../persisters/persister-libsql/with-schemas/index.d.ts';
33
35
  export * from '../../persisters/persister-partykit-client/with-schemas/index.d.ts';
34
36
  export * from '../../persisters/persister-partykit-server/with-schemas/index.d.ts';
37
+ export * from '../../persisters/persister-pg/with-schemas/index.d.ts';
35
38
  export * from '../../persisters/persister-pglite/with-schemas/index.d.ts';
36
39
  export * from '../../persisters/persister-postgres/with-schemas/index.d.ts';
37
40
  export * from '../../persisters/persister-powersync/with-schemas/index.d.ts';
@@ -41,6 +44,7 @@ export * from '../../persisters/persister-remote/with-schemas/index.d.ts';
41
44
  export * from '../../persisters/persister-sqlite-bun/with-schemas/index.d.ts';
42
45
  export * from '../../persisters/persister-sqlite-wasm/with-schemas/index.d.ts';
43
46
  export * from '../../persisters/persister-sqlite3/with-schemas/index.d.ts';
47
+ export * from '../../persisters/persister-supabase/with-schemas/index.d.ts';
44
48
  export * from '../../persisters/persister-yjs/with-schemas/index.d.ts';
45
49
  export * from '../../persisters/with-schemas/index.d.ts';
46
50
  export * from '../../queries/with-schemas/index.d.ts';
@@ -1927,11 +1927,18 @@ export function createCustomPersister<
1927
1927
  * ideas on how to build your own Persister type, and as functional examples.
1928
1928
  * Examine the implementation of the createSqlite3Persister function as a good
1929
1929
  * starting point, for example.
1930
+ *
1931
+ * Your `executeCommand` function is given SQL that uses SQLite's anonymous `?`
1932
+ * placeholders, with the parameters to bind to them in order. SQLite drivers
1933
+ * agree on that form, whereas they vary in how they handle the numbered
1934
+ * placeholders that PostgreSQL requires, so you should not need to rewrite
1935
+ * statements before passing them on.
1930
1936
  * @param store The Store to persist.
1931
1937
  * @param configOrStoreTableName A DatabasePersisterConfig object, or a string
1932
1938
  * that will be used as the name of the Store's table in the database.
1933
1939
  * @param executeCommand A function that will execute a command against the
1934
- * database.
1940
+ * database. It receives SQL with anonymous `?` placeholders, and the parameters
1941
+ * to bind to them in order.
1935
1942
  * @param addChangeListener A function that will register a listener for changes
1936
1943
  * to the database.
1937
1944
  * @param delChangeListener A function that will unregister the listener for
@@ -0,0 +1,178 @@
1
+ /**
2
+ * The persister-better-sqlite3 module of the TinyBase project lets you save and
3
+ * load Store data to and from a local SQLite database, via the synchronous
4
+ * [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3) module.
5
+ * @see Database Persistence guide
6
+ * @packageDocumentation
7
+ * @module persister-better-sqlite3
8
+ * @since 9.6.0
9
+ */
10
+ import type {Database} from 'better-sqlite3';
11
+ import type {MergeableStore} from '../../mergeable-store/index.d.ts';
12
+ import type {Store} from '../../store/index.d.ts';
13
+ import type {
14
+ DatabasePersisterConfig,
15
+ DpcJson,
16
+ Persister,
17
+ Persists,
18
+ } from '../index.d.ts';
19
+
20
+ /**
21
+ * The BetterSqlite3Persister interface represents a Persister that lets you
22
+ * save and load Store data to and from a local SQLite database.
23
+ *
24
+ * You should use the createBetterSqlite3Persister function to create a
25
+ * BetterSqlite3Persister object.
26
+ *
27
+ * It is a minor extension to the Persister interface and simply provides an
28
+ * extra getDb method for accessing a reference to the database the Store is
29
+ * being persisted to.
30
+ * @category Persister
31
+ * @since 9.6.0
32
+ */
33
+ export interface BetterSqlite3Persister extends Persister<Persists.StoreOrMergeableStore> {
34
+ /**
35
+ * The getDb method returns a reference to the database the Store is being
36
+ * persisted to.
37
+ * @returns A reference to the database.
38
+ * @example
39
+ * This example creates a Persister object against a newly-created Store and
40
+ * then gets the database back out again.
41
+ *
42
+ * ```js
43
+ * import Database from 'better-sqlite3';
44
+ * import {createStore} from 'tinybase';
45
+ * import {createBetterSqlite3Persister} from 'tinybase/persisters/persister-better-sqlite3';
46
+ *
47
+ * const db = new Database(':memory:');
48
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
49
+ * const persister = createBetterSqlite3Persister(store, db, 'my_tinybase');
50
+ *
51
+ * console.log(persister.getDb() == db);
52
+ * // -> true
53
+ *
54
+ * await persister.destroy();
55
+ * db.close();
56
+ * ```
57
+ * @category Getter
58
+ * @since 9.6.0
59
+ */
60
+ getDb(): Database;
61
+ }
62
+
63
+ /**
64
+ * The createBetterSqlite3Persister function creates a BetterSqlite3Persister
65
+ * object that can persist a Store to a local SQLite database.
66
+ *
67
+ * A BetterSqlite3Persister supports regular Store objects, and can also be used
68
+ * to persist the metadata of a MergeableStore when using the JSON serialization
69
+ * mode, as described below.
70
+ *
71
+ * As well as providing a reference to the Store to persist, you must provide a
72
+ * `db` parameter which is the database returned from `new Database(...)`.
73
+ *
74
+ * A database Persister uses one of two modes: either a JSON serialization of
75
+ * the whole Store stored in a single row of a table (the default), or a tabular
76
+ * mapping of Table Ids to database table names and vice-versa).
77
+ *
78
+ * The third argument is a DatabasePersisterConfig object that configures which
79
+ * of those modes to use, and settings for each. If the third argument is simply
80
+ * a string, it is used as the `storeTableName` property of the JSON
81
+ * serialization.
82
+ *
83
+ * See the documentation for the DpcJson and DpcTabular types for more
84
+ * information on how both of those modes can be configured.
85
+ *
86
+ * Note that `better-sqlite3` does not signal when the database changes, so if
87
+ * you enable automatic loading with the startAutoLoad method, it polls the
88
+ * database for changes. The Sqlite3Persister, which uses the asynchronous
89
+ * `sqlite3` module, is notified of changes as they happen, and may suit you
90
+ * better if that matters.
91
+ * @param store The Store or MergeableStore to persist.
92
+ * @param db The database that was returned from `new Database(...)`.
93
+ * @param configOrStoreTableName A DatabasePersisterConfig to configure the
94
+ * persistence mode (or a string to set the `storeTableName` property of the
95
+ * JSON serialization).
96
+ * @param onSqlCommand An optional handler called every time the Persister
97
+ * executes a SQL command or query. This is suitable for logging persistence
98
+ * behavior in a development environment.
99
+ * @param onIgnoredError An optional handler for the errors that the Persister
100
+ * would otherwise ignore when trying to save or load data. This is suitable for
101
+ * debugging persistence issues in a development environment.
102
+ * @returns A reference to the new BetterSqlite3Persister object.
103
+ * @example
104
+ * This example creates a BetterSqlite3Persister object and persists the Store
105
+ * to a local SQLite database as a JSON serialization into the `my_tinybase`
106
+ * table. It makes a change to the database directly and then reloads it back
107
+ * into the Store.
108
+ *
109
+ * ```js
110
+ * import Database from 'better-sqlite3';
111
+ * import {createStore} from 'tinybase';
112
+ * import {createBetterSqlite3Persister} from 'tinybase/persisters/persister-better-sqlite3';
113
+ *
114
+ * const db = new Database(':memory:');
115
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
116
+ * const persister = createBetterSqlite3Persister(store, db, 'my_tinybase');
117
+ *
118
+ * await persister.save();
119
+ * // Store will be saved to the database.
120
+ *
121
+ * console.log(db.prepare('SELECT * FROM my_tinybase;').all());
122
+ * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
123
+ *
124
+ * db.prepare('UPDATE my_tinybase SET store = ? WHERE _id = ?;').run(
125
+ * '[{"pets":{"felix":{"species":"cat"}}},{}]',
126
+ * '_',
127
+ * );
128
+ * await persister.load();
129
+ * console.log(store.getTables());
130
+ * // -> {pets: {felix: {species: 'cat'}}}
131
+ *
132
+ * await persister.destroy();
133
+ * db.close();
134
+ * ```
135
+ * @example
136
+ * This example creates a BetterSqlite3Persister object and persists the Store
137
+ * to a local SQLite database with tabular mapping.
138
+ *
139
+ * ```js
140
+ * import Database from 'better-sqlite3';
141
+ * import {createStore} from 'tinybase';
142
+ * import {createBetterSqlite3Persister} from 'tinybase/persisters/persister-better-sqlite3';
143
+ *
144
+ * const db = new Database(':memory:');
145
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
146
+ * const persister = createBetterSqlite3Persister(store, db, {
147
+ * mode: 'tabular',
148
+ * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
149
+ * });
150
+ *
151
+ * await persister.save();
152
+ * console.log(db.prepare('SELECT * FROM pets;').all());
153
+ * // -> [{_id: 'fido', species: 'dog'}]
154
+ *
155
+ * db.prepare(
156
+ * `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
157
+ * ).run();
158
+ * await persister.load();
159
+ * console.log(store.getTables());
160
+ * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
161
+ *
162
+ * await persister.destroy();
163
+ * db.close();
164
+ * ```
165
+ * @category Creation
166
+ * @since 9.6.0
167
+ */
168
+ export function createBetterSqlite3Persister<StoreType extends Store>(
169
+ store: StoreType,
170
+ db: Database,
171
+ configOrStoreTableName?:
172
+ | (NoInfer<StoreType> extends MergeableStore
173
+ ? DpcJson
174
+ : DatabasePersisterConfig)
175
+ | string,
176
+ onSqlCommand?: (sql: string, params?: any[]) => void,
177
+ onIgnoredError?: (error: any) => void,
178
+ ): BetterSqlite3Persister;
@@ -0,0 +1,202 @@
1
+ /**
2
+ * The persister-better-sqlite3 module of the TinyBase project lets you save and
3
+ * load Store data to and from a local SQLite database, via the synchronous
4
+ * [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3) module.
5
+ * @see Database Persistence guide
6
+ * @packageDocumentation
7
+ * @module persister-better-sqlite3
8
+ * @since 9.6.0
9
+ */
10
+ import type {Database} from 'better-sqlite3';
11
+ import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.ts';
12
+ import type {
13
+ OptionalSchemas,
14
+ Store,
15
+ } from '../../../store/with-schemas/index.d.ts';
16
+ import type {
17
+ DatabasePersisterConfig,
18
+ DpcJson,
19
+ Persister,
20
+ Persists,
21
+ } from '../../with-schemas/index.d.ts';
22
+
23
+ /**
24
+ * The BetterSqlite3Persister interface represents a Persister that lets you
25
+ * save and load Store data to and from a local SQLite database.
26
+ *
27
+ * You should use the createBetterSqlite3Persister function to create a
28
+ * BetterSqlite3Persister object.
29
+ *
30
+ * It is a minor extension to the Persister interface and simply provides an
31
+ * extra getDb method for accessing a reference to the database the Store is
32
+ * being persisted to.
33
+ * @category Persister
34
+ * @since 9.6.0
35
+ */
36
+ export interface BetterSqlite3Persister<
37
+ Schemas extends OptionalSchemas,
38
+ > extends Persister<Schemas, Persists.StoreOrMergeableStore> {
39
+ /**
40
+ * The getDb method returns a reference to the database the Store is being
41
+ * persisted to.
42
+ * @returns A reference to the database.
43
+ * @example
44
+ * This example creates a Persister object against a newly-created Store and
45
+ * then gets the database back out again.
46
+ *
47
+ * ```js
48
+ * import Database from 'better-sqlite3';
49
+ * import {createStore} from 'tinybase';
50
+ * import {createBetterSqlite3Persister} from 'tinybase/persisters/persister-better-sqlite3';
51
+ *
52
+ * const db = new Database(':memory:');
53
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
54
+ * const persister = createBetterSqlite3Persister(store, db, 'my_tinybase');
55
+ *
56
+ * console.log(persister.getDb() == db);
57
+ * // -> true
58
+ *
59
+ * await persister.destroy();
60
+ * db.close();
61
+ * ```
62
+ * @category Getter
63
+ * @since 9.6.0
64
+ */
65
+ getDb(): Database;
66
+ }
67
+
68
+ /**
69
+ * The createBetterSqlite3Persister function creates a BetterSqlite3Persister
70
+ * object that can persist a Store to a local SQLite database.
71
+ *
72
+ * This has schema-based typing. The following is a simplified representation:
73
+ *
74
+ * ```ts override
75
+ * createBetterSqlite3Persister<StoreType extends Store>(
76
+ * store: StoreType,
77
+ * db: Database,
78
+ * configOrStoreTableName?:
79
+ * | (NoInfer<StoreType> extends MergeableStore
80
+ * ? DpcJson
81
+ * : DatabasePersisterConfig)
82
+ * | string,
83
+ * onSqlCommand?: (sql: string, params?: any[]) => void,
84
+ * onIgnoredError?: (error: any) => void,
85
+ * ): BetterSqlite3Persister;
86
+ * ```
87
+ *
88
+ * A BetterSqlite3Persister supports regular Store objects, and can also be used
89
+ * to persist the metadata of a MergeableStore when using the JSON serialization
90
+ * mode, as described below.
91
+ *
92
+ * As well as providing a reference to the Store to persist, you must provide a
93
+ * `db` parameter which is the database returned from `new Database(...)`.
94
+ *
95
+ * A database Persister uses one of two modes: either a JSON serialization of
96
+ * the whole Store stored in a single row of a table (the default), or a tabular
97
+ * mapping of Table Ids to database table names and vice-versa).
98
+ *
99
+ * The third argument is a DatabasePersisterConfig object that configures which
100
+ * of those modes to use, and settings for each. If the third argument is simply
101
+ * a string, it is used as the `storeTableName` property of the JSON
102
+ * serialization.
103
+ *
104
+ * See the documentation for the DpcJson and DpcTabular types for more
105
+ * information on how both of those modes can be configured.
106
+ *
107
+ * Note that `better-sqlite3` does not signal when the database changes, so if
108
+ * you enable automatic loading with the startAutoLoad method, it polls the
109
+ * database for changes. The Sqlite3Persister, which uses the asynchronous
110
+ * `sqlite3` module, is notified of changes as they happen, and may suit you
111
+ * better if that matters.
112
+ * @param store The Store or MergeableStore to persist.
113
+ * @param db The database that was returned from `new Database(...)`.
114
+ * @param configOrStoreTableName A DatabasePersisterConfig to configure the
115
+ * persistence mode (or a string to set the `storeTableName` property of the
116
+ * JSON serialization).
117
+ * @param onSqlCommand An optional handler called every time the Persister
118
+ * executes a SQL command or query. This is suitable for logging persistence
119
+ * behavior in a development environment.
120
+ * @param onIgnoredError An optional handler for the errors that the Persister
121
+ * would otherwise ignore when trying to save or load data. This is suitable for
122
+ * debugging persistence issues in a development environment.
123
+ * @returns A reference to the new BetterSqlite3Persister object.
124
+ * @example
125
+ * This example creates a BetterSqlite3Persister object and persists the Store
126
+ * to a local SQLite database as a JSON serialization into the `my_tinybase`
127
+ * table. It makes a change to the database directly and then reloads it back
128
+ * into the Store.
129
+ *
130
+ * ```js
131
+ * import Database from 'better-sqlite3';
132
+ * import {createStore} from 'tinybase';
133
+ * import {createBetterSqlite3Persister} from 'tinybase/persisters/persister-better-sqlite3';
134
+ *
135
+ * const db = new Database(':memory:');
136
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
137
+ * const persister = createBetterSqlite3Persister(store, db, 'my_tinybase');
138
+ *
139
+ * await persister.save();
140
+ * // Store will be saved to the database.
141
+ *
142
+ * console.log(db.prepare('SELECT * FROM my_tinybase;').all());
143
+ * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
144
+ *
145
+ * db.prepare('UPDATE my_tinybase SET store = ? WHERE _id = ?;').run(
146
+ * '[{"pets":{"felix":{"species":"cat"}}},{}]',
147
+ * '_',
148
+ * );
149
+ * await persister.load();
150
+ * console.log(store.getTables());
151
+ * // -> {pets: {felix: {species: 'cat'}}}
152
+ *
153
+ * await persister.destroy();
154
+ * db.close();
155
+ * ```
156
+ * @example
157
+ * This example creates a BetterSqlite3Persister object and persists the Store
158
+ * to a local SQLite database with tabular mapping.
159
+ *
160
+ * ```js
161
+ * import Database from 'better-sqlite3';
162
+ * import {createStore} from 'tinybase';
163
+ * import {createBetterSqlite3Persister} from 'tinybase/persisters/persister-better-sqlite3';
164
+ *
165
+ * const db = new Database(':memory:');
166
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
167
+ * const persister = createBetterSqlite3Persister(store, db, {
168
+ * mode: 'tabular',
169
+ * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
170
+ * });
171
+ *
172
+ * await persister.save();
173
+ * console.log(db.prepare('SELECT * FROM pets;').all());
174
+ * // -> [{_id: 'fido', species: 'dog'}]
175
+ *
176
+ * db.prepare(
177
+ * `INSERT INTO pets (_id, species) VALUES ('felix', 'cat')`,
178
+ * ).run();
179
+ * await persister.load();
180
+ * console.log(store.getTables());
181
+ * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
182
+ *
183
+ * await persister.destroy();
184
+ * db.close();
185
+ * ```
186
+ * @category Creation
187
+ * @since 9.6.0
188
+ */
189
+ export function createBetterSqlite3Persister<Schemas extends OptionalSchemas>(
190
+ store: MergeableStore<Schemas>,
191
+ db: Database,
192
+ configOrStoreTableName?: DpcJson | string,
193
+ onSqlCommand?: (sql: string, params?: any[]) => void,
194
+ onIgnoredError?: (error: any) => void,
195
+ ): BetterSqlite3Persister<Schemas>;
196
+ export function createBetterSqlite3Persister<Schemas extends OptionalSchemas>(
197
+ store: Store<Schemas> & {getMergeableContent?: never},
198
+ db: Database,
199
+ configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
200
+ onSqlCommand?: (sql: string, params?: any[]) => void,
201
+ onIgnoredError?: (error: any) => void,
202
+ ): BetterSqlite3Persister<Schemas>;
@@ -0,0 +1,181 @@
1
+ /**
2
+ * The persister-capacitor-sqlite module of the TinyBase project lets you save
3
+ * and load Store data to and from a SQLite database in a Capacitor app, via the
4
+ * `@capacitor-community/sqlite` plugin.
5
+ * @see Database Persistence guide
6
+ * @packageDocumentation
7
+ * @module persister-capacitor-sqlite
8
+ * @since 9.6.0
9
+ */
10
+ import type {SQLiteDBConnection} from '@capacitor-community/sqlite';
11
+ import type {MergeableStore} from '../../mergeable-store/index.d.ts';
12
+ import type {Store} from '../../store/index.d.ts';
13
+ import type {
14
+ DatabasePersisterConfig,
15
+ DpcJson,
16
+ Persister,
17
+ Persists,
18
+ } from '../index.d.ts';
19
+
20
+ /**
21
+ * The CapacitorSqlitePersister interface represents a Persister that lets you
22
+ * save and load Store data to and from a SQLite database in a Capacitor app.
23
+ *
24
+ * You should use the createCapacitorSqlitePersister function to create a
25
+ * CapacitorSqlitePersister object.
26
+ *
27
+ * It is a minor extension to the Persister interface and simply provides an
28
+ * extra getDb method for accessing a reference to the database connection the
29
+ * Store is being persisted to.
30
+ * @category Persister
31
+ * @since 9.6.0
32
+ */
33
+ export interface CapacitorSqlitePersister extends Persister<Persists.StoreOrMergeableStore> {
34
+ /**
35
+ * The getDb method returns a reference to the database connection the Store
36
+ * is being persisted to.
37
+ * @returns A reference to the database connection.
38
+ * @example
39
+ * This example creates a Persister object against a newly-created Store and
40
+ * then gets the database connection back out again.
41
+ *
42
+ * ```js ignore
43
+ * import {
44
+ * CapacitorSQLite,
45
+ * SQLiteConnection,
46
+ * } from '@capacitor-community/sqlite';
47
+ * import {createStore} from 'tinybase';
48
+ * import {createCapacitorSqlitePersister} from 'tinybase/persisters/persister-capacitor-sqlite';
49
+ *
50
+ * const sqlite = new SQLiteConnection(CapacitorSQLite);
51
+ * const db = await sqlite.createConnection(
52
+ * 'my.db',
53
+ * false,
54
+ * 'no-encryption',
55
+ * 1,
56
+ * false,
57
+ * );
58
+ * await db.open();
59
+ *
60
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
61
+ * const persister = createCapacitorSqlitePersister(store, db, 'my_tinybase');
62
+ *
63
+ * console.log(persister.getDb() == db);
64
+ * // -> true
65
+ *
66
+ * await persister.destroy();
67
+ * ```
68
+ * @category Getter
69
+ * @since 9.6.0
70
+ */
71
+ getDb(): SQLiteDBConnection;
72
+ }
73
+
74
+ /**
75
+ * The createCapacitorSqlitePersister function creates a
76
+ * CapacitorSqlitePersister object that can persist a Store to a SQLite database
77
+ * in a Capacitor app.
78
+ *
79
+ * A CapacitorSqlitePersister supports regular Store objects, and can also be
80
+ * used to persist the metadata of a MergeableStore when using the JSON
81
+ * serialization mode, as described below.
82
+ *
83
+ * As well as providing a reference to the Store to persist, you must provide a
84
+ * `db` parameter which is an open SQLiteDBConnection, as returned from the
85
+ * plugin's `createConnection` method.
86
+ *
87
+ * A database Persister uses one of two modes: either a JSON serialization of
88
+ * the whole Store stored in a single row of a table (the default), or a tabular
89
+ * mapping of Table Ids to database table names and vice-versa).
90
+ *
91
+ * The third argument is a DatabasePersisterConfig object that configures which
92
+ * of those modes to use, and settings for each. If the third argument is simply
93
+ * a string, it is used as the `storeTableName` property of the JSON
94
+ * serialization.
95
+ *
96
+ * See the documentation for the DpcJson and DpcTabular types for more
97
+ * information on how both of those modes can be configured.
98
+ *
99
+ * The plugin does not signal when the database changes underneath the
100
+ * Persister, so if you enable automatic loading with the startAutoLoad method,
101
+ * it polls the database for changes in the same way that the LibSqlPersister
102
+ * does.
103
+ *
104
+ * Note: TinyBase's tests for this module run against a mocked plugin rather
105
+ * than a real device, since `@capacitor-community/sqlite` needs a native iOS or
106
+ * Android runtime (or its `jeep-sqlite` web component) that a Node test suite
107
+ * cannot provide. The SQL behavior itself is covered by the other SQLite
108
+ * Persisters, which share all of it, but the binding to the plugin is only
109
+ * verified against those mocks. Please report anything that behaves differently
110
+ * on a real device.
111
+ * @param store The Store or MergeableStore to persist.
112
+ * @param db The database connection that was returned from
113
+ * `createConnection(...)`.
114
+ * @param configOrStoreTableName A DatabasePersisterConfig to configure the
115
+ * persistence mode (or a string to set the `storeTableName` property of the
116
+ * JSON serialization).
117
+ * @param onSqlCommand An optional handler called every time the Persister
118
+ * executes a SQL command or query. This is suitable for logging persistence
119
+ * behavior in a development environment.
120
+ * @param onIgnoredError An optional handler for the errors that the Persister
121
+ * would otherwise ignore when trying to save or load data. This is suitable for
122
+ * debugging persistence issues in a development environment.
123
+ * @returns A reference to the new CapacitorSqlitePersister object.
124
+ * @example
125
+ * This example creates a CapacitorSqlitePersister object and persists the Store
126
+ * to a SQLite database as a JSON serialization into the `my_tinybase` table. It
127
+ * makes a change to the database directly and then reloads it back into the
128
+ * Store.
129
+ *
130
+ * ```js ignore
131
+ * import {
132
+ * CapacitorSQLite,
133
+ * SQLiteConnection,
134
+ * } from '@capacitor-community/sqlite';
135
+ * import {createStore} from 'tinybase';
136
+ * import {createCapacitorSqlitePersister} from 'tinybase/persisters/persister-capacitor-sqlite';
137
+ *
138
+ * const sqlite = new SQLiteConnection(CapacitorSQLite);
139
+ * const db = await sqlite.createConnection(
140
+ * 'my.db',
141
+ * false,
142
+ * 'no-encryption',
143
+ * 1,
144
+ * false,
145
+ * );
146
+ * await db.open();
147
+ *
148
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
149
+ * const persister = createCapacitorSqlitePersister(store, db, 'my_tinybase');
150
+ *
151
+ * await persister.save();
152
+ * // Store will be saved to the database.
153
+ *
154
+ * console.log((await db.query('SELECT * FROM my_tinybase;')).values);
155
+ * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
156
+ *
157
+ * await db.run(
158
+ * 'UPDATE my_tinybase SET store = ? WHERE _id = ?;',
159
+ * ['[{"pets":{"felix":{"species":"cat"}}},{}]', '_'],
160
+ * false,
161
+ * );
162
+ * await persister.load();
163
+ * console.log(store.getTables());
164
+ * // -> {pets: {felix: {species: 'cat'}}}
165
+ *
166
+ * await persister.destroy();
167
+ * ```
168
+ * @category Creation
169
+ * @since 9.6.0
170
+ */
171
+ export function createCapacitorSqlitePersister<StoreType extends Store>(
172
+ store: StoreType,
173
+ db: SQLiteDBConnection,
174
+ configOrStoreTableName?:
175
+ | (NoInfer<StoreType> extends MergeableStore
176
+ ? DpcJson
177
+ : DatabasePersisterConfig)
178
+ | string,
179
+ onSqlCommand?: (sql: string, params?: any[]) => void,
180
+ onIgnoredError?: (error: any) => void,
181
+ ): CapacitorSqlitePersister;