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

bookshelf-model-relations

v0.0.2

Published

Provides a way to see what relations/associations a bookshelf model has

Downloads

3

Readme

bookshelf-model-relations

Provides a way to see what relations/associations a bookshelf model has.

NPM

Media Suite

Build Status

Installation

npm install bookshelf-model-relations --save

Relation definition

The relation definition object that is returned by this module when you pass it a bookshelf model takes the following form:

{
  user: {
    name: 'user', // the name of the relation
    type: 'belongsTo', // belongsTo, hasMany, etc
    modelFrom: 'Post', // always the name of the model passed into bookshelf-model-relations
    keyFrom: 'user_id', // the key on the model desribed in modelFrom
    modelTo: 'User', // the model that is related to via type
    keyTo: 'id', // the key on the model described in modelTo
    modelThrough: null, // if a through model is used (eg. many to many) that model is listed here
    keyThrough: null, // the key on the through model (if present) that points to modelTo
    multiple: false // whether the relation returns a single model or multiple
  },
  comments: {
    name: 'comments',
    type: 'hasMany',
    modelFrom: 'Post',
    keyFrom: 'id',
    modelTo: 'Comment',
    keyTo: 'post_id',
    modelThrough: null,
    keyThrough: null,
    multiple: true
  }
}

Usage

Require the module

const relations = require('bookshelf-model-relations')

Pass it a bookshelf model

const relationships = relations(Post, options)

/*
{
  user: {
    name: 'user',
    type: 'belongsTo',
    keyFrom: 'user_id',
    modelFrom: 'Post',
    keyTo: 'id',
    modelTo: 'User',
    keyThrough: null,
    modelThrough: null,
    multiple: false
  },
  comments: {
    name: 'comments',
    type: 'hasMany',
    keyFrom: 'id',
    modelFrom: 'Post',
    keyTo: 'post_id',
    modelTo: 'Comment',
    keyThrough: null,
    modelThrough: null,
    multiple: true
  }
}
*/

options

options.modelName

bookshelf-model-relations attempts to work out the name of the model by taking the value of tableName singularizing it and then UpperCamelCasing it. If this is incorrect, you will need to manually set this via options.modelName like so:

const relationships = relations(Post, {modelName: 'MyCustomClassName'})

Overriding a models relations

bookshelf-model-relations attempts to work out relationships via parsing the model function strings based on bookshelf model definition conventions.

If for some reason your relations are different you can tell bookshelf-model-relations about your models relations in 1 of 2 ways:

relationship comment string

Inside the body of a relationship function you can define a comment string of the format:

/* relationship key1:value1 key2:value2 */

For example:

const Post = bookshelf.Model.extend({
  author: function () {
    /* relationship
      type:belongsTo
      keyFrom:author_id
      modelFrom:Post
      modelTo:Author
      keyTo:id
      modelThrough:null
      keyThrough:null
      multiple:false */
  }
})

relations model property

The other way you can let bookshelf-model-relations know about a model's relations is to define an object on the model under the key relations

For example:

const Post = bookshelf.Model.extend({
  relations: {
    author: {
      type: 'belongsTo',
      keyFrom: 'author_id',
      modelFrom: 'Post',
      modelTo: 'Author',
      keyTo: 'id',
      modelThrough: null,
      keyThrough: null,
      multiple: false
    }
  }
  author: function () { return /*...*/ }
})