Skip to content

Commit

Permalink
Add tests to dynamic selects
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Jul 30, 2024
1 parent 24a365a commit d8845cb
Show file tree
Hide file tree
Showing 5 changed files with 584 additions and 374 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export default {
})
.execute()

// Or in a modular approach
const employeeList = await qb.select<Employee>('employees').where('active = ?', true).execute()

// You get IDE type hints on each employee data, like:
// employeeList.results[0].name

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class QueryBuilder<GenericResultWrapper> {
}

select<GenericResult = DefaultReturnObject>(tableName: string): SelectBuilder<GenericResultWrapper, GenericResult> {
return new SelectBuilder(
return new SelectBuilder<GenericResultWrapper, GenericResult>(
{
tableName: tableName,
},
Expand Down Expand Up @@ -423,16 +423,29 @@ export class QueryBuilder<GenericResultWrapper> {
if (!value) return ''
if (typeof value === 'string') return ` ORDER BY ${value}`

const order: Array<Record<string, string> | string> = []
if (Array.isArray(value)) {
return ` ORDER BY ${value.join(', ')}`
for (const val of value) {
// @ts-ignore
order.push(val)
}
} else {
order.push(value)
}

const order: Array<string> = []
Object.entries(value).forEach(([key, item]) => {
order.push(`${key} ${item}`)
const result = order.map((obj) => {
if (typeof obj === 'object') {
const objs: Array<string> = []
Object.entries(obj).forEach(([key, item]) => {
objs.push(`${key} ${item}`)
})
return objs.join(', ')
} else {
return obj
}
})

return ` ORDER BY ${order.join(', ')}`
return ` ORDER BY ${result.join(', ')}`
}

_limit(value?: number): string {
Expand Down
53 changes: 30 additions & 23 deletions src/modularBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Query } from './tools'

export class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnObject> {
_debugger = false
_options: Partial<SelectAll> = {}
_queryBuilder: (params: SelectAll) => Query
private _options: Partial<SelectAll> = {}
private _queryBuilder: (params: SelectAll) => Query

constructor(options: Partial<SelectAll>, queryBuilder: (params: SelectAll) => Query) {
this._options = options
Expand All @@ -15,12 +15,8 @@ export class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnOb
this._debugger = state
}

async execute(): Promise<Query<ArrayResult<GenericResultWrapper, GenericResult>>['execute']> {
return this._queryBuilder(this._options as SelectAll).execute
}

tableName(tableName: SelectAll['tableName']): SelectBuilder<GenericResultWrapper> {
return new SelectBuilder<GenericResultWrapper>(
tableName(tableName: SelectAll['tableName']): SelectBuilder<GenericResultWrapper, GenericResult> {
return new SelectBuilder<GenericResultWrapper, GenericResult>(
{
...this._options,
tableName: tableName,
Expand All @@ -29,11 +25,14 @@ export class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnOb
)
}

fields(fields: SelectAll['fields']): SelectBuilder<GenericResultWrapper> {
fields(fields: SelectAll['fields']): SelectBuilder<GenericResultWrapper, GenericResult> {
return this._parseArray('fields', this._options.fields, fields)
}

where(conditions: string | Array<string>, params?: Primitive | Primitive[]): SelectBuilder<GenericResultWrapper> {
where(
conditions: string | Array<string>,
params?: Primitive | Primitive[]
): SelectBuilder<GenericResultWrapper, GenericResult> {
if (!Array.isArray(conditions)) {
conditions = [conditions]
}
Expand All @@ -43,14 +42,14 @@ export class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnOb
}

if ((this._options.where as any)?.conditions) {
conditions.concat((this._options.where as any).conditions)
conditions = (this._options.where as any).conditions.concat(conditions)
}

if ((this._options.where as any)?.params) {
params.concat((this._options.where as any).params)
params = (this._options.where as any).params.concat(params)
}

return new SelectBuilder<GenericResultWrapper>(
return new SelectBuilder<GenericResultWrapper, GenericResult>(
{
...this._options,
where: {
Expand All @@ -62,24 +61,24 @@ export class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnOb
)
}

join(join: SelectAll['join']): SelectBuilder<GenericResultWrapper> {
join(join: SelectAll['join']): SelectBuilder<GenericResultWrapper, GenericResult> {
return this._parseArray('join', this._options.join, join)
}

groupBy(groupBy: SelectAll['groupBy']): SelectBuilder<GenericResultWrapper> {
groupBy(groupBy: SelectAll['groupBy']): SelectBuilder<GenericResultWrapper, GenericResult> {
return this._parseArray('groupBy', this._options.groupBy, groupBy)
}

having(having: SelectAll['having']): SelectBuilder<GenericResultWrapper> {
having(having: SelectAll['having']): SelectBuilder<GenericResultWrapper, GenericResult> {
return this._parseArray('having', this._options.having, having)
}

orderBy(orderBy: SelectAll['orderBy']): SelectBuilder<GenericResultWrapper> {
orderBy(orderBy: SelectAll['orderBy']): SelectBuilder<GenericResultWrapper, GenericResult> {
return this._parseArray('orderBy', this._options.orderBy, orderBy)
}

offset(offset: SelectAll['offset']): SelectBuilder<GenericResultWrapper> {
return new SelectBuilder<GenericResultWrapper>(
offset(offset: SelectAll['offset']): SelectBuilder<GenericResultWrapper, GenericResult> {
return new SelectBuilder<GenericResultWrapper, GenericResult>(
{
...this._options,
offset: offset,
Expand All @@ -88,8 +87,8 @@ export class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnOb
)
}

limit(limit: SelectAll['limit']): SelectBuilder<GenericResultWrapper> {
return new SelectBuilder<GenericResultWrapper>(
limit(limit: SelectAll['limit']): SelectBuilder<GenericResultWrapper, GenericResult> {
return new SelectBuilder<GenericResultWrapper, GenericResult>(
{
...this._options,
limit: limit,
Expand All @@ -98,7 +97,7 @@ export class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnOb
)
}

_parseArray(fieldName: string, option: any, value: any): SelectBuilder<GenericResultWrapper> {
private _parseArray(fieldName: string, option: any, value: any): SelectBuilder<GenericResultWrapper, GenericResult> {
let val = []
if (!Array.isArray(value)) {
val.push(value)
Expand All @@ -108,12 +107,20 @@ export class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnOb
val = [...option, ...val]
}

return new SelectBuilder<GenericResultWrapper>(
return new SelectBuilder<GenericResultWrapper, GenericResult>(
{
...this._options,
[fieldName]: val as Array<string>,
},
this._queryBuilder
)
}

getQuery(): Query<ArrayResult<GenericResultWrapper, GenericResult>> {
return this._queryBuilder(this._options as SelectAll)
}

async execute(): Promise<ArrayResult<GenericResultWrapper, GenericResult>> {
return this._queryBuilder(this._options as SelectAll).execute()
}
}
Loading

0 comments on commit d8845cb

Please sign in to comment.