CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
sql.d.ts231 linesDownload Raw Back to sql
1import type { CasingCache } from "../casing.js";2import { entityKind } from "../entity.js";3import type { SelectResult } from "../query-builders/select.types.js";4import { Subquery } from "../subquery.js";5import type { Assume, Equal } from "../utils.js";6import type { AnyColumn } from "../column.js";7import { Column } from "../column.js";8import { Table } from "../table.js";9/**10 * This class is used to indicate a primitive param value that is used in `sql` tag.11 * It is only used on type level and is never instantiated at runtime.12 * If you see a value of this type in the code, its runtime value is actually the primitive param value.13 */14export declare class FakePrimitiveParam {15    static readonly [entityKind]: string;16}17export type Chunk = string | Table | View | AnyColumn | Name | Param | Placeholder | SQL;18export interface BuildQueryConfig {19    casing: CasingCache;20    escapeName(name: string): string;21    escapeParam(num: number, value: unknown): string;22    escapeString(str: string): string;23    prepareTyping?: (encoder: DriverValueEncoder<unknown, unknown>) => QueryTypingsValue;24    paramStartIndex?: {25        value: number;26    };27    inlineParams?: boolean;28    invokeSource?: 'indexes' | undefined;29}30export type QueryTypingsValue = 'json' | 'decimal' | 'time' | 'timestamp' | 'uuid' | 'date' | 'none';31export interface Query {32    sql: string;33    params: unknown[];34}35export interface QueryWithTypings extends Query {36    typings?: QueryTypingsValue[];37}38/**39 * Any value that implements the `getSQL` method. The implementations include:40 * - `Table`41 * - `Column`42 * - `View`43 * - `Subquery`44 * - `SQL`45 * - `SQL.Aliased`46 * - `Placeholder`47 * - `Param`48 */49export interface SQLWrapper {50    getSQL(): SQL;51    shouldOmitSQLParens?(): boolean;52}53export declare function isSQLWrapper(value: unknown): value is SQLWrapper;54export declare class StringChunk implements SQLWrapper {55    static readonly [entityKind]: string;56    readonly value: string[];57    constructor(value: string | string[]);58    getSQL(): SQL<unknown>;59}60export declare class SQL<T = unknown> implements SQLWrapper {61    readonly queryChunks: SQLChunk[];62    static readonly [entityKind]: string;63    _: {64        brand: 'SQL';65        type: T;66    };67    private shouldInlineParams;68    constructor(queryChunks: SQLChunk[]);69    append(query: SQL): this;70    toQuery(config: BuildQueryConfig): QueryWithTypings;71    buildQueryFromSourceParams(chunks: SQLChunk[], _config: BuildQueryConfig): Query;72    private mapInlineParam;73    getSQL(): SQL;74    as(alias: string): SQL.Aliased<T>;75    /**76     * @deprecated77     * Use ``sql<DataType>`query`.as(alias)`` instead.78     */79    as<TData>(): SQL<TData>;80    /**81     * @deprecated82     * Use ``sql<DataType>`query`.as(alias)`` instead.83     */84    as<TData>(alias: string): SQL.Aliased<TData>;85    mapWith<TDecoder extends DriverValueDecoder<any, any> | DriverValueDecoder<any, any>['mapFromDriverValue']>(decoder: TDecoder): SQL<GetDecoderResult<TDecoder>>;86    inlineParams(): this;87    /**88     * This method is used to conditionally include a part of the query.89     *90     * @param condition - Condition to check91     * @returns itself if the condition is `true`, otherwise `undefined`92     */93    if(condition: any | undefined): this | undefined;94}95export type GetDecoderResult<T> = T extends Column ? T['_']['data'] : T extends DriverValueDecoder<infer TData, any> | DriverValueDecoder<infer TData, any>['mapFromDriverValue'] ? TData : never;96/**97 * Any DB name (table, column, index etc.)98 */99export declare class Name implements SQLWrapper {100    readonly value: string;101    static readonly [entityKind]: string;102    protected brand: 'Name';103    constructor(value: string);104    getSQL(): SQL<unknown>;105}106/**107 * Any DB name (table, column, index etc.)108 * @deprecated Use `sql.identifier` instead.109 */110export declare function name(value: string): Name;111export interface DriverValueDecoder<TData, TDriverParam> {112    mapFromDriverValue(value: TDriverParam): TData;113}114export interface DriverValueEncoder<TData, TDriverParam> {115    mapToDriverValue(value: TData): TDriverParam | SQL;116}117export declare function isDriverValueEncoder(value: unknown): value is DriverValueEncoder<any, any>;118export declare const noopDecoder: DriverValueDecoder<any, any>;119export declare const noopEncoder: DriverValueEncoder<any, any>;120export interface DriverValueMapper<TData, TDriverParam> extends DriverValueDecoder<TData, TDriverParam>, DriverValueEncoder<TData, TDriverParam> {121}122export declare const noopMapper: DriverValueMapper<any, any>;123/** Parameter value that is optionally bound to an encoder (for example, a column). */124export declare class Param<TDataType = unknown, TDriverParamType = TDataType> implements SQLWrapper {125    readonly value: TDataType;126    readonly encoder: DriverValueEncoder<TDataType, TDriverParamType>;127    static readonly [entityKind]: string;128    protected brand: 'BoundParamValue';129    /**130     * @param value - Parameter value131     * @param encoder - Encoder to convert the value to a driver parameter132     */133    constructor(value: TDataType, encoder?: DriverValueEncoder<TDataType, TDriverParamType>);134    getSQL(): SQL<unknown>;135}136/** @deprecated Use `sql.param` instead. */137export declare function param<TData, TDriver>(value: TData, encoder?: DriverValueEncoder<TData, TDriver>): Param<TData, TDriver>;138/**139 * Anything that can be passed to the `` sql`...` `` tagged function.140 */141export type SQLChunk = StringChunk | SQLChunk[] | SQLWrapper | SQL | Table | View | Subquery | AnyColumn | Param | Name | undefined | FakePrimitiveParam | Placeholder;142export declare function sql<T>(strings: TemplateStringsArray, ...params: any[]): SQL<T>;143export declare namespace sql {144    function empty(): SQL;145    /** @deprecated - use `sql.join()` */146    function fromList(list: SQLChunk[]): SQL;147    /**148     * Convenience function to create an SQL query from a raw string.149     * @param str The raw SQL query string.150     */151    function raw(str: string): SQL;152    /**153     * Join a list of SQL chunks with a separator.154     * @example155     * ```ts156     * const query = sql.join([sql`a`, sql`b`, sql`c`]);157     * // sql`abc`158     * ```159     * @example160     * ```ts161     * const query = sql.join([sql`a`, sql`b`, sql`c`], sql`, `);162     * // sql`a, b, c`163     * ```164     */165    function join(chunks: SQLChunk[], separator?: SQLChunk): SQL;166    /**167     * Create a SQL chunk that represents a DB identifier (table, column, index etc.).168     * When used in a query, the identifier will be escaped based on the DB engine.169     * For example, in PostgreSQL, identifiers are escaped with double quotes.170     *171     * **WARNING: This function does not offer any protection against SQL injections, so you must validate any user input beforehand.**172     *173     * @example ```ts174     * const query = sql`SELECT * FROM ${sql.identifier('my-table')}`;175     * // 'SELECT * FROM "my-table"'176     * ```177     */178    function identifier(value: string): Name;179    function placeholder<TName extends string>(name: TName): Placeholder<TName>;180    function param<TData, TDriver>(value: TData, encoder?: DriverValueEncoder<TData, TDriver>): Param<TData, TDriver>;181}182export declare namespace SQL {183    class Aliased<T = unknown> implements SQLWrapper {184        readonly sql: SQL;185        readonly fieldAlias: string;186        static readonly [entityKind]: string;187        _: {188            brand: 'SQL.Aliased';189            type: T;190        };191        constructor(sql: SQL, fieldAlias: string);192        getSQL(): SQL;193    }194}195export declare class Placeholder<TName extends string = string, TValue = any> implements SQLWrapper {196    readonly name: TName;197    static readonly [entityKind]: string;198    protected: TValue;199    constructor(name: TName);200    getSQL(): SQL;201}202/** @deprecated Use `sql.placeholder` instead. */203export declare function placeholder<TName extends string>(name: TName): Placeholder<TName>;204export declare function fillPlaceholders(params: unknown[], values: Record<string, unknown>): unknown[];205export type ColumnsSelection = Record<string, unknown>;206export declare abstract class View<TName extends string = string, TExisting extends boolean = boolean, TSelection extends ColumnsSelection = ColumnsSelection> implements SQLWrapper {207    static readonly [entityKind]: string;208    _: {209        brand: 'View';210        viewBrand: string;211        name: TName;212        existing: TExisting;213        selectedFields: TSelection;214    };215    readonly $inferSelect: InferSelectViewModel<View<Assume<TName, string>, TExisting, TSelection>>;216    constructor({ name, schema, selectedFields, query }: {217        name: TName;218        schema: string | undefined;219        selectedFields: ColumnsSelection;220        query: SQL | undefined;221    });222    getSQL(): SQL<unknown>;223}224export declare function isView(view: unknown): view is View;225export declare function getViewName<T extends View>(view: T): T['_']['name'];226export type InferSelectViewModel<TView extends View> = Equal<TView['_']['selectedFields'], {227    [x: string]: unknown;228}> extends true ? {229    [x: string]: unknown;230} : SelectResult<TView['_']['selectedFields'], 'single', Record<TView['_']['name'], 'not-null'>>;231