JSPM

  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q18987F
  • License MIT

Js ORM inspired by Eloquent

Package Exports

  • fluent-orm

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fluent-orm) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

fluent

Javascript ORM Inspired by Eloquent.

Installation

npm install fluent-orm --save

or

yarn add fluent-orm

"Working" features

This is a experimental project, can dramatically change its structure at any time.

  • Select (With EagerLoader)
  • Delete
  • Update
  • Insert
  • Transaction

Roadmap

  • Add others type of relations (Only hasMany supported so far)
  • Add more driver support (Only Support Mysql right now)
  • Bind Select Result to Js Model to use save and delete function direct from the model.

Use

Model

import {Model} from "fluent-orm";
import Permission from "./Column";
import Email from "./Column";

class Person extends Model {

    Permissions() {
        return this.hasMany(Permission, 'permission_id', 'id');
    }

    Emails() {
        return this.hasMany(Email);
    }

}

export default Person;

Select

Person.query().with('Permissions', 'Emails').get().then(response => {
    console.log(response);
}).catch(error => {
    console.log(error);
});

Create/Insert

Create insert a single element and return it with id. Insert is a bulk function.

Person.create({name, password}).then(person => {
    const dataEmails = emails.map(email => ({
        person_id: person.id,
        email: email
    }));

    Email.insert(dataEmails).then(success => {
        console.log({
            person,
            success
        });
    });
}).catch(error => {
    console.log(error);
})

Delete

Person.transaction((transaction, commit, rollback) => {
    Person.query().where('id', id).firstOrFail({transaction}).then(person => {
        Person.query().where('id', person.id).delete({transaction}).then(() => {
            commit();
            res.json({
                success: 'Person deleted successfully'
            });
        });
    }).catch(error => {
        res.status(500).send({
            error: error,
            message: error.toString()
        });
        rollback();
    })
})

Update

Person.transaction((transaction, commit, rollback) => {
    Person.query().where('id', id).firstOrFail({transaction}).then(person => {
        Person.query().where('id', person.id).update({
            name: name
        }, {transaction}).then(() => {
            commit();
            res.json({
                success: 'Person Updated successfully'
            });
        }).catch(error => {
            res.status(500).send({
                error: error,
                message: error.toString()
            });
            rollback();
        });
    }).catch(error => {
        res.status(500).send({
            error: error,
            message: error.toString()
        });
        rollback();
    })
});