JSPM

  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q18986F
  • 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

Configuration

You can use ENV variable or call the configuration method.

ENV

create a .env file like the example bellow.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=1234

Configuration

In the main script you can configure like this:

import {Configuration} from "fluent-orm";

const env = (env, default_value) => {
    return process.env[env] || default_value;
};

Configuration({
    'default': 'my_default_connection',
    'connections': {
        'my_default_connection': {
            'driver': 'mysql',
            'host': '127.0.0.1',
            'port': '3306',
            'database': env('DB_DATABASE', 'forge'), //you can also use env here
            'user': 'forge',
            'password': '',
            'charset': 'utf8mb4',
            'collation': 'utf8mb4_unicode_ci',
            'prefix': '',
            'prefix_indexes': true,
            'strict': true
        }
    }
});

"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();
    })
});