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

bookshelf-spotparse

v1.0.2

Published

A bookshelf plugin that makes formatting, parsing and finding models easier.

Downloads

9

Readme

bookshelf-spotparse

A Bookshelf.js plugin that makes formatting, parsing and finding models easier.

See also: bookshelf-update, a plugin that makes updating models more intuitive.

Installation

  • Install with npm using npm i bookshelf-spotparse
  • After creating a bookshelf instance, call the plugin method:
const bookshelf = require('bookshelf')(knex);
bookshelf.plugin('bookshelf-spotparse');
  • SpotParse can now be used on all models.

Usage

SpotParse allows you to create an arrangement for the variables inside your models. You can now specify for each variable how they should be inserted and read to and from the database:

// Model definition
let User = bookshelf.model('users', {
  tableName: 'users',
  // Sample relation
  organisation: function() {
    return this.belongsTo('organisations');
  }
}, {
  arrangement: {
    password: {
      format: (password) => encrypt(password),
      parse: (password) => decrypt(password)
    }
  }
});

The above code will encrypt a password (if present) when storing the model to the database and decrypt the password (if present) when getting a user out of the database. This makes it so the database always has the encrypted password and user.get('password') will always return the decrypted password.

To use an unformatted field to retrieve a model from the database the spot() function can be used:

User.spot({username: 'PietHein', password: 'decryptedpassword'})
.fetch().then(user => console.log(user.get('status') + user.get('password')));
// Prints: decryptedpassword

In this case the password will be formatted (encrypted) before being passed to the database query.

Overriding

WARNING: SpotParse makes the format and parse functions static to be used with spot().
Overriding the format and parse functions inside your model will result in spot({key: value}) no longer parsing the value properly.
You can however override the format and parse functions in the static (constructor) part of your module, for instance:

// Model definition
let User = bookshelf.model('users', {
  tableName: 'users',
  // Sample relation
  organisation: function() {
    return this.belongsTo('organisations');
  },
  // WARNING: Overriding attributes here will result in spot()
  // not being able to parse a password for instance.
  // format: function(attributes) {
  //   if (attributes.password && attributes.method) attributes.method = 'login';
  //   return attributes;
  // }
}, {
  arrangement: {
    password: {
      format: (password) => encrypt(password),
      parse: (password) => decrypt(password)
    }
  },
  // Format can be overridden like this, inside the static part.
  format: function(attributes) {
    if (attributes.password && attributes.method) attributes.method = 'login';
    // Because of formatArrangement (also see parseArrangement)
    // the password will still be formatted (encrypted) if present.
    return this.formatArrangement(attributes);
  }
});

Leaving out this.formatArrangement() will nullify the workings of the arrangement property, the spot() method will still use the overridden code.