tinybase 6.1.0-beta.0 → 6.1.0-beta.2

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 (155) hide show
  1. package/@types/_internal/ui-react/with-schemas/index.d.ts +1 -1
  2. package/@types/common/index.d.ts +4 -4
  3. package/@types/common/with-schemas/index.d.ts +4 -4
  4. package/@types/index.d.ts +1 -1
  5. package/@types/persisters/persister-automerge/index.d.ts +1 -1
  6. package/@types/persisters/persister-automerge/with-schemas/index.d.ts +1 -1
  7. package/@types/persisters/persister-cr-sqlite-wasm/index.d.ts +1 -1
  8. package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.ts +1 -1
  9. package/@types/persisters/persister-electric-sql/index.d.ts +4 -4
  10. package/@types/persisters/persister-electric-sql/with-schemas/index.d.ts +4 -4
  11. package/@types/persisters/persister-expo-sqlite/index.d.ts +1 -1
  12. package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.ts +1 -1
  13. package/@types/persisters/persister-libsql/index.d.ts +1 -1
  14. package/@types/persisters/persister-libsql/with-schemas/index.d.ts +1 -1
  15. package/@types/persisters/persister-partykit-client/index.d.ts +1 -1
  16. package/@types/persisters/persister-partykit-client/with-schemas/index.d.ts +1 -1
  17. package/@types/persisters/persister-partykit-server/index.d.ts +7 -7
  18. package/@types/persisters/persister-partykit-server/with-schemas/index.d.ts +1 -1
  19. package/@types/persisters/persister-pglite/index.d.ts +1 -1
  20. package/@types/persisters/persister-pglite/with-schemas/index.d.ts +1 -1
  21. package/@types/persisters/persister-postgres/index.d.ts +1 -1
  22. package/@types/persisters/persister-postgres/with-schemas/index.d.ts +1 -1
  23. package/@types/persisters/persister-powersync/index.d.ts +1 -1
  24. package/@types/persisters/persister-powersync/with-schemas/index.d.ts +1 -1
  25. package/@types/persisters/persister-sqlite3/index.d.ts +1 -1
  26. package/@types/persisters/persister-sqlite3/with-schemas/index.d.ts +1 -1
  27. package/@types/persisters/persister-yjs/index.d.ts +1 -1
  28. package/@types/persisters/persister-yjs/with-schemas/index.d.ts +1 -1
  29. package/@types/synchronizers/synchronizer-ws-client/index.d.ts +1 -1
  30. package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.ts +1 -1
  31. package/@types/synchronizers/synchronizer-ws-server/index.d.ts +1 -1
  32. package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.ts +1 -1
  33. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.ts +1 -1
  34. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.ts +1 -1
  35. package/@types/ui-react/index.d.ts +3 -3
  36. package/@types/ui-react/with-schemas/index.d.ts +1 -1
  37. package/@types/ui-react-dom/index.d.ts +1 -1
  38. package/@types/ui-react-dom/with-schemas/index.d.ts +1 -1
  39. package/@types/ui-react-inspector/with-schemas/index.d.ts +1 -1
  40. package/@types/with-schemas/index.d.ts +1 -1
  41. package/index.js +1579 -1567
  42. package/indexes/index.js +15 -3
  43. package/indexes/with-schemas/index.js +15 -3
  44. package/min/index.js +1 -1
  45. package/min/index.js.gz +0 -0
  46. package/min/indexes/index.js +1 -1
  47. package/min/indexes/index.js.gz +0 -0
  48. package/min/indexes/with-schemas/index.js +1 -1
  49. package/min/indexes/with-schemas/index.js.gz +0 -0
  50. package/min/persisters/index.js +1 -1
  51. package/min/persisters/index.js.gz +0 -0
  52. package/min/persisters/with-schemas/index.js +1 -1
  53. package/min/persisters/with-schemas/index.js.gz +0 -0
  54. package/min/ui-react/index.js +1 -1
  55. package/min/ui-react/index.js.gz +0 -0
  56. package/min/ui-react/with-schemas/index.js +1 -1
  57. package/min/ui-react/with-schemas/index.js.gz +0 -0
  58. package/min/with-schemas/index.js +1 -1
  59. package/min/with-schemas/index.js.gz +0 -0
  60. package/package.json +7 -10
  61. package/persisters/index.js +95 -95
  62. package/persisters/with-schemas/index.js +95 -95
  63. package/readme.md +13 -13
  64. package/releases.md +25 -25
  65. package/synchronizers/synchronizer-ws-server-durable-object/index.js +1 -1
  66. package/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +1 -1
  67. package/ui-react/index.js +64 -53
  68. package/ui-react/with-schemas/index.js +64 -53
  69. package/ui-react-dom/index.js +3 -3
  70. package/ui-react-dom/with-schemas/index.js +3 -3
  71. package/ui-react-inspector/index.js +4 -4
  72. package/ui-react-inspector/with-schemas/index.js +4 -4
  73. package/with-schemas/index.js +1579 -1567
  74. package/@types/_internal/queries/index.d.cts +0 -0
  75. package/@types/_internal/queries/with-schemas/index.d.cts +0 -22
  76. package/@types/_internal/store/index.d.cts +0 -3
  77. package/@types/_internal/store/with-schemas/index.d.cts +0 -106
  78. package/@types/_internal/ui-react/index.d.cts +0 -0
  79. package/@types/_internal/ui-react/with-schemas/index.d.cts +0 -1130
  80. package/@types/checkpoints/index.d.cts +0 -1059
  81. package/@types/checkpoints/with-schemas/index.d.cts +0 -1151
  82. package/@types/common/index.d.cts +0 -158
  83. package/@types/common/with-schemas/index.d.cts +0 -158
  84. package/@types/index.d.cts +0 -17
  85. package/@types/indexes/index.d.cts +0 -1064
  86. package/@types/indexes/with-schemas/index.d.cts +0 -1210
  87. package/@types/mergeable-store/index.d.cts +0 -1139
  88. package/@types/mergeable-store/with-schemas/index.d.cts +0 -1628
  89. package/@types/metrics/index.d.cts +0 -917
  90. package/@types/metrics/with-schemas/index.d.cts +0 -1004
  91. package/@types/persisters/index.d.cts +0 -1877
  92. package/@types/persisters/persister-automerge/index.d.cts +0 -165
  93. package/@types/persisters/persister-automerge/with-schemas/index.d.cts +0 -180
  94. package/@types/persisters/persister-browser/index.d.cts +0 -185
  95. package/@types/persisters/persister-browser/with-schemas/index.d.cts +0 -208
  96. package/@types/persisters/persister-cr-sqlite-wasm/index.d.cts +0 -159
  97. package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.cts +0 -178
  98. package/@types/persisters/persister-durable-object-storage/index.d.cts +0 -122
  99. package/@types/persisters/persister-durable-object-storage/with-schemas/index.d.cts +0 -136
  100. package/@types/persisters/persister-electric-sql/index.d.cts +0 -185
  101. package/@types/persisters/persister-electric-sql/with-schemas/index.d.cts +0 -204
  102. package/@types/persisters/persister-expo-sqlite/index.d.cts +0 -186
  103. package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.cts +0 -205
  104. package/@types/persisters/persister-file/index.d.cts +0 -94
  105. package/@types/persisters/persister-file/with-schemas/index.d.cts +0 -107
  106. package/@types/persisters/persister-indexed-db/index.d.cts +0 -120
  107. package/@types/persisters/persister-indexed-db/with-schemas/index.d.cts +0 -135
  108. package/@types/persisters/persister-libsql/index.d.cts +0 -158
  109. package/@types/persisters/persister-libsql/with-schemas/index.d.cts +0 -177
  110. package/@types/persisters/persister-partykit-client/index.d.cts +0 -195
  111. package/@types/persisters/persister-partykit-client/with-schemas/index.d.cts +0 -210
  112. package/@types/persisters/persister-partykit-server/index.d.cts +0 -650
  113. package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +0 -695
  114. package/@types/persisters/persister-pglite/index.d.cts +0 -177
  115. package/@types/persisters/persister-pglite/with-schemas/index.d.cts +0 -196
  116. package/@types/persisters/persister-postgres/index.d.cts +0 -166
  117. package/@types/persisters/persister-postgres/with-schemas/index.d.cts +0 -185
  118. package/@types/persisters/persister-powersync/index.d.cts +0 -174
  119. package/@types/persisters/persister-powersync/with-schemas/index.d.cts +0 -193
  120. package/@types/persisters/persister-remote/index.d.cts +0 -117
  121. package/@types/persisters/persister-remote/with-schemas/index.d.cts +0 -133
  122. package/@types/persisters/persister-sqlite-wasm/index.d.cts +0 -175
  123. package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.cts +0 -195
  124. package/@types/persisters/persister-sqlite3/index.d.cts +0 -176
  125. package/@types/persisters/persister-sqlite3/with-schemas/index.d.cts +0 -195
  126. package/@types/persisters/persister-yjs/index.d.cts +0 -161
  127. package/@types/persisters/persister-yjs/with-schemas/index.d.cts +0 -176
  128. package/@types/persisters/with-schemas/index.d.cts +0 -2054
  129. package/@types/queries/index.d.cts +0 -3695
  130. package/@types/queries/with-schemas/index.d.cts +0 -4016
  131. package/@types/relationships/index.d.cts +0 -1320
  132. package/@types/relationships/with-schemas/index.d.cts +0 -1474
  133. package/@types/store/index.d.cts +0 -7598
  134. package/@types/store/with-schemas/index.d.cts +0 -9278
  135. package/@types/synchronizers/index.d.cts +0 -485
  136. package/@types/synchronizers/synchronizer-broadcast-channel/index.d.cts +0 -121
  137. package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.cts +0 -137
  138. package/@types/synchronizers/synchronizer-local/index.d.cts +0 -95
  139. package/@types/synchronizers/synchronizer-local/with-schemas/index.d.cts +0 -114
  140. package/@types/synchronizers/synchronizer-ws-client/index.d.cts +0 -160
  141. package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.cts +0 -179
  142. package/@types/synchronizers/synchronizer-ws-server/index.d.cts +0 -736
  143. package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.cts +0 -765
  144. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.cts +0 -311
  145. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.cts +0 -349
  146. package/@types/synchronizers/synchronizer-ws-server-simple/index.d.cts +0 -144
  147. package/@types/synchronizers/synchronizer-ws-server-simple/with-schemas/index.d.cts +0 -144
  148. package/@types/synchronizers/with-schemas/index.d.cts +0 -503
  149. package/@types/ui-react/index.d.cts +0 -16640
  150. package/@types/ui-react/with-schemas/index.d.cts +0 -17281
  151. package/@types/ui-react-dom/index.d.cts +0 -1862
  152. package/@types/ui-react-dom/with-schemas/index.d.cts +0 -1994
  153. package/@types/ui-react-inspector/index.d.cts +0 -79
  154. package/@types/ui-react-inspector/with-schemas/index.d.cts +0 -1985
  155. package/@types/with-schemas/index.d.cts +0 -17
@@ -1,177 +0,0 @@
1
- /**
2
- * The persister-pglite module of the TinyBase project lets you save and load
3
- * Store data to and from a PGlite database (in an appropriate environment).
4
- * @see Database Persistence guide
5
- * @packageDocumentation
6
- * @module persister-pglite
7
- * @since 5.2.0
8
- */
9
- import type {MergeableStore} from '../../mergeable-store/index.d.cts';
10
- import type {Store} from '../../store/index.d.cts';
11
- import type {DatabasePersisterConfig, Persister, Persists} from '../index.d.cts';
12
- import type {PGlite} from '@electric-sql/pglite';
13
-
14
- /**
15
- * The PglitePersister interface represents a Persister that lets you save and
16
- * load Store data to and from a local PGlite database.
17
- *
18
- * You should use the createPglitePersister function to create a PglitePersister
19
- * object.
20
- *
21
- * It is a minor extension to the Persister interface and simply provides an
22
- * extra getPglite method for accessing a reference to the database connection
23
- * the Store is being persisted to.
24
- * @category Persister
25
- * @since 5.2.0
26
- */
27
- export interface PglitePersister
28
- extends Persister<Persists.StoreOrMergeableStore> {
29
- /**
30
- * The getPglite method returns a reference to the database connection the
31
- * Store is being persisted to.
32
- * @returns A reference to the database connection.
33
- * @example
34
- * This example creates a Persister object against a newly-created Store and
35
- * then gets the database connection back out again.
36
- *
37
- * ```js
38
- * import {PGlite} from '@electric-sql/pglite';
39
- * import {createStore} from 'tinybase';
40
- * import {createPglitePersister} from 'tinybase/persisters/persister-pglite';
41
- *
42
- * const pglite = await PGlite.create();
43
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
44
- * const persister = await createPglitePersister(
45
- * store,
46
- * pglite,
47
- * 'my_tinybase',
48
- * );
49
- *
50
- * console.log(persister.getPglite() == pglite);
51
- * // -> true
52
- *
53
- * persister.destroy();
54
- * await pglite.close();
55
- * ```
56
- * @category Getter
57
- * @since 5.2.0
58
- */
59
- getPglite(): PGlite;
60
- }
61
-
62
- /**
63
- * The createPglitePersister function creates a PglitePersister object that can
64
- * persist the Store to a local PGlite database.
65
- *
66
- * A PglitePersister supports regular Store objects, and can also be used to
67
- * persist the metadata of a MergeableStore when using the JSON serialization
68
- * mode, as described below.
69
- *
70
- * As well as providing a reference to the Store to persist, you must provide a
71
- * `pglite` parameter which identifies the database connection.
72
- *
73
- * A database Persister uses one of two modes: either a JSON serialization of
74
- * the whole Store stored in a single row of a table (the default), or a tabular
75
- * mapping of Table Ids to database table names and vice-versa).
76
- *
77
- * The third argument is a DatabasePersisterConfig object that configures which
78
- * of those modes to use, and settings for each. If the third argument is simply
79
- * a string, it is used as the `storeTableName` property of the JSON
80
- * serialization.
81
- *
82
- * See the documentation for the DpcJson and DpcTabular types for more
83
- * information on how both of those modes can be configured.
84
- *
85
- * This method is asynchronous because it will await the creation of dedicated
86
- * new connections to the database. You will need to `await` a call to this
87
- * function or handle the return type natively as a Promise.
88
- * @param store The Store or MergeableStore to persist.
89
- * @param pglite The database connection that was returned from
90
- * `PGlite.create(...)`.
91
- * @param configOrStoreTableName A DatabasePersisterConfig to configure the
92
- * persistence mode (or a string to set the `storeTableName` property of the
93
- * JSON serialization).
94
- * @param onSqlCommand An optional handler called every time the Persister
95
- * executes a SQL command or query. This is suitable for logging persistence
96
- * behavior in a development environment.
97
- * @param onIgnoredError An optional handler for the errors that the Persister
98
- * would otherwise ignore when trying to save or load data. This is suitable for
99
- * debugging persistence issues in a development environment.
100
- * @returns A reference to the new PglitePersister object.
101
- * @example
102
- * This example creates a PglitePersister object and persists the Store to a
103
- * local PGlite database as a JSON serialization into the `my_tinybase`
104
- * table. It makes a change to the database directly and then reloads it back
105
- * into the Store.
106
- *
107
- * ```js
108
- * import {PGlite} from '@electric-sql/pglite';
109
- * import {createStore} from 'tinybase';
110
- * import {createPglitePersister} from 'tinybase/persisters/persister-pglite';
111
- *
112
- * const pglite = await PGlite.create();
113
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
114
- * const persister = await createPglitePersister(
115
- * store,
116
- * pglite,
117
- * 'my_tinybase',
118
- * );
119
- * await persister.save();
120
- * // Store will be saved to the database.
121
- *
122
- * console.log((await pglite.query('SELECT * FROM my_tinybase;')).rows);
123
- * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
124
- *
125
- * await pglite.query(`UPDATE my_tinybase SET store = $1 WHERE _id = '_';`, [
126
- * '[{"pets":{"felix":{"species":"cat"}}},{}]',
127
- * ]);
128
- *
129
- * await persister.load();
130
- * console.log(store.getTables());
131
- * // -> {pets: {felix: {species: 'cat'}}}
132
- *
133
- * persister.destroy();
134
- * await pglite.close();
135
- * ```
136
- * @example
137
- * This example creates a PglitePersister object and persists the Store to a
138
- * local PGlite database with tabular mapping.
139
- *
140
- * ```js
141
- * import {PGlite} from '@electric-sql/pglite';
142
- * import {createStore} from 'tinybase';
143
- * import {createPglitePersister} from 'tinybase/persisters/persister-pglite';
144
- *
145
- * const pglite = await PGlite.create();
146
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
147
- * const persister = await createPglitePersister(store, pglite, {
148
- * mode: 'tabular',
149
- * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
150
- * });
151
- *
152
- * await persister.save();
153
- * console.log((await pglite.query('SELECT * FROM pets;')).rows);
154
- * // -> [{_id: 'fido', species: '"dog"'}]
155
- * // Note that Cells and Values are JSON-encoded in PostgreSQL databases.
156
- *
157
- * await pglite.query(
158
- * `INSERT INTO pets (_id, species) VALUES ('felix', '"cat"')`,
159
- * );
160
- * await persister.load();
161
- * console.log(store.getTables());
162
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
163
- *
164
- * persister.destroy();
165
- * await pglite.query('DROP TABLE IF EXISTS pets');
166
- * await pglite.close();
167
- * ```
168
- * @category Creation
169
- * @since 5.2.0
170
- */
171
- export function createPglitePersister(
172
- store: Store | MergeableStore,
173
- pglite: PGlite,
174
- configOrStoreTableName?: DatabasePersisterConfig | string,
175
- onSqlCommand?: (sql: string, params?: any[]) => void,
176
- onIgnoredError?: (error: any) => void,
177
- ): Promise<PglitePersister>;
@@ -1,196 +0,0 @@
1
- /**
2
- * The persister-pglite module of the TinyBase project lets you save and load
3
- * Store data to and from a PGlite database (in an appropriate environment).
4
- * @see Database Persistence guide
5
- * @packageDocumentation
6
- * @module persister-pglite
7
- * @since 5.2.0
8
- */
9
- import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.cts';
10
- import type {
11
- OptionalSchemas,
12
- Store,
13
- } from '../../../store/with-schemas/index.d.cts';
14
- import type {
15
- DatabasePersisterConfig,
16
- Persister,
17
- Persists,
18
- } from '../../with-schemas/index.d.cts';
19
- import type {PGlite} from '@electric-sql/pglite';
20
-
21
- /**
22
- * The PglitePersister interface represents a Persister that lets you save and
23
- * load Store data to and from a local PGlite database.
24
- *
25
- * You should use the createPglitePersister function to create a PglitePersister
26
- * object.
27
- *
28
- * It is a minor extension to the Persister interface and simply provides an
29
- * extra getPglite method for accessing a reference to the database connection
30
- * the Store is being persisted to.
31
- * @category Persister
32
- * @since 5.2.0
33
- */
34
- export interface PglitePersister<Schemas extends OptionalSchemas>
35
- extends Persister<Schemas, Persists.StoreOrMergeableStore> {
36
- /**
37
- * The getPglite method returns a reference to the database connection the
38
- * Store is being persisted to.
39
- * @returns A reference to the database connection.
40
- * @example
41
- * This example creates a Persister object against a newly-created Store and
42
- * then gets the database connection back out again.
43
- *
44
- * ```js
45
- * import {PGlite} from '@electric-sql/pglite';
46
- * import {createStore} from 'tinybase';
47
- * import {createPglitePersister} from 'tinybase/persisters/persister-pglite';
48
- *
49
- * const pglite = await PGlite.create();
50
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
51
- * const persister = await createPglitePersister(
52
- * store,
53
- * pglite,
54
- * 'my_tinybase',
55
- * );
56
- *
57
- * console.log(persister.getPglite() == pglite);
58
- * // -> true
59
- *
60
- * persister.destroy();
61
- * await pglite.close();
62
- * ```
63
- * @category Getter
64
- * @since 5.2.0
65
- */
66
- getPglite(): PGlite;
67
- }
68
-
69
- /**
70
- * The createPglitePersister function creates a PglitePersister object that can
71
- * persist the Store to a local PGlite database.
72
- *
73
- * This has schema-based typing. The following is a simplified representation:
74
- *
75
- * ```ts override
76
- * createPglitePersister(
77
- * store: Store | MergeableStore,
78
- * pglite: PGlite,
79
- * configOrStoreTableName?: DatabasePersisterConfig | string,
80
- * onSqlCommand?: (sql: string, params?: any[]) => void,
81
- * onIgnoredError?: (error: any) => void,
82
- * ): Promise<PglitePersister>;
83
- * ```
84
- *
85
- * A PglitePersister supports regular Store objects, and can also be used to
86
- * persist the metadata of a MergeableStore when using the JSON serialization
87
- * mode, as described below.
88
- *
89
- * As well as providing a reference to the Store to persist, you must provide a
90
- * `pglite` parameter which identifies the database connection.
91
- *
92
- * A database Persister uses one of two modes: either a JSON serialization of
93
- * the whole Store stored in a single row of a table (the default), or a tabular
94
- * mapping of Table Ids to database table names and vice-versa).
95
- *
96
- * The third argument is a DatabasePersisterConfig object that configures which
97
- * of those modes to use, and settings for each. If the third argument is simply
98
- * a string, it is used as the `storeTableName` property of the JSON
99
- * serialization.
100
- *
101
- * See the documentation for the DpcJson and DpcTabular types for more
102
- * information on how both of those modes can be configured.
103
- *
104
- * This method is asynchronous because it will await the creation of dedicated
105
- * new connections to the database. You will need to `await` a call to this
106
- * function or handle the return type natively as a Promise.
107
- * @param store The Store or MergeableStore to persist.
108
- * @param pglite The database connection that was returned from
109
- * `PGlite.create(...)`.
110
- * @param configOrStoreTableName A DatabasePersisterConfig to configure the
111
- * persistence mode (or a string to set the `storeTableName` property of the
112
- * JSON serialization).
113
- * @param onSqlCommand An optional handler called every time the Persister
114
- * executes a SQL command or query. This is suitable for logging persistence
115
- * behavior in a development environment.
116
- * @param onIgnoredError An optional handler for the errors that the Persister
117
- * would otherwise ignore when trying to save or load data. This is suitable for
118
- * debugging persistence issues in a development environment.
119
- * @returns A reference to the new PglitePersister object.
120
- * @example
121
- * This example creates a PglitePersister object and persists the Store to a
122
- * local PGlite database as a JSON serialization into the `my_tinybase`
123
- * table. It makes a change to the database directly and then reloads it back
124
- * into the Store.
125
- *
126
- * ```js
127
- * import {PGlite} from '@electric-sql/pglite';
128
- * import {createStore} from 'tinybase';
129
- * import {createPglitePersister} from 'tinybase/persisters/persister-pglite';
130
- *
131
- * const pglite = await PGlite.create();
132
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
133
- * const persister = await createPglitePersister(
134
- * store,
135
- * pglite,
136
- * 'my_tinybase',
137
- * );
138
- * await persister.save();
139
- * // Store will be saved to the database.
140
- *
141
- * console.log((await pglite.query('SELECT * FROM my_tinybase;')).rows);
142
- * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
143
- *
144
- * await pglite.query(`UPDATE my_tinybase SET store = $1 WHERE _id = '_';`, [
145
- * '[{"pets":{"felix":{"species":"cat"}}},{}]',
146
- * ]);
147
- *
148
- * await persister.load();
149
- * console.log(store.getTables());
150
- * // -> {pets: {felix: {species: 'cat'}}}
151
- *
152
- * persister.destroy();
153
- * await pglite.close();
154
- * ```
155
- * @example
156
- * This example creates a PglitePersister object and persists the Store to a
157
- * local PGlite database with tabular mapping.
158
- *
159
- * ```js
160
- * import {PGlite} from '@electric-sql/pglite';
161
- * import {createStore} from 'tinybase';
162
- * import {createPglitePersister} from 'tinybase/persisters/persister-pglite';
163
- *
164
- * const pglite = await PGlite.create();
165
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
166
- * const persister = await createPglitePersister(store, pglite, {
167
- * mode: 'tabular',
168
- * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
169
- * });
170
- *
171
- * await persister.save();
172
- * console.log((await pglite.query('SELECT * FROM pets;')).rows);
173
- * // -> [{_id: 'fido', species: '"dog"'}]
174
- * // Note that Cells and Values are JSON-encoded in PostgreSQL databases.
175
- *
176
- * await pglite.query(
177
- * `INSERT INTO pets (_id, species) VALUES ('felix', '"cat"')`,
178
- * );
179
- * await persister.load();
180
- * console.log(store.getTables());
181
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
182
- *
183
- * persister.destroy();
184
- * await pglite.query('DROP TABLE IF EXISTS pets');
185
- * await pglite.close();
186
- * ```
187
- * @category Creation
188
- * @since 5.2.0
189
- */
190
- export function createPglitePersister<Schemas extends OptionalSchemas>(
191
- store: Store<Schemas> | MergeableStore<Schemas>,
192
- pglite: PGlite,
193
- configOrStoreTableName?: DatabasePersisterConfig<Schemas> | string,
194
- onSqlCommand?: (sql: string, params?: any[]) => void,
195
- onIgnoredError?: (error: any) => void,
196
- ): Promise<PglitePersister<Schemas>>;
@@ -1,166 +0,0 @@
1
- /**
2
- * The persister-postgres module of the TinyBase project lets you save and load
3
- * Store data to and from a PostgreSQL database (in an appropriate environment).
4
- * @see Database Persistence guide
5
- * @packageDocumentation
6
- * @module persister-postgres
7
- * @since 5.2.0
8
- */
9
- import type {MergeableStore} from '../../mergeable-store/index.d.cts';
10
- import type {Store} from '../../store/index.d.cts';
11
- import type {DatabasePersisterConfig, Persister, Persists} from '../index.d.cts';
12
- import type {Sql} from 'postgres';
13
-
14
- /**
15
- * The PostgresPersister interface represents a Persister that lets you save and
16
- * load Store data to and from a local PostgreSQL database.
17
- *
18
- * You should use the createPostgresPersister function to create a
19
- * PostgresPersister object.
20
- *
21
- * It is a minor extension to the Persister interface and simply provides an
22
- * extra getSql method for accessing a reference to the database connection the
23
- * Store is being persisted to.
24
- * @category Persister
25
- * @since 5.2.0
26
- */
27
- export interface PostgresPersister
28
- extends Persister<Persists.StoreOrMergeableStore> {
29
- /**
30
- * The getSql method returns a reference to the database connection the Store
31
- * is being persisted to.
32
- * @returns A reference to the database connection.
33
- * @example
34
- * This example creates a Persister object against a newly-created Store and
35
- * then gets the database connection back out again.
36
- *
37
- * ```js
38
- * import postgres from 'postgres';
39
- * import {createStore} from 'tinybase';
40
- * import {createPostgresPersister} from 'tinybase/persisters/persister-postgres';
41
- *
42
- * const sql = postgres('postgres://localhost:5432/tinybase');
43
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
44
- * const persister = await createPostgresPersister(store, sql, 'my_tinybase');
45
- *
46
- * console.log(persister.getSql() == sql);
47
- * // -> true
48
- *
49
- * persister.destroy();
50
- * await sql.end();
51
- * ```
52
- * @category Getter
53
- * @since 5.2.0
54
- */
55
- getSql(): Sql;
56
- }
57
-
58
- /**
59
- * The createPostgresPersister function creates a PostgresPersister object that
60
- * can persist the Store to a local PostgreSQL database.
61
- *
62
- * A PostgresPersister supports regular Store objects, and can also be used to
63
- * persist the metadata of a MergeableStore when using the JSON serialization
64
- * mode, as described below.
65
- *
66
- * As well as providing a reference to the Store to persist, you must provide a
67
- * `sql` parameter which identifies the database connection.
68
- *
69
- * A database Persister uses one of two modes: either a JSON serialization of
70
- * the whole Store stored in a single row of a table (the default), or a tabular
71
- * mapping of Table Ids to database table names and vice-versa).
72
- *
73
- * The third argument is a DatabasePersisterConfig object that configures which
74
- * of those modes to use, and settings for each. If the third argument is simply
75
- * a string, it is used as the `storeTableName` property of the JSON
76
- * serialization.
77
- *
78
- * See the documentation for the DpcJson and DpcTabular types for more
79
- * information on how both of those modes can be configured.
80
- *
81
- * This method is asynchronous because it will await the creation of dedicated
82
- * new connections to the database. You will need to `await` a call to this
83
- * function or handle the return type natively as a Promise.
84
- * @param store The Store or MergeableStore to persist.
85
- * @param sql The database connection that was returned from `postgres(...)`.
86
- * @param configOrStoreTableName A DatabasePersisterConfig to configure the
87
- * persistence mode (or a string to set the `storeTableName` property of the
88
- * JSON serialization).
89
- * @param onSqlCommand An optional handler called every time the Persister
90
- * executes a SQL command or query. This is suitable for logging persistence
91
- * behavior in a development environment.
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 PostgresPersister object.
96
- * @example
97
- * This example creates a PostgresPersister object and persists the Store to a
98
- * local PostgreSQL database as a JSON serialization into the `my_tinybase`
99
- * table. It makes a change to the database directly and then reloads it back
100
- * into the Store.
101
- *
102
- * ```js
103
- * import postgres from 'postgres';
104
- * import {createStore} from 'tinybase';
105
- * import {createPostgresPersister} from 'tinybase/persisters/persister-postgres';
106
- *
107
- * const sql = postgres('postgres://localhost:5432/tinybase');
108
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
109
- * const persister = await createPostgresPersister(store, sql, 'my_tinybase');
110
- *
111
- * await persister.save();
112
- * // Store will be saved to the database.
113
- *
114
- * console.log(await sql`SELECT * FROM my_tinybase;`);
115
- * // -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
116
- *
117
- * const json = '[{"pets":{"felix":{"species":"cat"}}},{}]';
118
- * await sql`UPDATE my_tinybase SET store = ${json} WHERE _id = '_';`;
119
- *
120
- * await persister.load();
121
- * console.log(store.getTables());
122
- * // -> {pets: {felix: {species: 'cat'}}}
123
- *
124
- * persister.destroy();
125
- * await sql.end();
126
- * ```
127
- * @example
128
- * This example creates a PostgresPersister object and persists the Store to a
129
- * local PostgreSQL database with tabular mapping.
130
- *
131
- * ```js
132
- * import postgres from 'postgres';
133
- * import {createStore} from 'tinybase';
134
- * import {createPostgresPersister} from 'tinybase/persisters/persister-postgres';
135
- *
136
- * const sql = postgres('postgres://localhost:5432/tinybase');
137
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
138
- * const persister = await createPostgresPersister(store, sql, {
139
- * mode: 'tabular',
140
- * tables: {load: {pets: 'pets'}, save: {pets: 'pets'}},
141
- * });
142
- *
143
- * await persister.save();
144
- * console.log(await sql`SELECT * FROM pets;`);
145
- * // -> [{_id: 'fido', species: '"dog"'}]
146
- * // Note that Cells and Values are JSON-encoded in PostgreSQL databases.
147
- *
148
- * await sql`INSERT INTO pets (_id, species) VALUES ('felix', '"cat"')`;
149
- * await persister.load();
150
- * console.log(store.getTables());
151
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
152
- *
153
- * persister.destroy();
154
- * await sql`DROP TABLE IF EXISTS pets`;
155
- * await sql.end();
156
- * ```
157
- * @category Creation
158
- * @since 5.2.0
159
- */
160
- export function createPostgresPersister(
161
- store: Store | MergeableStore,
162
- sql: Sql,
163
- configOrStoreTableName?: DatabasePersisterConfig | string,
164
- onSqlCommand?: (sql: string, params?: any[]) => void,
165
- onIgnoredError?: (error: any) => void,
166
- ): Promise<PostgresPersister>;