node-mysql-query-utils
Advanced tools
Comparing version 1.0.1 to 1.0.2
# Changelog | ||
## [1.0.2] - 2024-09-30 | ||
### Added | ||
- Added `options` parameter to `buildQuery` method to support query formatting. | ||
- Made `queryParts` private to prevent accidental exposure. `queryParts` is an internal structure used to build SQL queries and should not be accessed directly. | ||
## [1.0.1] - 2024-09-27 | ||
@@ -4,0 +9,0 @@ ### Fixed |
@@ -1,5 +0,6 @@ | ||
import { ColumnData, DeleteQueryBuilder, FromQueryBuilder, GroupByField, GroupByQueryBuilder, InsertOptions, InsertQueryBuilder, InsertValue, JoinQueryBuilder, JoinType, LimitQueryBuilder, OffsetQueryBuilder, OrderByField, OrderByQueryBuilder, QueryFunction, SelectFields, SelectQueryBuilder, SetQueryBuilder, SQL_CONSTRUCTORS, UpdateOptions, UpdateQueryBuilder, UpdateQueryBuilderWithoutSet, WhereCondition, WhereQueryBuilder } from './types'; | ||
export declare class SQLBuilder<ColumnKeys extends string, QueryReturnType = any> implements SelectQueryBuilder<ColumnKeys, QueryReturnType>, FromQueryBuilder<ColumnKeys, QueryReturnType>, JoinQueryBuilder<ColumnKeys, QueryReturnType>, WhereQueryBuilder<ColumnKeys, QueryReturnType>, GroupByQueryBuilder<QueryReturnType>, OrderByQueryBuilder<QueryReturnType>, LimitQueryBuilder<QueryReturnType>, OffsetQueryBuilder<QueryReturnType>, DeleteQueryBuilder<ColumnKeys, QueryReturnType>, UpdateQueryBuilder<ColumnKeys, QueryReturnType>, UpdateQueryBuilderWithoutSet<ColumnKeys, QueryReturnType>, SetQueryBuilder<ColumnKeys, QueryReturnType>, InsertQueryBuilder<QueryReturnType> { | ||
queryParts: SQL_CONSTRUCTORS; | ||
private queryFn?; | ||
import { BuildQueryOptions, ColumnData, DeleteQueryBuilder, FromQueryBuilder, GroupByField, GroupByQueryBuilder, InsertOptions, InsertQueryBuilder, InsertValue, JoinQueryBuilder, JoinType, LimitQueryBuilder, OffsetQueryBuilder, OrderByField, OrderByQueryBuilder, QueryFunction, SelectFields, SelectQueryBuilder, SetQueryBuilder, UpdateOptions, UpdateQueryBuilder, UpdateQueryBuilderWithoutSet, WhereCondition, WhereQueryBuilder } from './types'; | ||
export declare class SQLBuilder<ColumnKeys extends string, QueryReturnType = any> { | ||
#private; | ||
queryFn?: QueryFunction; | ||
message: string; | ||
constructor(queryFn?: QueryFunction); | ||
@@ -35,3 +36,3 @@ private extractTableAndAlias; | ||
deleteFrom(table: string): DeleteQueryBuilder<ColumnKeys, QueryReturnType>; | ||
buildQuery(): { | ||
buildQuery(options?: BuildQueryOptions): { | ||
sql: string; | ||
@@ -38,0 +39,0 @@ params: any[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SQLBuilder = void 0; | ||
const mysql2_1 = require("mysql2"); | ||
class SQLBuilder { | ||
queryParts; | ||
#queryParts; | ||
queryFn; | ||
message = 'Call .buildQuery() or .executeQuery() to get the result'; | ||
constructor(queryFn) { | ||
this.queryParts = { | ||
this.#queryParts = { | ||
count: { sql: "", params: [] }, | ||
@@ -183,6 +185,6 @@ select: { sql: "", params: [] }, | ||
const countClause = `COUNT(${field === '*' ? '*' : '??'})`; | ||
this.queryParts.select.sql = `SELECT ${alias ? `${countClause} AS ??` : countClause}`; | ||
this.queryParts.select.params = field === '*' ? [] : [field]; | ||
this.#queryParts.select.sql = `SELECT ${alias ? `${countClause} AS ??` : countClause}`; | ||
this.#queryParts.select.params = field === '*' ? [] : [field]; | ||
if (alias) { | ||
this.queryParts.select.params.push(alias); | ||
this.#queryParts.select.params.push(alias); | ||
} | ||
@@ -192,19 +194,19 @@ return this; | ||
max(field, alias) { | ||
this.queryParts.select.sql = `SELECT MAX(??)${alias ? ` AS ??` : ''}`; | ||
this.queryParts.select.params = alias ? [field, alias] : [field]; | ||
this.#queryParts.select.sql = `SELECT MAX(??)${alias ? ` AS ??` : ''}`; | ||
this.#queryParts.select.params = alias ? [field, alias] : [field]; | ||
return this; | ||
} | ||
min(field, alias) { | ||
this.queryParts.select.sql = `SELECT MIN(??)${alias ? ` AS ??` : ''}`; | ||
this.queryParts.select.params = alias ? [field, alias] : [field]; | ||
this.#queryParts.select.sql = `SELECT MIN(??)${alias ? ` AS ??` : ''}`; | ||
this.#queryParts.select.params = alias ? [field, alias] : [field]; | ||
return this; | ||
} | ||
avg(field, alias) { | ||
this.queryParts.select.sql = `SELECT AVG(??)${alias ? ` AS ??` : ''}`; | ||
this.queryParts.select.params = alias ? [field, alias] : [field]; | ||
this.#queryParts.select.sql = `SELECT AVG(??)${alias ? ` AS ??` : ''}`; | ||
this.#queryParts.select.params = alias ? [field, alias] : [field]; | ||
return this; | ||
} | ||
sum(field, alias) { | ||
this.queryParts.select.sql = `SELECT SUM(??)${alias ? ` AS ??` : ''}`; | ||
this.queryParts.select.params = alias ? [field, alias] : [field]; | ||
this.#queryParts.select.sql = `SELECT SUM(??)${alias ? ` AS ??` : ''}`; | ||
this.#queryParts.select.params = alias ? [field, alias] : [field]; | ||
return this; | ||
@@ -215,4 +217,4 @@ } | ||
const { sql, params } = this.processFields(_fields); | ||
this.queryParts.select.sql = sql; | ||
this.queryParts.select.params = params; | ||
this.#queryParts.select.sql = sql; | ||
this.#queryParts.select.params = params; | ||
return this; | ||
@@ -223,7 +225,7 @@ } | ||
const [tableName, extractedAlias] = this.extractTableAndAlias(table, alias); | ||
this.queryParts.from.sql = `FROM ??`; | ||
this.queryParts.from.params = [tableName]; | ||
this.#queryParts.from.sql = `FROM ??`; | ||
this.#queryParts.from.params = [tableName]; | ||
if (extractedAlias) { | ||
this.queryParts.from.sql += ` AS ??`; | ||
this.queryParts.from.params.push(alias || extractedAlias); | ||
this.#queryParts.from.sql += ` AS ??`; | ||
this.#queryParts.from.params.push(alias || extractedAlias); | ||
} | ||
@@ -247,4 +249,4 @@ return this; | ||
// Append the new join clause and parameters | ||
this.queryParts.join.sql += (this.queryParts.join.sql ? ' ' : '') + joinClause; | ||
this.queryParts.join.params.push(...joinParams); | ||
this.#queryParts.join.sql += (this.#queryParts.join.sql ? ' ' : '') + joinClause; | ||
this.#queryParts.join.params.push(...joinParams); | ||
return this; | ||
@@ -254,4 +256,4 @@ } | ||
const { clause, params } = this.buildWhereClause(conditions); | ||
this.queryParts.where.sql = clause ? `WHERE ${clause}` : ''; | ||
this.queryParts.where.params = params; | ||
this.#queryParts.where.sql = clause ? `WHERE ${clause}` : ''; | ||
this.#queryParts.where.params = params; | ||
return this; | ||
@@ -261,8 +263,8 @@ } | ||
if (typeof fields === 'string') { | ||
this.queryParts.groupBy.sql = 'GROUP BY ??'; | ||
this.queryParts.groupBy.params = [fields]; | ||
this.#queryParts.groupBy.sql = 'GROUP BY ??'; | ||
this.#queryParts.groupBy.params = [fields]; | ||
} | ||
else if (Array.isArray(fields) && fields.length > 0) { | ||
this.queryParts.groupBy.sql = 'GROUP BY ' + fields.map(() => '??').join(', '); | ||
this.queryParts.groupBy.params = fields; | ||
this.#queryParts.groupBy.sql = 'GROUP BY ' + fields.map(() => '??').join(', '); | ||
this.#queryParts.groupBy.params = fields; | ||
} | ||
@@ -274,4 +276,4 @@ return this; | ||
return this; | ||
this.queryParts.orderBy.sql = `ORDER BY ${fields.map(({ field, direction }) => `?? ${direction || 'ASC'}`).join(', ')}`; | ||
this.queryParts.orderBy.params = fields.map(({ field }) => field); | ||
this.#queryParts.orderBy.sql = `ORDER BY ${fields.map(({ field, direction }) => `?? ${direction || 'ASC'}`).join(', ')}`; | ||
this.#queryParts.orderBy.params = fields.map(({ field }) => field); | ||
return this; | ||
@@ -282,4 +284,4 @@ } | ||
return this; | ||
this.queryParts.limit.sql = `LIMIT ?`; | ||
this.queryParts.limit.params = [limit]; | ||
this.#queryParts.limit.sql = `LIMIT ?`; | ||
this.#queryParts.limit.params = [limit]; | ||
return this; | ||
@@ -290,4 +292,4 @@ } | ||
return this; | ||
this.queryParts.offset.sql = `OFFSET ?`; | ||
this.queryParts.offset.params = [offset]; | ||
this.#queryParts.offset.sql = `OFFSET ?`; | ||
this.#queryParts.offset.params = [offset]; | ||
return this; | ||
@@ -308,4 +310,4 @@ } | ||
} | ||
this.queryParts.update.sql = `UPDATE ??`; | ||
this.queryParts.update.params = [table]; | ||
this.#queryParts.update.sql = `UPDATE ??`; | ||
this.#queryParts.update.params = [table]; | ||
this.throwEmptyObjectError(values, this.printPrefixMessage('Update :: Data cannot be empty')); | ||
@@ -322,4 +324,4 @@ if (values) { | ||
const setParams = Object.entries(values).flatMap(([key, value]) => [key, value]); | ||
this.queryParts.set.sql = `SET ${setClauses.join(', ')}`; | ||
this.queryParts.set.params = setParams; | ||
this.#queryParts.set.sql = `SET ${setClauses.join(', ')}`; | ||
this.#queryParts.set.params = setParams; | ||
return this; | ||
@@ -354,4 +356,4 @@ } | ||
} | ||
this.queryParts.insert.sql = insertClause; | ||
this.queryParts.insert.params = [table, ...columnParams, ...valueParams]; | ||
this.#queryParts.insert.sql = insertClause; | ||
this.#queryParts.insert.params = [table, ...columnParams, ...valueParams]; | ||
return this; | ||
@@ -361,42 +363,43 @@ } | ||
this.checkTableName(table, 'delete'); | ||
this.queryParts.delete.sql = `DELETE FROM ??`; | ||
this.queryParts.delete.params = [table]; | ||
this.#queryParts.delete.sql = `DELETE FROM ??`; | ||
this.#queryParts.delete.params = [table]; | ||
return this; | ||
} | ||
buildQuery() { | ||
buildQuery(options) { | ||
const { format = false } = options || {}; | ||
const sql = [ | ||
// Reminder: The order of these parts matter | ||
this.queryParts.count.sql, | ||
this.queryParts.select.sql, | ||
this.queryParts.update.sql, | ||
this.queryParts.insert.sql, | ||
this.queryParts.delete.sql, | ||
this.queryParts.set.sql, | ||
this.queryParts.from.sql, | ||
this.queryParts.join.sql, | ||
this.queryParts.where.sql, | ||
this.queryParts.groupBy.sql, | ||
this.queryParts.orderBy.sql, | ||
this.queryParts.limit.sql, | ||
this.queryParts.offset.sql, | ||
this.#queryParts.count.sql, | ||
this.#queryParts.select.sql, | ||
this.#queryParts.update.sql, | ||
this.#queryParts.insert.sql, | ||
this.#queryParts.delete.sql, | ||
this.#queryParts.set.sql, | ||
this.#queryParts.from.sql, | ||
this.#queryParts.join.sql, | ||
this.#queryParts.where.sql, | ||
this.#queryParts.groupBy.sql, | ||
this.#queryParts.orderBy.sql, | ||
this.#queryParts.limit.sql, | ||
this.#queryParts.offset.sql, | ||
].filter(Boolean).join(' ').trim(); | ||
const params = [ | ||
// Reminder: The order of these parts matter | ||
...(this.queryParts.count.params || []), | ||
...(this.queryParts.select.params || []), | ||
...(this.queryParts.update.params || []), | ||
...(this.queryParts.insert.params || []), | ||
...(this.queryParts.delete.params || []), | ||
...(this.queryParts.set.params || []), | ||
...(this.queryParts.from.params || []), | ||
...(this.queryParts.join.params || []), | ||
...(this.queryParts.where.params || []), | ||
...(this.queryParts.groupBy.params || []), | ||
...(this.queryParts.orderBy.params || []), | ||
...(this.queryParts.limit.params || []), | ||
...(this.queryParts.offset.params || []), | ||
...(this.#queryParts.count.params || []), | ||
...(this.#queryParts.select.params || []), | ||
...(this.#queryParts.update.params || []), | ||
...(this.#queryParts.insert.params || []), | ||
...(this.#queryParts.delete.params || []), | ||
...(this.#queryParts.set.params || []), | ||
...(this.#queryParts.from.params || []), | ||
...(this.#queryParts.join.params || []), | ||
...(this.#queryParts.where.params || []), | ||
...(this.#queryParts.groupBy.params || []), | ||
...(this.#queryParts.orderBy.params || []), | ||
...(this.#queryParts.limit.params || []), | ||
...(this.#queryParts.offset.params || []), | ||
]; | ||
return { | ||
sql, | ||
params, | ||
sql: format ? (0, mysql2_1.format)(sql, params) : sql, | ||
params: format ? [] : params, | ||
// For allowing array destructuring | ||
@@ -403,0 +406,0 @@ [Symbol.iterator]() { |
@@ -125,2 +125,5 @@ import { ConnectionOptions } from "mysql2/promise"; | ||
export type JoinType = 'INNER' | 'LEFT' | 'RIGHT' | 'FULL'; | ||
export type BuildQueryOptions = { | ||
format?: boolean; | ||
}; | ||
export type BuildQueryResult = { | ||
@@ -133,3 +136,3 @@ sql: string; | ||
export type QueryAction<QueryReturnType> = { | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -139,3 +142,3 @@ }; | ||
from(table: string, alias?: string): FromQueryBuilder<ColumnKeys, QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -147,3 +150,3 @@ }; | ||
groupBy(fields: GroupByField<ColumnKeys>): GroupByQueryBuilder<QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -158,3 +161,3 @@ } | ||
limit(limit: number): LimitQueryBuilder<QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -166,3 +169,3 @@ }; | ||
limit(limit: number): LimitQueryBuilder<T>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<K = T>(): Promise<K>; | ||
@@ -174,7 +177,7 @@ } | ||
offset(offset: number): OffsetQueryBuilder<QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
} | ||
export interface OffsetQueryBuilder<QueryReturnType> { | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -185,3 +188,3 @@ } | ||
where(conditions: WhereCondition<ColumnKeys>): WhereQueryBuilder<ColumnKeys, QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -191,3 +194,3 @@ } | ||
where(conditions: WhereCondition<ColumnKeys>): WhereQueryBuilder<ColumnKeys, QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -197,7 +200,7 @@ } | ||
where(conditions: WhereCondition<ColumnKeys>): WhereQueryBuilder<ColumnKeys, QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
} | ||
export interface InsertQueryBuilder<QueryReturnType> { | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -208,3 +211,3 @@ } | ||
limit(limit: number): LimitQueryBuilder<QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -211,0 +214,0 @@ } |
import { | ||
BuildQueryOptions, | ||
ColumnData, | ||
@@ -28,22 +29,10 @@ DeleteQueryBuilder, | ||
} from '@dto/types'; | ||
import { format as _format } from 'mysql2'; | ||
export class SQLBuilder<ColumnKeys extends string, QueryReturnType = any> implements | ||
SelectQueryBuilder<ColumnKeys, QueryReturnType>, | ||
FromQueryBuilder<ColumnKeys, QueryReturnType>, | ||
JoinQueryBuilder<ColumnKeys, QueryReturnType>, | ||
WhereQueryBuilder<ColumnKeys, QueryReturnType>, | ||
GroupByQueryBuilder<QueryReturnType>, | ||
OrderByQueryBuilder<QueryReturnType>, | ||
LimitQueryBuilder<QueryReturnType>, | ||
OffsetQueryBuilder<QueryReturnType>, | ||
DeleteQueryBuilder<ColumnKeys, QueryReturnType>, | ||
UpdateQueryBuilder<ColumnKeys, QueryReturnType>, | ||
UpdateQueryBuilderWithoutSet<ColumnKeys, QueryReturnType>, | ||
SetQueryBuilder<ColumnKeys, QueryReturnType>, | ||
InsertQueryBuilder<QueryReturnType> { | ||
queryParts: SQL_CONSTRUCTORS; | ||
private queryFn?: QueryFunction; | ||
export class SQLBuilder<ColumnKeys extends string, QueryReturnType = any> { | ||
#queryParts: SQL_CONSTRUCTORS; | ||
queryFn?: QueryFunction; | ||
message: string = 'Call .buildQuery() or .executeQuery() to get the result'; | ||
constructor(queryFn?: QueryFunction) { | ||
this.queryParts = { | ||
this.#queryParts = { | ||
count: { sql: "", params: [] }, | ||
@@ -228,6 +217,6 @@ select: { sql: "", params: [] }, | ||
const countClause = `COUNT(${field === '*' ? '*' : '??'})`; | ||
this.queryParts.select.sql = `SELECT ${alias ? `${countClause} AS ??` : countClause}`; | ||
this.queryParts.select.params = field === '*' ? [] : [field]; | ||
this.#queryParts.select.sql = `SELECT ${alias ? `${countClause} AS ??` : countClause}`; | ||
this.#queryParts.select.params = field === '*' ? [] : [field]; | ||
if (alias) { | ||
this.queryParts.select.params.push(alias); | ||
this.#queryParts.select.params.push(alias); | ||
} | ||
@@ -238,4 +227,4 @@ return this; | ||
max(field: ColumnKeys, alias?: string): SelectQueryBuilder<ColumnKeys, QueryReturnType> { | ||
this.queryParts.select.sql = `SELECT MAX(??)${alias ? ` AS ??` : ''}`; | ||
this.queryParts.select.params = alias ? [field, alias] : [field]; | ||
this.#queryParts.select.sql = `SELECT MAX(??)${alias ? ` AS ??` : ''}`; | ||
this.#queryParts.select.params = alias ? [field, alias] : [field]; | ||
return this; | ||
@@ -245,4 +234,4 @@ } | ||
min(field: ColumnKeys, alias?: string): SelectQueryBuilder<ColumnKeys, QueryReturnType> { | ||
this.queryParts.select.sql = `SELECT MIN(??)${alias ? ` AS ??` : ''}`; | ||
this.queryParts.select.params = alias ? [field, alias] : [field]; | ||
this.#queryParts.select.sql = `SELECT MIN(??)${alias ? ` AS ??` : ''}`; | ||
this.#queryParts.select.params = alias ? [field, alias] : [field]; | ||
return this; | ||
@@ -252,4 +241,4 @@ } | ||
avg(field: ColumnKeys, alias?: string): SelectQueryBuilder<ColumnKeys, QueryReturnType> { | ||
this.queryParts.select.sql = `SELECT AVG(??)${alias ? ` AS ??` : ''}`; | ||
this.queryParts.select.params = alias ? [field, alias] : [field]; | ||
this.#queryParts.select.sql = `SELECT AVG(??)${alias ? ` AS ??` : ''}`; | ||
this.#queryParts.select.params = alias ? [field, alias] : [field]; | ||
return this; | ||
@@ -259,4 +248,4 @@ } | ||
sum(field: ColumnKeys, alias?: string): SelectQueryBuilder<ColumnKeys, QueryReturnType> { | ||
this.queryParts.select.sql = `SELECT SUM(??)${alias ? ` AS ??` : ''}`; | ||
this.queryParts.select.params = alias ? [field, alias] : [field]; | ||
this.#queryParts.select.sql = `SELECT SUM(??)${alias ? ` AS ??` : ''}`; | ||
this.#queryParts.select.params = alias ? [field, alias] : [field]; | ||
return this; | ||
@@ -268,4 +257,4 @@ } | ||
const { sql, params } = this.processFields<ColumnKeys>(_fields); | ||
this.queryParts.select.sql = sql; | ||
this.queryParts.select.params = params; | ||
this.#queryParts.select.sql = sql; | ||
this.#queryParts.select.params = params; | ||
return this | ||
@@ -278,7 +267,7 @@ } | ||
this.queryParts.from.sql = `FROM ??`; | ||
this.queryParts.from.params = [tableName]; | ||
this.#queryParts.from.sql = `FROM ??`; | ||
this.#queryParts.from.params = [tableName]; | ||
if (extractedAlias) { | ||
this.queryParts.from.sql += ` AS ??`; | ||
this.queryParts.from.params.push(alias || extractedAlias); | ||
this.#queryParts.from.sql += ` AS ??`; | ||
this.#queryParts.from.params.push(alias || extractedAlias); | ||
} | ||
@@ -309,4 +298,4 @@ return this; | ||
// Append the new join clause and parameters | ||
this.queryParts.join.sql += (this.queryParts.join.sql ? ' ' : '') + joinClause; | ||
this.queryParts.join.params.push(...joinParams); | ||
this.#queryParts.join.sql += (this.#queryParts.join.sql ? ' ' : '') + joinClause; | ||
this.#queryParts.join.params.push(...joinParams); | ||
return this; | ||
@@ -317,4 +306,4 @@ } | ||
const { clause, params } = this.buildWhereClause(conditions); | ||
this.queryParts.where.sql = clause ? `WHERE ${clause}` : ''; | ||
this.queryParts.where.params = params; | ||
this.#queryParts.where.sql = clause ? `WHERE ${clause}` : ''; | ||
this.#queryParts.where.params = params; | ||
return this; | ||
@@ -325,7 +314,7 @@ } | ||
if (typeof fields === 'string') { | ||
this.queryParts.groupBy.sql = 'GROUP BY ??'; | ||
this.queryParts.groupBy.params = [fields]; | ||
this.#queryParts.groupBy.sql = 'GROUP BY ??'; | ||
this.#queryParts.groupBy.params = [fields]; | ||
} else if (Array.isArray(fields) && fields.length > 0) { | ||
this.queryParts.groupBy.sql = 'GROUP BY ' + fields.map(() => '??').join(', '); | ||
this.queryParts.groupBy.params = fields; | ||
this.#queryParts.groupBy.sql = 'GROUP BY ' + fields.map(() => '??').join(', '); | ||
this.#queryParts.groupBy.params = fields; | ||
} | ||
@@ -337,4 +326,4 @@ return this; | ||
if (!Array.isArray(fields) || fields.length === 0) return this; | ||
this.queryParts.orderBy.sql = `ORDER BY ${fields.map(({ field, direction }) => `?? ${direction || 'ASC'}`).join(', ')}`; | ||
this.queryParts.orderBy.params = fields.map(({ field }) => field); | ||
this.#queryParts.orderBy.sql = `ORDER BY ${fields.map(({ field, direction }) => `?? ${direction || 'ASC'}`).join(', ')}`; | ||
this.#queryParts.orderBy.params = fields.map(({ field }) => field); | ||
return this; | ||
@@ -345,4 +334,4 @@ } | ||
if (isNaN(limit) || limit < 0) return this; | ||
this.queryParts.limit.sql = `LIMIT ?`; | ||
this.queryParts.limit.params = [limit]; | ||
this.#queryParts.limit.sql = `LIMIT ?`; | ||
this.#queryParts.limit.params = [limit]; | ||
return this; | ||
@@ -353,4 +342,4 @@ } | ||
if (isNaN(offset) || offset < 0) return this; | ||
this.queryParts.offset.sql = `OFFSET ?`; | ||
this.queryParts.offset.params = [offset]; | ||
this.#queryParts.offset.sql = `OFFSET ?`; | ||
this.#queryParts.offset.params = [offset]; | ||
return this; | ||
@@ -382,4 +371,4 @@ } | ||
this.queryParts.update.sql = `UPDATE ??`; | ||
this.queryParts.update.params = [table]; | ||
this.#queryParts.update.sql = `UPDATE ??`; | ||
this.#queryParts.update.params = [table]; | ||
@@ -399,4 +388,4 @@ this.throwEmptyObjectError(values as Object, this.printPrefixMessage('Update :: Data cannot be empty')); | ||
const setParams = Object.entries(values).flatMap(([key, value]) => [key, value]); | ||
this.queryParts.set.sql = `SET ${setClauses.join(', ')}`; | ||
this.queryParts.set.params = setParams; | ||
this.#queryParts.set.sql = `SET ${setClauses.join(', ')}`; | ||
this.#queryParts.set.params = setParams; | ||
return this; | ||
@@ -446,4 +435,4 @@ } | ||
this.queryParts.insert.sql = insertClause; | ||
this.queryParts.insert.params = [table, ...columnParams, ...valueParams]; | ||
this.#queryParts.insert.sql = insertClause; | ||
this.#queryParts.insert.params = [table, ...columnParams, ...valueParams]; | ||
return this; | ||
@@ -454,8 +443,8 @@ } | ||
this.checkTableName(table, 'delete'); | ||
this.queryParts.delete.sql = `DELETE FROM ??`; | ||
this.queryParts.delete.params = [table]; | ||
this.#queryParts.delete.sql = `DELETE FROM ??`; | ||
this.#queryParts.delete.params = [table]; | ||
return this; | ||
} | ||
buildQuery(): { | ||
buildQuery(options?: BuildQueryOptions): { | ||
sql: string; | ||
@@ -465,17 +454,18 @@ params: any[]; | ||
} { | ||
const { format = false } = options || {}; | ||
const sql = [ | ||
// Reminder: The order of these parts matter | ||
this.queryParts.count.sql, | ||
this.queryParts.select.sql, | ||
this.queryParts.update.sql, | ||
this.queryParts.insert.sql, | ||
this.queryParts.delete.sql, | ||
this.queryParts.set.sql, | ||
this.queryParts.from.sql, | ||
this.queryParts.join.sql, | ||
this.queryParts.where.sql, | ||
this.queryParts.groupBy.sql, | ||
this.queryParts.orderBy.sql, | ||
this.queryParts.limit.sql, | ||
this.queryParts.offset.sql, | ||
this.#queryParts.count.sql, | ||
this.#queryParts.select.sql, | ||
this.#queryParts.update.sql, | ||
this.#queryParts.insert.sql, | ||
this.#queryParts.delete.sql, | ||
this.#queryParts.set.sql, | ||
this.#queryParts.from.sql, | ||
this.#queryParts.join.sql, | ||
this.#queryParts.where.sql, | ||
this.#queryParts.groupBy.sql, | ||
this.#queryParts.orderBy.sql, | ||
this.#queryParts.limit.sql, | ||
this.#queryParts.offset.sql, | ||
].filter(Boolean).join(' ').trim(); | ||
@@ -485,19 +475,19 @@ | ||
// Reminder: The order of these parts matter | ||
...(this.queryParts.count.params || []), | ||
...(this.queryParts.select.params || []), | ||
...(this.queryParts.update.params || []), | ||
...(this.queryParts.insert.params || []), | ||
...(this.queryParts.delete.params || []), | ||
...(this.queryParts.set.params || []), | ||
...(this.queryParts.from.params || []), | ||
...(this.queryParts.join.params || []), | ||
...(this.queryParts.where.params || []), | ||
...(this.queryParts.groupBy.params || []), | ||
...(this.queryParts.orderBy.params || []), | ||
...(this.queryParts.limit.params || []), | ||
...(this.queryParts.offset.params || []), | ||
...(this.#queryParts.count.params || []), | ||
...(this.#queryParts.select.params || []), | ||
...(this.#queryParts.update.params || []), | ||
...(this.#queryParts.insert.params || []), | ||
...(this.#queryParts.delete.params || []), | ||
...(this.#queryParts.set.params || []), | ||
...(this.#queryParts.from.params || []), | ||
...(this.#queryParts.join.params || []), | ||
...(this.#queryParts.where.params || []), | ||
...(this.#queryParts.groupBy.params || []), | ||
...(this.#queryParts.orderBy.params || []), | ||
...(this.#queryParts.limit.params || []), | ||
...(this.#queryParts.offset.params || []), | ||
]; | ||
return { | ||
sql, | ||
params, | ||
sql: format ? _format(sql, params) : sql, | ||
params: format ? [] : params, | ||
// For allowing array destructuring | ||
@@ -504,0 +494,0 @@ [Symbol.iterator](): Iterator<any> { |
@@ -79,3 +79,3 @@ import { ConnectionOptions } from "mysql2/promise"; | ||
// patchField?: string; | ||
} & Omit<UpdateOptions, 'primaryKey'>>; | ||
} & Omit<UpdateOptions, 'primaryKey'>>; | ||
@@ -94,2 +94,3 @@ export type SoftDeleteOptions = Prettify<{ | ||
export type BuildQueryOptions = { format?: boolean; }; | ||
export type BuildQueryResult = { sql: string; params: any[];[Symbol.iterator](): Iterator<any> }; | ||
@@ -99,3 +100,3 @@ export type QueryFunction = <T>(sql: string, params?: any[]) => Promise<T> | ||
export type QueryAction<QueryReturnType> = { | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -106,3 +107,3 @@ } | ||
from(table: string, alias?: string): FromQueryBuilder<ColumnKeys, QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -115,3 +116,3 @@ } | ||
groupBy(fields: GroupByField<ColumnKeys>): GroupByQueryBuilder<QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -127,3 +128,3 @@ } | ||
limit(limit: number): LimitQueryBuilder<QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -137,3 +138,3 @@ } | ||
limit(limit: number): LimitQueryBuilder<T>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<K = T>(): Promise<K>; | ||
@@ -144,7 +145,7 @@ } | ||
offset(offset: number): OffsetQueryBuilder<QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
} | ||
export interface OffsetQueryBuilder<QueryReturnType> { | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -155,3 +156,3 @@ } | ||
where(conditions: WhereCondition<ColumnKeys>): WhereQueryBuilder<ColumnKeys, QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -161,3 +162,3 @@ } | ||
where(conditions: WhereCondition<ColumnKeys>): WhereQueryBuilder<ColumnKeys, QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -167,7 +168,7 @@ } | ||
where(conditions: WhereCondition<ColumnKeys>): WhereQueryBuilder<ColumnKeys, QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
} | ||
export interface InsertQueryBuilder<QueryReturnType> { | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -178,3 +179,3 @@ } | ||
limit(limit: number): LimitQueryBuilder<QueryReturnType>; | ||
buildQuery(): BuildQueryResult; | ||
buildQuery(options?: BuildQueryOptions): BuildQueryResult; | ||
executeQuery<ReturnType = QueryReturnType>(): Promise<ReturnType>; | ||
@@ -181,0 +182,0 @@ } |
{ | ||
"name": "node-mysql-query-utils", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A simple MySQL query utils for Node.js", | ||
@@ -25,3 +25,3 @@ "author": "John Tam <johntam718@gmail.com>", | ||
"dev": "tsx watch test/index.ts", | ||
"dev:node14": "nodemon --watch dist test/index.js", | ||
"dev:node14": "nodemon test/index.js", | ||
"pre:publish": "yarn run build", | ||
@@ -28,0 +28,0 @@ "publish": "npm publish", |
@@ -275,2 +275,4 @@ # node-mysql-query-utils | ||
const [sql, params] = sqlBuilder.select().from(tableName).buildQuery(); | ||
// BuildQuery also accepts an options object. it will return complete query with format. params will be empty array if format is true | ||
const [sql, params] = sqlBuilder.select().from(tableName).buildQuery({ format: true }); | ||
@@ -277,0 +279,0 @@ // run your own query function if you don't connect DB by DatabaseManagement class from this package |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
185868
507
2618