diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-10-13 12:40:21 +0200 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-10-13 12:40:21 +0200 |
commit | 91454fa171e6a5616c9cbceb725a290963fc0c31 (patch) | |
tree | ad40dfb1ef62ca7aef73ae5ef37a8a72b53bff97 | |
parent | cf9b3887a46de7419c278126d09e14a228126e2d (diff) |
keep data free of `null`s
-rw-r--r-- | backend/app/Main.hs | 16 | ||||
-rw-r--r-- | docs/api-reference.md | 3 | ||||
-rw-r--r-- | nix/sources.json | 2 |
3 files changed, 15 insertions, 6 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 + ) diff --git a/docs/api-reference.md b/docs/api-reference.md index 064993e..248ab8c 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -154,12 +154,9 @@ EOF Again, the response contains the full entity after the update. -TODO sanitize `null` fields - ```json { "$fileName": "ccccc18c-f3dc-4f98-b4d2-290ef76adb6b.json", - "description": null, "name": "Entity 2" } ``` diff --git a/nix/sources.json b/nix/sources.json index a60a18b..bef3f88 100644 --- a/nix/sources.json +++ b/nix/sources.json @@ -2,7 +2,7 @@ "json2sql": { "branch": "main", "repo": "git@code.nomath.org:~/json2sql", - "rev": "56fccc922fd3d7fdf748b7d2b6cf1df4e48bceb2", + "rev": "ea241da2457aa9992cad7d64796d1ef40b0264b0", "type": "git" }, "nixpkgs": { |