nodejs-mw-select-builder/select-builder-pgsql.d.ts

99 lines
3.3 KiB
TypeScript

// Not finished
declare class Text
{
constructor(sql: string, bind?: Value[]);
toString(): string;
concat(text: Text): Text;
}
export type Tables = (string | Text | TableList);
export type TableList = {
[tableName: string]: (
string
| Text
| [ 'inner'|'left'|'right'|'full', string | Text, Where ]
| TableList
)
};
export type Fields = string | string[] | { [field: string]: string };
export type Value = string | number | null | {};
// - string: 'a=b AND c=d'
// - array: [ 'a=b', [ 'a=? or b=?', 1, 2 ], [ 'a', [ 1, 2 ] ], [ '(a, b)', [ [ 1, 2 ], [ 3, 4 ] ] ] ]
// - object: { a: 1, b: [ 1, 2 ], 'a = b': [], '(a, b)': [ [ 1, 2 ], [ 2, 3 ] ], 'c=? or d=?': [ 2, 3 ] }
// - key does not contain '?', value is a scalar or non-empty array => (key IN ...)
// - key does not contain '?', value is an empty array => just (key)
// - key contains '?', value is a scalar or non-empty array => (key) with bind params (...value)
// - key is numeric, then value is treated as in array
export type Where = string
| (string | Text | Value | Value[] | [ string, Value[]|Value[][] ])
| { [field: string]: Value | Value[] | Value[][] };
export type SelectOptions = null | {
distinct_on?: string | string[],
order_by?: string | string[],
group_by?: string | string[],
limit?: string | number,
offset?: string | number,
for_update?: boolean,
lock_share?: boolean,
calc_found_rows?: boolean,
};
export type InsertValues = { [field: string]: Value } | { [field: string]: Value }[];
export type InsertOptions = {
upsert?: string|string[]|true,
ignore?: string|string[]|true,
returning?: string,
};
export type DeleteOptions = {
returning?: string,
};
export type UpdateSet = { [field: string]: Value };
export type UpdateOptions = {
returning?: string,
};
export function select_builder(t: Tables, f: Fields, w: Where, o: SelectOptions): Text;
export function where_builder(w: Where): Text;
export function quote(a: any): string;
export function quote_inline(sql: string, bind: string[]): string;
export const MS_HASH = 0;
export const MS_LIST = 1;
export const MS_ROW = 2;
export const MS_COL = 4;
export const MS_VALUE = 6;
export function select(dbh: object, t: Tables, f: Fields, w: Where, o?: SelectOptions, format?: 0): Promise<{}[]>;
export function select(dbh: object, t: Tables, f: Fields, w: Where, o?: SelectOptions, format?: 1): Promise<any[][]>;
export function select(dbh: object, t: Tables, f: Fields, w: Where, o?: SelectOptions, format?: 2): Promise<{}>;
export function select(dbh: object, t: Tables, f: Fields, w: Where, o?: SelectOptions, format?: 3): Promise<any[]>;
export function select(dbh: object, t: Tables, f: Fields, w: Where, o?: SelectOptions, format?: 4): Promise<any[]>;
export function select(dbh: object, t: Tables, f: Fields, w: Where, o?: SelectOptions, format?: 6): Promise<any>;
export function select(dbh: object, t: Tables, f: Fields, w: Where, o?: SelectOptions, format?: any): Promise<any>;
//export function insert<T>(dbh: { query(string): Promise<T> }, t: string, r: InsertValues, o?: InsertOptions): Promise<T>;
export function insert(dbh: object, table: string, rows: InsertValues, o?: InsertOptions): Promise<any>;
export function _delete(dbh: object, table: string, where: Where, o?: DeleteOptions): Promise<any>;