Patio query engine and ORM
Patio is a Sequel inspired query engine.
###Installation To install patio run
npm install comb patio
If you want to use the patio executable for migrations
npm install -g patio
To run the tests
To run just the postgres tests
To run just the mysql tests
###Why Use Patio?
Patio is different because it allows the developers to choose the level of abtraction they are comfortable with.
Model definitions are defined by the tables in the database.
As you add models the definition is automatically defined from the table definition. This is particularly useful when you want to define your model from a schema designed using another tool (i.e. ActiveRecord, Sequel, etc...)
Patio tries to stay out of your way when querying.
When you define a model you still have the freedom to do any type of query you want.
Only want certain columns?
MyModelselect"id" "name" "created"forEach//record only has the id, name, and created columns;
You want to join with another table?
MyModeljoin"otherTable" id: patiosqlidentifier"myModelId"forEach//Record has columns from your join table now!;
You want to run raw SQL?
MyModeldbrun"select * from my_model where name = 'Bob'"allchain//all records with a name that equals bob.;
You want to just query the database and not use a model?
var DB = patioconnect"pg://test:firstname.lastname@example.org:5432/test_db";DBfrom"myTable"filterid: 123all//records with id IN (1,2,3);
All the code for this example can be found here
Create a new database
psql -c "CREATE DATABASE reademe_example"
mysql -e "CREATE DATABASE readme_example"
Create a migration
mkdir migrationpatio migration-file -n createInitialTables ./migration
This will add a migration name
createdInitialTables in your migration directory.
Add the following code to your migration
moduleexports =//up is called when you migrate your database up//create a table called state;return dbcreateTable"state"thisprimaryKey"id";thisnameString;thispopulation"integer";thisfoundedDate;thisclimateString;thisdescription"text";chain//create another table called capitalreturn dbcreateTable"capital"thisprimaryKey"id";thispopulation"integer";thisnameString;thisfoundedDate;thisforeignKey"stateId" "state" key: "id" onDelete: "CASCADE";;;//down is called when you migrate your database down//drop the state and capital tablesreturn dbdropTable"capital" "state";;
Run your migration
patio migrate -v --camelize -u "<DB_CONNECTION_STRING>" -d ./migration
Connect and query!
var patio = require"patio";//set camelize = true if you want snakecase database columns as camelcasepatiocamelize = true;patioconnect"pg://email@example.com:5432/readme_example";//define a State model with a relationship to capitalvar State = patioaddModel"state"oneToOne"capital";//define a Capital model with a relationship to Statevar Capital = patioaddModel"capital"manyToOne"state";//save a stateStatesavename: "Nebraska"population: 1796619founded: 1867 2 4climate: "continental"//notice the capital relationship is inlinecapital:name: "Lincoln"founded: 1856 0 1population: 258379chain//save a Capitalreturn Capitalsavename: "Austin"founded: 1835 0 1population: 790390//define the state inlinestate:name: "Texas"population: 25674681founded: 1845 11 29;chain//Query all the states by namereturn Stateorder"name"forEach//Get the associated capitalreturn statecapitalchainconsole.log"%s's capital is %s." statename capitalname;;;chainprocessexitconsole.logerrprocessexit1;;