Skip to content

Commit

Permalink
Added a batch delete operation to DataSync
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Feb 4, 2022
1 parent 7752e0c commit 87dbe2c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions IHP/DataSync/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ instance (
sqlExecWithRLS "DELETE FROM ? WHERE id = ?" (PG.Identifier table, id)
sendJSON DidDeleteRecord { requestId }
handleMessage DeleteRecordsMessage { table, ids, requestId } = do
ensureRLSEnabled table
sqlExecWithRLS "DELETE FROM ? WHERE id IN ?" (PG.Identifier table, PG.In ids)
sendJSON DidDeleteRecords { requestId }
forever do
Expand Down
2 changes: 2 additions & 0 deletions IHP/DataSync/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ data DataSyncMessage
| UpdateRecordMessage { table :: !Text, id :: !UUID, patch :: !(HashMap Text Value), requestId :: !Int }
| UpdateRecordsMessage { table :: !Text, ids :: ![UUID], patch :: !(HashMap Text Value), requestId :: !Int }
| DeleteRecordMessage { table :: !Text, id :: !UUID, requestId :: !Int }
| DeleteRecordsMessage { table :: !Text, ids :: ![UUID], requestId :: !Int }
deriving (Eq, Show)

data DataSyncResponse
Expand All @@ -32,6 +33,7 @@ data DataSyncResponse
| DidUpdateRecord { requestId :: !Int, record :: ![Field] } -- ^ Response to 'UpdateRecordMessage'
| DidUpdateRecords { requestId :: !Int, records :: ![[Field]] } -- ^ Response to 'UpdateRecordsMessage'
| DidDeleteRecord { requestId :: !Int }
| DidDeleteRecords { requestId :: !Int }

data Subscription = Subscription { id :: !UUID, channelSubscription :: !PGListener.Subscription }

Expand Down
19 changes: 19 additions & 0 deletions lib/IHP/DataSync/ihp-datasync.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,25 @@ export async function deleteRecord(table, id) {
}
}

export async function deleteRecords(table, ids) {
if (typeof table !== "string") {
throw new Error(`Table name needs to be a string, you passed ${JSON.stringify(table)} in a call to deleteRecords(${JSON.stringify(table)}, ${JSON.stringify(ids)})`);
}
if (!Array.isArray(ids)) {
throw new Error(`IDs needs to be an array, you passed ${JSON.stringify(ids)} in a call to deleteRecords(${JSON.stringify(table)}, ${JSON.stringify(ids)})`);
}

const request = { tag: 'DeleteRecordsMessage', table, ids };

try {
const response = await DataSyncController.getInstance().sendMessage(request);

return;
} catch (e) {
throw new Error(e.message);
}
}

export async function createRecords(table, records) {
if (typeof table !== "string") {
throw new Error(`Table name needs to be a string, you passed ${JSON.stringify(table)} in a call to createRecords(${JSON.stringify(table)}, ${JSON.stringify(records, null, 4)})`);
Expand Down

0 comments on commit 87dbe2c

Please sign in to comment.