import type { Knex } from 'knex' import _ from 'lodash' import RestQueriesFactory from '../../lib/knex_rest_queries.ts' import emitter from '../../lib/emitter.ts' export const columns = ['id', 'name', 'createdAt', 'createdById', 'modifiedById', 'modifiedAt'] export const Selects = (knex: Knex) => ({ createdBy: knex .select(knex.raw('row_to_json(users)')) .from(knex.select('id', 'email', 'createdAt').from('user').where('id', knex.ref('role.created_by_id')).as('users')), modifiedBy: knex .select(knex.raw('row_to_json(users)')) .from( knex.select('id', 'email', 'createdAt').from('user').where('id', knex.ref('role.modified_by_id')).as('users'), ), }) export default ({ knex }: { knex: Knex }) => { function findByIds(ids: string[], client = knex) { return client.table('role').select(columns).whereIn('id', ids).then(_.identity) } function findByNames(names: string[], client = knex) { return client.table('role').select(columns).whereIn('name', names).then(_.identity) } return { ...RestQueriesFactory({ emitter, knex, table: 'role', columns, selects: Selects(knex), }), findByIds, findByNames, } }