79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
// @ts-nocheck
|
|
// import { type Knex } from 'kysely'
|
|
// import _ from 'lodash'
|
|
|
|
// import { where } from './kysely_helpers.ts'
|
|
|
|
// type QueryName =
|
|
// | 'create'
|
|
// | 'createMany'
|
|
// | 'find'
|
|
// | 'findOne'
|
|
// | 'findById'
|
|
// | 'getAll'
|
|
// | 'paginate'
|
|
// | 'remove'
|
|
// | 'removeById'
|
|
// | 'replace'
|
|
// | 'update'
|
|
|
|
// interface Options {
|
|
// kysely: Knex
|
|
// emitter?: ANY
|
|
// pick?: QueryName[]
|
|
// omit?: QueryName[]
|
|
// columns: string[]
|
|
// table: string
|
|
// selects?: Record<string, ANY>
|
|
// }
|
|
|
|
// export const count = ({ kysely, table, columns }: Pick<Options, 'kysely' | 'table' | 'columns'>) =>
|
|
// function count(query: Record<string, ANY>, client = kysely) {
|
|
// let builder = client.table(table)
|
|
|
|
// if (!_.isEmpty(query)) {
|
|
// builder = where(builder, _.pick(query, columns))
|
|
// }
|
|
|
|
// return builder.count().then((count) => parseInt(count[0].count, 10))
|
|
// }
|
|
|
|
export const find = ({
|
|
kysely,
|
|
table,
|
|
columns,
|
|
allSelects,
|
|
defaults = {},
|
|
}: Pick<Options, 'kysely' | 'table' | 'columns'> & { allSelects: string[]; defaults: Record<string, any> }) =>
|
|
function find(json, select, client: Knex) {
|
|
if (!client) {
|
|
// TODO figure out if better, eg instanceof, check is possible
|
|
if (select && select.andWhereNotBetween) {
|
|
client = select
|
|
select = null
|
|
} else {
|
|
client = kysely
|
|
}
|
|
}
|
|
|
|
const { offset = defaults.offset, limit = defaults.limit, sort = defaults.sort, ...query } = json
|
|
|
|
let builder = client.table(table).select(select ? _.pick(allSelects, select) : allSelects)
|
|
|
|
if (!_.isEmpty(query)) {
|
|
builder = where(builder, _.pick(query, columns))
|
|
}
|
|
|
|
builder = builder.orderBy(sort || 'id', sort?.startsWith('-') ? 'desc' : 'asc')
|
|
|
|
if (limit) {
|
|
builder = builder.limit(limit)
|
|
}
|
|
|
|
if (offset) {
|
|
builder = builder.offset(offset)
|
|
}
|
|
|
|
return builder
|
|
}
|