1
1
# postgres-schema-ts
2
2
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
4
4
5
5
# Usage
6
6
7
7
``` bash
8
8
# 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
10
10
11
11
# 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
13
13
```
14
14
15
15
tip: You can pipe the output of postgres-schema-ts into a file with ` npx postgres-schema-ts <args> > schema.ts `
16
16
17
17
## Demo
18
18
19
- For the following SQL schema: (from the musicbrainz database)
19
+ For the following SQL schema:
20
20
21
21
``` 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
+ )
56
29
```
57
30
58
31
run:
59
32
60
33
``` bash
61
- $ npx postgres-schema-ts mysql ://root @localhost:3306/musicbrainz
34
+ $ npx postgres-schema-ts postgresql ://postgres @localhost:5433/db ? currentSchema=public
62
35
```
63
36
64
37
``` 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
97
44
}
98
45
```
99
46
@@ -112,7 +59,7 @@ postgres-schema-ts is inpired by the awesome [schemats](https://github.com/Sweet
112
59
But takes a simpler, more blunt, and configuration free approach:
113
60
114
61
- Simpler defaults
115
- - MySQL support only
62
+ - Postgres support only
116
63
- Inline enums
117
64
- No support for namespaces
118
65
0 commit comments