Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active January 18, 2024 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james2doyle/6d9b9737972ecc1e58d1f305a34ae4f7 to your computer and use it in GitHub Desktop.
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)
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