All Projects → DavidDuwaer → Coloquent

DavidDuwaer / Coloquent

Licence: mit
Javascript/Typescript library mapping objects and their interrelations to JSON API, with a clean, fluent ActiveRecord-like (e.g. similar to Laravel's Eloquent) syntax for creating, retrieving, updating and deleting model objects.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Coloquent

Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (+28.19%)
Mutual labels:  json-api, jsonapi, json, activerecord
Json Api Dart
JSON:API client for Dart/Flutter
Stars: ✭ 53 (-64.43%)
Mutual labels:  json-api, jsonapi, json
Json Api
Implementation of JSON API in PHP 7
Stars: ✭ 171 (+14.77%)
Mutual labels:  json-api, jsonapi, json
Jsonapi parameters
Rails-way to consume JSON:API input
Stars: ✭ 50 (-66.44%)
Mutual labels:  json-api, jsonapi, json
Symfony Jsonapi
JSON API Transformer Bundle for Symfony 2 and Symfony 3
Stars: ✭ 114 (-23.49%)
Mutual labels:  json-api, jsonapi, json
Jsonapi Rails
Rails gem for fast jsonapi-compliant APIs.
Stars: ✭ 242 (+62.42%)
Mutual labels:  json-api, jsonapi, json
Jsonapi Rb
Efficiently produce and consume JSON API documents.
Stars: ✭ 219 (+46.98%)
Mutual labels:  json-api, jsonapi, json
Laravel5 Jsonapi
Laravel 5 JSON API Transformer Package
Stars: ✭ 313 (+110.07%)
Mutual labels:  json-api, jsonapi, json
Jsonapi Client
JSON API (jsonapi.org) client for Python
Stars: ✭ 63 (-57.72%)
Mutual labels:  json-api, jsonapi
Dictfier
Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Stars: ✭ 67 (-55.03%)
Mutual labels:  json-api, json
Neo
Create blazing fast multithreaded Web Apps
Stars: ✭ 1,219 (+718.12%)
Mutual labels:  json, frontend
Weld
Full fake REST API generator written with Rust
Stars: ✭ 146 (-2.01%)
Mutual labels:  json-api, json
Jsonapi
[Bolt Extension] JSON API for Bolt CMS
Stars: ✭ 55 (-63.09%)
Mutual labels:  jsonapi, json
Mir Ion
WIP, use libmir/asdf package for now
Stars: ✭ 78 (-47.65%)
Mutual labels:  jsonapi, json
Loopback Component Jsonapi
JSONAPI support for loopback.
Stars: ✭ 104 (-30.2%)
Mutual labels:  json-api, jsonapi
Rki Covid Api
🦠🇩🇪📈 An API for the spread of covid-19 in Germany. Data from Robert-Koch-Institut.
Stars: ✭ 98 (-34.23%)
Mutual labels:  json-api, json
Athena Express
athena-express makes it easier to execute SQL queries on Amazon Athena by chaining together a bunch of methods in the AWS SDK. This allows you to execute SQL queries AND fetch JSON results in the same synchronous call - well suited for web applications.
Stars: ✭ 111 (-25.5%)
Mutual labels:  json-api, json
Sarala
Javascript library to communicate with RESTful API built following JSON API specification. inspired by Laravel’s Eloquent
Stars: ✭ 101 (-32.21%)
Mutual labels:  json-api, eloquent
Jsonapi.rb
Lightweight, simple and maintained JSON:API support for your next Ruby HTTP API.
Stars: ✭ 116 (-22.15%)
Mutual labels:  json-api, jsonapi
Framework
Strongly-typed JavaScript object with support for validation and error handling.
Stars: ✭ 136 (-8.72%)
Mutual labels:  json, frontend

Coloquent

Javascript/Typescript library mapping objects and their interrelations to JSON API, with a clean, fluent ActiveRecord (e.g. similar to Laravel's Eloquent) syntax for creating, retrieving, updating and deleting model objects. For example:

Teacher
    .where('gender', 'm')                   // sets a filter
    .with('students')                       // eager loads related models
    .with('schools.address')                // eager loads directly and indirectly related models
    .get()                                  // submits the HTTP request, returns an ES6 Promise
    .then(coloquentResponse => {
        // do stuff with response of full-fledged, interrelated model objects
    });

npm version Build Status

To get started, see our more elaborate guide in our wiki! A short version is featured below.

Install

$ npm install coloquent

Requirements

  • Use in a project of which the runtime code is Javascript ES6 or higher. If your compile target is ES5 or lower, you will run into this issue.
  • The attribute jsonApiBaseUrl explained in the Setup section of this readme must point to an API implementing the JSON API standard.

Usage

Retrieving

To retrieve a single page of models of a certain type:

Artist.get();

To retrieve the second page

Artist.get(2);

The page size can be configured, this is covered the Setup section.

To add a filter, add a where clause

Artist
    .where('country', 'US')
    .get();

To eager load related models within the same HTTP request, add a with clause.

Artist
    .with('songs')
    .get();

To sort the result set server-side (indispensible for pagination), add an orderBy clause:

Artist
    .orderBy('birthDate', 'desc')
    .get();

The second argument denoting the sorting direction is optional and is either asc (default) or desc (or SortDirection.ASC and SortDirection.DESC). If you are only interested in the youngest Artist, it is more efficient to use first instead of get:

Artist
    .orderBy('birthDate', 'desc')
    .first();

This will retrieve only a single model from the server. To retrieve a single model by ID:

Artist.find(324);

To query a relation of an object you've instantiated:

artist.albums()
    .orderBy('name', SortDirection.DESC)
    .get()

If, for some reason, you need to add a raw URL query parameter (e.g. ?_foo=somevalue) to the underlying endpoint, use the option clause:

Artist
    .option('_foo', 'somevalue')
    .get();

All of the queries above return an ES6 Promise to which an instance of -depending on whether a single or multiple models were requested- SingularResponse or PluralResponse is passed. From these classes both requested models and eagerly loaded models can be obtained, e.g.:

var teacher = coloquentResponse.getData[0];
var schoolAddress = teacher.getSchools()[0].getAddress();
var student = teacher.getStudents()[0];

The variables teacher, schoolAddress and student now all contain full-fledged model objects.

Creating / updating

To save an instance of Artist to the server:

artist.save();

If artist has the property id set, Coloquent will attempt a PATCH request to update an existing object; otherwise it will perform a POST request, creating a new object server-side.

Deleting

To delete an instance of Artist from the server:

artist.delete();

Setup

All you need to do is extend Coloquent's Model class with your own model:

import {Model} from 'coloquent';

class Artist extends Model
{
    public getJsonApiBaseUrl(): string
    {
        return 'http://www.app.com/api/';
    }
    
    protected jsonApiType = 'artists';
    
    protected pageSize = 30;
}

If there are settings that you want the same for all your models, it is useful to make an intermediary class that extends Coloquent's Model, and have your model classes extend that class. This is done in the following example.

Example setup

We are configuring 3 models: Artist, Album and Song. In the following example, Typescript type assertions (e.g. : Artists[]) are included in the syntax, but if you don't use Typescript, remember that Coloquent also works in Javascript without these type assertions.

import {Model} from 'coloquent';

class AppModel extends Model
{
    getJsonApiBaseUrl(): string
    {
        return 'http://www.app.com/api/';
    }
}

class Artist extends AppModel
{
    jsonApiType = 'artists';
    
    readOnlyAttributes = [
        'age'
    ];

    albums(): ToManyRelation
    {
        return this.hasMany(Album);
    }

    getAlbums(): Album[]
    {
        return this.getRelation('albums');
    }
    
    getBirthDate(): string
    {
        return this.getAttribute('birtDate');
    }
    
    getAge(): number
    {
        return this.getAttribute('age');
    }
    
    getCountry(): string
    {
        return this.getAttribute('country');
    }
    
    setCountry(country: string)
    {
        this.setAttribute('country', country);
    }
}

class Album extends AppModel
{
    jsonApiType = 'albums';

    artist(): ToOneRelation
    {
        return this.hasOne(Artist);
    }

    songs(): ToManyRelation
    {
        return this.hasMany(Song);
    }

    getArtist(): Artist
    {
        return this.getRelation('artist');
    }

    getSongs(): Song[]
    {
        return this.getRelation('songs');
    }
}

class Song extends AppModel
{
    jsonApiType = 'songs';

    album(): ToOneRelation
    {
        return this.hasOne(Album);
    }

    getAlbum(): Album
    {
        return this.getRelation('album');
    }
}

Now we can query these models in the fashion shown in the Usage section of this readme. Note that the models contain getters, and that these getters get the values of relationships and attributes with this.getRelation and this.getAttribute, respectively. Attributes can conversely be set with a this.setAttribute method.

Also note the methods that return an object of type ToManyRelation or ToOneRelation. These are relationship declarations: they tell Coloquent what kind of relationship there exists. It is required that they bear the same name as the cosponding relationship in the underlying JSON API.

Finally, note that the Artist class overrides an array called readOnlyAttributes. This array is for attributes that should be excluded from the payload sent to the server when saving an instance of Artist (using the save() method).

Feedback

If something is missing from this library that makes it not fit your use case today, or if you find a bug that spoils it for you, don't hesitate to create an Issue or a Pull Request. Coloquent is in active development and all feedback and contributions are sincerely appreciated.

License

The content of this project is licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].