tinybase 6.1.0-beta.1 → 6.1.0-beta.3

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 (108) hide show
  1. package/@types/common/index.d.ts +4 -4
  2. package/@types/common/with-schemas/index.d.ts +4 -4
  3. package/@types/mergeable-store/index.d.ts +45 -20
  4. package/@types/mergeable-store/with-schemas/index.d.ts +49 -21
  5. package/@types/persisters/index.d.ts +1 -1
  6. package/@types/persisters/with-schemas/index.d.ts +1 -1
  7. package/index.js +19 -11
  8. package/indexes/index.js +15 -3
  9. package/indexes/with-schemas/index.js +15 -3
  10. package/mergeable-store/index.js +4 -8
  11. package/mergeable-store/with-schemas/index.js +4 -8
  12. package/min/index.js +1 -1
  13. package/min/index.js.gz +0 -0
  14. package/min/indexes/index.js +1 -1
  15. package/min/indexes/index.js.gz +0 -0
  16. package/min/indexes/with-schemas/index.js +1 -1
  17. package/min/indexes/with-schemas/index.js.gz +0 -0
  18. package/min/mergeable-store/index.js +1 -1
  19. package/min/mergeable-store/index.js.gz +0 -0
  20. package/min/mergeable-store/with-schemas/index.js +1 -1
  21. package/min/mergeable-store/with-schemas/index.js.gz +0 -0
  22. package/min/with-schemas/index.js +1 -1
  23. package/min/with-schemas/index.js.gz +0 -0
  24. package/package.json +9 -9
  25. package/readme.md +2 -2
  26. package/with-schemas/index.js +19 -11
  27. package/@types/_internal/queries/index.d.cts +0 -0
  28. package/@types/_internal/queries/with-schemas/index.d.cts +0 -22
  29. package/@types/_internal/store/index.d.cts +0 -3
  30. package/@types/_internal/store/with-schemas/index.d.cts +0 -106
  31. package/@types/_internal/ui-react/index.d.cts +0 -0
  32. package/@types/_internal/ui-react/with-schemas/index.d.cts +0 -1130
  33. package/@types/checkpoints/index.d.cts +0 -1059
  34. package/@types/checkpoints/with-schemas/index.d.cts +0 -1151
  35. package/@types/common/index.d.cts +0 -158
  36. package/@types/common/with-schemas/index.d.cts +0 -158
  37. package/@types/index.d.cts +0 -17
  38. package/@types/indexes/index.d.cts +0 -1064
  39. package/@types/indexes/with-schemas/index.d.cts +0 -1210
  40. package/@types/mergeable-store/index.d.cts +0 -1139
  41. package/@types/mergeable-store/with-schemas/index.d.cts +0 -1628
  42. package/@types/metrics/index.d.cts +0 -917
  43. package/@types/metrics/with-schemas/index.d.cts +0 -1004
  44. package/@types/persisters/index.d.cts +0 -1877
  45. package/@types/persisters/persister-automerge/index.d.cts +0 -165
  46. package/@types/persisters/persister-automerge/with-schemas/index.d.cts +0 -180
  47. package/@types/persisters/persister-browser/index.d.cts +0 -185
  48. package/@types/persisters/persister-browser/with-schemas/index.d.cts +0 -208
  49. package/@types/persisters/persister-cr-sqlite-wasm/index.d.cts +0 -159
  50. package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.cts +0 -178
  51. package/@types/persisters/persister-durable-object-storage/index.d.cts +0 -122
  52. package/@types/persisters/persister-durable-object-storage/with-schemas/index.d.cts +0 -136
  53. package/@types/persisters/persister-electric-sql/index.d.cts +0 -185
  54. package/@types/persisters/persister-electric-sql/with-schemas/index.d.cts +0 -204
  55. package/@types/persisters/persister-expo-sqlite/index.d.cts +0 -186
  56. package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.cts +0 -205
  57. package/@types/persisters/persister-file/index.d.cts +0 -94
  58. package/@types/persisters/persister-file/with-schemas/index.d.cts +0 -107
  59. package/@types/persisters/persister-indexed-db/index.d.cts +0 -120
  60. package/@types/persisters/persister-indexed-db/with-schemas/index.d.cts +0 -135
  61. package/@types/persisters/persister-libsql/index.d.cts +0 -158
  62. package/@types/persisters/persister-libsql/with-schemas/index.d.cts +0 -177
  63. package/@types/persisters/persister-partykit-client/index.d.cts +0 -195
  64. package/@types/persisters/persister-partykit-client/with-schemas/index.d.cts +0 -210
  65. package/@types/persisters/persister-partykit-server/index.d.cts +0 -650
  66. package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +0 -695
  67. package/@types/persisters/persister-pglite/index.d.cts +0 -177
  68. package/@types/persisters/persister-pglite/with-schemas/index.d.cts +0 -196
  69. package/@types/persisters/persister-postgres/index.d.cts +0 -166
  70. package/@types/persisters/persister-postgres/with-schemas/index.d.cts +0 -185
  71. package/@types/persisters/persister-powersync/index.d.cts +0 -174
  72. package/@types/persisters/persister-powersync/with-schemas/index.d.cts +0 -193
  73. package/@types/persisters/persister-remote/index.d.cts +0 -117
  74. package/@types/persisters/persister-remote/with-schemas/index.d.cts +0 -133
  75. package/@types/persisters/persister-sqlite-wasm/index.d.cts +0 -175
  76. package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.cts +0 -195
  77. package/@types/persisters/persister-sqlite3/index.d.cts +0 -176
  78. package/@types/persisters/persister-sqlite3/with-schemas/index.d.cts +0 -195
  79. package/@types/persisters/persister-yjs/index.d.cts +0 -161
  80. package/@types/persisters/persister-yjs/with-schemas/index.d.cts +0 -176
  81. package/@types/persisters/with-schemas/index.d.cts +0 -2054
  82. package/@types/queries/index.d.cts +0 -3695
  83. package/@types/queries/with-schemas/index.d.cts +0 -4016
  84. package/@types/relationships/index.d.cts +0 -1320
  85. package/@types/relationships/with-schemas/index.d.cts +0 -1474
  86. package/@types/store/index.d.cts +0 -7598
  87. package/@types/store/with-schemas/index.d.cts +0 -9278
  88. package/@types/synchronizers/index.d.cts +0 -485
  89. package/@types/synchronizers/synchronizer-broadcast-channel/index.d.cts +0 -121
  90. package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.cts +0 -137
  91. package/@types/synchronizers/synchronizer-local/index.d.cts +0 -95
  92. package/@types/synchronizers/synchronizer-local/with-schemas/index.d.cts +0 -114
  93. package/@types/synchronizers/synchronizer-ws-client/index.d.cts +0 -160
  94. package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.cts +0 -179
  95. package/@types/synchronizers/synchronizer-ws-server/index.d.cts +0 -736
  96. package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.cts +0 -765
  97. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.cts +0 -311
  98. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.cts +0 -349
  99. package/@types/synchronizers/synchronizer-ws-server-simple/index.d.cts +0 -144
  100. package/@types/synchronizers/synchronizer-ws-server-simple/with-schemas/index.d.cts +0 -144
  101. package/@types/synchronizers/with-schemas/index.d.cts +0 -503
  102. package/@types/ui-react/index.d.cts +0 -16640
  103. package/@types/ui-react/with-schemas/index.d.cts +0 -17281
  104. package/@types/ui-react-dom/index.d.cts +0 -1862
  105. package/@types/ui-react-dom/with-schemas/index.d.cts +0 -1994
  106. package/@types/ui-react-inspector/index.d.cts +0 -79
  107. package/@types/ui-react-inspector/with-schemas/index.d.cts +0 -1985
  108. 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 {PGlite} from '@electric-sql/pglite';
10
- import type {MergeableStore} from '../../mergeable-store/index.d.cts';
11
- import type {Store} from '../../store/index.d.cts';
12
- import type {DatabasePersisterConfig, Persister, Persists} from '../index.d.cts';
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 {PGlite} from '@electric-sql/pglite';
10
- import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.cts';
11
- import type {
12
- OptionalSchemas,
13
- Store,
14
- } from '../../../store/with-schemas/index.d.cts';
15
- import type {
16
- DatabasePersisterConfig,
17
- Persister,
18
- Persists,
19
- } from '../../with-schemas/index.d.cts';
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 {Sql} from 'postgres';
10
- import type {MergeableStore} from '../../mergeable-store/index.d.cts';
11
- import type {Store} from '../../store/index.d.cts';
12
- import type {DatabasePersisterConfig, Persister, Persists} from '../index.d.cts';
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>;