Last active
March 13, 2017 14:42
-
-
Save jgoux/a130371948f06512ef1b4df2fbf4e798 to your computer and use it in GitHub Desktop.
Database service
This file contains 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 R from 'ramda'; | |
import knex from 'knex'; | |
export const connect = knex; | |
export const disconnect = async client => { | |
return await client.destroy(); | |
}; | |
export const withClient = R.curry(async (config, cb) => { | |
const client = connect(config); | |
try { | |
return await cb(client); | |
} finally { | |
await disconnect(client); | |
} | |
}); | |
export const withTransaction = R.curry(async (client, cb) => { | |
return await client.transaction(cb); | |
}); | |
export const execute = R.curry(async (client, { sql, values }) => { | |
return await client.raw(sql, values); | |
}); | |
export const query = R.curry(async (client, { sql, values }) => { | |
const { rows } = await client.raw(sql, values); | |
return rows; | |
}); | |
export const queryOne = R.curry(async (client, { sql, values }) => { | |
const { rows: [row = null] } = await client.raw(sql, values); | |
return row; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment