UNPKG

bigquery-client

Version:

A feature-rich Node.js client for Google BigQuery with support for CRUD operations, transactions, query building, and advanced features like aggregate functions, pagination, and logging.

385 lines (384 loc) 15.7 kB
/** * Class representing a SQL query builder for BigQuery. */ export declare class QueryBuilder { private table; private columns; private values; private joins; private conditions; private setClauses; private groupByColumns; private orderByColumns; private limitValue?; private offsetValue?; private aggregateFunctions; private queryType; private tableSamplePercentage?; private isDistinct; /** * Creates an instance of QueryBuilder. * @param table - The name of the table to query. */ constructor(table: string); /** * Sets the columns to select in a SELECT query. * @param columns - The columns to select. Defaults to ['*']. * @returns The current instance of QueryBuilder. */ select(columns?: string[]): this; /** * Sets the columns to select in a SELECT query with DISTINCT. * @param columns - The columns to select. Defaults to ['*']. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no columns are provided. */ selectDistinct(columns?: string[]): this; /** * Sets the aggregate functions to select in a SELECT query. * @param aggregates - An array of objects containing the aggregate function and the column to apply it on. * @returns The current instance of QueryBuilder. */ selectAggregate(aggregates: { func: string; column: string; }[]): this; /** * Adds a CASE expression to the SELECT query. * @param column - The column to check in the CASE expression. * @param conditions - An array of objects containing `when` condition and `then` value. * @param elseValue - The value to return if none of the conditions match. * @param alias - The alias for the CASE result. * @returns The current instance of QueryBuilder. */ case(column: string, conditions: { when: string; then: any; }[], elseValue?: any, alias?: string): this; /** * Sets the rows to insert in an INSERT query. * @param rows - An array of objects representing the rows to insert. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no rows are provided. */ insert(rows: Record<string, any>[]): this; /** * Sets the columns and values to update in an UPDATE query. * @param set - An object representing the columns and values to update. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no columns are provided. */ update(set: Record<string, any>): this; /** * Sets the query type to DELETE. * @returns The current instance of QueryBuilder. */ delete(): this; /** * Adds a JOIN clause to the query. * @param table - The table to join. * @param on - An object representing the join condition. * @param type - The type of join. Defaults to 'INNER'. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no table or condition is provided. */ join(table: string, on: Record<string, string>, type?: string): this; /** * Sets the columns for the GROUP BY clause. * @param columns - The columns to group by. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no columns are provided. */ groupBy(columns: string[]): this; /** * Sets the columns and directions for the ORDER BY clause. * @param order - An array of objects representing the columns and directions to order by. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no columns are provided. */ orderBy(order: { column: string; direction?: 'ASC' | 'DESC'; }[]): this; /** * Sets the LIMIT clause. * @param limit - The maximum number of rows to return. * @returns The current instance of QueryBuilder. * @throws Will throw an error if the limit is negative. */ limit(limit: number): this; /** * Sets the OFFSET clause. * @param offset - The number of rows to skip. * @returns The current instance of QueryBuilder. * @throws Will throw an error if the offset is negative. */ offset(offset: number): this; /** * Adds a subquery to the SELECT clause. * @param subquery - The subquery to add. * @param alias - The alias for the subquery. * @returns The current instance of QueryBuilder. */ selectSubquery(subquery: string, alias: string): this; /** * Adds a HAVING clause to the query. * @param conditions - An object representing the conditions for the HAVING clause. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no conditions are provided. */ having(conditions: Record<string, any>): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to check for. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided. */ whereLike(column: string, pattern: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param pattern - The pattern to check for. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or pattern is provided. */ whereNotLike(column: string, pattern: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param min - The minimum value. * @param max - The maximum value. * @returns The current instance of QueryBuilder. */ whereExists(subquery: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param min - The minimum value. * @param max - The maximum value. * @returns The current instance of QueryBuilder. */ whereNotExists(subquery: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param min - The minimum value. * @param max - The maximum value. * @returns The current instance of QueryBuilder. */ whereBetween(column: string, min: any, max: any): this; /** * Adds a UNION clause to the query. * @param query - The query to union with. * @param all - Whether to use UNION ALL. * @returns The current instance of QueryBuilder. */ union(query: string, all?: boolean): this; /** * Adds a WHERE clause to the query. * @param conditions - An object representing the conditions for the WHERE clause. * @returns The current instance of QueryBuilder. */ where(conditions: Record<string, any>): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param values - The values to check for. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or values are provided. */ whereArray(column: string, values: any[]): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to check for. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided. */ wherePartition(column: string, value: string | number): this; /** * Adds a TABLESAMPLE clause to the query. * @param percentage - The percentage of the table to sample. * @returns The current instance of QueryBuilder. * @throws Will throw an error if the percentage is not between 0 and 100. */ tableSample(percentage: number): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param jsonPath - The JSON path to check. * @param value - The value to check for. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column, jsonPath, or value is provided. */ whereJsonField(column: string, jsonPath: string, value: any): this; /** * Adds a WHERE clause to the query. * @param column - The column to check for null. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column is provided. */ whereNull(column: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check for null. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column is provided. */ whereNotNull(column: string): this; /** * Adds a WHERE clause to the query. * @param conditions - An object representing the conditions for the WHERE clause. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no conditions are provided. */ whereStructField(structColumn: string, field: string, value: any): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to check for. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided. */ whereDateBetween(column: string, startDate: string, endDate: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to check for. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided. */ whereExtract(column: string, part: 'YEAR' | 'MONTH' | 'DAY', value: number): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param day - The day of the week to check for. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or day is provided. */ whereDayOfWeek(column: string, day: number): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param operator - The comparison operator. * @param value - The value to compare against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column, operator, or value is provided. */ whereDateComparison(column1: string, operator: '=' | '>' | '<' | '>=' | '<=', column2: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param format - The format to use for the comparison. * @param value - The value to compare against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column, format, or value is provided. */ selectFormattedDate(column: string, format: string, alias?: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to compare against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided. */ whereCurrentDate(column: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to compare against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided. */ whereTimestampBetween(column: string, startDate: string, endDate: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to compare against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided. */ whereDateTrunc(column: string, part: 'DAY' | 'MONTH' | 'YEAR', alias?: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to compare against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided */ whereTimestampTrunc(column: string, part: 'HOUR' | 'DAY' | 'MONTH' | 'YEAR', alias?: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to compare against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided. */ whereContains(column: string, searchTerm: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to compare against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided. */ whereArrayContains(column: string, value: any): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param length - The length to compare against. * @param operator - The comparison operator. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column, length, or operator is provided. */ whereArrayLength(column: string, length: number, operator: '=' | '>' | '<' | '>=' | '<='): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param value - The value to compare against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or value is provided. */ whereNotEqualNullSafe(column: string, value: any): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param values - The values to check against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or values are provided. */ selectStringAgg(column: string, separator: string, alias?: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param values - The values to check against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or values are provided. */ selectConditionalSum(column: string, condition: string, alias?: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param values - The values to check against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or values are provided. */ selectWindowFunction(func: 'ROW_NUMBER' | 'RANK' | 'DENSE_RANK', partitionBy: string, orderBy: string, alias?: string): this; /** * Adds a WHERE clause to the query. * @param column - The column to check. * @param values - The values to check against. * @returns The current instance of QueryBuilder. * @throws Will throw an error if no column or values are provided. */ selectJsonField(column: string, jsonPath: string, alias?: string): this; /** * Builds the SQL query string and returns it along with the parameters. * @returns An object containing the query string and the parameters. */ build(): { query: string; params: any[]; }; }