Created
April 1, 2017 11:10
-
-
Save abcang/7a5d1649c2a550a5a5b64580b5bf1866 to your computer and use it in GitHub Desktop.
knexでスネークケースとキャメルケースを変換してくれる設定
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 config = {}; | |
const knex = require('knex')(config); | |
const { decamelize, camelizeKeys } = require('humps'); | |
// スネークケースをキャメルケースに変換 | |
const formatterPrototype = knex.client.formatter().constructor.prototype; | |
// eslint-disable-next-line no-underscore-dangle | |
const originalWrapString = formatterPrototype._wrapString; | |
// eslint-disable-next-line no-underscore-dangle | |
formatterPrototype._wrapString = function _wrapString(value) { | |
return decamelize(Reflect.apply(originalWrapString, this, [value])); | |
}; | |
const originQuery = knex.client.query; | |
knex.client.query = function query(connection, obj) { | |
const promise = Reflect.apply(originQuery, this, [connection, obj]); | |
promise.then((res) => { | |
res.response[0] = camelizeKeys(res.response[0]); | |
return res; | |
}); | |
return promise; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
感謝
knex/knex#1278
Formatterの位置が変わってたので、その点を直した