OSDN Git Service

Regular updates
[twpd/master.git] / bookshelf.md
1 ---
2 title: Bookshelf.js
3 category: JavaScript libraries
4 ---
5
6 Model
7 -----
8
9 ```js
10 Summary = bookshelf.Model.extend({
11   tableName: 'summaries',
12   hasTimestamps: true,
13   hasTimestamps: ['created_at', 'updated_at'],
14 })
15 ```
16
17 ### Associations
18
19 ```js
20 Summary = bookshelf.Model.extend({
21   book () {
22     return this.belongsTo(Book)
23   },
24   author () {
25     return this.hasOne(Author)
26   }
27   // belongsToMany
28   // hasMany
29   // hasMany().through()
30 })
31 ```
32
33 ### CRUD
34
35 ```js
36 Book.create({ title: '..' }).save()
37 new Book({ title: '..' }).save()
38
39 new Book({ id: 1 }).fetch()
40
41 Book.where({ id: 1 }).fetch()
42 Book.where('favorite_color', 'red').fetch()
43 Book.where('favorite_color', '<>', 'red').fetch()
44 Book
45   .query((q) => q.orderBy('updated_at')
46 ```