laravel-query-api-frontend
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Laravel Query API Frontend

The package for writing beautiful eloquent style queries in frontend

For backend usage see this package

Installation

Via npm:

npm i laravel-query-api-frontend

Usage

  1. Import query builder:
import { QueryBuilder } from 'laravel-query-api-frontend'
  1. Write a query in laravel eloquent builder style, for ex.:
const query = QueryBuilder.fetch('App\\Post', 'post')
  .where(['id', '>', '10'])
  .whereHas('author', function(query) {
    return query.where('name', 'LIKE', '%John%');
  })
  .paginate(1, 10);

This query assumes to select all posts with ID > 10, where author name contains "John", paginated for 10 elements per page and returns page number 1

  1. Render the query to json:
const jsonQueryData = query.render();

This will return something like this:

{
    "query": "fetch",
    "key": "post",
    "type": "App\\Post",
    "params": {
        "per_page": 10,
        "page": 1,
        "parts": [
            {
                "kind": "where",
                "args": {
                    "params": [
                        "id",
                        "=",
                        1
                    ]
                }
            },
            {
                "kind": "whereHas",
                "args": {
                    "relation": "author",
                    "subquery": {
                        "query": "fetch",
                        "key": "k6o7daqzwcrkktkvltx",
                        "type": "author",
                        "params": {
                            "parts": [
                                {
                                    "kind": "where",
                                    "args": {
                                        "params": "name"
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}
  1. Send http request to your backend with jsonQueryData
import { QueryBuilder, QueryRunner, QueryResult } from 'laravel-query-api-frontend';

/**
 * for "http" you can use axios instance
 * apiURL - your query backend endpoint 
 */
const runner = new QueryRunner(http, apiURL);
const commentsQuery = QueryBuilder.fetch('App\\Comments', 'comments')
  .where(['popular', '=', true])
  .paginate(1, 10);

runner.addQuery(commentsQuery);
runner.runTransaction().then((result: QueryResult) => {
  let comments = result.getContent('comments');
  // do some stuff with received data 

})
.catch((error: any) => {
  // handle error
});

Available queries:

create

custom

delete

fetch

find

update

You can import query classes like this:

import { queries } from 'laravel-query-api-backend'

Supportable list of fetch conditions:

where

whereHas (orWhereHas)

whereDoesntHave (orWhereDoesntHave)

whereIn (whereNotIn)

whereNull (orWhereNull)

whereNotNull (orWhereNotNull)

whereColumn

whereExists

limit

offset

scope

with

Enjoy!

Change log

Please see the changelog for more information on what has changed recently.

Testing

npm run test

Security

If you discover any security related issues, please email shirokovnv@gmail.com instead of using the issue tracker.

Credits

  • [Nickolai Shirokov][link-author]
  • [All Contributors][link-contributors]

License

MIT. Please see the license file for more information.

Package Sidebar

Install

npm i laravel-query-api-frontend

Weekly Downloads

4

Version

0.1.0

License

MIT

Unpacked Size

63.2 kB

Total Files

82

Last publish

Collaborators

  • shirokovnv