Skip to content

Repository files navigation

#Patio

Patio is a Sequel inspired query engine.

###Why Use Patio?

Patio is different because it allows the developers to choose the level of abtraction they are comfortable with.

If you want to use ORM functionality you can. If you dont you can just use the Database and Datasets as a querying API, and if you need toyou can write plain SQL

###Installation To install patio run

npm install comb patio

If you want to use the patio executable for migrations

npm install -g patio

###Getting Started

Create some tables.

varpatio=require("patio"),comb=require("comb"),when=comb.when,serial=comb.serial;//set all db name to camelize patio.camelize=true;patio.configureLogging();//connect to the db varDB=patio.connect(<CONNECTION_URI>); function errorHandler(error) {console.log(error);patio.disconnect();}; function createTables() {returncomb.serial([function(){returnDB.forceDropTable(["capital","state"]);},function(){returnDB.createTable("state",function(){this.primaryKey("id");this.name(String)this.population("integer");this.founded(Date);this.climate(String);this.description("text");});},function(){returnDB.createTable("capital",function(){this.primaryKey("id");this.population("integer");this.name(String);this.founded(Date);this.foreignKey("stateId","state",{key:"id"});});}]);}; createTables().then(function () {patio.disconnect();}, errorHandler); 

Next lets create some models for the tables created.

varState=patio.addModel("state",{static:{init:function(){this._super(arguments);this.oneToOne("capital");}}});varCapital=patio.addModel("capital",{static:{init:function(){this._super(arguments);this.manyToOne("state");}}});

Next you'll need to sync your models

patio.syncModels();

Use your models.

//comb.when waits for the save opertareturncomb.when(State.save({name:"Nebraska",population:1796619,founded:newDate(1867,2,4),climate:"continental",capital:{name:"Lincoln",founded:newDate(1856,0,1),population:258379}}),Capital.save({name:"Austin",founded:newDate(1835,0,1),population:790390,state:{name:"Texas",population:25674681,founded:newDate(1845,11,29)}}));

Now we can query the states and capitals we created.

State.order("name").forEach(function(state){//if you return a promise here it will prevent the foreach from//resolving until all inner processing has finished.returnstate.capital.then(function(capital){console.log("%s's capital is %s.",state.name,capital.name);});});
Capital.order("name").forEach(function(capital){//if you return a promise here it will prevent the foreach from//resolving until all inner processing has finished.returncapital.state.then(function(state){console.log(comb.string.format("%s is the capital of %s.",capital.name,state.name));});});

###Features

About

ORM for node.js

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors