1.0.4 • Published 7 years ago

sarina.activerecord v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

Sarina.ActiveRecord

This plugin has implmeented ActiveRecord design pattern for sarina. Plugin created by JavadParvaresh.

In software engineering, the active record pattern is an architectural pattern found in software that stores in-memory object data in relational databases. It was named by Martin Fowler in his 2003 book Patterns of Enterprise Application Architecture.1 The interface of an object conforming to this pattern would include functions such as Insert, Update, and Delete, plus properties that correspond more or less directly to the columns in the underlying database table.(wikipedia)

Plugin has build on top of Knex and bookshelf.

Table of contents

Quick start

Several quick start options are available:

  • Clone the repo: git clone https://github.com/javadparvaresh/Sarina-ActiveRecord.git
  • Install with npm: npm install sarinaactiverecord

Bugs and feature requests

Have a bug or a feature request? please open a new issue.

The Basics

var sarina=require("sarina");
var sarinaactiverecord=require("sarinaactiverecord");

// create a sarina app by passing configuration
var app=sarina.create({
    db:{
        "default":{
            "client":"mysql",
            "host":"127.0.0.1",
            "database":"SampleDb",
            "user":"root",
            "password":""
        }
    }
});

// add to modules
app.module(sarinaactiverecord);

// Defining model
app.factory("Sample",["sarina.activerecord.provider"],function(ar){
    return ar.define()
        .config("default")
        .table("TbSample")
        .column("id","INT",11,["unique"])
        .column("TITLE","VARCHAR",50)
        .create();
});


// using model
app.service("sample.dataprovider",["Sample"],function(model){

    return {
        insert:function(title){
            return new Promise(function(resolve,reject){
                new model({
                    "title":title
                    })
                    .insert()
                    .then(resolve)
                    .catch(reject);
            });
        },
        update:function(id,title){
            return new Promise(function(resolve,reject){
                new model()
                    .where("id",id)
                    .update({
                        "title":title
                    })
                    .then(resolve)
                    .catch(reject);
            });
        },
        remove:function(id){
            return new Promise(function(resolve,reject){
                new model()
                    .where("id",id)
                    .remove()
                    .then(resolve)
                    .catch(reject);
            })
        },
        fetchAll:function(){
            return new Promise(function(resolve,reject){
                return new model()
                    .fetchAll()
                    .then(resolve)
                    .catch(reject);
            })
            
        }
    }
    

});


// add a executable process to sarina
app.exec("runner",["sample.dataprovider"],function(sampledp){
    return {
        run:function(){
            return new Promise(function(resolve,reject){
                sampledp.fetchAll()
                    .then(function(result){
                        console.log("Result:",result);
                    }).catch(reject);
            })
        }
    }
});

// finally we need to start app
app.start();

The Configuration

var sarina=require("sarina");
var sarinaactiverecord=require("sarinaactiverecord");

var app=sarina.create({});

app.config("sarina.customConfig",["sarina.activerecord.provider"],function(provider){

    provider.set("myConfig",{
        "client":"mysql",
        "host":"127.0.0.1",
        "database":"database",
        "user":"root",
        "password":""
    });

    app.factory("simpleModel",["sarina.activerecord"],function(ar){
        return 
            ar.define()
            .config("myConfig") // using my custom config
            .table("tbSimple")
            .column("id","INT",33,["unique"])
            .column("title","VARCHAR",200)
            .create();
    });

});

Apis

  1. Query
    //....

    new Model()
        .where("column1","equalto") 
        .where("column2","test") // an other where clause
        .whereRaw("columnd3 >= :column3",value)
        .orderBy("column4") // order by ascending
        .orderByDesc("column4") // order by descending
        .limit(1) // limiting fetched rows
        .skip(2) // skip rows 

        .count() // return count of Query
        .fetchOne() // return first matched record
        .fetchAll() // return all matched records

        .query() // raw created query as string
    //....
  1. Manipulating Data
    // ...

    // Insert or Update
    new Model({
        Column1 : "New Value"
    }).where("id",1)
    .save();

    // Insert
    new Model({
        Column1 : "New Value"
    })
    .insert();

    // Update
    new Model({
        Column1 : "New Value"
    }).where("id",1)
    .update();

    // Remove
    new Model()
        .where("id",1)
        .remove();

    // ...
1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago