Last active
July 8, 2021 17:25
-
-
Save jhewlett/a2a4f01f0ed3724ffce0e4db8aa2d8de to your computer and use it in GitHub Desktop.
io-ts + slonik
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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