aboutsummaryrefslogtreecommitdiffstats
path: root/backend/app
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-10-13 12:40:21 +0200
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-10-13 12:40:21 +0200
commit91454fa171e6a5616c9cbceb725a290963fc0c31 (patch)
treead40dfb1ef62ca7aef73ae5ef37a8a72b53bff97 /backend/app
parentcf9b3887a46de7419c278126d09e14a228126e2d (diff)
keep data free of `null`s
Diffstat (limited to 'backend/app')
-rw-r--r--backend/app/Main.hs16
1 files changed, 14 insertions, 2 deletions
diff --git a/backend/app/Main.hs b/backend/app/Main.hs
index 917a6d9..0985a3a 100644
--- a/backend/app/Main.hs
+++ b/backend/app/Main.hs
@@ -349,13 +349,15 @@ restApi root ref repoT app req resp = do
o <- J.throwDecode @J.Object =<< W.lazyRequestBody req
resp . W.responseLBS W.status200 [] . J.encode . headMay
=<< ( Q.withStore root ref do
- Q.query (fromString (printf "UPDATE %s SET %s WHERE %s.$fileName == \"%s\"" c (LB.toString (J.encode o)) c i))
+ _ <- Q.query (fromString (printf "UPDATE %s SET %s WHERE %s.$fileName == \"%s\"" c (LB.toString (J.encode o)) c i))
+ [J.Object r] <- Q.query (fromString (printf "SELECT %s FROM %s WHERE %s.$fileName == \"%s\"" c c c i))
+ _ <- Q.query (fromString (printf "INSERT %s INTO %s" (LB.toString (J.encode (dropNulls r))) c))
Q.query (fromString (printf "SELECT %s FROM %s WHERE %s.$fileName == \"%s\"" c c c i))
)
("POST", ["collection", c]) -> do
when (not (sameCommit lastCompatibleCommit lastCommit)) $ error "not implemented"
i <- ((<> ".json") . U.toText) <$> getUUID
- o <- J.throwDecode @J.Object =<< W.lazyRequestBody req
+ o <- fmap dropNulls . J.throwDecode @J.Object =<< W.lazyRequestBody req
resp . W.responseLBS W.status200 [] . J.encode
=<< ( Q.withStore root ref do
_ <- Q.query (fromString (printf "INSERT %s INTO %s" (LB.toString (J.encode (JM.insert "$fileName" (J.String i) o))) c))
@@ -380,3 +382,13 @@ lastCompatible (Just v) commits
isCompatible :: Version -> Commit -> Bool
isCompatible v c = c.schemaVersion <= v
+
+dropNulls :: J.Object -> J.Object
+dropNulls =
+ JM.mapMaybe
+ ( \v ->
+ case v of
+ J.Null -> Nothing
+ (J.Object v') -> Just (J.Object (dropNulls v'))
+ _ -> Just v
+ )