|
107 | 107 | * MergeableStore objects. When used with a MergeableStore, it can persist the |
108 | 108 | * complete CRDT metadata required for proper merging operations. |
109 | 109 | * |
110 | | - * The DurableObjectSqlStoragePersister uses SQL tables to store TinyBase data, |
111 | | - * creating structured tables for tables and values with proper CRDT metadata |
112 | | - * including timestamps and hashes for merging operations. |
| 110 | + * A database Persister uses one of three modes: either a JSON serialization of |
| 111 | + * the whole Store stored in a single row of a table (the default), a key-value |
| 112 | + * mode that stores each piece of data separately to avoid Cloudflare's 2MB row |
| 113 | + * limit, or a tabular mapping of Table Ids to database table names and vice-versa). |
113 | 114 | * |
114 | | - * A database Persister uses one of two modes: either a JSON serialization of |
115 | | - * the whole Store stored in a single row of a table (the default), or a tabular |
116 | | - * mapping of Table Ids to database table names and vice-versa). |
| 115 | + * **JSON Mode (Default)**: Stores the entire Store as JSON in a single database |
| 116 | + * row. This is efficient for smaller stores but may hit Cloudflare's 2MB row |
| 117 | + * limit for very large stores. Uses fewer database writes. |
| 118 | + * |
| 119 | + * **Key-Value Mode**: Stores each table, row, cell, and value as separate database |
| 120 | + * rows. Use this mode if you're concerned about hitting Cloudflare's 2MB row |
| 121 | + * limit with large stores in JSON mode. This mode creates more database writes |
| 122 | + * but avoids row size limitations. |
| 123 | + * |
| 124 | + * **Tabular Mode**: Maps TinyBase tables directly to database tables for |
| 125 | + * traditional relational database usage. |
117 | 126 | * |
118 | 127 | * The third argument is a DatabasePersisterConfig object that configures which |
119 | 128 | * of those modes to use, and settings for each. If the third argument is simply |
120 | 129 | * a string, it is used as the `storeTableName` property of the JSON |
121 | | - * serialization. |
| 130 | + * serialization. If it is the string 'key-value', it enables key-value mode. |
122 | 131 | * |
123 | | - * See the documentation for the DpcJson and DpcTabular types for more |
124 | | - * information on how both of those modes can be configured. |
| 132 | + * See the documentation for the DpcJson, DpcKeyValue, and DpcTabular types for more |
| 133 | + * information on how all of those modes can be configured. |
125 | 134 | * |
126 | 135 | * As well as providing a reference to the Store or MergeableStore to persist, you must |
127 | 136 | * provide a `sqlStorage` parameter which identifies the Durable Object SQLite storage to |
|
130 | 139 | * @param sqlStorage The Durable Object SQL storage to persist the Store to. |
131 | 140 | * @param configOrStoreTableName A DatabasePersisterConfig to configure the |
132 | 141 | * persistence mode (or a string to set the `storeTableName` property of the |
133 | | - * JSON serialization). |
| 142 | + * JSON serialization, or 'key-value' to enable key-value mode). |
134 | 143 | * @param onSqlCommand An optional handler called every time the Persister |
135 | 144 | * executes a SQL command or query. This is suitable for logging persistence |
136 | 145 | * behavior in a development environment. |
|
184 | 193 | * } |
185 | 194 | * ``` |
186 | 195 | * @example |
| 196 | + * This example creates a DurableObjectSqlStoragePersister object using key-value |
| 197 | + * mode to avoid Cloudflare's 2MB row limit for large stores. |
| 198 | + * |
| 199 | + * ```js yolo |
| 200 | + * import {createMergeableStore} from 'tinybase'; |
| 201 | + * import {createDurableObjectSqlStoragePersister} from 'tinybase/persisters/persister-durable-object-sql-storage'; |
| 202 | + * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object'; |
| 203 | + * |
| 204 | + * export class MyDurableObject extends WsServerDurableObject { |
| 205 | + * createPersister() { |
| 206 | + * const store = createMergeableStore(); |
| 207 | + * const persister = createDurableObjectSqlStoragePersister( |
| 208 | + * store, |
| 209 | + * this.ctx.storage.sql, |
| 210 | + * 'key-value', |
| 211 | + * ); |
| 212 | + * return persister; |
| 213 | + * } |
| 214 | + * } |
| 215 | + * ``` |
| 216 | + * @example |
| 217 | + * This example creates a DurableObjectSqlStoragePersister object using key-value |
| 218 | + * mode with a custom storage prefix. |
| 219 | + * |
| 220 | + * ```js yolo |
| 221 | + * import {createMergeableStore} from 'tinybase'; |
| 222 | + * import {createDurableObjectSqlStoragePersister} from 'tinybase/persisters/persister-durable-object-sql-storage'; |
| 223 | + * import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object'; |
| 224 | + * |
| 225 | + * export class MyDurableObject extends WsServerDurableObject { |
| 226 | + * createPersister() { |
| 227 | + * const store = createMergeableStore(); |
| 228 | + * const persister = createDurableObjectSqlStoragePersister( |
| 229 | + * store, |
| 230 | + * this.ctx.storage.sql, |
| 231 | + * {mode: 'key-value', storagePrefix: 'my_app_'}, |
| 232 | + * ); |
| 233 | + * return persister; |
| 234 | + * } |
| 235 | + * } |
| 236 | + * ``` |
| 237 | + * @example |
187 | 238 | * This example creates a DurableObjectSqlStoragePersister object with tabular |
188 | 239 | * mapping configuration for direct table-to-table persistence. |
189 | 240 | * |
|
0 commit comments