( context: SyncRequestContext, input: EntityChange )
| 15 | } |
| 16 | |
| 17 | export async function performMutation( |
| 18 | context: SyncRequestContext, |
| 19 | input: EntityChange |
| 20 | ): Promise<void> { |
| 21 | const { schema, db } = context; |
| 22 | |
| 23 | if (!getIsChangeDataValid(context, input)) { |
| 24 | throw new Error("Invalid change data"); |
| 25 | } |
| 26 | |
| 27 | if (!(await getIsChangeAllowed(context, input))) { |
| 28 | throw new Error("Change is not allowed"); |
| 29 | } |
| 30 | |
| 31 | const idField = getIdKeyForEntity(schema, input.entity); |
| 32 | |
| 33 | switch (input.type) { |
| 34 | case "remove": { |
| 35 | db.table(input.entity).where(idField, input.id).delete(); |
| 36 | return; |
| 37 | } |
| 38 | case "update": { |
| 39 | db.table(input.entity).where(idField, input.id).update(input.data); |
| 40 | return; |
| 41 | } |
| 42 | case "create": { |
| 43 | db.table(input.entity).insert(input.data); |
| 44 | return; |
| 45 | } |
| 46 | } |
| 47 | } |
no test coverage detected