( config: LocalOnlyCollectionConfig<T, TSchema, TKey>, )
| 172 | } |
| 173 | |
| 174 | export function localOnlyCollectionOptions< |
| 175 | T extends object = object, |
| 176 | TSchema extends StandardSchemaV1 = never, |
| 177 | TKey extends string | number = string | number, |
| 178 | >( |
| 179 | config: LocalOnlyCollectionConfig<T, TSchema, TKey>, |
| 180 | ): LocalOnlyCollectionOptionsResult<T, TKey, TSchema> & { |
| 181 | schema?: StandardSchemaV1 |
| 182 | } { |
| 183 | const { initialData, onInsert, onUpdate, onDelete, id, ...restConfig } = |
| 184 | config |
| 185 | |
| 186 | const collectionId = id ?? safeRandomUUID() |
| 187 | |
| 188 | // Create the sync configuration with transaction confirmation capability |
| 189 | const syncResult = createLocalOnlySync<T, TKey>(initialData) |
| 190 | |
| 191 | /** |
| 192 | * Create wrapper handlers that call user handlers first, then confirm transactions |
| 193 | * Wraps the user's onInsert handler to also confirm the transaction immediately |
| 194 | */ |
| 195 | const wrappedOnInsert = async ( |
| 196 | params: InsertMutationFnParams<T, TKey, LocalOnlyCollectionUtils>, |
| 197 | ) => { |
| 198 | // Call user handler first if provided |
| 199 | let handlerResult |
| 200 | if (onInsert) { |
| 201 | handlerResult = (await onInsert(params)) ?? {} |
| 202 | } |
| 203 | |
| 204 | // Then synchronously confirm the transaction by looping through mutations |
| 205 | syncResult.confirmOperationsSync(params.transaction.mutations) |
| 206 | |
| 207 | return handlerResult |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Wrapper for onUpdate handler that also confirms the transaction immediately |
| 212 | */ |
| 213 | const wrappedOnUpdate = async ( |
| 214 | params: UpdateMutationFnParams<T, TKey, LocalOnlyCollectionUtils>, |
| 215 | ) => { |
| 216 | // Call user handler first if provided |
| 217 | let handlerResult |
| 218 | if (onUpdate) { |
| 219 | handlerResult = (await onUpdate(params)) ?? {} |
| 220 | } |
| 221 | |
| 222 | // Then synchronously confirm the transaction by looping through mutations |
| 223 | syncResult.confirmOperationsSync(params.transaction.mutations) |
| 224 | |
| 225 | return handlerResult |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Wrapper for onDelete handler that also confirms the transaction immediately |
| 230 | */ |
| 231 | const wrappedOnDelete = async ( |
no test coverage detected