|
1 | 1 | /** |
2 | | - * This persister uses Cloudflare's SQLite storage backend, which offers significantly |
3 | | - * better pricing compared to the Key-value storage backend. The SQLite storage backend |
4 | | - * is Cloudflare's recommended storage option for new Durable Object namespaces. |
5 | | - * |
6 | | - * **Important:** Before using this persister, you must configure your Durable Object |
7 | | - * class to use SQLite storage by adding a migration to your `wrangler.toml` or |
8 | | - * `wrangler.json` configuration file. Use `new_sqlite_classes` in your migration |
9 | | - * configuration to enable SQLite storage for your Durable Object class. |
10 | | - * |
| 2 | + * The persister-durable-object-sql-storage module of the TinyBase project lets |
| 3 | + * you save and load Store data to and from Cloudflare Durable Object SQLite |
| 4 | + * storage (in an appropriate environment). |
| 5 | + * |
| 6 | + * Cloudflare's SQLite storage backend for Durable Objects offers significantly |
| 7 | + * better pricing compared to the key-value storage backend. The SQLite storage |
| 8 | + * backend is Cloudflare's recommended storage option for new Durable Object |
| 9 | + * namespaces. |
| 10 | + * |
| 11 | + * **Important:** Before using this persister, you must configure your Durable |
| 12 | + * Object class to use SQLite storage by adding a migration to your |
| 13 | + * `wrangler.toml` or `wrangler.json` configuration file. Use |
| 14 | + * `new_sqlite_classes` in your migration configuration to enable SQLite storage |
| 15 | + * for your Durable Object class. |
| 16 | + * |
| 17 | + * See [Cloudflare's |
| 18 | + * documentation](https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/) |
| 19 | + * for more details. |
11 | 20 | * @see Cloudflare Durable Objects guide |
12 | 21 | * @see Persistence guides |
13 | | - * @see {@link https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/ | Durable Objects migrations} |
14 | 22 | * @packageDocumentation |
15 | 23 | * @module persister-durable-object-sql-storage |
16 | | - * @since v6.2.0 |
| 24 | + * @since v6.3.0 |
17 | 25 | */ |
18 | 26 | /// persister-durable-object-sql-storage |
19 | 27 |
|
20 | 28 | /** |
21 | | - * The DpcFragmented type represents the configuration for fragmented persistence mode |
22 | | - * in a DurableObjectSqlStoragePersister. |
| 29 | + * The DpcFragmented type represents the configuration for fragmented |
| 30 | + * persistence mode in a DurableObjectSqlStoragePersister. |
23 | 31 | * |
24 | 32 | * This mode stores each table, row, cell, and value as separate database rows, |
25 | | - * avoiding Cloudflare's 2MB row limit that can be hit with large stores in JSON mode. |
26 | | - * While this creates more database writes, it provides better scalability for |
27 | | - * larger datasets. |
28 | | - * |
| 33 | + * avoiding Cloudflare's 2MB row limit that can be hit with large stores in JSON |
| 34 | + * mode. While this creates more database writes, it provides better scalability |
| 35 | + * for larger datasets. |
29 | 36 | * @example |
30 | 37 | * This example shows how to configure a DurableObjectSqlStoragePersister to use |
31 | | - * fragmented mode with a custom storage prefix: |
| 38 | + * fragmented mode with a custom storage prefix. |
| 39 | + * |
| 40 | + * ```js yolo |
| 41 | + * import {createMergeableStore} from 'tinybase'; |
| 42 | + * import {createDurableObjectSqlStoragePersister} from 'tinybase/persisters/persister-durable-object-sql-storage'; |
| 43 | + * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object'; |
32 | 44 | * |
33 | | - * ```js |
34 | 45 | * const config = { |
35 | 46 | * mode: 'fragmented', |
36 | | - * storagePrefix: 'my_app_' |
| 47 | + * storagePrefix: 'my_app_', |
37 | 48 | * }; |
38 | 49 | * |
39 | | - * const persister = createDurableObjectSqlStoragePersister( |
40 | | - * store, |
41 | | - * ctx.storage.sql, |
42 | | - * config |
43 | | - * ); |
| 50 | + * export class MyDurableObject extends WsServerDurableObject { |
| 51 | + * createPersister() { |
| 52 | + * const store = createMergeableStore(); |
| 53 | + * const persister = createDurableObjectSqlStoragePersister( |
| 54 | + * store, |
| 55 | + * this.ctx.storage.sql, |
| 56 | + * config, |
| 57 | + * ); |
| 58 | + * return persister; |
| 59 | + * } |
| 60 | + * } |
44 | 61 | * ``` |
45 | | - * |
46 | 62 | * @category Configuration |
47 | | - * @since v6.2.0 |
| 63 | + * @since v6.3.0 |
48 | 64 | */ |
49 | 65 | /// DpcFragmented |
50 | 66 | { |
51 | 67 | /** |
52 | | - * The mode property must be set to 'fragmented' to enable fragmented persistence mode. |
53 | | - * @since v6.2.0 |
| 68 | + * The mode property must be set to 'fragmented' to enable fragmented |
| 69 | + * persistence mode. |
| 70 | + * @category Configuration |
| 71 | + * @since v6.3.0 |
54 | 72 | */ |
55 | 73 | /// DpcFragmented.mode |
56 | 74 | /** |
57 | | - * The storagePrefix property lets you specify an optional prefix for the database |
58 | | - * table names used in fragmented mode. |
59 | | - * |
60 | | - * This is useful when you have multiple stores or applications sharing the same |
61 | | - * Durable Object SQL storage and want to avoid table name conflicts. |
| 75 | + * The storagePrefix property lets you specify an optional prefix for the |
| 76 | + * database table names used in fragmented mode. |
62 | 77 | * |
63 | | - * The prefix will be sanitized to only include alphanumeric characters and underscores. |
64 | | - * For example, a prefix of 'my-app!' becomes 'my_app_'. |
| 78 | + * This is useful when you have multiple stores or applications sharing the |
| 79 | + * same Durable Object SQL storage and want to avoid table name conflicts. |
65 | 80 | * |
| 81 | + * The prefix will be sanitized to only include alphanumeric characters and |
| 82 | + * underscores. For example, a prefix of 'my-app!' becomes 'my_app_'. |
66 | 83 | * @example |
67 | | - * This example shows how the storagePrefix affects table names: |
68 | | - * |
69 | | - * ```js |
70 | | - * // With storagePrefix: 'user_data_' |
71 | | - * // Creates tables: user_data_tinybase_tables, user_data_tinybase_values |
72 | | - * |
73 | | - * const config = { |
| 84 | + * This example shows a configuration using the storagePrefix setting. With a |
| 85 | + * `storagePrefix` of 'user_data_', it creates `user_data_tinybase_tables` and |
| 86 | + * `user_data_tinybase_values` tables. |
| 87 | + * ```json |
| 88 | + * { |
74 | 89 | * mode: 'fragmented', |
75 | | - * storagePrefix: 'user_data_' |
| 90 | + * storagePrefix: 'user_data_', |
76 | 91 | * }; |
77 | 92 | * ``` |
78 | | - * |
79 | | - * @since v6.2.0 |
| 93 | + * @category Configuration |
| 94 | + * @since v6.3.0 |
80 | 95 | */ |
81 | 96 | /// DpcFragmented.storagePrefix |
82 | 97 | } |
|
85 | 100 | * The DurableObjectSqlDatabasePersisterConfig type represents the union of all |
86 | 101 | * possible configuration types for a DurableObjectSqlStoragePersister. |
87 | 102 | * |
88 | | - * This allows the persister to support multiple persistence modes: |
89 | | - * - JSON mode (via DpcJson): Stores the entire Store as JSON in a single row |
90 | | - * - Fragmented mode (via DpcFragmented): Stores each piece of data as separate rows |
91 | | - * |
| 103 | + * This allows the persister to support multiple persistence modes. |
| 104 | + * - JSON mode (via DpcJson): Stores the entire Store as JSON in a single row. |
| 105 | + * - Fragmented mode (via DpcFragmented): Stores each piece of data as separate |
| 106 | + * rows. |
92 | 107 | * @example |
93 | | - * This example shows the different configuration options: |
94 | | - * |
95 | | - * ```js |
| 108 | + * These examples show some different configuration options. |
| 109 | + * ```json |
96 | 110 | * // JSON mode (default) |
97 | | - * const jsonConfig = { |
| 111 | + * { |
98 | 112 | * mode: 'json', |
99 | | - * storeTableName: 'my_store' |
| 113 | + * storeTableName: 'my_store', |
100 | 114 | * }; |
101 | 115 | * |
102 | 116 | * // Fragmented mode |
103 | | - * const fragmentedConfig = { |
| 117 | + * { |
104 | 118 | * mode: 'fragmented', |
105 | | - * storagePrefix: 'app_' |
| 119 | + * storagePrefix: 'app_', |
106 | 120 | * }; |
107 | | - * |
108 | 121 | * ``` |
109 | | - * |
110 | 122 | * @category Configuration |
111 | | - * @since v6.2.0 |
| 123 | + * @since v6.3.0 |
112 | 124 | */ |
113 | 125 | /// DurableObjectSqlDatabasePersisterConfig |
114 | 126 |
|
115 | 127 | /** |
116 | | - * The DurableObjectSqlStoragePersister interface represents a Persister that lets |
117 | | - * you save and load Store data to and from Cloudflare Durable Object SQL storage. |
| 128 | + * The DurableObjectSqlStoragePersister interface represents a Persister that |
| 129 | + * lets you save and load Store data to and from Cloudflare Durable Object SQL |
| 130 | + * storage. |
118 | 131 | * |
119 | | - * You should use the createDurableObjectSqlStoragePersister function to create a |
120 | | - * DurableObjectSqlStoragePersister object, most likely within the createPersister |
121 | | - * method of a WsServerDurableObject. |
| 132 | + * You should use the createDurableObjectSqlStoragePersister function to create |
| 133 | + * a DurableObjectSqlStoragePersister object, most likely within the |
| 134 | + * createPersister method of a WsServerDurableObject. |
122 | 135 | * |
123 | 136 | * It is a minor extension to the Persister interface and simply provides an |
124 | | - * extra getSqlStorage method for accessing a reference to the SQL storage that the |
125 | | - * Store is being persisted to. |
| 137 | + * extra getSqlStorage method for accessing a reference to the SQL storage that |
| 138 | + * the Store is being persisted to. |
126 | 139 | * @category Persister |
127 | | - * @since v6.2.0 |
| 140 | + * @since v6.3.0 |
128 | 141 | */ |
129 | 142 | /// DurableObjectSqlStoragePersister |
130 | 143 | { |
131 | 144 | /** |
132 | | - * The getSqlStorage method returns a reference to the SQL storage that the Store is |
133 | | - * being persisted to. |
| 145 | + * The getSqlStorage method returns a reference to the SQL storage that the |
| 146 | + * Store is being persisted to. |
134 | 147 | * @returns The reference to the SQL storage. |
135 | 148 | * @example |
136 | 149 | * This example creates a Persister object against a newly-created Store |
|
157 | 170 | * } |
158 | 171 | * ``` |
159 | 172 | * @category Getter |
160 | | - * @since v6.2.0 |
| 173 | + * @since v6.3.0 |
161 | 174 | */ |
162 | 175 | /// DurableObjectSqlStoragePersister.getSqlStorage |
163 | 176 | } |
164 | 177 | /** |
165 | 178 | * The createDurableObjectSqlStoragePersister function creates a |
166 | | - * DurableObjectSqlStoragePersister object that can persist the Store to and from |
167 | | - * Cloudflare Durable Object SQLite storage. |
| 179 | + * DurableObjectSqlStoragePersister object that can persist the Store to and |
| 180 | + * from Cloudflare Durable Object SQLite storage. |
168 | 181 | * |
169 | 182 | * You will mostly use this within the createPersister method of a |
170 | 183 | * WsServerDurableObject. |
171 | 184 | * |
172 | | - * This persister uses Cloudflare's SQLite storage backend, which provides better |
173 | | - * pricing and performance compared to the legacy Key-value storage backend. |
174 | | - * |
175 | | - * **Important Prerequisites:** |
176 | | - * Before using this persister, you must configure your Durable Object class to use |
177 | | - * SQLite storage by adding a migration to your Wrangler configuration file. In your |
178 | | - * `wrangler.toml`, add: |
| 185 | + * This persister uses Cloudflare's SQLite storage backend, which provides |
| 186 | + * better pricing and performance compared to the legacy Key-value storage |
| 187 | + * backend. |
179 | 188 | * |
| 189 | + * **Important Prerequisites:** Before using this persister, you must configure |
| 190 | + * your Durable Object class to use SQLite storage by adding a migration to your |
| 191 | + * Wrangler configuration file. In your `wrangler.toml`, add the following. |
180 | 192 | * ```toml |
181 | 193 | * [[migrations]] |
182 | 194 | * tag = "v1" |
|
196 | 208 | * } |
197 | 209 | * ``` |
198 | 210 | * |
199 | | - * For more details on Durable Object migrations, see the |
200 | | - * {@link https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/ | Cloudflare documentation}. |
| 211 | + * For more details on Durable Object migrations, see the [Cloudflare |
| 212 | + * documentation](https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/). |
201 | 213 | * |
202 | 214 | * A database Persister uses one of two modes: either a JSON serialization of |
203 | 215 | * the whole Store stored in a single row of a table (the default), a fragmented |
|
206 | 218 | * |
207 | 219 | * **JSON Mode (Default)**: Stores the entire Store as JSON in a single database |
208 | 220 | * row. This is efficient for smaller stores but may hit Cloudflare's 2MB row |
209 | | - * limit for very large stores. Uses fewer database writes. |
| 221 | + * limit for very large stores and uses fewer database writes. |
210 | 222 | * |
211 | | - * **Fragmented Mode**: Stores each table, row, cell, and value as separate database |
212 | | - * rows. Use this mode if you're concerned about hitting Cloudflare's 2MB row |
213 | | - * limit with large stores in JSON mode. This mode creates more database writes |
214 | | - * but avoids row size limitations. |
| 223 | + * **Fragmented Mode**: Stores each table, row, cell, and value as separate |
| 224 | + * database rows. Use this mode if you're concerned about hitting Cloudflare's |
| 225 | + * 2MB row limit with large stores in JSON mode. This mode creates more database |
| 226 | + * writes but avoids row size limitations. |
215 | 227 | * |
216 | 228 | * The third argument is a DatabasePersisterConfig object that configures which |
217 | 229 | * of those modes to use, and settings for each. If the third argument is simply |
218 | 230 | * a string, it is used as the `storeTableName` property of the JSON |
219 | 231 | * serialization. If it is the string 'fragmented', it enables fragmented mode. |
220 | 232 | * |
221 | | - * See the documentation for the DpcJson, DpcFragmented, and DpcTabular types for more |
222 | | - * information on how all of those modes can be configured. |
| 233 | + * See the documentation for the DpcJson, DpcFragmented, and DpcTabular types |
| 234 | + * for more information on how all of those modes can be configured. |
223 | 235 | * |
224 | | - * As well as providing a reference to the Store or MergeableStore to persist, you must |
225 | | - * provide a `sqlStorage` parameter which identifies the Durable Object SQLite storage to |
226 | | - * persist it to. |
| 236 | + * As well as providing a reference to the Store or MergeableStore to persist, |
| 237 | + * you must provide a `sqlStorage` parameter which identifies the Durable Object |
| 238 | + * SQLite storage to persist it to. |
227 | 239 | * @param store The Store or MergeableStore to persist. |
228 | 240 | * @param sqlStorage The Durable Object SQL storage to persist the Store to. |
229 | 241 | * @param configOrStoreTableName A DatabasePersisterConfig to configure the |
|
237 | 249 | * debugging persistence issues in a development environment. |
238 | 250 | * @returns A reference to the new DurableObjectSqlStoragePersister object. |
239 | 251 | * @example |
240 | | - * This example creates a DurableObjectSqlStoragePersister object and persists the |
241 | | - * Store to Durable Object SQLite storage as a JSON serialization into the default |
242 | | - * `tinybase` table. It uses this within the createPersister method of a |
| 252 | + * This example creates a DurableObjectSqlStoragePersister object and persists |
| 253 | + * the Store to Durable Object SQLite storage as a JSON serialization into the |
| 254 | + * default `tinybase` table. It uses this within the createPersister method of a |
243 | 255 | * WsServerDurableObject instance. |
244 | 256 | * |
245 | 257 | * ```js yolo |
|
282 | 294 | * } |
283 | 295 | * ``` |
284 | 296 | * @example |
285 | | - * This example creates a DurableObjectSqlStoragePersister object using fragmented |
286 | | - * mode to avoid Cloudflare's 2MB row limit for large stores. |
| 297 | + * This example creates a DurableObjectSqlStoragePersister object using |
| 298 | + * fragmented mode to avoid Cloudflare's 2MB row limit for large stores. |
287 | 299 | * |
288 | 300 | * ```js yolo |
289 | 301 | * import {createMergeableStore} from 'tinybase'; |
|
303 | 315 | * } |
304 | 316 | * ``` |
305 | 317 | * @example |
306 | | - * This example creates a DurableObjectSqlStoragePersister object using fragmented |
307 | | - * mode with a custom storage prefix. |
| 318 | + * This example creates a DurableObjectSqlStoragePersister object using |
| 319 | + * fragmented mode with a custom storage prefix. |
308 | 320 | * |
309 | 321 | * ```js yolo |
310 | 322 | * import {createMergeableStore} from 'tinybase'; |
|
324 | 336 | * } |
325 | 337 | * ``` |
326 | 338 | * @category Creation |
327 | | - * @since v6.2.0 |
| 339 | + * @since v6.3.0 |
328 | 340 | */ |
329 | 341 | /// createDurableObjectSqlStoragePersister |
0 commit comments