From 87dbe2c5f9458a9227a8db886b4a67c0c8ab87e6 Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Fri, 4 Feb 2022 10:23:00 +0100 Subject: [PATCH] Added a batch delete operation to DataSync --- IHP/DataSync/Controller.hs | 7 +++++++ IHP/DataSync/Types.hs | 2 ++ lib/IHP/DataSync/ihp-datasync.js | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/IHP/DataSync/Controller.hs b/IHP/DataSync/Controller.hs index 105083011..d40f6fd6e 100644 --- a/IHP/DataSync/Controller.hs +++ b/IHP/DataSync/Controller.hs @@ -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 diff --git a/IHP/DataSync/Types.hs b/IHP/DataSync/Types.hs index 7f6db67cc..7c010a35d 100644 --- a/IHP/DataSync/Types.hs +++ b/IHP/DataSync/Types.hs @@ -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 @@ -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 } diff --git a/lib/IHP/DataSync/ihp-datasync.js b/lib/IHP/DataSync/ihp-datasync.js index 098d550d9..70e98fa0a 100644 --- a/lib/IHP/DataSync/ihp-datasync.js +++ b/lib/IHP/DataSync/ihp-datasync.js @@ -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)})`);