CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
custom.d.ts156 linesDownload Raw Back to columns
1import type { ColumnBuilderBaseConfig } from "../../column-builder.js";2import type { ColumnBaseConfig } from "../../column.js";3import { entityKind } from "../../entity.js";4import type { SQL } from "../../sql/sql.js";5import type { AnySQLiteTable } from "../table.js";6import { type Equal } from "../../utils.js";7import { SQLiteColumn, SQLiteColumnBuilder } from "./common.js";8export type ConvertCustomConfig<TName extends string, T extends Partial<CustomTypeValues>> = {9    name: TName;10    dataType: 'custom';11    columnType: 'SQLiteCustomColumn';12    data: T['data'];13    driverParam: T['driverData'];14    enumValues: undefined;15} & (T['notNull'] extends true ? {16    notNull: true;17} : {}) & (T['default'] extends true ? {18    hasDefault: true;19} : {});20export interface SQLiteCustomColumnInnerConfig {21    customTypeValues: CustomTypeValues;22}23export declare class SQLiteCustomColumnBuilder<T extends ColumnBuilderBaseConfig<'custom', 'SQLiteCustomColumn'>> extends SQLiteColumnBuilder<T, {24    fieldConfig: CustomTypeValues['config'];25    customTypeParams: CustomTypeParams<any>;26}, {27    sqliteColumnBuilderBrand: 'SQLiteCustomColumnBuilderBrand';28}> {29    static readonly [entityKind]: string;30    constructor(name: T['name'], fieldConfig: CustomTypeValues['config'], customTypeParams: CustomTypeParams<any>);31}32export declare class SQLiteCustomColumn<T extends ColumnBaseConfig<'custom', 'SQLiteCustomColumn'>> extends SQLiteColumn<T> {33    static readonly [entityKind]: string;34    private sqlName;35    private mapTo?;36    private mapFrom?;37    constructor(table: AnySQLiteTable<{38        name: T['tableName'];39    }>, config: SQLiteCustomColumnBuilder<T>['config']);40    getSQLType(): string;41    mapFromDriverValue(value: T['driverParam']): T['data'];42    mapToDriverValue(value: T['data']): T['driverParam'];43}44export type CustomTypeValues = {45    /**46     * Required type for custom column, that will infer proper type model47     *48     * Examples:49     *50     * If you want your column to be `string` type after selecting/or on inserting - use `data: string`. Like `text`, `varchar`51     *52     * If you want your column to be `number` type after selecting/or on inserting - use `data: number`. Like `integer`53     */54    data: unknown;55    /**56     * Type helper, that represents what type database driver is accepting for specific database data type57     */58    driverData?: unknown;59    /**60     * What config type should be used for {@link CustomTypeParams} `dataType` generation61     */62    config?: Record<string, any>;63    /**64     * Whether the config argument should be required or not65     * @default false66     */67    configRequired?: boolean;68    /**69     * If your custom data type should be notNull by default you can use `notNull: true`70     *71     * @example72     * const customSerial = customType<{ data: number, notNull: true, default: true }>({73     * 	  dataType() {74     * 	    return 'serial';75     *    },76     * });77     */78    notNull?: boolean;79    /**80     * If your custom data type has default you can use `default: true`81     *82     * @example83     * const customSerial = customType<{ data: number, notNull: true, default: true }>({84     * 	  dataType() {85     * 	    return 'serial';86     *    },87     * });88     */89    default?: boolean;90};91export interface CustomTypeParams<T extends CustomTypeValues> {92    /**93     * Database data type string representation, that is used for migrations94     * @example95     * ```96     * `jsonb`, `text`97     * ```98     *99     * If database data type needs additional params you can use them from `config` param100     * @example101     * ```102     * `varchar(256)`, `numeric(2,3)`103     * ```104     *105     * To make `config` be of specific type please use config generic in {@link CustomTypeValues}106     *107     * @example108     * Usage example109     * ```110     *   dataType() {111     *     return 'boolean';112     *   },113     * ```114     * Or115     * ```116     *   dataType(config) {117     * 	   return typeof config.length !== 'undefined' ? `varchar(${config.length})` : `varchar`;118     * 	 }119     * ```120     */121    dataType: (config: T['config'] | (Equal<T['configRequired'], true> extends true ? never : undefined)) => string;122    /**123     * Optional mapping function, between user input and driver124     * @example125     * For example, when using jsonb we need to map JS/TS object to string before writing to database126     * ```127     * toDriver(value: TData): string {128     * 	 return JSON.stringify(value);129     * }130     * ```131     */132    toDriver?: (value: T['data']) => T['driverData'] | SQL;133    /**134     * Optional mapping function, that is responsible for data mapping from database to JS/TS code135     * @example136     * For example, when using timestamp we need to map string Date representation to JS Date137     * ```138     * fromDriver(value: string): Date {139     * 	return new Date(value);140     * },141     * ```142     */143    fromDriver?: (value: T['driverData']) => T['data'];144}145/**146 * Custom sqlite database data type generator147 */148export declare function customType<T extends CustomTypeValues = CustomTypeValues>(customTypeParams: CustomTypeParams<T>): Equal<T['configRequired'], true> extends true ? {149    <TConfig extends Record<string, any> & T['config']>(fieldConfig: TConfig): SQLiteCustomColumnBuilder<ConvertCustomConfig<'', T>>;150    <TName extends string>(dbName: TName, fieldConfig: T['config']): SQLiteCustomColumnBuilder<ConvertCustomConfig<TName, T>>;151} : {152    (): SQLiteCustomColumnBuilder<ConvertCustomConfig<'', T>>;153    <TConfig extends Record<string, any> & T['config']>(fieldConfig?: TConfig): SQLiteCustomColumnBuilder<ConvertCustomConfig<'', T>>;154    <TName extends string>(dbName: TName, fieldConfig?: T['config']): SQLiteCustomColumnBuilder<ConvertCustomConfig<TName, T>>;155};156