CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
relations.d.ts216 linesDownload Raw Back to drizzle-orm
1import { type AnyTable, type InferModelFromColumns, Table } from "./table.js";2import { type AnyColumn, Column } from "./column.js";3import { entityKind } from "./entity.js";4import { and, asc, between, desc, exists, ilike, inArray, isNotNull, isNull, like, not, notBetween, notExists, notIlike, notInArray, notLike, or } from "./sql/expressions/index.js";5import { type Placeholder, SQL, sql } from "./sql/sql.js";6import type { Assume, ColumnsWithTable, Equal, Simplify, ValueOrArray } from "./utils.js";7export declare abstract class Relation<TTableName extends string = string> {8    readonly sourceTable: Table;9    readonly referencedTable: AnyTable<{10        name: TTableName;11    }>;12    readonly relationName: string | undefined;13    static readonly [entityKind]: string;14    readonly $brand: 'Relation';15    readonly referencedTableName: TTableName;16    fieldName: string;17    constructor(sourceTable: Table, referencedTable: AnyTable<{18        name: TTableName;19    }>, relationName: string | undefined);20    abstract withFieldName(fieldName: string): Relation<TTableName>;21}22export declare class Relations<TTableName extends string = string, TConfig extends Record<string, Relation> = Record<string, Relation>> {23    readonly table: AnyTable<{24        name: TTableName;25    }>;26    readonly config: (helpers: TableRelationsHelpers<TTableName>) => TConfig;27    static readonly [entityKind]: string;28    readonly $brand: 'Relations';29    constructor(table: AnyTable<{30        name: TTableName;31    }>, config: (helpers: TableRelationsHelpers<TTableName>) => TConfig);32}33export declare class One<TTableName extends string = string, TIsNullable extends boolean = boolean> extends Relation<TTableName> {34    readonly config: RelationConfig<TTableName, string, AnyColumn<{35        tableName: TTableName;36    }>[]> | undefined;37    readonly isNullable: TIsNullable;38    static readonly [entityKind]: string;39    protected $relationBrand: 'One';40    constructor(sourceTable: Table, referencedTable: AnyTable<{41        name: TTableName;42    }>, config: RelationConfig<TTableName, string, AnyColumn<{43        tableName: TTableName;44    }>[]> | undefined, isNullable: TIsNullable);45    withFieldName(fieldName: string): One<TTableName>;46}47export declare class Many<TTableName extends string> extends Relation<TTableName> {48    readonly config: {49        relationName: string;50    } | undefined;51    static readonly [entityKind]: string;52    protected $relationBrand: 'Many';53    constructor(sourceTable: Table, referencedTable: AnyTable<{54        name: TTableName;55    }>, config: {56        relationName: string;57    } | undefined);58    withFieldName(fieldName: string): Many<TTableName>;59}60export type TableRelationsKeysOnly<TSchema extends Record<string, unknown>, TTableName extends string, K extends keyof TSchema> = TSchema[K] extends Relations<TTableName> ? K : never;61export type ExtractTableRelationsFromSchema<TSchema extends Record<string, unknown>, TTableName extends string> = ExtractObjectValues<{62    [K in keyof TSchema as TableRelationsKeysOnly<TSchema, TTableName, K>]: TSchema[K] extends Relations<TTableName, infer TConfig> ? TConfig : never;63}>;64export type ExtractObjectValues<T> = T[keyof T];65export type ExtractRelationsFromTableExtraConfigSchema<TConfig extends unknown[]> = ExtractObjectValues<{66    [K in keyof TConfig as TConfig[K] extends Relations<any> ? K : never]: TConfig[K] extends Relations<infer TRelationConfig> ? TRelationConfig : never;67}>;68export declare function getOperators(): {69    and: typeof and;70    between: typeof between;71    eq: import("./index.js").BinaryOperator;72    exists: typeof exists;73    gt: import("./index.js").BinaryOperator;74    gte: import("./index.js").BinaryOperator;75    ilike: typeof ilike;76    inArray: typeof inArray;77    isNull: typeof isNull;78    isNotNull: typeof isNotNull;79    like: typeof like;80    lt: import("./index.js").BinaryOperator;81    lte: import("./index.js").BinaryOperator;82    ne: import("./index.js").BinaryOperator;83    not: typeof not;84    notBetween: typeof notBetween;85    notExists: typeof notExists;86    notLike: typeof notLike;87    notIlike: typeof notIlike;88    notInArray: typeof notInArray;89    or: typeof or;90    sql: typeof sql;91};92export type Operators = ReturnType<typeof getOperators>;93export declare function getOrderByOperators(): {94    sql: typeof sql;95    asc: typeof asc;96    desc: typeof desc;97};98export type OrderByOperators = ReturnType<typeof getOrderByOperators>;99export type FindTableByDBName<TSchema extends TablesRelationalConfig, TTableName extends string> = ExtractObjectValues<{100    [K in keyof TSchema as TSchema[K]['dbName'] extends TTableName ? K : never]: TSchema[K];101}>;102export type DBQueryConfig<TRelationType extends 'one' | 'many' = 'one' | 'many', TIsRoot extends boolean = boolean, TSchema extends TablesRelationalConfig = TablesRelationalConfig, TTableConfig extends TableRelationalConfig = TableRelationalConfig> = {103    columns?: {104        [K in keyof TTableConfig['columns']]?: boolean;105    } | undefined;106    with?: {107        [K in keyof TTableConfig['relations']]?: true | DBQueryConfig<TTableConfig['relations'][K] extends One ? 'one' : 'many', false, TSchema, FindTableByDBName<TSchema, TTableConfig['relations'][K]['referencedTableName']>> | undefined;108    } | undefined;109    extras?: Record<string, SQL.Aliased> | ((fields: Simplify<[110        TTableConfig['columns']111    ] extends [never] ? {} : TTableConfig['columns']>, operators: {112        sql: Operators['sql'];113    }) => Record<string, SQL.Aliased>) | undefined;114} & (TRelationType extends 'many' ? {115    where?: SQL | undefined | ((fields: Simplify<[116        TTableConfig['columns']117    ] extends [never] ? {} : TTableConfig['columns']>, operators: Operators) => SQL | undefined);118    orderBy?: ValueOrArray<AnyColumn | SQL> | ((fields: Simplify<[119        TTableConfig['columns']120    ] extends [never] ? {} : TTableConfig['columns']>, operators: OrderByOperators) => ValueOrArray<AnyColumn | SQL>) | undefined;121    limit?: number | Placeholder | undefined;122} & (TIsRoot extends true ? {123    offset?: number | Placeholder | undefined;124} : {}) : {});125export interface TableRelationalConfig {126    tsName: string;127    dbName: string;128    columns: Record<string, Column>;129    relations: Record<string, Relation>;130    primaryKey: AnyColumn[];131    schema?: string;132}133export type TablesRelationalConfig = Record<string, TableRelationalConfig>;134export interface RelationalSchemaConfig<TSchema extends TablesRelationalConfig> {135    fullSchema: Record<string, unknown>;136    schema: TSchema;137    tableNamesMap: Record<string, string>;138}139export type ExtractTablesWithRelations<TSchema extends Record<string, unknown>> = {140    [K in keyof TSchema as TSchema[K] extends Table ? K : never]: TSchema[K] extends Table ? {141        tsName: K & string;142        dbName: TSchema[K]['_']['name'];143        columns: TSchema[K]['_']['columns'];144        relations: ExtractTableRelationsFromSchema<TSchema, TSchema[K]['_']['name']>;145        primaryKey: AnyColumn[];146    } : never;147};148export type ReturnTypeOrValue<T> = T extends (...args: any[]) => infer R ? R : T;149export type BuildRelationResult<TSchema extends TablesRelationalConfig, TInclude, TRelations extends Record<string, Relation>> = {150    [K in NonUndefinedKeysOnly<TInclude> & keyof TRelations]: TRelations[K] extends infer TRel extends Relation ? BuildQueryResult<TSchema, FindTableByDBName<TSchema, TRel['referencedTableName']>, Assume<TInclude[K], true | Record<string, unknown>>> extends infer TResult ? TRel extends One ? TResult | (Equal<TRel['isNullable'], false> extends true ? null : never) : TResult[] : never : never;151};152export type NonUndefinedKeysOnly<T> = ExtractObjectValues<{153    [K in keyof T as T[K] extends undefined ? never : K]: K;154}> & keyof T;155export type BuildQueryResult<TSchema extends TablesRelationalConfig, TTableConfig extends TableRelationalConfig, TFullSelection extends true | Record<string, unknown>> = Equal<TFullSelection, true> extends true ? InferModelFromColumns<TTableConfig['columns']> : TFullSelection extends Record<string, unknown> ? Simplify<(TFullSelection['columns'] extends Record<string, unknown> ? InferModelFromColumns<{156    [K in Equal<Exclude<TFullSelection['columns'][keyof TFullSelection['columns'] & keyof TTableConfig['columns']], undefined>, false> extends true ? Exclude<keyof TTableConfig['columns'], NonUndefinedKeysOnly<TFullSelection['columns']>> : {157        [K in keyof TFullSelection['columns']]: Equal<TFullSelection['columns'][K], true> extends true ? K : never;158    }[keyof TFullSelection['columns']] & keyof TTableConfig['columns']]: TTableConfig['columns'][K];159}> : InferModelFromColumns<TTableConfig['columns']>) & (TFullSelection['extras'] extends Record<string, unknown> | ((...args: any[]) => Record<string, unknown>) ? {160    [K in NonUndefinedKeysOnly<ReturnTypeOrValue<TFullSelection['extras']>>]: Assume<ReturnTypeOrValue<TFullSelection['extras']>[K], SQL.Aliased>['_']['type'];161} : {}) & (TFullSelection['with'] extends Record<string, unknown> ? BuildRelationResult<TSchema, TFullSelection['with'], TTableConfig['relations']> : {})> : never;162export interface RelationConfig<TTableName extends string, TForeignTableName extends string, TColumns extends AnyColumn<{163    tableName: TTableName;164}>[]> {165    relationName?: string;166    fields: TColumns;167    references: ColumnsWithTable<TTableName, TForeignTableName, TColumns>;168}169export declare function extractTablesRelationalConfig<TTables extends TablesRelationalConfig>(schema: Record<string, unknown>, configHelpers: (table: Table) => any): {170    tables: TTables;171    tableNamesMap: Record<string, string>;172};173export declare function relations<TTableName extends string, TRelations extends Record<string, Relation<any>>>(table: AnyTable<{174    name: TTableName;175}>, relations: (helpers: TableRelationsHelpers<TTableName>) => TRelations): Relations<TTableName, TRelations>;176export declare function createOne<TTableName extends string>(sourceTable: Table): <TForeignTable extends Table, TColumns extends [AnyColumn<{177    tableName: TTableName;178}>, ...AnyColumn<{179    tableName: TTableName;180}>[]]>(table: TForeignTable, config?: RelationConfig<TTableName, TForeignTable["_"]["name"], TColumns>) => One<TForeignTable["_"]["name"], Equal<TColumns[number]["_"]["notNull"], true>>;181export declare function createMany(sourceTable: Table): <TForeignTable extends Table>(referencedTable: TForeignTable, config?: {182    relationName: string;183}) => Many<TForeignTable["_"]["name"]>;184export interface NormalizedRelation {185    fields: AnyColumn[];186    references: AnyColumn[];187}188export declare function normalizeRelation(schema: TablesRelationalConfig, tableNamesMap: Record<string, string>, relation: Relation): NormalizedRelation;189export declare function createTableRelationsHelpers<TTableName extends string>(sourceTable: AnyTable<{190    name: TTableName;191}>): {192    one: <TForeignTable extends Table, TColumns extends [AnyColumn<{193        tableName: TTableName;194    }>, ...AnyColumn<{195        tableName: TTableName;196    }>[]]>(table: TForeignTable, config?: RelationConfig<TTableName, TForeignTable["_"]["name"], TColumns> | undefined) => One<TForeignTable["_"]["name"], Equal<TColumns[number]["_"]["notNull"], true>>;197    many: <TForeignTable extends Table>(referencedTable: TForeignTable, config?: {198        relationName: string;199    }) => Many<TForeignTable["_"]["name"]>;200};201export type TableRelationsHelpers<TTableName extends string> = ReturnType<typeof createTableRelationsHelpers<TTableName>>;202export interface BuildRelationalQueryResult<TTable extends Table = Table, TColumn extends Column = Column> {203    tableTsKey: string;204    selection: {205        dbKey: string;206        tsKey: string;207        field: TColumn | SQL | SQL.Aliased;208        relationTableTsKey: string | undefined;209        isJson: boolean;210        isExtra?: boolean;211        selection: BuildRelationalQueryResult<TTable>['selection'];212    }[];213    sql: TTable | SQL;214}215export declare function mapRelationalRow(tablesConfig: TablesRelationalConfig, tableConfig: TableRelationalConfig, row: unknown[], buildQueryResultSelection: BuildRelationalQueryResult['selection'], mapColumnValue?: (value: unknown) => unknown): Record<string, unknown>;216