knex: Allow to customize how identifiers must be wrapped

This would unlock scenarios such as automatically converting camelCase to snake_case and vise verse. See https://medium.com/p/956357872fe4

It could look something like this (db.js):

import knex from 'knex'

function wrapIdentifier(type, value) {
  if (type === 'as') return ....;
  if (type === 'select') return ....;
  return ...;
}

export default knex({
  client: 'pg',
  connection: process.env.DATABASE_URL,
  wrapIdentifier,
});

Please vote up if you’re planning to use this feature, or vote down if you think it shouldn’t be in the library.

Ref #929

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 14
  • Comments: 15 (7 by maintainers)

Most upvoted comments

before christmas, maybe this month, maybe next

@kanekotic Those wrapper cannot distinguish if the identifier is table name or column name. So you cannot do that only for table names. However if you want to have all identifiers, without quotes that is completely possible.

Just do something like this (https://knexjs.org/#Installation-wrap-identifier)

var knex = require('knex')({
  client: 'mysql',
  // return identifier without any wrapping
  wrapIdentifier: (value, origImpl, queryContext) => value)
});

This will be released in knex 0.14

Or perhaps just a transform function. My code currently looks rather unpleasant having to do this manually.

This would be nice:

knex({
  client: 'pg',
  transformInput: identifier => toSnakeCase(identifier),
  transformOutput: identifier => toCamelCase(identifier)
});

or

knex({
  client: 'pg',
  transform: (identifier, method) => {
    if (method === 'get') {
      return toCamelCase(identifier);
    } else if (method === 'set') {
      return toSnakeCase(identifier);
    }
  }
});