cheatsheets/bookshelf.md

48 lines
738 B
Markdown
Raw Permalink Normal View History

2013-10-14 04:36:58 +02:00
---
title: Bookshelf.js
2015-11-24 06:02:17 +01:00
category: JavaScript libraries
2013-10-14 04:36:58 +02:00
---
2013-09-17 12:06:00 +02:00
Model
-----
### Model
2016-01-07 05:47:48 +01:00
```js
Summary = bookshelf.Model.extend({
tableName: 'summaries',
hasTimestamps: true,
hasTimestamps: ['created_at', 'updated_at'],
})
```
2013-09-17 12:06:00 +02:00
### Associations
2016-01-07 05:47:48 +01:00
```js
Summary = bookshelf.Model.extend({
book () {
return this.belongsTo(Book)
},
author () {
return this.hasOne(Author)
}
// belongsToMany
// hasMany
// hasMany().through()
})
```
### CRUD
```js
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')
```