Skip to content

feat: batch endpoints for column creation and retrieval #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: use typebox for column create args type
  • Loading branch information
soedirgo committed Mar 20, 2022
commit 69515d85f17ae844ad6b281ba14881762a80fa3e
47 changes: 20 additions & 27 deletions src/lib/PostgresMetaColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,7 @@ import { ident, literal } from 'pg-format'
import PostgresMetaTables from './PostgresMetaTables'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
import { columnsSql } from './sql'
import { PostgresMetaResult, PostgresColumn } from './types'

interface ColumnCreationRequest {
table_id: number
name: string
type: string
default_value?: any
default_value_format?: 'expression' | 'literal'
is_identity?: boolean
identity_generation?: 'BY DEFAULT' | 'ALWAYS'
is_nullable?: boolean
is_primary_key?: boolean
is_unique?: boolean
comment?: string
check?: string
}

interface ColumnBatchInfoRequest {
ids?: string[]
names?: string[]
table?: string
schema?: string
}
import { PostgresMetaResult, PostgresColumn, PostgresColumnCreate } from './types'

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

async batchRetrieve({ ids }: { ids: string[] }): Promise<PostgresMetaResult<PostgresColumn[]>>
async batchRetrieve({
names,
table,
schema,
}: {
names: string[]
table: string
schema: string
}): Promise<PostgresMetaResult<PostgresColumn[]>>
async batchRetrieve({
ids,
names,
table,
schema = 'public',
}: ColumnBatchInfoRequest): Promise<PostgresMetaResult<PostgresColumn[]>> {
}: {
ids?: string[]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really dislike this way of everything being optional - can we use a discriminated union type instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a TS thing - the 2 function declaration above it means it's either ids or names + table + schema

names?: string[]
table?: string
schema?: string
}): Promise<PostgresMetaResult<PostgresColumn[]>> {
if (ids && ids.length > 0) {
const regexp = /^(\d+)\.(\d+)$/
const filteringClauses = ids
Expand Down Expand Up @@ -145,7 +138,7 @@ export default class PostgresMetaColumns {
}
}

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

async batchCreate(cols: ColumnCreationRequest[]): Promise<PostgresMetaResult<PostgresColumn[]>> {
async batchCreate(cols: PostgresColumnCreate[]): Promise<PostgresMetaResult<PostgresColumn[]>> {
if (cols.length < 1) {
throw new Error('no columns provided for creation')
}
Expand Down Expand Up @@ -199,7 +192,7 @@ COMMIT;
is_unique = false,
comment,
check,
}: ColumnCreationRequest,
}: PostgresColumnCreate,
schema: string,
table: string
) {
Expand Down
20 changes: 20 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ export const postgresColumnSchema = Type.Object({
})
export type PostgresColumn = Static<typeof postgresColumnSchema>

export const postgresColumnCreateSchema = Type.Object({
table_id: Type.Integer(),
name: Type.String(),
type: Type.String(),
default_value: Type.Optional(Type.Any()),
default_value_format: Type.Optional(
Type.Union([Type.Literal('expression'), Type.Literal('literal')])
),
is_identity: Type.Optional(Type.Boolean()),
identity_generation: Type.Optional(
Type.Union([Type.Literal('BY DEFAULT'), Type.Literal('ALWAYS')])
),
is_nullable: Type.Optional(Type.Boolean()),
is_primary_key: Type.Optional(Type.Boolean()),
is_unique: Type.Optional(Type.Boolean()),
comment: Type.Optional(Type.String()),
check: Type.Optional(Type.String()),
})
export type PostgresColumnCreate = Static<typeof postgresColumnCreateSchema>

// TODO Rethink config.sql
export const postgresConfigSchema = Type.Object({
name: Type.Unknown(),
Expand Down