Yes, I love result types and I dearly (and daily) miss them so much in TypeScript.
import postgres from "postgres";
import { Result, Some, Option, Ok, Err, None } from "ts-results";
const pg = postgres({});
export async function sql<T extends readonly (object | undefined)[]>(
sql: TemplateStringsArray,
...params: any[]
): Promise<Result<Option<T>, unknown>> {
try {
const rows = await pg<T>(sql, ...params);
if (rows.length === 0) {
return Ok(None);
}
return Ok(Some(rows));
} catch (error) {
return Err(error);
}
}
export default sql;