Giter Site home page Giter Site logo

es-entity's Introduction

es-entity

ORM framework for Node js based on Ecmascript 6 and Typescript.

Contributors

Nitin Bansal https://github.com/nitinbansal1989

Installation

$ npm install es-entity

Usage

Create DB context and provide connection configuration.

const es = require("es-entity");

Entity Class

Entity class is the reference to the table in the database. The property in this class are field objects which refers to the columns of the table.

Note: The class members are camel cased from the snake case database columns. Annotation for custom column name will be implemented in future.

class Employee {
    constructor() {
        this.id = new es.Number();
        this.name = new es.String();
        this.description = new es.String();
    }
}

DB Configuration

Context is provided with the connection parameters of the database to connect.

var config = new es.ConnectionConfig();
config.handler = "mysql";
config.hostname = "localhost";
config.name = "mysql";
config.username = "root";
config.password = "application";
config.database = "test";

Context

Context class is created which extends from es.Context. This class works as database context for all your queries. The property in this class as queryable object to the respective classes.

const Employee_1 = require("./Employee");
class EmpContext extends es.Context {
    constructor(config, entityPath) {
        super(config, entityPath);
        this.employees = new es.DBSet(Employee_1.default);
        this.init();
    }
}

Provide the database config object and mapping folder object to the context object to bind.

const EmpContext_1 = require("./modal/EmpContext");
var context = new EmpContext_1.default(config);

Operations

Select Entities

let p = context.employees.where((a) => {
    return (a.id.lt(q)).or(a.id.eq(2));
}).list();

p.then((v) => {
    for (var i = 0; i < v.length; i++) {
        var j = v[i];
        console.log("id: " + j.id + ", name: " + j.name + ", desc: " + j.description);
    }
});

Select Entity By Id

let p = context.employees.get(1);

p.then((v) => {
    console.log("id: " + v.id + ", name: " + v.name + ", desc: " + v.description);
});

Updating Entity

v.description.set("test update 2");
let p = context.employees.update(v);
p.then((v) => {
    console.log("id: " + v.id + ", name: " + v.name + ", desc: " + v.description);
    console.log("updated");
});

Inserting Entity

let a = context.employees.getEntity();
a.name.set("name 2");
a.description.set("desc insert 2");
let p = context.employees.insert(a);
p.then((v) => {
    console.log("id: " + v.id + ", name: " + v.name + ", desc: " + v.description);
    console.log("inserted");
});

Deleting Entity

let p = context.employees.delete(v);
p.then(() => {
    console.log("deleted");
});

Note: Currently it is very basic and only supports mysql. Please provide suggestions if any.

es-entity's People

Contributors

nitinet avatar rohankanojia avatar

Watchers

 avatar  avatar

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.