@zpnk
{
"id": 1,
"name": "Foo Fighters",
"bio": "Foo Fighters are an American rock band, \
formed in Seattle in 1994."
}
{
"id": 1,
"title": "Echoes, Silence, Patience & Grace",
"released": "September 25, 2007",
"artist_id": 1
}
{
"id": 1,
"title": "The Pretender",
"length": 269,
"album_id": 1
}
{
"id": 1,
"name": "Foo Fighters",
"bio": "Foo Fighters are an American rock band, \
formed in Seattle in 1994."
}
knex.schema.createTable('artists', function(column) {
column.increments('id').primary()
column.text('name').notNullable()
column.text('bio').notNullable()
})
var artist = {
name: "Foo Fighters",
bio: "Foo Fighters are an American rock band,
formed in Seattle in 1994."
}
knex.insert(artist).into('artists').then(console.log)
// {id: 1, name: "..", bio: ".."}
knex.select().from('artists').where({id: 1}).then(console.log)
// {id: 1, name: "Foo Fighters", bio: "..."}
exports.up = function(knex) {
return knex.schema.table('artists', function(column) {
column.text('origin').notNullable()
})
}
exports.down = function(knex) {
return knex.schema.table('artists', function(table) {
table.dropColumn('origin')
})
}
knex('artists').update({origin: "Seattle, WA"}).where({id: 1})
// {id: 1, name: "...", bio: "...", origin: "Seattle, WA"}
knex.schema.createTable('albums', function(column) {
column.increments('id').primary()
column.text('title').notNullable()
column.date('released').notNullable()
column.integer('artist_id').references('id').inTable('artists')
})
{
"id": 1,
"title": "Echoes, Silence, Patience & Grace",
"released": "September 25, 2007",
"artist_id": 1
}
var album = {
title: "Echoes, Silence, Patience & Grace",
released: "September 25, 2007",
artist_id: 1
}
knex.insert(album).into('albums').then(console.log)
// {id: 1, title: "...", released: "...", artist_id: "..."}
knex('albums')
.innerJoin('artists', 'artists.id', 'albums.artist_id')
.then(console.log)
// {id: 1, title: "..", length: "..", ...}
knex.schema.createTable('songs', function(column) {
column.increments('id').primary()
column.text('title').notNullable()
column.integer('length').notNullable()
column.integer('album_id').references('id').inTable('albums')
})
{
"id": 1,
"title": "The Pretender",
"length": 269,
"album_id": 1
}
var songs = [
{title: "The Pretender", length: 269},
{title: "Erase/Replace", length: 245}
]
knex.insert(songs).into('songs').then(console.log)
// [{id: 1, ...}, {id: 2, ...}]
var Artist = bookshelf.Model.extend({
tableName: 'artists',
albums: function() {
return this.hasMany(Album)
},
songs: function() {
return this.hasMany(Song).through(Album)
}
})
knex.select().from('artists').where({id: 1})
new Artist({id: 1}).fetch()
var artist = new Artist({id: 1})
artist.albums().fetch()
// [{id: 1, title: "Echoes...", released: "September..."}]
artist.songs().fetch()
// [{title: "The Pretender", length: 269}, {title: ...}]
knexjs.org
bookshelfjs.org
github.com/c2fo/patio
sequelizejs.com
github.com/philwaldmann/openrecord
github.com/tryghost/ghost