11# postgres-schema-ts
22
3- > postgres-schema-ts is a simple npm module you can use to convert a mysql schema into typescript interfaces
3+ > postgres-schema-ts is a simple npm module you can use to convert a postgres schema into typescript interfaces
44
55# Usage
66
77``` bash
88# to infer an entire schema
9- $ npx postgres-schema-ts mysql ://root @localhost:3306/database
9+ $ npx postgres-schema-ts postgresql ://postgres @localhost:5433/db ? currentSchema=public
1010
1111# to infer a specific table
12- $ npx postgres-schema-ts mysql ://root @localhost:3306/database table_name
12+ $ npx postgres-schema-ts postgresql ://postgres @localhost:5433/db ? currentSchema=public table_name
1313```
1414
1515tip: You can pipe the output of postgres-schema-ts into a file with ` npx postgres-schema-ts <args> > schema.ts `
1616
1717## Demo
1818
19- For the following SQL schema: (from the musicbrainz database)
19+ For the following SQL schema:
2020
2121``` sql
22- CREATE TABLE IF NOT EXISTS artist ( -- replicate (verbose)
23- id SERIAL ,
24- gid CHAR (36 ) NOT NULL ,
25- name VARCHAR (255 ) NOT NULL ,
26- sort_name VARCHAR (255 ) NOT NULL ,
27- begin_date_year SMALLINT ,
28- begin_date_month SMALLINT ,
29- begin_date_day SMALLINT ,
30- end_date_year SMALLINT ,
31- end_date_month SMALLINT ,
32- end_date_day SMALLINT ,
33- type INTEGER , -- references artist_type.id
34- area INTEGER , -- references area.id
35- gender INTEGER , -- references gender.id
36- comment VARCHAR (255 ) NOT NULL DEFAULT ' ' ,
37- edits_pending INTEGER NOT NULL DEFAULT 0 CHECK (edits_pending >= 0 ),
38- last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
39- ended CHAR (1 ) NOT NULL DEFAULT FALSE
40- );
41-
42- CREATE TABLE IF NOT EXISTS track ( -- replicate (verbose)
43- id SERIAL ,
44- gid CHAR (36 ) NOT NULL ,
45- recording INTEGER NOT NULL , -- references recording.id
46- medium INTEGER NOT NULL , -- references medium.id
47- position INTEGER NOT NULL ,
48- number TEXT NOT NULL ,
49- name VARCHAR (255 ) NOT NULL ,
50- artist_credit INTEGER NOT NULL , -- references artist_credit.id
51- length INTEGER CHECK (length IS NULL OR length > 0 ),
52- edits_pending INTEGER NOT NULL DEFAULT 0 CHECK (edits_pending >= 0 ),
53- last_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
54- is_data_track CHAR (1 ) NOT NULL DEFAULT FALSE
55- ) CHARACTER SET utf8 COLLATE utf8_general_ci;
22+ CREATE TABLE account (
23+ username VARCHAR (50 ) UNIQUE NOT NULL ,
24+ password VARCHAR (50 ) NOT NULL ,
25+ email VARCHAR (355 ) UNIQUE NOT NULL ,
26+ created_on TIMESTAMP NOT NULL ,
27+ last_login TIMESTAMP
28+ )
5629```
5730
5831run:
5932
6033``` bash
61- $ npx postgres-schema-ts mysql ://root @localhost:3306/musicbrainz
34+ $ npx postgres-schema-ts postgresql ://postgres @localhost:5433/db ? currentSchema=public
6235```
6336
6437``` typescript
65- export interface artist {
66- id: number
67- gid: string
68- name: string
69- sort_name: string
70- begin_date_year: number | null
71- begin_date_month: number | null
72- begin_date_day: number | null
73- end_date_year: number | null
74- end_date_month: number | null
75- end_date_day: number | null
76- type: number | null
77- area: number | null
78- gender: number | null
79- comment: string
80- edits_pending: number
81- last_updated: Date
82- ended: string
83- }
84- export interface track {
85- id: number
86- gid: string
87- recording: number
88- medium: number
89- position: number
90- number_: string
91- name: string
92- artist_credit: number
93- length: number | null
94- edits_pending: number
95- last_updated: Date
96- is_data_track: string
38+ export interface account {
39+ username: string
40+ password: string
41+ email: string
42+ created_on: Date
43+ last_login: Date | null
9744}
9845```
9946
@@ -112,7 +59,7 @@ postgres-schema-ts is inpired by the awesome [schemats](https://github.com/Sweet
11259But takes a simpler, more blunt, and configuration free approach:
11360
11461- Simpler defaults
115- - MySQL support only
62+ - Postgres support only
11663- Inline enums
11764- No support for namespaces
11865
0 commit comments