0.1.0 • Published 4 years ago

@sprv/knex-case v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

@sprv/knex-case

Database columns case converter for Knex.

Knex extension to convert database column names, because you may want to use snake_cased names in database and camelCased names in code.

The conversion is done using knex's postProcessResponse and wrapIdentifier hooks.

Installation

npm install @sprv/knex-case

Usage

This extension can be used in two different ways:

  1. By overriding the postProcessResponse and wrapIdentifier methods completely.
  2. By calling the post and wrap methods in your own knex hooks.

Simple example:

const Knex = require('knex')
const { withCase } = require('@sprv/knex-case')

const knex = Knex(withCase({
  // knex configuration
}))

Advanced example:

const Knex = require('knex')
const { post, wrap } = require('@sprv/knex-case')

const knex = Knex({
  // knex configuration
  postProcessResponse (res) {
    // custom post logic
    return post(res)
  },
  wrapIdentifier (key, origImpl) {
    // custom wrap logic
    return wrap(key, origImpl)
  }
})