Created
August 31, 2022 21:33
-
-
Save tswaters/24ca6cf40fcedd655a4c80f9c775925f to your computer and use it in GitHub Desktop.
crud operations using json_to_recordset
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 TABLE _table ( | |
pk SERIAL PRIMARY KEY, | |
field1 TEXT, | |
field2 TEXT, | |
date_deleted TIMESTAMPTZ | |
); |
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
client.query(` | |
UPDATE _table orig SET | |
field1 = r.field1, | |
field2 = r.field2 | |
FROM JSON_TO_RECORD($1) AS r( | |
pk int, | |
field1 text, | |
field2 text | |
) | |
WHERE orig.pk = r.pk | |
`, [record]) | |
client.query(` | |
INSERT INTO _table (field1, field2) | |
SELECT r.field1, r.field2 | |
FROM JSON_TO_RECORD($1) | |
`, [record]) | |
client.query(` | |
UPDATE _table orig | |
SET date_deleted = NOW() | |
WHERE $1 | |
`, [pk]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment