| 36 | |
| 37 | // Electric collections use txid to track sync |
| 38 | const configMutationFn = async ({ |
| 39 | transaction, |
| 40 | }: { |
| 41 | transaction: Transaction |
| 42 | }) => { |
| 43 | const txids: Array<number> = [] |
| 44 | |
| 45 | // Handle inserts |
| 46 | const inserts = transaction.mutations.filter((m) => m.type === `insert`) |
| 47 | for (const mutation of inserts) { |
| 48 | const response = await api.config.create(mutation.modified) |
| 49 | txids.push(response.txid) |
| 50 | } |
| 51 | |
| 52 | // Handle updates |
| 53 | const updates = transaction.mutations.filter((m) => m.type === `update`) |
| 54 | for (const mutation of updates) { |
| 55 | if (!(`id` in mutation.original)) { |
| 56 | throw new Error(`Original config not found for update`) |
| 57 | } |
| 58 | const response = await api.config.update( |
| 59 | mutation.original.id, |
| 60 | mutation.changes, |
| 61 | ) |
| 62 | txids.push(response.txid) |
| 63 | } |
| 64 | |
| 65 | // Wait for all txids to sync back to the collection |
| 66 | await Promise.all( |
| 67 | txids.map((txid) => electricConfigCollection.utils.awaitTxid(txid)), |
| 68 | ) |
| 69 | } |
| 70 | |
| 71 | return ( |
| 72 | <TodoApp |