Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

sarala

milroyfraser538MIT0.3.10

Javascript library to communicate with RESTful API built following JSON API specification. inspired by Laravel’s Eloquent.

restful-api, json-api, laravel, eloquent, javascript, orm

readme

Coverage Status

Sarala JS

Javascript library to communicate with RESTful API built following JSON API specification. inspired by Laravel’s Eloquent

API Documentation | Background Story

Install

$ npm i sarala --save
$ yarn add sarala

Basic Usage

Model Implementation

app/models/BaseModel.js
import { Model } from 'sarala';
import axios from 'axios';

export default class BaseModel extends Model
{
    baseUrl () {
        return 'https://sarala-demo.app/api';
    }

    request (config) {
        return axios.request(config);
    }
}
app/models/Post.js
import Model from './BaseModel';
import Tag from './Tag';

export default class Post extends Model {
    resourceName () {
        return 'posts';
    }

    fields () {
        return ['title', 'subtitle', 'body', 'slug'];
    }

    relationships () {
        return {
            tags: new Tag()
        };
    }
}
app/models/Tag.js
import Model from './BaseModel';

export default class Tag extends Model {
    resourceName () {
        return 'tags';
    }

    fields () {
        return ['name'];
    }
}

Fetching data

import Post from './../models/Post';

const post = new Post();

// makes a GET request to https://sarala-demo.app/api/posts
const fetchAllPosts = async () => {
    let posts = await post.with(['tags']).all();
};

Insert

app/components/MyComponent.js
import Tag from './../models/Tag';

const tag = new Tag();
tag.name = 'json-api';

// makes a POST request to https://sarala-demo.app/api/tags
tag.save(); 
// or you can directly call tag.create();

Change log

Please see CHANGELOG for more information on what has changed recently.

API Documentation | Background Story

changelog

Changelog

All notable changes to sarala will be documented in this file.

v0.2.2 - 2018-03-10

Fixed

  • Generating malformed url when querying from same instance multiple times

v0.2.3 - 2018-03-10

Added

  • Introduced data format configuration. Now dates method should return an object. key name is the field name and value format will be used to serialize date value before sending it back to the api.

Removed

  • dataFormat method has been removed from Model class.

v0.2.4 - 2018-03-11

Added

  • Accept Date object, string or moment object as date field value.

v0.2.5 - 2018-03-16

Added

  • Added the ability to clone an model object by calling modelObj.clone();.

v0.3.0 - 2018-03-16

Fixed

Added

v0.3.1 - 2018-03-17

Added

v0.3.2 - 2018-04-05

Fixed

v0.3.4 - 2019-01-02

  • security update

v0.3.5, v0.3.6, v0.3.7 - 2019-01-09

  • added api docs
  • shit happens: fixed npm issues

v0.3.8 - 2019-01-09

  • Introduced headers preparation #6

v0.3.9 - 2019-01-09

Fixed