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

jsonmonger

v0.3.0

Published

A json:api-compliant abstraction layer for your JavaScript applications.

Downloads

4

Readme

jsonmonger

jsonmonger is an abstraction layer between your application and your json:api-compliant server. Think of it like an ORM for your json:api-compliant back-end.

Introduction

json:api is fantastic, but working with it from application code can be a bit challenging:

  • it’s got a rigid and deep data structure over which you probably have very little control, if any
  • navigating related data can be involved
  • building requests to manipulate data on the server can be very verbose

jsonmonger abstracts all of this complexity away, allowing you to write expressive code that focuses on business logic, not API transactions.

Usage

Install jsonmonger with npm or yarn:

npm install --save jsonmonger

    // or…

yarn add jsonmonger

Models

jsonmonger exposes a Model constructor. With this, you can define an object with which to manage all records of a specific type. Let’s say our application catalogues science fiction novels and their authors; you might write an Author model like this:

// my_app/models/Author.js

const Model = require('jsonmonger').Model;
const Author = new Model({
  // Tell Jsonmonger which json:api object type should be
  // handled with this model and where to make requests for it.
  type: 'user',
  endpoint: '/users',

  // Map json:api attributes quite simply.
  firstName: 'attributes.first_name',
  lastName: 'attributes.last_name',
  dateOfBirth: 'attributes.date_of_birth',

  // Functions work as both getter and setter functions.
  fullName: value => {
    if (value) {
      const splitName = value.split(' ', 2);
      this.firstName = split[0];
      this.lastName = split[1];
      return fullName;
    } else {
      return `${this.firstName} ${this.lastName}`;
    }
  },

  // Relationships are mapped just like attributes.
  books: 'relationships.books_authored',
});

We can now use that model to manage records in our json:api-compliant server. Say we want to create a record for China Miéville, we might have a create_author controller with the following code:

// my_app/controllers/create_author.js

const Author = require('../models/Author');

// Create a new record for an author.
const china = new Author({
  fullName: 'China Miéville',
});

// Saving the record makes a request to the json:api server,
// returning a JavaScript Promise. In this case, Jsonmonger
// can tell this is a new record, so it makes a POST request.
china.save();

Chances are we’re going to need to edit China’s record at some point, so let’s say we have an edit_author controller in our application with the following code:

// my_app/controllers/edit_author.js

// Make changes to your record the way you would any JS object
// and Jsonmonger will store it in the appropriate location.
china.dateOfBirth = new Date('September 6, 1972');

// Jsonmonger knows this is an existing record, so it makes a
// PATCH request, as per json:api spec.
china.save();

But what about relationships? jsonmonger makes it straightforward:

// my_app/controllers/edit_author.js

const Author = require('../models/Author');
const Book = require('../models/Book');

// Fetch an existing record for Kraken.
const kraken = await new Book({
  id: 'kraken_id', // presuming we know this
}).fetch();

// Create a new record for The City & the City.
const theCity = await new Book({
  title: 'The City & the City',
  yearPublished: '2009',
}).save();

// Fetch China’s record and add books to it.
const china = await new Author({
  id: 'china_id', // presuming we know this
}).fetch();

china.books = [ kraken, theCity ];

// A PATCH request is made updating the relationship.
china.save();

We can also update related records, if needed. Say a user needs to update This Census-Taker’s record to correctly categorize it as a novella, not a novel. Since jsonmonger loads relationships with their respective Models (if one is available for that type), you can edit the book in place and save it.

// my_app/controllers/edit_author.js

const Author = require('../models/Author');

const china = await new Author({
  id: 'china_id', // presuming we know this
}).fetch();

const censusTaker = china.books.find(book => book.title === 'This Census-Taker');

censusTaker.category = 'novella';

censusTaker.save();

And, finally, you can delete records from your json:api server like so:

new Author({ id: 'china_id' }).destroy();