Last active
September 20, 2017 11:38
-
-
Save alexandrebodin/05a108d57fdde1ad091e44f272cd7ac2 to your computer and use it in GitHub Desktop.
db transaction overlay
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 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 | |
})); | |
} |
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 createDB from 'db'; | |
const db = createDB(); | |
db.startTransaction(trx => { | |
return trx.saveUser(user); | |
}); |
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
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