Skip to content

Commit

Permalink
Update typegen to handle numeric(x,y) types (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
johncsnyder authored Oct 30, 2022
1 parent 59b34ba commit 623fed9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/typegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const generate = async (params: Partial<Options>) => {
return itemType.match(/^\w+$/) ? `${itemType}[]` : `Array<${itemType}>`
}

if (regtype.match(/\(\d+\)/)) {
if (regtype.match(/\([\d, ]+\)/)) {
// e.g. `character varying(10)`, which is the regtype from `create table t(s varchar(10))`
return getTypeScriptType(regtype.split('(')[0], typeName)
}
Expand Down
26 changes: 25 additions & 1 deletion packages/typegen/test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ beforeEach(async () => {
j json,
jb jsonb,
j_nn json not null,
jb_nn jsonb not null
jb_nn jsonb not null,
d_p numeric(8),
d_ps numeric(8, 4)
);
comment on column test_table.t is 'Some custom comment on "t"';
Expand Down Expand Up @@ -76,6 +78,8 @@ test('write types', async () => {
where n = 1
)
\`,
sql\`select d_p from test_table\`,
sql\`select d_ps from test_table\`,
]
`,
},
Expand Down Expand Up @@ -123,6 +127,8 @@ test('write types', async () => {
where n = 1
)
\`,
sql<queries.TestTable_dP>\`select d_p from test_table\`,
sql<queries.TestTable_dPs>\`select d_ps from test_table\`,
]
export declare namespace queries {
Expand Down Expand Up @@ -172,6 +178,12 @@ test('write types', async () => {
/** column: \`options_test.test_table.jb_nn\`, not null: \`true\`, regtype: \`jsonb\` */
jb_nn: unknown
/** column: \`options_test.test_table.d_p\`, regtype: \`numeric(8,0)\` */
d_p: number | null
/** column: \`options_test.test_table.d_ps\`, regtype: \`numeric(8,4)\` */
d_ps: number | null
}
/**
Expand Down Expand Up @@ -294,6 +306,18 @@ test('write types', async () => {
/** column: \`options_test.test_table.t_nn\`, not null: \`true\`, regtype: \`text\` */
t_nn_aliased: string
}
/** - query: \`select d_p from test_table\` */
export interface TestTable_dP {
/** column: \`options_test.test_table.d_p\`, regtype: \`numeric(8,0)\` */
d_p: number | null
}
/** - query: \`select d_ps from test_table\` */
export interface TestTable_dPs {
/** column: \`options_test.test_table.d_ps\`, regtype: \`numeric(8,4)\` */
d_ps: number | null
}
}
"
`)
Expand Down

0 comments on commit 623fed9

Please sign in to comment.