From 91454fa171e6a5616c9cbceb725a290963fc0c31 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Sun, 13 Oct 2024 12:40:21 +0200 Subject: keep data free of `null`s --- backend/app/Main.hs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'backend/app/Main.hs') 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 + ) -- cgit v1.2.3