Created
May 27, 2015 18:29
-
-
Save devi/e9356d5a5bdce810c162 to your computer and use it in GitHub Desktop.
jsonb manipulator based on https://gist.github.com/matheusoliveira/9488951
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION jb_merge(jsonb, jsonb) | |
RETURNS jsonb | |
IMMUTABLE | |
LANGUAGE SQL | |
AS $$ | |
WITH xxx AS | |
(SELECT * FROM jsonb_each_text($1) | |
UNION ALL | |
SELECT * FROM jsonb_each_text($2)) | |
SELECT json_object_agg(key, value)::jsonb FROM xxx; | |
$$; | |
CREATE FUNCTION jb_add(jsonb, text, text) | |
RETURNS jsonb | |
IMMUTABLE | |
LANGUAGE SQL | |
AS $$ | |
SELECT jb_merge($1, json_build_object($2, $3)::jsonb); | |
$$; | |
CREATE FUNCTION jb_add_pairs(jsonb, variadic text[]) | |
RETURNS jsonb | |
IMMUTABLE | |
LANGUAGE SQL | |
AS $$ | |
SELECT jb_merge($1, json_object($2)::jsonb); | |
$$; | |
CREATE FUNCTION jb_delete(jsonb, variadic text[]) | |
RETURNS jsonb | |
IMMUTABLE | |
LANGUAGE SQL | |
AS $$ | |
SELECT json_object_agg(key, value)::jsonb | |
FROM ( | |
SELECT * FROM jsonb_each_text($1) | |
WHERE key <> ALL($2) | |
) t; | |
$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment