<commit_before># Sails-JSON-API-Blueprints

[![NPM version](https://badge.fury.io/js/sails-json-api-blueprints.svg)](http://badge.fury.io/js/sails-json-api-blueprints) &nbsp;  [![Build Status](https://travis-ci.org/dynamiccast/sails-json-api-blueprints.svg)](https://travis-ci.org/dynamiccast/sails-json-api-blueprints)

Sails hook to automatically turn a Sails.js API into a [JSON API](http://jsonapi.org/) compatible interface.

### This hook is still in intense development. See the *Roadmap* section for more information on what is yet to be implemented

While this module will provide your API with a generic implementation of JSON API, it was primarily intented to ease the communication between Sails.js and Ember.js. Ember works by default with JSON API. Here is a demonstration of *sails-json-api-blueprints* working with an Ember application : https://github.com/dynamiccast/sails-ember-super-rentals-example

# Install

Being a sails hook. There is not much thing to do to make your sails app JSON API compatible. all you have to do is install this node module.

````
npm install --save sails-json-api-blueprints
````

Please note the following :
- Being a set of blueprints this only works if `sails.config.blueprints.rest` is set to true (is it by default)
- `sails.config.blueprints.pluralize` will be set to true to match the JSON API specification

# Usage

With the hook being installed, all your auto generated controller actions will be JSON API compliant.
This module also injects a service available as `JsonApiService` in your controllers to help you deal with JSON API.

## Serialize data

As shown in [tests/dummy/api/controllers/UserController.js:14](https://github.com/dynamiccast/sails-json-api-blueprints/blob/master/tests/dummy/api/controllers/UserController.js#L14), `JsonApiService.serialize` allows to serialize any waterline data into a JSON API compliant format. Just call :

````
JsonApiService(modelName, DataObject);
````

## Call blueprints from custom action

As shown in [tests/dummy/api/controllers/UserController.js:24](https://github.com/dynamiccast/sails-json-api-blueprints/blob/master/tests/dummy/api/controllers/UserController.js#L24), `JsonApiService` proxies blueprints to be accessible from any controller. Simply call the following with `req` and `res` as parameter:

- `findRecords` GET /{model}
- `findOneRecord` GET /{model}/{id}
- `createRecord` POST /{model}
- `destroyOneRecord` DELETE /{model}
- `updateOneRecord` PATCH /{model}/{id}

# Roadmap

- JSON API implementation
  - [X] GET all resources
  - [X] POST resource
  - [X] DELETE resource
  - [X] PATCH resource
  - [ ] Return proper error if any
  - [ ] Relationships
  - [ ] Compound Document
  - [ ] Links
  - [ ] Fields
  - [ ] Sorting
  - [ ] Pagination
  - [ ] Filtering
- Sails integration
  - [X] Allow the use of auto CreatedAt and UpdatedAt (see #3)
  - [ ] Pubsub integration
  - [X] Provide a service to serialize as JSON API for custom endpoints
- Repository
  - [X] Add tests on travis
  - [X] Provide status on the build on Github

# Tests

Simply run `npm test`

Tests expect the database to be empty. Local disk is used in the dummy app and can be deleted with `rm tests/dummy/.tmp/localDiskDb.db`.

# Thanks

This work is greatly inspired from https://github.com/mphasize/sails-generate-ember-blueprints
@mphasize did no longer actively maintain this repositiory, so I decided to fork it and to focus on JSON API compatibility.
<commit_msg>Modify README to indicate default responses will be overridden<commit_after># Sails-JSON-API-Blueprints

[![NPM version](https://badge.fury.io/js/sails-json-api-blueprints.svg)](http://badge.fury.io/js/sails-json-api-blueprints) &nbsp;  [![Build Status](https://travis-ci.org/dynamiccast/sails-json-api-blueprints.svg)](https://travis-ci.org/dynamiccast/sails-json-api-blueprints)

Sails hook to automatically turn a Sails.js API into a [JSON API](http://jsonapi.org/) compatible interface.

### This hook is still in intense development. See the *Roadmap* section for more information on what is yet to be implemented

While this module will provide your API with a generic implementation of JSON API, it was primarily intented to ease the communication between Sails.js and Ember.js. Ember works by default with JSON API. Here is a demonstration of *sails-json-api-blueprints* working with an Ember application : https://github.com/dynamiccast/sails-ember-super-rentals-example

# Install

Being a sails hook. There is not much thing to do to make your sails app JSON API compatible. all you have to do is install this node module.

````
npm install --save sails-json-api-blueprints
````

Please note the following :
- Being a set of blueprints this only works if `sails.config.blueprints.rest` is set to true (is it by default)
- `sails.config.blueprints.pluralize` will be set to true to match the JSON API specification
- Default responses will be overridden to respond with valid JSON API errors

# Usage

With the hook being installed, all your auto generated controller actions will be JSON API compliant.
This module also injects a service available as `JsonApiService` in your controllers to help you deal with JSON API.

## Serialize data

As shown in [tests/dummy/api/controllers/UserController.js:14](https://github.com/dynamiccast/sails-json-api-blueprints/blob/master/tests/dummy/api/controllers/UserController.js#L14), `JsonApiService.serialize` allows to serialize any waterline data into a JSON API compliant format. Just call :

````
JsonApiService(modelName, DataObject);
````

## Call blueprints from custom action

As shown in [tests/dummy/api/controllers/UserController.js:24](https://github.com/dynamiccast/sails-json-api-blueprints/blob/master/tests/dummy/api/controllers/UserController.js#L24), `JsonApiService` proxies blueprints to be accessible from any controller. Simply call the following with `req` and `res` as parameter:

- `findRecords` GET /{model}
- `findOneRecord` GET /{model}/{id}
- `createRecord` POST /{model}
- `destroyOneRecord` DELETE /{model}
- `updateOneRecord` PATCH /{model}/{id}

# Roadmap

- JSON API implementation
  - [X] GET all resources
  - [X] POST resource
  - [X] DELETE resource
  - [X] PATCH resource
  - [ ] Return proper error if any
  - [ ] Relationships
  - [ ] Compound Document
  - [ ] Links
  - [ ] Fields
  - [ ] Sorting
  - [ ] Pagination
  - [ ] Filtering
- Sails integration
  - [X] Allow the use of auto CreatedAt and UpdatedAt (see #3)
  - [ ] Pubsub integration
  - [X] Provide a service to serialize as JSON API for custom endpoints
- Repository
  - [X] Add tests on travis
  - [X] Provide status on the build on Github

# Tests

Simply run `npm test`

Tests expect the database to be empty. Local disk is used in the dummy app and can be deleted with `rm tests/dummy/.tmp/localDiskDb.db`.

# Thanks

This work is greatly inspired from https://github.com/mphasize/sails-generate-ember-blueprints
@mphasize did no longer actively maintain this repositiory, so I decided to fork it and to focus on JSON API compatibility.

{'fraction_non_alphanumeric': 0.09946236559139784, 'fraction_numerical': 0.0025466893039049238, 'mean_word_length': 4.13362381989833, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '157685', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2072, 'n_words': 788}