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 🙏

© 2026 – Pkg Stats / Ryan Hefner

knex-stringcase

v1.6.0

Published

Helper switches key case for npm knex

Readme

knex-stringcase

Convert database column names when using knex.

This library helps map database naming conventions such as snake_case to application-friendly formats such as camelCase, and back again, using knex’s built-in hooks. By default, database keys are assumed to be snake_case and application keys camelCase.

But this can be changed if needed.

Why

If your database uses columns like:

  • is_verified
  • deleted_at

but your application code prefers:

  • isVerified
  • deletedAt

this library performs the conversion automatically when querying and when returning results.

Example:

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

Result:

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

You write application-style keys. The database still uses its own conventions.

How it works

knex exposes two configuration hooks:

  • wrapIdentifier
  • postProcessResponse

This library wires those hooks for you and applies configurable key conversion in both directions. You can still provide your own hooks. They will be run at the appropriate stage.

Installation

npm install knex-stringcase

Usage

CommonJS

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

ESM (recommended)

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 internally. If you want to provide your own versions, pass them as options. They will be run when keys are still in database format.

If you need hooks that run when keys are in application format, use the app* variants described below.

Options

stringcase

Default: 'snakecase'

Controls how keys are converted when sending queries to the database. Accepts a string supported by the stringcase package or a custom function. Multiple transformations may be provided as an array.

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

appStringcase

Default: 'camelcase'

Controls how keys are converted when data is returned to the application. Accepts the same formats as stringcase.

wrapIdentifier

Custom knex wrapIdentifier hook. If provided, it will run after key conversion, when keys are in database format.

See knex documentation: https://knexjs.org/guide/#wrapidentifier

appWrapIdentifier

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

Runs before key conversion, while keys are still in application format.

postProcessResponse

Custom knex postProcessResponse hook. If provided, it will run before application-level conversion, when keys are still in database format.

See knex documentation: https://knexjs.org/guide/#postprocessresponse

appPostProcessResponse

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

Runs after conversion, when keys are already in application format.

recursiveStringcase

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

Controls whether nested objects are converted. The function receives the object, its dot-path, and the query context. Return true to convert. Useful for subqueries or JSON fields.

Upgrade notes

1.6

Should support esm and commonjs environments equally.

1.5.5

knexStringcase() no longer wraps the entire knex configuration object. It can now be spread directly into the options.

1.5.0

TypeScript support added.

Contributing

Issues and pull requests are welcome. Dependencies are kept minimal by design.