Skip to content

Instantly share code, notes, and snippets.

@jgoux
Last active March 13, 2017 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgoux/a130371948f06512ef1b4df2fbf4e798 to your computer and use it in GitHub Desktop.
Save jgoux/a130371948f06512ef1b4df2fbf4e798 to your computer and use it in GitHub Desktop.
Database service
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