Last active
August 29, 2015 14:12
-
-
Save spro/52877b9752aff39b36eb to your computer and use it in GitHub Desktop.
Bookshelf.js Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
knex = require('knex')(client: 'pg', connection: 'postgres:///test1') | |
bookshelf = require('bookshelf')(knex) | |
util = require 'util' | |
inspect = (o) -> console.log util.inspect o.toJSON(), colors: true | |
# Schema | |
# ------ | |
User = bookshelf.Model.extend | |
tableName: 'users' | |
posts: -> @hasMany Post | |
transactions: -> @hasMany Transaction | |
Post = bookshelf.Model.extend | |
tableName: 'posts' | |
user: -> @belongsTo User | |
Transaction = bookshelf.Model.extend | |
tableName: 'transactions' | |
user: -> @belongsTo User | |
# Queries | |
# ------- | |
# Fetch all transactions with users attached | |
Transaction.fetchAll(withRelated: ['user']) | |
.then (transactions) -> inspect transactions | |
# Fetch a single user with all posts and all transactions | |
User.where(id: 5).fetch(withRelated: ['posts', 'transactions']) | |
.then (user) -> inspect user | |
# Fetch a single user with all posts and positive transactions | |
deposits = (q) -> q.where('amount', '>', 0) | |
User.where(id: 5).fetch(withRelated: ['posts', transactions: deposits]) | |
.then (user) -> inspect user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select * from users; | |
id | name | username | email | |
----+-------------------------+----------------------+------------------------------- | |
1 | Mattie Schaden | Granville_Roberts41 | [email protected] | |
2 | Frank Fahey | Lonie.Barton | [email protected] | |
3 | Archibald Lebsack | Reynold43 | [email protected] | |
4 | Loraine Rau | Dejuan_Conn87 | [email protected] | |
5 | Ernestine Klein | German_Homenick8 | [email protected] | |
6 | Adrain Waters | Pattie.Grimes | [email protected] | |
7 | Jazmin Schoen | Kim6 | [email protected] | |
8 | Naomie Stroman | Francesco_McCullough | [email protected] | |
9 | Noe Gottlieb | Fermin32 | [email protected] | |
10 | Leone Flatley | Germaine73 | [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table users { | |
id serial primary key, | |
name varchar(256), | |
username varchar(256), | |
email varchar(256) | |
} | |
create table posts { | |
id serial primary key, | |
body text, | |
user_id integer not null references users(id) | |
} | |
create table transactions { | |
id serial primary key, | |
summary varchar(256), | |
amount real, | |
date date, | |
user_id integer not null references users(id) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment