Skip to content
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

add generic interface support for tableSchema function #1437

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 30 additions & 5 deletions src/Schema/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,47 @@

// NOTE: Only require files needed (critical path on web)
import invariant from '../utils/common/invariant'
import type { $Exact, $RE } from '../types'
import type { $Exact, $RE, GetType, IsOptional, RequireKey } from '../types'

import type Model from '../Model'

export type TableName<T extends Model> = string
export type ColumnName = string

export type ColumnType = 'string' | 'number' | 'boolean'
export type ColumnSchema = $RE<{

export type StandardColumn = $RE<{
name: ColumnName
type: ColumnType
isOptional?: boolean
isIndexed?: boolean
}>

export type MappedColumn<T extends object, K extends keyof T> = {
name: K
type: GetType<T[K]>
isOptional?: IsOptional<T[K]>
isIndexed?: boolean
}

type MakeMappedColumn<
T extends object,
U = {
[K in keyof T]: IsOptional<T[K]> extends true
? RequireKey<MappedColumn<T, K>, 'isOptional'>
: MappedColumn<T, K>
},
> = U[keyof U]

export type ColumnSchema<T extends {} | undefined = undefined> = T extends undefined
? StandardColumn
: MakeMappedColumn<T>

export type ColumnMap = { [name: ColumnName]: ColumnSchema }

export type TableSchemaSpec = $Exact<{
export type TableSchemaSpec<T extends {} = unknown> = $Exact<{
name: TableName<any>
columns: ColumnSchema[]
columns: ColumnSchema<T>[]
unsafeSql?: (string) => string
}>

Expand Down Expand Up @@ -59,4 +80,8 @@ export function appSchema({ version, tables: tableList, unsafeSql }: AppSchemaSp

export function validateColumnSchema(column: ColumnSchema): void

export function tableSchema({ name, columns: columnArray, unsafeSql }: TableSchemaSpec): TableSchema
export function tableSchema<T extends {} = unknown>({
name,
columns: columnArray,
unsafeSql,
}: TableSchemaSpec<T>): TableSchema
13 changes: 13 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,16 @@ export type Array<Type> = Type[]
export type $Call<F, T> = any

export type $ReadOnlyArray<T> = T[]

export type RequireKey<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>

export type IsOptional<T> = Exclude<T, string | number | boolean> extends never ? false : true

// Assumes that arrays and objects will be stored as json strings
export type GetType<T> = T extends string
? 'string'
: T extends number
? 'number'
: T extends boolean
? 'boolean'
: 'string'