Skip to content

Instantly share code, notes, and snippets.

@abcang
Created April 1, 2017 11:10
Show Gist options
  • Save abcang/7a5d1649c2a550a5a5b64580b5bf1866 to your computer and use it in GitHub Desktop.
Save abcang/7a5d1649c2a550a5a5b64580b5bf1866 to your computer and use it in GitHub Desktop.
knexでスネークケースとキャメルケースを変換してくれる設定
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;
};
@abcang
Copy link
Author

abcang commented Apr 1, 2017

感謝
knex/knex#1278

Formatterの位置が変わってたので、その点を直した

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment