@phyng/typeschema
Version:
A database schema definition library using TypeScript or JSON
52 lines (50 loc) • 1.28 kB
TypeScript
type BaseFieldType = {
name: string;
label?: string;
null?: boolean;
pk?: boolean;
increment?: boolean;
};
type BaseFieldOptions = {
label?: string;
null?: boolean;
pk?: boolean;
increment?: boolean;
};
type IntFieldType = BaseFieldType & {
type: 'int';
};
type StringFieldType = BaseFieldType & {
type: 'string';
maxLength?: number;
};
type StringFieldOptions = BaseFieldOptions & {
maxLength?: number;
};
type BooleanFieldType = BaseFieldType & {
type: 'boolean';
};
type FieldType = IntFieldType | StringFieldType | BooleanFieldType;
type FieldTypeMap = {
int: IntFieldType;
string: StringFieldType;
boolean: BooleanFieldType;
};
type FieldOptionsMap = {
int: BaseFieldOptions;
string: StringFieldOptions;
boolean: BaseFieldOptions;
};
type TableType = {
name: string;
fields: FieldType[];
};
type DbType = {
type: 'db';
tables: TableType[];
};
declare const Db: (tables: TableType[]) => DbType;
declare const Table: (name: string, fields: FieldType[]) => TableType;
declare const Field: <T extends FieldType["type"]>(name: string, type: T, options?: FieldOptionsMap[T]) => FieldTypeMap[T];
export { Db, Field, Table };
export type { DbType, FieldType, FieldTypeMap, TableType };