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

laravel-rest-api-query-builder

v2.1.0

Published

JavaScript library to build RESTful API HTTP calls. This is made to work with RESTful APIs that use laravel-rest-api package. Inspired by Eloquent's Syntax. All credit goes to milroyfraser for his package sarala.

Downloads

119

Readme

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.

Original package's documentation

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 Model from './BaseModel';
import Comment from './Comment';
import Tag from './Tag';
import User from './User';

export default class Post extends Model {
    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); 

Learn More: Original package's documentation