Skip to content

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

License

Notifications You must be signed in to change notification settings

robinmarechal/laravel-rest-api-query-builder

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

laravel-rest-api-query-builder

npm version apm

JavaScript library to build RESTful API HTTP calls with Eloquent's-like syntax. This is made to work with RESTful APIs that use laravel-rest-api package.

This package is just a modification to milroyfraser/sarala in order to make it compatible with laravel-rest-api package.

All credits goes to milroyfraser.

Install

$ npm i laravel-rest-api-query-builder --save
$ yarn add laravel-rest-api-query-builder

Basic Usage

Model Implementation

app/models/BaseModel.js
import Model from 'laravel-rest-api-query-builder';

export default class BaseModel extends Model
{
    getBaseUrl(){
        return "https://myserver.com/api";
    }
}
app/models/Post.js
import BaseModel from './BaseModel';
import Comment from './Comment';
import Tag from './Tag';
import User from './User';

export default class Post extends BaseModel {
    getNamespace () {
        return 'posts';
    }

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

    getDates () {
	// one of 'datetime', 'date' and 'time'
        return { 
	    created_at: 'datetime',
	    updated_at: 'datetime'
        };
    }

    getRelations () {
        return {
            author: {
	        class: User,
	        list: false
            },
            tags: {
	        class: Tag,
	        list: true
            },
            comments: {
                class: Comment,
                list: true
	    },
        };
    }

    computed () {
        return {
            full_date (post) {
                return post.published_at.format('MMMM Do YYYY');
            },

            human_date (post) {
                return post.published_at.fromNow();
            }
        };
    }
}
app/models/Tag.js
import Model from './BaseModel';
import Post from './Post';

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

    getFields () {
        return ['name'];
    }
    
    getRelations(){
	return {
	    posts: {
		class: Post,
		list: true
	    }
	}
    }
}

Fetching data

import Customer from './Customer';
import Post from './Post';
import { Query } from 'laravel-rest-api-query-builder'

// Get the post with id 7
Query.model(Post)
    .find(7)
    .then(post => console.log(post));

// Get all posts
Query.model(Post)
    .all()
    .then(posts => console.log(posts));

// Get all posts with their author and tags
Query.model(Post)
    .with('author', 'tags')
    .all()
    .then(posts => console.log(posts));

// Get, sort and limit
Query.model(Post)
    .orderBy('-id', 'title')
    .limit(10)
    .all()
    .then(posts => console.log(posts));

// Paginate : 10 per page, 5th page
Query.model(Post)
    .paginate(10, 5)
    .then(posts => console.log(posts));

// Get a post then get its author
Query.model(Post)
    .find(7)
    .then(post => Query.model(User).of(post).all())
    .then(users => console.log(users[0]));

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.create()
    .then(tag => {
        tag.name = 'json-api-2';
        return tag.update();
    })
    .then(console.log); 

// save
const tag2 = new Tag();
tag2.name = 'tag2';
tag2.save()
    .then(tag => {
        tag.name = 'tag2-3';
        return tag.save();
    })
    .then(console.log); 

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 98.4%
  • Makefile 1.6%