PGIntro
PGIntro introspects your Postgres database and generates corresponding TypeScript types.
Currently, the following database objects are utilized:
- Enums: Generate string enums
- Tables: Generate interfaces
- Views: Generate interfaces
Example
The following SQL:
.age_group as enum ( 'child', 'adolescent', 'adult'); .person ( id uuid primary key, first_name text not null, last_name text, aliases text[], birth_date timestamp not null, age_group age_group not null); comment on table my_schema.person is 'A single person.';
would generate the following typescript code:
/** * A single person. */
Usage
; ;await intro.Instrospect, 'my_database_types.ts';
The only argument required for constructing Introspecter
are optional postgres connection options. These are passed as-is to pg.Client
constructor. If omitted, pg.Client
will attempt to use environment variables as specified here: https://node-postgres.com/features/connecting
About
PGIntro is a small amount of glue that utilizes the excellent introspection query from PostGraphQL and the also excellent ts-simple-ast project. It's lightly tested and far from full-featured at the moment.
Pull requests are extremely welcome
Running the tests
Create a file .test.env
in the root of the repository that contains a TEST_DATABASE_URL value, like so:
# .test.env
TEST_DATABASE_URL='http://user:dolphins@localhost:5432'
The tests will create a temporary database upon startup and delete it once the tests have completed.