npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

knex-stringcase

v1.5.5

Published

Helper switches key case for npm knex

Downloads

58,091

Readme

knex-stringcase

Easily convert database column names for use in your Node.js application when using knex.

By default, this library assumes your database uses snake_case and your Node.js application uses camelCase. However, these settings can be changed.

🚀 Why Knex Stringcase?

If your database columns follow a snake_case convention (a common practice), you might want to convert them into a more JavaScript-friendly camelCase for use in your application.

For example, you might have database columns id, is_verified, deleted_at, but your application prefers id, isVerified, deletedAt. This library takes care of that conversion.

Example usage:

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

This will return an object:

{
  "id": "xxxx",
  "isVerified": true
}

This is maps database field is_verified to isVerified and allows you to refer to deleted_at using deletedAt. No more snake_case to camelCase troubles!

🔧 How It Works

By leveraging knex’s built-in configuration options: wrapIdentifier and postProcessResponse. You can use these configuration options yourself, this library just makes the conversions simpler.

🌟 Features

  • Full TypeScript support as of version 1.5.0.
  • Automatic conversion between snake_case and camelCase or custom formats.
  • Extend and modify conversion logic with custom functions.
  • Handles nested objects and subqueries.

📦 Installation

Use npm (or yarn, pnpm, etc.):

npm i knex-stringcase

📘 Usage

Add knex-stringcase to your knex configuration.

import knex from 'knex';
import knexStringcase from 'knex-stringcase';

const db = knex({
  client: 'mysql',
  connection: {
    host: '127.0.0.1',
    user: 'your_database_user',
    password: 'your_database_password',
    database: 'myapp_test'
  },
  ...knexStringcase(),
});

This library overwrites wrapIdentifier and postProcessResponse pass them as library options instead, they will be run when keys are in database format. If you wish to run when keys are in application format use appWrapIdentifier and appPostProcessResponse.

📰 Library Options

stringcase

Default: 'snakecase'

Define how keys are modified when heading to the database. Accepts a string found in stringcase (e.g. 'snakecase'), or a custom function.

This parameter may be an array describing more than one alteration in sequence.

stringcase: ['snakecase', (value) => 'db_' + value]
// 'myKey' => 'db_my_key'

appStringcase

Default: 'camelcase'

Define how keys are converted when returning to the application. This attribute may also be be an array and operates closely to how stringcase operates above.

wrapIdentifier

In order to use this knex feature with the library ensure that you pass it as a parameter.

Knex documentation

appWrapIdentifier

(value: string, queryContext?: unknown) => string

Custom function to modify identifiers before conversion. Runs when keys are still in application format, on the way to the database.

postProcessResponse

In order to use this knex feature with the library ensure that you pass it as a parameter.

Knex documentation

appPostProcessResponse

(result: unknown, queryContext?: unknown) => unknown

Custom function to process the response after conversion. Runs when keys are in application format, after the data is retrieved from the database.

recursiveStringcase

(value: object, path: string, queryContext?: unknown) => boolean

A function to control nested object conversions (useful for subqueries or JSON fields). The function receives the object and its path in dot notation. Return true to convert the object.

🔄 Upgrade Guide (1.5.0 → 1.5.5)

1.5.5: knexStringcase() no longer has to wrap your entire knex configuration instead you can insert it into the options.

1.5.0: TypeScript support is available out-of-the-box.

🤝 Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request. Note that we avoid dependencies whenever possible.