Skip to content
This repository was archived by the owner on Jun 26, 2021. It is now read-only.

Commit d472f48

Browse files
committed
Add more complex schema
1 parent ea922fa commit d472f48

File tree

5 files changed

+153
-41
lines changed

5 files changed

+153
-41
lines changed

src/__tests__/create_user.sql

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ CREATE TABLE "user" (
22
"id" SERIAL,
33
"email" TEXT NOT NULL,
44
"name" TEXT,
5+
"boolColumn" bool NOT NULL,
6+
"charColumn" bpchar NULL,
7+
"dateColumn" date NOT NULL,
8+
"float4Column" float4 NULL,
9+
"float8Column" float8 NOT NULL,
10+
"int2Column" int2 NULL,
11+
"int4Column" int4 NOT NULL,
12+
"int8Column" int8 NULL,
13+
"numericColumn" numeric NOT NULL,
14+
"textColumn" text NULL,
15+
"timestampColumn" timestamp NOT NULL,
16+
"timestamptzColumn" timestamptz NULL,
17+
"varcharColumn" varchar NOT NULL,
518
PRIMARY KEY ("id")
619
);
720

8-
INSERT INTO "user"
9-
VALUES (DEFAULT, '[email protected]', 'michal');
10-
11-
INSERT INTO "user"
12-
VALUES (DEFAULT, '[email protected]', 'ania');
13-

src/__tests__/test.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ describe('integration tests', () => {
113113
const create = async () => {
114114
await db.none(sql(Path.join(__dirname, 'create_user.sql')));
115115
await db.none(sql(Path.join(__dirname, 'create_invoice.sql')));
116+
// const x = await db.one('SELECT * FROM "user";');
117+
// console.log(x, typeof x.editTime);
116118
};
117119
beforeAll(drop);
118120
beforeEach(create);
@@ -126,9 +128,22 @@ describe('integration tests', () => {
126128
expect(userSchema).toBeDefined();
127129

128130
expect(userSchema!.schema).toIncludeSameMembers([
129-
{ column_name: 'id', udt_name: 'int4', is_nullable: 'NO' },
130-
{ column_name: 'email', udt_name: 'text', is_nullable: 'NO' },
131-
{ column_name: 'name', udt_name: 'text', is_nullable: 'YES' },
131+
{ column_name: 'id', is_nullable: 'NO', udt_name: 'int4' },
132+
{ column_name: 'email', is_nullable: 'NO', udt_name: 'text' },
133+
{ column_name: 'name', is_nullable: 'YES', udt_name: 'text' },
134+
{ column_name: 'boolColumn', is_nullable: 'NO', udt_name: 'bool' },
135+
{ column_name: 'charColumn', is_nullable: 'YES', udt_name: 'bpchar' },
136+
{ column_name: 'dateColumn', is_nullable: 'NO', udt_name: 'date' },
137+
{ column_name: 'float4Column', is_nullable: 'YES', udt_name: 'float4' },
138+
{ column_name: 'float8Column', is_nullable: 'NO', udt_name: 'float8' },
139+
{ column_name: 'int2Column', is_nullable: 'YES', udt_name: 'int2' },
140+
{ column_name: 'int4Column', is_nullable: 'NO', udt_name: 'int4' },
141+
{ column_name: 'int8Column', is_nullable: 'YES', udt_name: 'int8' },
142+
{ column_name: 'numericColumn', is_nullable: 'NO', udt_name: 'numeric' },
143+
{ column_name: 'textColumn', is_nullable: 'YES', udt_name: 'text' },
144+
{ column_name: 'timestampColumn', is_nullable: 'NO', udt_name: 'timestamp' },
145+
{ column_name: 'timestamptzColumn', is_nullable: 'YES', udt_name: 'timestamptz' },
146+
{ column_name: 'varcharColumn', is_nullable: 'NO', udt_name: 'varchar' },
132147
]);
133148
});
134149

@@ -158,6 +173,19 @@ export const User = {
158173
id: { type: 'int4', notNull: true },
159174
email: { type: 'text', notNull: true },
160175
name: { type: 'text', notNull: false },
176+
boolColumn: { type: 'bool', notNull: true },
177+
charColumn: { type: 'bpchar', notNull: false },
178+
dateColumn: { type: 'date', notNull: true },
179+
float4Column: { type: 'float4', notNull: false },
180+
float8Column: { type: 'float8', notNull: true },
181+
int2Column: { type: 'int2', notNull: false },
182+
int4Column: { type: 'int4', notNull: true },
183+
int8Column: { type: 'int8', notNull: false },
184+
numericColumn: { type: 'numeric', notNull: true },
185+
textColumn: { type: 'text', notNull: false },
186+
timestampColumn: { type: 'timestamp', notNull: true },
187+
timestamptzColumn: { type: 'timestamptz', notNull: false },
188+
varcharColumn: { type: 'varchar', notNull: true },
161189
},
162190
} as const;
163191
`.trimLeft(),

src/generator/types.ts

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,89 @@ export type TableName = {
22
table_name: string;
33
};
44

5-
export type ColumnType =
5+
export type SingularColumnType =
6+
| 'int8'
7+
| 'bit'
8+
| 'varbit'
9+
| 'bool'
10+
| 'box'
11+
| 'bytea'
612
| 'bpchar'
7-
| 'char'
813
| 'varchar'
9-
| 'text'
10-
| 'citext'
11-
| 'uuid'
12-
| 'bytea'
14+
| 'cidr'
15+
| 'circle'
16+
| 'date'
17+
| 'float8'
1318
| 'inet'
14-
| 'time'
15-
| 'timetz'
16-
| 'interval'
17-
| 'name'
18-
| 'int2'
1919
| 'int4'
20-
| 'int8'
21-
| 'float4'
22-
| 'float8'
23-
| 'numeric'
24-
| 'money'
25-
| 'oid'
26-
| 'bool'
20+
| 'interval'
2721
| 'json'
2822
| 'jsonb'
29-
| 'date'
23+
| 'line'
24+
| 'lseg'
25+
| 'macaddr'
26+
| 'macaddr8'
27+
| 'money'
28+
| 'numeric'
29+
| 'path'
30+
| 'pg_lsn'
31+
| 'point'
32+
| 'polygon'
33+
| 'float4'
34+
| 'int2'
35+
| 'text'
36+
| 'time'
37+
| 'timetz'
3038
| 'timestamp'
3139
| 'timestamptz'
32-
| '_int2'
33-
| '_int4'
40+
| 'tsquery'
41+
| 'tsvector'
42+
| 'txid_snapshot'
43+
| 'uuid'
44+
| 'xml';
45+
46+
export type ArrayColumnType =
3447
| '_int8'
35-
| '_float4'
36-
| '_float8'
37-
| '_numeric'
38-
| '_money'
48+
| '_bit'
49+
| '_varbit'
3950
| '_bool'
40-
| '_varchar'
41-
| '_text'
42-
| '_citext'
43-
| '_uuid'
51+
| '_box'
4452
| '_bytea'
53+
| '_bpchar'
54+
| '_varchar'
55+
| '_cidr'
56+
| '_circle'
57+
| '_date'
58+
| '_float8'
59+
| '_inet'
60+
| '_int4'
61+
| '_interval'
4562
| '_json'
4663
| '_jsonb'
47-
| '_timestamptz';
64+
| '_line'
65+
| '_lseg'
66+
| '_macaddr'
67+
| '_macaddr8'
68+
| '_money'
69+
| '_numeric'
70+
| '_path'
71+
| '_pg_lsn'
72+
| '_point'
73+
| '_polygon'
74+
| '_float4'
75+
| '_int2'
76+
| '_text'
77+
| '_time'
78+
| '_timetz'
79+
| '_timestamp'
80+
| '_timestamptz'
81+
| '_tsquery'
82+
| '_tsvector'
83+
| '_txid_snapshot'
84+
| '_uuid'
85+
| '_xml';
86+
87+
export type ColumnType = SingularColumnType | ArrayColumnType;
4888

4989
export type ColumnSchema = {
5090
column_name: string;

src/index.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ async () => {
3131
.where(['id', Op.$in, [1, 2, 3]])
3232
.execute();
3333

34-
// $ExpectType { readonly name: string | null; readonly id: number; }[]
34+
// $ExpectType { readonly id: number; readonly name: string | null; }[]
3535
await db.from(User).select('*').execute();
3636
};

src/index.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,47 @@ type Pretty<T> = { [K in keyof T]: T[K] };
2727
* @description Convert SQL column string literal type to JavaScript type
2828
*/
2929
type SupportedTypes = {
30+
bit: 'Not supported yet!';
31+
bool: boolean;
32+
box: 'Not supported yet!';
33+
bytea: 'Not supported yet!';
34+
char: string;
35+
cidr: 'Not supported yet!';
36+
circle: 'Not supported yet!';
3037
date: Date;
31-
text: string;
38+
float4: number;
39+
float8: number;
40+
inet: 'Not supported yet!';
41+
int2: number;
3242
int4: number;
43+
int8: number;
44+
interval: 'Not supported yet!';
45+
json: 'Not supported yet!';
46+
jsonb: 'Not supported yet!';
47+
line: 'Not supported yet!';
48+
lseg: 'Not supported yet!';
49+
macaddr: 'Not supported yet!';
50+
macaddr8: 'Not supported yet!';
51+
money: 'Not supported yet!';
52+
numeric: number;
53+
path: 'Not supported yet!';
54+
pg_lsn: 'Not supported yet!';
55+
point: 'Not supported yet!';
56+
polygon: 'Not supported yet!';
57+
text: string;
58+
time: 'Not supported yet!';
59+
timestamp: number;
60+
timestamptz: number;
61+
timetz: 'Not supported yet!';
62+
tsquery: 'Not supported yet!';
63+
tsvector: 'Not supported yet!';
64+
txid_snapshot: 'Not supported yet!';
65+
uuid: 'Not supported yet!';
66+
varbit: 'Not supported yet!';
67+
varchar: string;
68+
xml: 'Not supported yet!';
3369
};
70+
3471
type GetJSTypeFromSqlType<
3572
T extends ColumnType,
3673
Nullable extends boolean

0 commit comments

Comments
 (0)