@@ -45,6 +45,7 @@ interface PendingSyncedTransaction<T extends object = Record<string, unknown>> {
4545 * await preloadCollection({
4646 * id: `users-${params.userId}`,
4747 * sync: { ... },
48+ * // mutationFn is optional - provide it if you need mutation capabilities
4849 * mutationFn: { ... }
4950 * });
5051 *
@@ -53,7 +54,7 @@ interface PendingSyncedTransaction<T extends object = Record<string, unknown>> {
5354 * ```
5455 *
5556 * @template T - The type of items in the collection
56- * @param config - Configuration for the collection, including id, sync, and mutationFn
57+ * @param config - Configuration for the collection, including id, sync, and optional mutationFn
5758 * @returns Promise that resolves when the initial sync is finished
5859 */
5960export function preloadCollection < T extends object = Record < string , unknown > > (
@@ -182,15 +183,12 @@ export class Collection<T extends object = Record<string, unknown>> {
182183 * Creates a new Collection instance
183184 *
184185 * @param config - Configuration object for the collection
185- * @throws Error if sync config or mutationFn is missing
186+ * @throws Error if sync config is missing
186187 */
187188 constructor ( config ?: CollectionConfig < T > ) {
188189 if ( ! config ?. sync ) {
189190 throw new Error ( `Collection requires a sync config` )
190191 }
191- if ( ! config . mutationFn as unknown ) {
192- throw new Error ( `Collection requires a mutationFn` )
193- }
194192
195193 this . transactionStore = new TransactionStore ( )
196194 this . transactionManager = getTransactionManager < T > (
@@ -517,6 +515,7 @@ export class Collection<T extends object = Record<string, unknown>> {
517515 * @param config - Optional configuration including metadata and custom keys
518516 * @returns A Transaction object representing the insert operation(s)
519517 * @throws {SchemaValidationError } If the data fails schema validation
518+ * @throws {Error } If mutationFn is not provided
520519 * @example
521520 * // Insert a single item
522521 * insert({ text: "Buy groceries", completed: false })
@@ -531,6 +530,13 @@ export class Collection<T extends object = Record<string, unknown>> {
531530 * insert({ text: "Buy groceries" }, { key: "grocery-task" })
532531 */
533532 insert = ( data : T | Array < T > , config ?: InsertConfig ) => {
533+ // Throw error if mutationFn is not provided
534+ if ( ! this . config . mutationFn ) {
535+ throw new Error (
536+ `Cannot use mutation operators without providing a mutationFn in the collection config`
537+ )
538+ }
539+
534540 const items = Array . isArray ( data ) ? data : [ data ]
535541 const mutations : Array < PendingMutation > = [ ]
536542
@@ -582,6 +588,7 @@ export class Collection<T extends object = Record<string, unknown>> {
582588 * @param maybeCallback - Update callback if config was provided
583589 * @returns A Transaction object representing the update operation(s)
584590 * @throws {SchemaValidationError } If the updated data fails schema validation
591+ * @throws {Error } If mutationFn is not provided
585592 * @example
586593 * // Update a single item
587594 * update(todo, (draft) => { draft.completed = true })
@@ -612,6 +619,13 @@ export class Collection<T extends object = Record<string, unknown>> {
612619 configOrCallback : ( ( draft : TItem | Array < TItem > ) => void ) | OperationConfig ,
613620 maybeCallback ?: ( draft : TItem | Array < TItem > ) => void
614621 ) {
622+ // Throw error if mutationFn is not provided
623+ if ( ! this . config . mutationFn ) {
624+ throw new Error (
625+ `Cannot use mutation operators without providing a mutationFn in the collection config`
626+ )
627+ }
628+
615629 if ( typeof items === `undefined` ) {
616630 throw new Error ( `The first argument to update is missing` )
617631 }
@@ -702,6 +716,7 @@ export class Collection<T extends object = Record<string, unknown>> {
702716 * @param items - Single item/key or array of items/keys to delete
703717 * @param config - Optional configuration including metadata
704718 * @returns A Transaction object representing the delete operation(s)
719+ * @throws {Error } If mutationFn is not provided
705720 * @example
706721 * // Delete a single item
707722 * delete(todo)
@@ -716,6 +731,13 @@ export class Collection<T extends object = Record<string, unknown>> {
716731 items : Array < T | string > | T | string ,
717732 config ?: OperationConfig
718733 ) => {
734+ // Throw error if mutationFn is not provided
735+ if ( ! this . config . mutationFn ) {
736+ throw new Error (
737+ `Cannot use mutation operators without providing a mutationFn in the collection config`
738+ )
739+ }
740+
719741 const itemsArray = Array . isArray ( items ) ? items : [ items ]
720742 const mutations : Array < PendingMutation > = [ ]
721743
0 commit comments