Skip to content

Instantly share code, notes, and snippets.

@jhewlett
Last active July 8, 2021 17:25
Show Gist options
  • Save jhewlett/a2a4f01f0ed3724ffce0e4db8aa2d8de to your computer and use it in GitHub Desktop.
Save jhewlett/a2a4f01f0ed3724ffce0e4db8aa2d8de to your computer and use it in GitHub Desktop.
io-ts + slonik
import {
TaggedTemplateLiteralInvocationType,
QueryResultRowType,
DatabasePoolType
} from 'slonik'
import * as t from 'io-ts'
import { Either } from 'fp-ts/Either'
const query = async <T>(
sqlQuery: TaggedTemplateLiteralInvocationType<QueryResultRowType>,
decoder: t.Type<T>,
pool: DatabasePoolType
) : Promise<Either<t.Errors, T[]>> => {
const result = await pool.query(sqlQuery)
return t.array(decoder).decode(result.rows)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment