Last active
January 18, 2024 19:31
-
-
Save james2doyle/6d9b9737972ecc1e58d1f305a34ae4f7 to your computer and use it in GitHub Desktop.
use SQLite to store JSON à la jsonb in PostgreSQL using virtual generated columns (https://www.sqlite.org/gencol.html)
This file contains 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 t ( | |
id integer PRIMARY KEY autoincrement, | |
data text | |
); | |
INSERT INTO my_objects (data) values('{"foo": "value", "bar": "other value"}'), ('{"foo": "baz", "bar": "qux"}'); | |
ALTER TABLE my_objects ADD COLUMN foo text GENERATED always AS (json_extract(data, $.foo)) virtual; | |
SELECT * FROM my_objects; | |
-- id data foo | |
-- -- -------------------------------------- ----- | |
-- 1 {"foo": "value", "bar": "other value"} value | |
-- 2 {"foo": "baz", "bar": "qux"} baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment