@iorder/iorder-query-builder
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
import { PoolConfig as PgPoolConfig } from 'pg'; | ||
import { ConnectionConfig as PgConnectionConfig } from 'pg'; | ||
import { PoolOptions as MySqlPoolOptions } from 'mysql2'; | ||
@@ -6,2 +6,2 @@ import { Conditions as TypeConditions } from './src/default/types'; | ||
export * from './src/default/genericRepository'; | ||
export type { PgPoolConfig, MySqlPoolOptions, TypeConditions }; | ||
export type { PgConnectionConfig, MySqlPoolOptions, TypeConditions }; |
@@ -17,3 +17,3 @@ "use strict"; | ||
dbClient = (0, pgClient_1.createPgClient)(pool); | ||
console.log(`@starbemtech/star-db-query-builder: Postgres db client created success`); | ||
console.log(`@iorder/iorder-query-builder: Postgres db client created success`); | ||
} | ||
@@ -23,3 +23,3 @@ else if (config.type === 'mysql') { | ||
dbClient = (0, mysqlClient_1.createMysqlClient)(pool); | ||
console.info(`@starbemtech/star-db-query-builder: MySQL db client created success.`); | ||
console.info(`@iorder/iorder-query-builder: MySQL db client created success.`); | ||
} | ||
@@ -26,0 +26,0 @@ else { |
import { QueryParams } from './types'; | ||
export declare const findFirst: <T>({ tableName, dbClient, select, where, groupBy, orderBy, }: QueryParams<T>) => Promise<T | null>; | ||
export declare const findMany: <T>({ tableName, dbClient, select, where, groupBy, orderBy, limit, }: QueryParams<T>) => Promise<T[]>; | ||
export declare const findMany: <T>({ tableName, dbClient, select, where, groupBy, orderBy, limit, offset, }: QueryParams<T>) => Promise<T[]>; | ||
export declare const insert: <P, R>({ tableName, dbClient, data, returning, }: QueryParams<R> & { | ||
@@ -15,2 +15,2 @@ data: P; | ||
}) => Promise<void>; | ||
export declare const findWithJoins: <T>({ tableName, dbClient, select, joins, where, }: QueryParams<T>) => Promise<T[]>; | ||
export declare const findWithJoins: <T>({ tableName, dbClient, select, joins, where, groupBy, orderBy, limit, offset, }: QueryParams<T>) => Promise<T[]>; |
@@ -23,3 +23,3 @@ "use strict"; | ||
exports.findFirst = findFirst; | ||
const findMany = async ({ tableName, dbClient, select, where, groupBy, orderBy, limit, }) => { | ||
const findMany = async ({ tableName, dbClient, select, where, groupBy, orderBy, limit, offset, }) => { | ||
if (!tableName) | ||
@@ -34,2 +34,3 @@ throw new Error('Table name is required'); | ||
const limitClause = (0, utils_1.createLimitClause)(limit); | ||
const offsetClause = (0, utils_1.createOffsetClause)(offset); | ||
const rows = await dbClient.query(`SELECT ${fields} FROM ${tableName} | ||
@@ -40,2 +41,3 @@ ${whereClause.length > 7 ? whereClause : ''} | ||
${limitClause} | ||
${offsetClause} | ||
`, params); | ||
@@ -94,3 +96,3 @@ return rows || []; | ||
if (returning && returning.length > 0) { | ||
query += ` RETURNIN ${(0, utils_1.createSelectFields)(returning, dbClient.clientType)}`; | ||
query += ` RETURNING ${(0, utils_1.createSelectFields)(returning, dbClient.clientType)}`; | ||
} | ||
@@ -123,3 +125,3 @@ } | ||
exports.deleteOne = deleteOne; | ||
const findWithJoins = async ({ tableName, dbClient, select, joins, where, }) => { | ||
const findWithJoins = async ({ tableName, dbClient, select, joins, where, groupBy, orderBy, limit, offset, }) => { | ||
if (!tableName) | ||
@@ -132,2 +134,6 @@ throw new Error('Table name is required'); | ||
const [whereClause, params] = (0, utils_1.createWhereClause)(where, 1, dbClient.clientType); | ||
const groupByClause = (0, utils_1.createGroupByClause)(groupBy); | ||
const orderByClause = (0, utils_1.createOrderByClause)(orderBy); | ||
const limitClause = (0, utils_1.createLimitClause)(limit); | ||
const offsetClause = (0, utils_1.createOffsetClause)(offset); | ||
const queryBuilder = { | ||
@@ -138,2 +144,6 @@ select: [selectFields], | ||
where: whereClause, | ||
groupBy: [groupByClause], | ||
orderBy: orderByClause, | ||
limit: limitClause, | ||
offset: offsetClause, | ||
}; | ||
@@ -155,4 +165,16 @@ const queryString = await buildQuery(queryBuilder); | ||
} | ||
if (params.groupBy) { | ||
queryString += `${params.groupBy}`; | ||
} | ||
if (params.orderBy) { | ||
queryString += `${params.orderBy}`; | ||
} | ||
if (params.limit) { | ||
queryString += `${params.limit}`; | ||
} | ||
if (params.offset) { | ||
queryString += `${params.offset}`; | ||
} | ||
return queryString; | ||
} | ||
//# sourceMappingURL=genericRepository.js.map |
@@ -35,2 +35,3 @@ import { IDatabaseClient } from '../db/IDatabaseClient'; | ||
limit?: number; | ||
offset?: number; | ||
joins?: JoinClause[]; | ||
@@ -48,3 +49,7 @@ } | ||
where?: string; | ||
groupBy?: string[]; | ||
orderBy?: string; | ||
limit?: string; | ||
offset?: string; | ||
} | ||
export {}; |
@@ -9,1 +9,2 @@ import { Conditions, OrderBy, DBClients } from './types'; | ||
export declare const createLimitClause: (limit?: number) => string; | ||
export declare const createOffsetClause: (offset?: number) => string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createLimitClause = exports.createGroupByClause = exports.createOrderByClause = exports.createWhereClause = exports.generateSetClause = exports.generatePlaceholders = exports.createSelectFields = void 0; | ||
exports.createOffsetClause = exports.createLimitClause = exports.createGroupByClause = exports.createOrderByClause = exports.createWhereClause = exports.generateSetClause = exports.generatePlaceholders = exports.createSelectFields = void 0; | ||
const arrayToStringWithQuotes = (items, clientType) => { | ||
const itemsWithQuotes = items.map((item) => clientType === 'pg' ? `"${item}"` : `${item}`); | ||
const itemsWithQuotes = items.map((item) => clientType === 'pg' ? `${item}` : `${item}`); | ||
return itemsWithQuotes.join(', '); | ||
@@ -44,3 +44,3 @@ }; | ||
whereParts.push(clientType === 'pg' | ||
? `"${key}" ${operator} ${placeholders.replace(', ', ' AND ')}` | ||
? `${key} ${operator} ${placeholders.replace(', ', ' AND ')}` | ||
: `${key} ${operator} ${placeholders.replace(', ', ' AND ')}`); | ||
@@ -50,3 +50,3 @@ } | ||
whereParts.push(clientType === 'pg' | ||
? `"${key}" ${operator} ${placeholders.replace(', ', ' AND ')}` | ||
? `${key} ${operator} ${placeholders.replace(', ', ' AND ')}` | ||
: `${key} ${operator} (${placeholders})`); | ||
@@ -56,3 +56,3 @@ } | ||
whereParts.push(clientType === 'pg' | ||
? `"${key}" ${operator} (${placeholders})` | ||
? `${key} ${operator} (${placeholders})` | ||
: `${key} ${operator} ${placeholders}`); | ||
@@ -64,3 +64,3 @@ } | ||
whereParts.push(clientType === 'pg' | ||
? `"${key}" ${operator} ${pgPlaceholderGenerator(index++)}` | ||
? `${key} ${operator} ${pgPlaceholderGenerator(index++)}` | ||
: `${key} ${operator} ${mysqlPlaceholderGenerator()}`); | ||
@@ -153,2 +153,8 @@ values.push(value); | ||
exports.createLimitClause = createLimitClause; | ||
const createOffsetClause = (offset) => { | ||
if (!offset) | ||
return ''; | ||
return ` OFFSET ${offset}`; | ||
}; | ||
exports.createOffsetClause = createOffsetClause; | ||
//# sourceMappingURL=utils.js.map |
@@ -25,2 +25,3 @@ import { IDatabaseClient } from './src/db/IDatabaseClient' | ||
limit, | ||
offset, | ||
}: QueryParams<T>): Promise<T[]> | ||
@@ -53,6 +54,9 @@ | ||
dbClient, | ||
id, | ||
select, | ||
joins, | ||
where, | ||
}: QueryParams<T>): Promise<T[]> | ||
groupBy, | ||
orderBy, | ||
limit, | ||
offset, | ||
}: QueryParams<T>): Promise<T[]> |
@@ -1,2 +0,2 @@ | ||
import { PoolConfig as PgPoolConfig } from 'pg' | ||
import { ConnectionConfig as PgConnectionConfig } from 'pg' | ||
import { PoolOptions as MySqlPoolOptions } from 'mysql2' | ||
@@ -12,2 +12,2 @@ import { Conditions as TypeConditions } from './src/default/types' | ||
// Types Definition | ||
export type { PgPoolConfig, MySqlPoolOptions, TypeConditions } | ||
export type { PgConnectionConfig, MySqlPoolOptions, TypeConditions } |
{ | ||
"name": "@iorder/iorder-query-builder", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A query builder to be used with mysql or postgres", | ||
@@ -45,3 +45,2 @@ "author": "Julio Sousa <julio@iorder.com.br>", | ||
"eslint-plugin-prettier": "^5.1.3", | ||
"git-commit-msg-linter": "^5.0.7", | ||
"husky": "^9.0.11", | ||
@@ -55,3 +54,3 @@ "jest": "^29.7.0", | ||
"dependencies": { | ||
"mysql2": "^3.9.3", | ||
"mysql2": "^3.9.7", | ||
"pg": "^8.11.3", | ||
@@ -58,0 +57,0 @@ "uuid": "^9.0.1" |
@@ -21,3 +21,3 @@ import { Pool as PgPool } from 'pg' | ||
console.log( | ||
`@starbemtech/star-db-query-builder: Postgres db client created success` | ||
`@iorder/iorder-query-builder: Postgres db client created success` | ||
) | ||
@@ -29,3 +29,3 @@ } else if (config.type === 'mysql') { | ||
console.info( | ||
`@starbemtech/star-db-query-builder: MySQL db client created success.` | ||
`@iorder/iorder-query-builder: MySQL db client created success.` | ||
) | ||
@@ -32,0 +32,0 @@ } else { |
@@ -11,2 +11,3 @@ import { v4 as uuid } from 'uuid' | ||
generateSetClause, | ||
createOffsetClause, | ||
} from './utils' | ||
@@ -35,3 +36,3 @@ | ||
const rows = await dbClient.query<T>( | ||
const rows = await dbClient.query<T[]>( | ||
`SELECT ${fields} FROM ${tableName} | ||
@@ -56,2 +57,3 @@ ${whereClause.length > 7 ? whereClause : ''} | ||
limit, | ||
offset, | ||
}: QueryParams<T>): Promise<T[]> => { | ||
@@ -66,2 +68,3 @@ if (!tableName) throw new Error('Table name is required') | ||
const limitClause = createLimitClause(limit) | ||
const offsetClause = createOffsetClause(offset) | ||
@@ -74,2 +77,3 @@ const rows = await dbClient.query<T[]>( | ||
${limitClause} | ||
${offsetClause} | ||
`, | ||
@@ -157,3 +161,3 @@ params | ||
if (returning && returning.length > 0) { | ||
query += ` RETURNIN ${createSelectFields(returning, dbClient.clientType)}` | ||
query += ` RETURNING ${createSelectFields(returning, dbClient.clientType)}` | ||
} | ||
@@ -211,2 +215,6 @@ } | ||
where, | ||
groupBy, | ||
orderBy, | ||
limit, | ||
offset, | ||
}: QueryParams<T>): Promise<T[]> => { | ||
@@ -219,2 +227,6 @@ if (!tableName) throw new Error('Table name is required') | ||
const [whereClause, params] = createWhereClause(where, 1, dbClient.clientType) | ||
const groupByClause = createGroupByClause(groupBy) | ||
const orderByClause = createOrderByClause(orderBy) | ||
const limitClause = createLimitClause(limit) | ||
const offsetClause = createOffsetClause(offset) | ||
@@ -226,2 +238,6 @@ const queryBuilder: QueryBuilder = { | ||
where: whereClause, | ||
groupBy: [groupByClause], | ||
orderBy: orderByClause, | ||
limit: limitClause, | ||
offset: offsetClause, | ||
} | ||
@@ -249,3 +265,19 @@ | ||
if (params.groupBy) { | ||
queryString += `${params.groupBy}` | ||
} | ||
if (params.orderBy) { | ||
queryString += `${params.orderBy}` | ||
} | ||
if (params.limit) { | ||
queryString += `${params.limit}` | ||
} | ||
if (params.offset) { | ||
queryString += `${params.offset}` | ||
} | ||
return queryString | ||
} |
@@ -55,2 +55,3 @@ import { IDatabaseClient } from '../db/IDatabaseClient' | ||
limit?: number | ||
offset?: number | ||
joins?: JoinClause[] | ||
@@ -70,2 +71,6 @@ } | ||
where?: string | ||
groupBy?: string[] | ||
orderBy?: string | ||
limit?: string | ||
offset?: string | ||
} |
@@ -8,3 +8,3 @@ import { Conditions, Condition, OrderBy, DBClients } from './types' | ||
const itemsWithQuotes = items.map((item) => | ||
clientType === 'pg' ? `"${item}"` : `${item}` | ||
clientType === 'pg' ? `${item}` : `${item}` | ||
) | ||
@@ -71,3 +71,3 @@ return itemsWithQuotes.join(', ') | ||
clientType === 'pg' | ||
? `"${key}" ${operator} ${placeholders.replace(', ', ' AND ')}` | ||
? `${key} ${operator} ${placeholders.replace(', ', ' AND ')}` | ||
: `${key} ${operator} ${placeholders.replace(', ', ' AND ')}` | ||
@@ -78,3 +78,3 @@ ) | ||
clientType === 'pg' | ||
? `"${key}" ${operator} ${placeholders.replace(', ', ' AND ')}` | ||
? `${key} ${operator} ${placeholders.replace(', ', ' AND ')}` | ||
: `${key} ${operator} (${placeholders})` | ||
@@ -85,3 +85,3 @@ ) | ||
clientType === 'pg' | ||
? `"${key}" ${operator} (${placeholders})` | ||
? `${key} ${operator} (${placeholders})` | ||
: `${key} ${operator} ${placeholders}` | ||
@@ -94,3 +94,3 @@ ) | ||
clientType === 'pg' | ||
? `"${key}" ${operator} ${pgPlaceholderGenerator(index++)}` | ||
? `${key} ${operator} ${pgPlaceholderGenerator(index++)}` | ||
: `${key} ${operator} ${mysqlPlaceholderGenerator()}` | ||
@@ -197,1 +197,6 @@ ) | ||
} | ||
export const createOffsetClause = (offset?: number) => { | ||
if (!offset) return '' | ||
return ` OFFSET ${offset}` | ||
} |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
102202
3.13%17
-5.56%1838
4.31%Updated