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

dustcover

v0.1.0

Published

JSONAPI plugin for the bookshelfjs ORM

Downloads

4

Readme

dustcover

JSONAPI plugin for the bookshelfjs ORM

NPM

Media Suite

Build Status

Features

The plan for dustcover is to provide a full featured jsonapi implementation for bookshelf with little to no configuration. It's very early (alpha) days at this point and the only thing thats even partially supported is jsonapi serialization. Stay tuned. Chip in if this interests you, PRs and collaboration welcome.

Installation

npm install dustcover --save

Usage

Require dependencies

const Bookshelf = require('bookshelf')
const Knex = require('knex')
const dustcover = require('dustcover')

Setup bookshelf

const bookshelf = Bookshelf(Knex({
  client: 'sqlite3',
  connection: { filename: ':memory:' },
  useNullAsDefault: true
}))

const options = {
  // Set the host that will be used in links during serialization
  // default: host omitted from links
  host: 'http://localhost:3000',

  // Specify whether models should be serialized by default, or
  // require opting in to serialization
  // when true, models will need to opt in to serialization by
  // defining a key `jsonapi` and setting it to true. (See below)
  // default: false
  optIn: false
}
bookshelf.plugin(dustcover, options)

Setup models

const Cat = bookshelf.Model.extend({

  // standard bookshelf table definition
  tableName: 'cat',

  // dustcover jsonapi model type definition
  // see jsonapi spec for type information.
  // This is usually (but not required to be) plural
  type: 'cats',

  // dustcover requires you to specify which relationships
  // should be handled. You do so by specifying them in an array
  relationships: ['owner', 'mice'],

  // allows opting in or out of jsonapi serialization by default for
  // the model. If optIn (see above) is set to true then you will
  // need to set jsonapi: true on every model you wish to be serialized
  // to jsonapi when calling `model.toJSON`
  // You can override these settings by setting jsonapi: true in toJSON
  // like so: model.toJSON({jsonapi: true})
  jsonapi: true,

  // standard bookshelf relationship definitions
  // these will be referenced by dustcover if they have been
  // specified in the relationships array above.
  owner () {
    return this.belongsTo(Owner)
  },
  mice () {
    return this.hasMany(Mouse)
  }
})

Serializing models

dustcover overrides the serialize methods of models and collections such that when you call collection.toJSON() or model.toJSON() you will get jsonapi compatible data instead of just the usual attributes hash.

new Cat().fetch().then(cat => {
  cat.toJSON()
})

will output something like:

{
  data: {
    id: '1',
    type: 'cats',
    attributes: {
      name: 'Boris'
    },
    links: {
      self: 'http://localhost:3000/cats/1'
    },
    relationships: {
      owner: {
        links: {
          related: 'http://localhost:3000/cats/1/owner'
        }
      },
      mice: {
        links: {
          related: 'http://localhost:3000/cats/1/mice'
        }
      }
    }
  }
}

You can override a couple things when you call toJSON

Change the type field like so:

cat.toJSON({type: 'kittycats'})

Remove or adjust relationship serialization like so:

cat.toJSON({relationships: false})

Force jsonapi serialization or non serialization like so: (see above for additional details)

cat.toJSON({jsonapi: false})