DEV Community

Lam
Lam

Posted on

Bookshelf.Js Cheat Sheet

CRUD

Book.create({ title: '..' }).save()
new Book({ title: '..' }).save()

new Book({ id: 1 }).fetch()

Book.where({ id: 1 }).fetch()
Book.where('favorite_color', 'red').fetch()
Book.where('favorite_color', '<>', 'red').fetch()
Book
  .query((q) => q.orderBy('updated_at')
Enter fullscreen mode Exit fullscreen mode

Associations

Summary = bookshelf.Model.extend({
  book () {
    return this.belongsTo(Book)
  },
  author () {
    return this.hasOne(Author)
  }
  // belongsToMany
  // hasMany
  // hasMany().through()
})
Enter fullscreen mode Exit fullscreen mode

Reference

Top comments (0)