Skip to content

Commit 69515d8

Browse files
committed
chore: use typebox for column create args type
1 parent 6fd3b24 commit 69515d8

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

src/lib/PostgresMetaColumns.ts

+20-27
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,7 @@ import { ident, literal } from 'pg-format'
22
import PostgresMetaTables from './PostgresMetaTables'
33
import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
44
import { columnsSql } from './sql'
5-
import { PostgresMetaResult, PostgresColumn } from './types'
6-
7-
interface ColumnCreationRequest {
8-
table_id: number
9-
name: string
10-
type: string
11-
default_value?: any
12-
default_value_format?: 'expression' | 'literal'
13-
is_identity?: boolean
14-
identity_generation?: 'BY DEFAULT' | 'ALWAYS'
15-
is_nullable?: boolean
16-
is_primary_key?: boolean
17-
is_unique?: boolean
18-
comment?: string
19-
check?: string
20-
}
21-
22-
interface ColumnBatchInfoRequest {
23-
ids?: string[]
24-
names?: string[]
25-
table?: string
26-
schema?: string
27-
}
5+
import { PostgresMetaResult, PostgresColumn, PostgresColumnCreate } from './types'
286

297
export default class PostgresMetaColumns {
308
query: (sql: string) => Promise<PostgresMetaResult<any>>
@@ -97,12 +75,27 @@ export default class PostgresMetaColumns {
9775
return { data: null, error: { message: 'Invalid parameters on column retrieve' } }
9876
}
9977

78+
async batchRetrieve({ ids }: { ids: string[] }): Promise<PostgresMetaResult<PostgresColumn[]>>
79+
async batchRetrieve({
80+
names,
81+
table,
82+
schema,
83+
}: {
84+
names: string[]
85+
table: string
86+
schema: string
87+
}): Promise<PostgresMetaResult<PostgresColumn[]>>
10088
async batchRetrieve({
10189
ids,
10290
names,
10391
table,
10492
schema = 'public',
105-
}: ColumnBatchInfoRequest): Promise<PostgresMetaResult<PostgresColumn[]>> {
93+
}: {
94+
ids?: string[]
95+
names?: string[]
96+
table?: string
97+
schema?: string
98+
}): Promise<PostgresMetaResult<PostgresColumn[]>> {
10699
if (ids && ids.length > 0) {
107100
const regexp = /^(\d+)\.(\d+)$/
108101
const filteringClauses = ids
@@ -145,7 +138,7 @@ export default class PostgresMetaColumns {
145138
}
146139
}
147140

148-
async create(col: ColumnCreationRequest): Promise<PostgresMetaResult<PostgresColumn>> {
141+
async create(col: PostgresColumnCreate): Promise<PostgresMetaResult<PostgresColumn>> {
149142
const { data, error } = await this.batchCreate([col])
150143
if (data) {
151144
return { data: data[0], error: null }
@@ -155,7 +148,7 @@ export default class PostgresMetaColumns {
155148
return { data: null, error: { message: 'Invalid params' } }
156149
}
157150

158-
async batchCreate(cols: ColumnCreationRequest[]): Promise<PostgresMetaResult<PostgresColumn[]>> {
151+
async batchCreate(cols: PostgresColumnCreate[]): Promise<PostgresMetaResult<PostgresColumn[]>> {
159152
if (cols.length < 1) {
160153
throw new Error('no columns provided for creation')
161154
}
@@ -199,7 +192,7 @@ COMMIT;
199192
is_unique = false,
200193
comment,
201194
check,
202-
}: ColumnCreationRequest,
195+
}: PostgresColumnCreate,
203196
schema: string,
204197
table: string
205198
) {

src/lib/types.ts

+20
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ export const postgresColumnSchema = Type.Object({
4242
})
4343
export type PostgresColumn = Static<typeof postgresColumnSchema>
4444

45+
export const postgresColumnCreateSchema = Type.Object({
46+
table_id: Type.Integer(),
47+
name: Type.String(),
48+
type: Type.String(),
49+
default_value: Type.Optional(Type.Any()),
50+
default_value_format: Type.Optional(
51+
Type.Union([Type.Literal('expression'), Type.Literal('literal')])
52+
),
53+
is_identity: Type.Optional(Type.Boolean()),
54+
identity_generation: Type.Optional(
55+
Type.Union([Type.Literal('BY DEFAULT'), Type.Literal('ALWAYS')])
56+
),
57+
is_nullable: Type.Optional(Type.Boolean()),
58+
is_primary_key: Type.Optional(Type.Boolean()),
59+
is_unique: Type.Optional(Type.Boolean()),
60+
comment: Type.Optional(Type.String()),
61+
check: Type.Optional(Type.String()),
62+
})
63+
export type PostgresColumnCreate = Static<typeof postgresColumnCreateSchema>
64+
4565
// TODO Rethink config.sql
4666
export const postgresConfigSchema = Type.Object({
4767
name: Type.Unknown(),

0 commit comments

Comments
 (0)