Giter Site home page Giter Site logo

knex-stringcase's Introduction

knex-stringcase

Used with npm knex to convert database column names for use by a node application.

By default this library assumes your database columns use snakecase my_key and your node application uses camelcase myKey, however these settings can be changed.

Why

If the database has column names that are all in snakecase for example (a common practice), then that can be sub-ideal in your application.

With this library:

const user = await db('users')
    .first('key', 'isVerified')
    .where({ id: params.userId, deletedAt: null });

Returns an object { key: 'xxxx', isVerified: true } from a database where columns are named id, key, is_verified, and deleted_at. Removing your snakecase concerns.

How

By leveraging these configuration options provided by knex postProcessResponse and wrapIdentifier.

Knex provides these options but this library acts as a helper to make the conversions simpler.

Installation

npm i knex --save
npm i knex-stringcase --save

Usage

const knex = require('knex');
const knexStringcase = require('knex-stringcase');

const configFromKnexReadme = {
  client: 'mysql',
  connection: {
    host : '127.0.0.1',
    user : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test'
  }
};

const options = knexStringcase(configFromKnexReadme);
const db = knex(options);

The two knex config options this library overrides are postProcessResponse and wrapIdentifier. If you provide those options they will be run after string conversion has already taken place. If you wish to run before conversion has taken place use beforePostProcessResponse and beforeWrapIdentifier instead.

New options

beforePostProcessResponse (value: string) => string

A function which runs before modifications made by this library if needed.

beforeWrapIdentifier (value: string) => string

A function which runs before modifications made by this library if needed.

dbStringcase <default: 'snakecase'>

A function or a string which describes how keys should be modified when headed to the database. If a string is provided keys will be modified by their respective function found in npm stringcase. Alternatively a function can be passed, taking the string in its current state which will give you more control to suit your needs.

This parameter may be an array describing more than one alteration in sequence. eg ['snakecase', 'uppercase'].

appStringcase <default: 'camelcase'>

A function or a string which describes how keys should re-enter your application from the database. This attribute may also be be an array and operates very similarly to dbStringcase above.

Contribute

Sure!

knex-stringcase's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.