@ckpack/pg-helper
Version:
A small helper of node-postgres can help you build queries more easily and safely
65 lines (64 loc) • 1.95 kB
TypeScript
import type { EmptyObj } from '../types';
import type * as SqlUtilsType from '../types/sql-utils';
import type { SqlTemplate, SqlTempParams } from '../types/pg-helper';
declare const symbolLiteral: unique symbol;
/**
* sqlTemp to pg query params
* ```
* sqlTemp: `select * from ${USERS} where username = {username}`
* sqlParams: {username: 'xiaohong'}
* to
* sql: `select * from ${USERS} where username = $1`
* values: ['xiaohong']
* ```
* @param sqlTemp sql
* @param sqlParams value
*/
declare function sqlTemplate(sqlTemp: SqlTemplate, sqlParams?: SqlTempParams): {
sql: string;
values: any[];
};
/**
* build fields Sql
*/
declare function fieldsSql(params: string[]): string;
declare function objHump2underline(obj: EmptyObj): EmptyObj;
declare function rowsUnderline2hump(rows: EmptyObj[]): EmptyObj[];
/**
* build order sql
*/
declare function orderSql(orders?: SqlUtilsType.Order[]): string;
/**
* build where sql
*/
declare function whereSql(where?: SqlUtilsType.Where): string;
/**
* build limit offset sql
*/
declare function limitOffsetSql(params: SqlUtilsType.LimitOffset): string;
/**
* build include Sql
*/
declare function includeSql(params?: SqlUtilsType.Include[]): string;
/**
* build update sql
*/
declare function updateSql(params?: SqlUtilsType.Update): string;
/**
* build insert sql
*/
declare function insertSql(params: SqlUtilsType.Insert): {
sql: string;
data: any[];
};
/**
* build returning sql
*/
declare function returningSql(returning?: SqlUtilsType.Returning): string;
/**
* Functions used internally to build sql, It is useful to construct some special SQL, the returned sql will not be used as a template for the key
*/
declare function literalSql(sql: string): {
[symbolLiteral]: string;
};
export { sqlTemplate, fieldsSql, rowsUnderline2hump, objHump2underline, orderSql, whereSql, limitOffsetSql, includeSql, updateSql, insertSql, returningSql, literalSql };