|
| 1 | +--- |
| 2 | +id: PowerSyncTransactor |
| 3 | +title: PowerSyncTransactor |
| 4 | +--- |
| 5 | + |
| 6 | +# Class: PowerSyncTransactor |
| 7 | + |
| 8 | +Defined in: [PowerSyncTransactor.ts:51](https://github.com/powersync-ja/temp-tanstack-db/blob/main/packages/powersync-db-collection/src/PowerSyncTransactor.ts#L51) |
| 9 | + |
| 10 | +Applies mutations to the PowerSync database. This method is called automatically by the collection's |
| 11 | +insert, update, and delete operations. You typically don't need to call this directly unless you |
| 12 | +have special transaction requirements. |
| 13 | + |
| 14 | +## Example |
| 15 | + |
| 16 | +```typescript |
| 17 | +// Create a collection |
| 18 | +const collection = createCollection( |
| 19 | + powerSyncCollectionOptions<Document>({ |
| 20 | + database: db, |
| 21 | + table: APP_SCHEMA.props.documents, |
| 22 | + }) |
| 23 | +) |
| 24 | + |
| 25 | +const addTx = createTransaction({ |
| 26 | + autoCommit: false, |
| 27 | + mutationFn: async ({ transaction }) => { |
| 28 | + await new PowerSyncTransactor({ database: db }).applyTransaction(transaction) |
| 29 | + }, |
| 30 | +}) |
| 31 | + |
| 32 | +addTx.mutate(() => { |
| 33 | + for (let i = 0; i < 5; i++) { |
| 34 | + collection.insert({ id: randomUUID(), name: `tx-${i}` }) |
| 35 | + } |
| 36 | +}) |
| 37 | + |
| 38 | +await addTx.commit() |
| 39 | +await addTx.isPersisted.promise |
| 40 | +``` |
| 41 | + |
| 42 | +## Param |
| 43 | + |
| 44 | +The transaction containing mutations to apply |
| 45 | + |
| 46 | +## Constructors |
| 47 | + |
| 48 | +### Constructor |
| 49 | + |
| 50 | +```ts |
| 51 | +new PowerSyncTransactor(options): PowerSyncTransactor; |
| 52 | +``` |
| 53 | + |
| 54 | +Defined in: [PowerSyncTransactor.ts:55](https://github.com/powersync-ja/temp-tanstack-db/blob/main/packages/powersync-db-collection/src/PowerSyncTransactor.ts#L55) |
| 55 | + |
| 56 | +#### Parameters |
| 57 | + |
| 58 | +##### options |
| 59 | + |
| 60 | +[`TransactorOptions`](../type-aliases/TransactorOptions.md) |
| 61 | + |
| 62 | +#### Returns |
| 63 | + |
| 64 | +`PowerSyncTransactor` |
| 65 | + |
| 66 | +## Properties |
| 67 | + |
| 68 | +### database |
| 69 | + |
| 70 | +```ts |
| 71 | +database: AbstractPowerSyncDatabase; |
| 72 | +``` |
| 73 | + |
| 74 | +Defined in: [PowerSyncTransactor.ts:52](https://github.com/powersync-ja/temp-tanstack-db/blob/main/packages/powersync-db-collection/src/PowerSyncTransactor.ts#L52) |
| 75 | + |
| 76 | +*** |
| 77 | + |
| 78 | +### pendingOperationStore |
| 79 | + |
| 80 | +```ts |
| 81 | +pendingOperationStore: PendingOperationStore; |
| 82 | +``` |
| 83 | + |
| 84 | +Defined in: [PowerSyncTransactor.ts:53](https://github.com/powersync-ja/temp-tanstack-db/blob/main/packages/powersync-db-collection/src/PowerSyncTransactor.ts#L53) |
| 85 | + |
| 86 | +## Methods |
| 87 | + |
| 88 | +### applyTransaction() |
| 89 | + |
| 90 | +```ts |
| 91 | +applyTransaction(transaction): Promise<void>; |
| 92 | +``` |
| 93 | + |
| 94 | +Defined in: [PowerSyncTransactor.ts:63](https://github.com/powersync-ja/temp-tanstack-db/blob/main/packages/powersync-db-collection/src/PowerSyncTransactor.ts#L63) |
| 95 | + |
| 96 | +Persists a Transaction to the PowerSync SQLite database. |
| 97 | + |
| 98 | +#### Parameters |
| 99 | + |
| 100 | +##### transaction |
| 101 | + |
| 102 | +`Transaction`\<`any`\> |
| 103 | + |
| 104 | +#### Returns |
| 105 | + |
| 106 | +`Promise`\<`void`\> |
| 107 | + |
| 108 | +*** |
| 109 | + |
| 110 | +### handleDelete() |
| 111 | + |
| 112 | +```ts |
| 113 | +protected handleDelete( |
| 114 | + mutation, |
| 115 | + context, |
| 116 | +waitForCompletion): Promise<PendingOperation | null>; |
| 117 | +``` |
| 118 | + |
| 119 | +Defined in: [PowerSyncTransactor.ts:204](https://github.com/powersync-ja/temp-tanstack-db/blob/main/packages/powersync-db-collection/src/PowerSyncTransactor.ts#L204) |
| 120 | + |
| 121 | +#### Parameters |
| 122 | + |
| 123 | +##### mutation |
| 124 | + |
| 125 | +`PendingMutation`\<`any`\> |
| 126 | + |
| 127 | +##### context |
| 128 | + |
| 129 | +`LockContext` |
| 130 | + |
| 131 | +##### waitForCompletion |
| 132 | + |
| 133 | +`boolean` = `false` |
| 134 | + |
| 135 | +#### Returns |
| 136 | + |
| 137 | +`Promise`\<`PendingOperation` \| `null`\> |
| 138 | + |
| 139 | +*** |
| 140 | + |
| 141 | +### handleInsert() |
| 142 | + |
| 143 | +```ts |
| 144 | +protected handleInsert( |
| 145 | + mutation, |
| 146 | + context, |
| 147 | +waitForCompletion): Promise<PendingOperation | null>; |
| 148 | +``` |
| 149 | + |
| 150 | +Defined in: [PowerSyncTransactor.ts:149](https://github.com/powersync-ja/temp-tanstack-db/blob/main/packages/powersync-db-collection/src/PowerSyncTransactor.ts#L149) |
| 151 | + |
| 152 | +#### Parameters |
| 153 | + |
| 154 | +##### mutation |
| 155 | + |
| 156 | +`PendingMutation`\<`any`\> |
| 157 | + |
| 158 | +##### context |
| 159 | + |
| 160 | +`LockContext` |
| 161 | + |
| 162 | +##### waitForCompletion |
| 163 | + |
| 164 | +`boolean` = `false` |
| 165 | + |
| 166 | +#### Returns |
| 167 | + |
| 168 | +`Promise`\<`PendingOperation` \| `null`\> |
| 169 | + |
| 170 | +*** |
| 171 | + |
| 172 | +### handleOperationWithCompletion() |
| 173 | + |
| 174 | +```ts |
| 175 | +protected handleOperationWithCompletion( |
| 176 | + mutation, |
| 177 | + context, |
| 178 | + waitForCompletion, |
| 179 | +handler): Promise<PendingOperation | null>; |
| 180 | +``` |
| 181 | + |
| 182 | +Defined in: [PowerSyncTransactor.ts:232](https://github.com/powersync-ja/temp-tanstack-db/blob/main/packages/powersync-db-collection/src/PowerSyncTransactor.ts#L232) |
| 183 | + |
| 184 | +Helper function which wraps a persistence operation by: |
| 185 | +- Fetching the mutation's collection's SQLite table details |
| 186 | +- Executing the mutation |
| 187 | +- Returning the last pending diff operation if required |
| 188 | + |
| 189 | +#### Parameters |
| 190 | + |
| 191 | +##### mutation |
| 192 | + |
| 193 | +`PendingMutation`\<`any`\> |
| 194 | + |
| 195 | +##### context |
| 196 | + |
| 197 | +`LockContext` |
| 198 | + |
| 199 | +##### waitForCompletion |
| 200 | + |
| 201 | +`boolean` |
| 202 | + |
| 203 | +##### handler |
| 204 | + |
| 205 | +(`tableName`, `mutation`, `serializeValue`) => `Promise`\<`void`\> |
| 206 | + |
| 207 | +#### Returns |
| 208 | + |
| 209 | +`Promise`\<`PendingOperation` \| `null`\> |
| 210 | + |
| 211 | +*** |
| 212 | + |
| 213 | +### handleUpdate() |
| 214 | + |
| 215 | +```ts |
| 216 | +protected handleUpdate( |
| 217 | + mutation, |
| 218 | + context, |
| 219 | +waitForCompletion): Promise<PendingOperation | null>; |
| 220 | +``` |
| 221 | + |
| 222 | +Defined in: [PowerSyncTransactor.ts:177](https://github.com/powersync-ja/temp-tanstack-db/blob/main/packages/powersync-db-collection/src/PowerSyncTransactor.ts#L177) |
| 223 | + |
| 224 | +#### Parameters |
| 225 | + |
| 226 | +##### mutation |
| 227 | + |
| 228 | +`PendingMutation`\<`any`\> |
| 229 | + |
| 230 | +##### context |
| 231 | + |
| 232 | +`LockContext` |
| 233 | + |
| 234 | +##### waitForCompletion |
| 235 | + |
| 236 | +`boolean` = `false` |
| 237 | + |
| 238 | +#### Returns |
| 239 | + |
| 240 | +`Promise`\<`PendingOperation` \| `null`\> |
0 commit comments