import _ , { Dictionary } from "lodash" ; import { DBTables } from "../generated/ghost-db-tables" ; import { Context } from "../data-sources/Context" ; import { QueryBuilder } from "knex" ; export function byColumnLoader < Tbl extends keyof DBTables , Key extends Extract < keyof DBTables [ Tbl ] , string > , KeyType extends Extract < DBTables [ Tbl ] [ Key ] , string | number > > ( ctx , table , key , keys ) { return ctx . knex ( table ) . select ( `${ table }.*` ) . whereIn ( `${ table }.${ key }` , keys ) . then ( ( rows ) => { const keyed = _ . keyBy ( rows , key ) ; return keys . map ( ( k ) => { return keyed [ k ] || new Error ( `Missing row for ${ table }:${ key } ${ k }` ) ; } ) ; } ) ; } param ctx param table param key param keys export function manyByColumnLoader < Tbl extends keyof DBTables , Key extends Extract < keyof DBTables [ Tbl ] , string > , KeyType extends Extract < DBTables [ Tbl ] [ Key ] , string | number > > ( ctx , tableName , key , keys , scope = ( qb ) => qb ) { const builder = ctx . knex ( tableName ) . select ( `${ tableName }.*` ) . whereIn ( `${ tableName }.${ key }` , _ . uniq ( keys ) ) ; return scope ( builder ) . then ( ( rows ) => { const grouped = _ . groupBy ( rows , key ) ; return keys . map ( ( id ) => grouped [ id ] || [ ] ) ; } ) ; }