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

sails-generate-jsonapi-blueprints

v0.0.0

Published

Generate a JSONAPI.org Data compatible blueprints in your Sails project.

Downloads

8

Readme

Sails-Generate-JSON API-Blueprints

NPM version Build status

JSON API Data compatible blueprints for Sails v0.11+ This project is inspired by:

Sails override default Sails blueprints to support jsonapi.org compatible blueprints.

The blueprints provide JSON API formatted data. Ember support is provided by using the JSONAPIAdapter in Ember.

API

The generators support different flavors for your API.

basic: Basic blueprints should get you up and running in no time and serve as a good basis to start development. They come with a default configuration that sideloads all records found in the model's associations.

advanced: If you need more powerful control over your API, you may consider upgrading to the "advanced" blueprints. These blueprints allow fine-grained control over how API responses handle sideloading a model's associations.

json-api: Unfortunately not yet available, but it would also be great to support 100% json api compatible responses.

Getting started

  • Install the generator into your (new) Sails project npm install sails-generate-jsonapi-blueprints

  • Run the generator: sails generate jsonapi-blueprints

  • Configure sails to use pluralized blueprint routes.

    In myproject/config/blueprints.js set pluralize: true

    module.exports.blueprints = {
      // ...
      pluralize: true
    };
  • Generate some API resources, e.g. sails generate api user

  • Start your app with sails lift

Now you should be up and running and your JSON API Data app should be able to talk to your Sails backend.

Advanced Blueprints

The "basic" blueprints make a basic Sails app work with JSON API Data, but in a more complex project you may need more fine-grained control over how the Sails Rest API handles associations/relations and what is included in the API responses. Enter the "advanced" blueprints.

  • Run the generator with: sails generate jsonapi-blueprints advanced --force to update to the advanced blueprints.

  • Add a configuration option associations: { list: "link", detail: "record" } to myproject/config/models.js. This will determine the default behaviour.

    module.exports.models = {
      // ...
      associations: {
      	list: "link",
      	detail: "record"
      }
    };
  • Add a configuration option validations: { ignoreProperties: [ 'includeIn' ] } to myproject/config/models.js. This tells Sails to ignore our individual configuration on a model's attributes.

    module.exports.models = {
      // ...
      validations: {
      	ignoreProperties: ['includeIn']
      }
    };
  • Setup individual presentation on a model attribute by adding includeIn: { list: "option", detail: "option"} where option is one of link, index, record.

    attributes: {
      name : "string",
      posts: {
        collection: "post",
        via: "user",
        includeIn: {
          list: "record",
          detail: "record"
        }
      }
    }

Presentation options:
The link setting will generate jsonapi.org URL style links properties on the records, which JSON API Data can consume and load lazily.

The index setting will generate an array of ID references for JSON API Data, so be loaded as necessary.

The record setting will sideload the complete record.

Troubleshooting

If the generator exits with error: Something else already exists at ... you can try running it with the --force option (at your own risk!)

Some records from relations/associations are missing? Sails has a default limit of 30 records per relation when populating. Try increasing the limit as a work-around until a pagination solution exists.

JSONAPIAdapter

If you're using Ember CLI, you need to use the DS.JSONAPIAdapter adapter. This is now the default adapter in Ember 2.x+ Please see Ember Guides for using Adapters

Sideloading records in EmberJS

To enable this behavior, add the following lines to the config/blueprints.js file:

// config/blueprints.js
module.exports.blueprints = {
  // existing configuration
  // ...

  jsonapi: {
    sideload: true
  }
}

#JSONAPI Format

{
  "data": {
    "type": "articles",
    "id": "1",
    "attributes": {
      // ... this article's attributes
    },
    "relationships": {
      // ... this article's relationships
    }
  }
}

Todo

Many Since I just started.