@starbemtech/star-db-query-builder
Version:
A query builder to be used with mysql or postgres
134 lines (133 loc) • 5.02 kB
TypeScript
import { Conditions, OrderBy, DBClients } from './types';
/**
* Creates a SELECT clause for a query
*
* This function creates a SELECT clause for a query. It takes an array of fields
* and a database client type and returns a string with the fields separated by commas.
*
* @param fields - The array of fields to select
* @param clientType - The type of database client
* @returns A string with the fields separated by commas
*
* @example
* const fields = ['id', 'name', 'email']
* const clientType = 'pg'
* const result = createSelectFields(fields, clientType)
* // result will be: "id, name, email"
*/
export declare const createSelectFields: (fields: string[] | undefined, clientType: DBClients) => string;
/**
* Generates placeholders for a query
*
* This function generates placeholders for a query. It takes an array of keys
* and a database client type and returns a string with the placeholders separated by commas.
*
* @param keys - The array of keys to generate placeholders for
* @param clientType - The type of database client
* @returns A string with the placeholders separated by commas
*
* @example
* const keys = ['id', 'name', 'email']
* const clientType = 'pg'
* const result = generatePlaceholders(keys, clientType)
* // result will be: $1, $2, $3
*/
export declare const generatePlaceholders: (keys: any[], clientType: DBClients) => string;
/**
* Generates a SET clause for a query
*
* This function generates a SET clause for a query. It takes an array of keys
* and a database client type and returns a string with the keys and placeholders separated by commas.
*
* @param keys - The array of keys to generate SET clause for
* @param clientType - The type of database client
* @returns A string with the keys and placeholders separated by commas
*
* @example
* const keys = ['id', 'name', 'email']
* const clientType = 'pg'
* const result = generateSetClause(keys, clientType)
* // result will be: "id = $1, name = $2, email = $3"
*/
export declare const generateSetClause: (keys: any[], clientType: DBClients) => string;
/**
* Creates a WHERE clause for a query
*
* This function creates a WHERE clause for a query. It takes an array of conditions
* and a database client type and returns a string with the conditions separated by AND.
*
* @param conditions - The array of conditions to create WHERE clause for
* @param startIndex - The index of the first parameter
* @param clientType - The type of database client
* @param unaccent - Whether to use unaccent function
* @returns A string with the conditions separated by AND
*
* @example
* const conditions = [{ field: 'name', operator: '=', value: 'John Doe' }]
* const startIndex = 1
* const clientType = 'pg'
* const unaccent = true
* const result = createWhereClause(conditions, startIndex, clientType, unaccent)
* // result will be: "name = $1"
*/
export declare const createWhereClause: <T>(conditions: Conditions<T> | undefined, startIndex: number | undefined, clientType: DBClients, unaccent?: boolean) => [string, any[], number];
/**
* Creates an ORDER BY clause for a query
*
* This function creates an ORDER BY clause for a query. It takes an array of
* order by fields and returns a string with the fields separated by commas.
*
* @param orderBy - The array of order by fields
* @returns A string with the fields separated by commas
*
* @example
* const orderBy = [{ field: 'created_at', direction: 'DESC' }]
* const result = createOrderByClause(orderBy)
* // result will be: "ORDER BY created_at DESC"
*/
export declare const createOrderByClause: (orderBy?: OrderBy) => string;
/**
* Creates a GROUP BY clause for a query
*
* This function creates a GROUP BY clause for a query. It takes an array of
* group by fields and returns a string with the fields separated by commas.
*
* @param groupBy - The array of group by fields
* @returns A string with the fields separated by commas
*
* @example
* const groupBy = ['status']
* const result = createGroupByClause(groupBy)
* // result will be: "GROUP BY status"
*/
export declare const createGroupByClause: (groupBy?: string[]) => string;
/**
* Creates a LIMIT clause for a query
*
* This function creates a LIMIT clause for a query. It takes a limit number
* and returns a string with the limit.
*
* @param limit - The limit number
* @returns A string with the limit
*
* @example
* const limit = 10
* const result = createLimitClause(limit)
* // result will be: "LIMIT 10"
*/
export declare const createLimitClause: (limit?: number) => string;
/**
* Creates an OFFSET clause for a query
*
* This function creates an OFFSET clause for a query. It takes an offset number
* and returns a string with the offset.
*
* @param offset - The offset number
* @returns A string with the offset
*
* @example
* const offset = 10
* const result = createOffsetClause(offset)
* // result will be: "OFFSET 10"
*/
export declare const createOffsetClause: (offset?: number) => string;