Coder Social home page Coder Social logo

graphqloquent's Introduction

Graphqloquent

Graphql javascript model based - inspired by Laravel Eloquent

Ideas

Graphql provide us very fast way to get data from backend. Laravel Eloquent provide model base working with database. The idea is provide a way to query data like Eloquent for javascript. And this package was born to provide fatest way to define model

Requrements

  • Postgresql
  • Hasura or an api for backend
  • Apollo client for graphql
  • Static method of javascript from ECMAScript 2015

Basic usage

Defining model

Define Model

import Model from 'graphqloquent';

export default class Flight extends Model {
}

Table name

import Model from 'graphqloquent';

export default class Flight extends Model {
  /**
   * table: is the table associated with model
   */

  constructor() {
    this.table = 'my_flights';
  }
}

Primary key

import Model from 'graphqloquent';

export default class Flight extends Model {

  /**
   * key: is the table primary key
   */
  constructor() {
    this.key = 'my_flights_id';
  }
}

Timestamps

By default, Graphloquent expects created_at and updated_at columns exist on your tables

import Model from 'graphqloquent';

export default class Flight extends Model {

  /**
   * key: is the table primary key
   */
  constructor() {
    this.key = 'my_flights_id';
  }
}

Retrieving Models

Get all flights

import Flight from '@/models/Fligt';
$flights = Flight.all();

With conditions

import Flight from '@/models/Flight';
$flight = Flight.where('active', 1)
                .orderBy('name', 'desc')
                .take(10)
                .get();

Retrieving Single Model

Get Single Model

import Flight from '@/models/Fligt';
$flight = Flight.find(1);

With conditions

import Flight from '@/models/Flight';
$flight = Flight.where('active', 1).first();

Notfound exception

import Flight from '@/models/Flight';
$flight = Flight.findOrFail(1);
$flight = Flight.where('leg', '>', 1).findOrFail();

Retrieving Aggregates

import Flight from '@/models/Flight';
$flight = Flight.where('active', 1).count();

Inserting and Updating model

Insert

import Flight from '@/models/Flight';
$flight = new Flight();
$flight.name = 'Airbus 320';
$flight.save();

Update

import Flight from '@/models/Flight';
$flight = Flight.find(1);
$flight.name = 'New Airbus 320';
$flight.save();

Delete

import Flight from '@/models/Flight';
$flight = Flight.find(1);
$flight.delete();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.