Skip to content

Commit

Permalink
Added a batch update operation to DataSync
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Feb 4, 2022
1 parent 65dfabd commit 7752e0c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
31 changes: 30 additions & 1 deletion IHP/DataSync/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,36 @@ instance (
case result of
[record] -> sendJSON DidUpdateRecord { requestId, record }
otherwise -> error "Unexpected result in CreateRecordMessage handler"
otherwise -> error "Unexpected result in UpdateRecordMessage handler"
pure ()
handleMessage UpdateRecordsMessage { table, ids, patch, requestId } = do
ensureRLSEnabled table
let columns = patch
|> HashMap.keys
|> map fieldNameToColumnName
|> map PG.Identifier
let values = patch
|> HashMap.elems
|> map aesonValueToPostgresValue
let keyValues = zip columns values
let setCalls = keyValues
|> map (\_ -> "? = ?")
|> ByteString.intercalate ", "
let query = "UPDATE ? SET " <> setCalls <> " WHERE id IN ? RETURNING *"
let params = [PG.toField (PG.Identifier table)]
<> (join (map (\(key, value) -> [PG.toField key, value]) keyValues))
<> [PG.toField (PG.In ids)]
records <- sqlQueryWithRLS (PG.Query query) params
sendJSON DidUpdateRecords { requestId, records }
pure ()
Expand Down
2 changes: 2 additions & 0 deletions IHP/DataSync/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ data DataSyncMessage
| CreateRecordMessage { table :: !Text, record :: !(HashMap Text Value), requestId :: !Int }
| CreateRecordsMessage { table :: !Text, records :: ![HashMap Text Value], requestId :: !Int }
| 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 }
deriving (Eq, Show)

Expand All @@ -29,6 +30,7 @@ data DataSyncResponse
| DidCreateRecord { requestId :: !Int, record :: ![Field] } -- ^ Response to 'CreateRecordMessage'
| DidCreateRecords { requestId :: !Int, records :: ![[Field]] } -- ^ Response to 'CreateRecordsMessage'
| DidUpdateRecord { requestId :: !Int, record :: ![Field] } -- ^ Response to 'UpdateRecordMessage'
| DidUpdateRecords { requestId :: !Int, records :: ![[Field]] } -- ^ Response to 'UpdateRecordsMessage'
| DidDeleteRecord { requestId :: !Int }

data Subscription = Subscription { id :: !UUID, channelSubscription :: !PGListener.Subscription }
Expand Down
22 changes: 22 additions & 0 deletions lib/IHP/DataSync/ihp-datasync.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,28 @@ export async function updateRecord(table, id, patch) {
}
}

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

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

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

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

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

0 comments on commit 7752e0c

Please sign in to comment.