Type-safe schema generator
Started working on a type-safe schema generator based off the sqlite
schema.
I'm pulling out the database schema using #bun's native sqlite driver with a simple query:
SELECT
tbl_name,
name,
type,
sql
FROM
sqlite_schema
WHERE
1=1
AND type = 'table'
AND sql IS NOT NULL
AND name != 'sqlite_sequence'
Then aggregating all the SQL statements, passing them through sql-parser-cst
, walking the AST collecting tables, columns, primary keys, etc...
Then passing it through ts-morph
to emit typescript source code with all the types. Could potentially scan queries, and try to derive parameter types based on schema, that would be pretty sweet.
In plans:
- Nominally-typed ID's, with
Brand
types. No more accidentally passingUserId
into aProductId
. - Could leverage
runtypes
to produce contracts...
Maybe even create contracts and derive types from that instead... Not a bad idea...
Will post a gist once done.