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

express-model-binding

v1.1.0

Published

Laravel-style route model binding for Express.js with support for multiple ORMs (Knex, Mongoose, TypeORM, Sequelize, Prisma)

Readme

express-model-binding

npm version License: MIT TypeScript

Route model binding for Express.js. If you've used Laravel, you know how useful this pattern is—automatically resolve route parameters to database models.

Works with Knex, Mongoose, TypeORM, Sequelize, and Prisma.

Why?

Instead of this:

app.get('/users/:id', async (req, res) => {
  const user = await db('users').where('id', req.params.id).first();
  if (!user) return res.status(404).json({ error: 'User not found' });
  res.json(user);
});

Write this:

app.get('/users/:user', bindModel('user', 'users'), (req, res) => {
  res.json(req.user);
});

Install

npm install express-model-binding

Then install your ORM:

npm install knex pg          # Knex + Postgres
npm install mongoose         # MongoDB
npm install typeorm          # TypeORM
npm install sequelize        # Sequelize
npm install @prisma/client   # Prisma

Setup

import express from 'express';
import Knex from 'knex';
import { ModelBinder, KnexAdapter, bindModel } from 'express-model-binding';

const app = express();
const knex = Knex({ client: 'pg', connection: process.env.DATABASE_URL });

// Configure once at startup
ModelBinder.setAdapter(new KnexAdapter(knex));

// Use in routes
app.get('/users/:user', bindModel('user', 'users'), (req, res) => {
  res.json(req.user);
});

Adapters

Knex

import { KnexAdapter } from 'express-model-binding';
ModelBinder.setAdapter(new KnexAdapter(knex));
app.get('/users/:user', bindModel('user', 'users'), handler);

Mongoose

import { MongooseAdapter } from 'express-model-binding';
ModelBinder.setAdapter(new MongooseAdapter());
app.get('/users/:user', bindModel('user', User), handler);

TypeORM

import { TypeORMAdapter } from 'express-model-binding';
ModelBinder.setAdapter(new TypeORMAdapter(dataSource));
app.get('/users/:user', bindModel('user', User), handler);

Sequelize

import { SequelizeAdapter } from 'express-model-binding';
ModelBinder.setAdapter(new SequelizeAdapter(sequelize));
app.get('/users/:user', bindModel('user', User), handler);

Prisma

import { PrismaAdapter } from 'express-model-binding';
ModelBinder.setAdapter(new PrismaAdapter(prisma));
app.get('/users/:user', bindModel('user', 'user'), handler);

Middleware

bindModel — Basic binding

app.get('/users/:user', bindModel('user', 'users'), handler);

bindModels — Multiple models

app.get('/users/:user/posts/:post', bindModels({
  user: { model: 'users' },
  post: { model: 'posts' },
}), handler);

bindOptional — Don't throw if missing

app.get('/users/:user', bindOptional('user', 'users'), handler);

bindByKey — Bind by slug, email, etc.

app.get('/posts/:slug', bindByKey('slug', 'posts', 'slug'), handler);

bindAs — Custom request property name

app.get('/profile/:id', bindAs('id', 'users', 'profile'), handler);

bindCached — With caching

app.get('/users/:user', bindCached('user', 'users', 60000), handler);

bindWithRelations — Eager load relations

app.get('/users/:user', bindWithRelations('user', 'users', ['posts']), handler);

Options

bindModel('user', 'users', {
  key: 'slug',              // Field to query (default: primary key)
  optional: true,           // Don't throw 404 if not found
  select: ['id', 'name'],   // Select specific fields
  include: ['posts'],       // Load relations
  where: { active: true },  // Extra conditions
  withTrashed: true,        // Include soft-deleted
  cache: true,              // Enable caching
  cacheTTL: 30000,          // Cache duration (ms)
  errorMessage: 'Not found',
});

Error Handling

import { ModelNotFoundError } from 'express-model-binding';

app.use((err, req, res, next) => {
  if (err instanceof ModelNotFoundError) {
    return res.status(404).json({ error: err.message });
  }
  next(err);
});

Utilities

Transformers — Convert parameter values

import { toNumber, toLowerCase } from 'express-model-binding';
bindModel('user', 'users', { transformValue: toNumber });

Validators — Check format before querying

import { isUUID, isObjectId } from 'express-model-binding';

Debugging

ModelBinder.setDebug(true);

API

ModelBinder.setAdapter(adapter)   // Set ORM adapter
ModelBinder.getAdapter()          // Get current adapter
ModelBinder.clearCache()          // Clear binding cache
ModelBinder.reset()               // Reset all state

License

MIT © Sepehr Mohseni

Links