import dayjs from 'dayjs' import { P } from 'lifts' import { Type } from '../../../lib/type' import { _createdAt } from '../../constants' import { FTypes } from '../../types' type _SpanOption = { since: dayjs.Dayjs; until: dayjs.Dayjs } export type SpanQueryOption = | _SpanOption | Type.SetOptional<_SpanOption, 'since'> | Type.SetOptional<_SpanOption, 'until'> export const CreatedWithin = (query: FTypes.Query) => ({ since, until, }: SpanQueryOption) => { return P( query, (q) => (since ? q.where(_createdAt, '>=', since.toDate()) : q), (q) => (until ? q.where(_createdAt, '<', until.toDate()) : q), ) } export const Combine = (query: FTypes.Query) => < Ss extends ((query: FTypes.Query) => (arg: any) => any)[] >( ...Selectors: Ss ) => { // eslint-disable-next-line // @ts-ignore return (...args: { [I in keyof Ss]: Parameters>[0] }) => { return Selectors.reduce((q, selector, i) => selector(q)(args[i]), query) } }