import {Row, ConditionLeaf, Table, Conditional} from "./types.js" export function and< xRow extends Row, C extends ConditionLeaf[] = ConditionLeaf[] >(...conditions: C): ["and", ...C] { return ["and", ...conditions] } export function or< xRow extends Row, C extends ConditionLeaf[] = ConditionLeaf[] >(...conditions: C): ["or", ...C] { return ["or", ...conditions] } export function find(...rows: Partial[]) { return rows.length ? {conditions: or(...rows.map(row => ({equal: row})))} : {conditions: false as const} } export function findAll( values: V[], valueForRow: (v: V) => Partial, ) { return {conditions: or(...values.map(v => ({equal: valueForRow(v)})))} } export async function assert( table: Table, conditional: Conditional, make: () => Promise, ) { let row = await table.readOne(conditional) if (!row) { row = await make() await table.create(row) } return row }