Skip to content

Instantly share code, notes, and snippets.

@alexandrebodin
Last active September 20, 2017 11:38
Show Gist options
  • Save alexandrebodin/05a108d57fdde1ad091e44f272cd7ac2 to your computer and use it in GitHub Desktop.
Save alexandrebodin/05a108d57fdde1ad091e44f272cd7ac2 to your computer and use it in GitHub Desktop.
db transaction overlay
import knex from 'knex';
import userQueries from './user';
function DB(qb) {
this.qb = qb;
}
DB.prototype.startTransaction = function(fn) {
this.knex.transaction(trx => {
return fn(new DB(trx));
});
};
Object.keys(userQueries).map(queryName => {
DB.prototype[queryName] = function(...args) {
return userQueries[queryName](this.qb)(...args);
};
})
export default function createDB() {
return new DB(knex({
connectionInfos
}));
}
import createDB from 'db';
const db = createDB();
db.startTransaction(trx => {
return trx.saveUser(user);
});
const saveUser = qb => user {
return qb.insert(user)
.into('vamos_user');
}
export default {
saveUser
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment