OSDN Git Service

Regular updates
[twpd/master.git] / sequelize.md
1 ---
2 title: Sequelize
3 category: Ruby libraries
4 ---
5
6 ### API
7
8     sequelize.sync().done -> ...
9
10 ### Models
11
12     Project = sequelize.define('Project', {
13       title: Sequelize.STRING,
14       description: Sequelize.TEXT,
15       myDate: { type: Sequelize.DATE, defaultValue: Sequelize.NOW },
16       title: { type: Sequelize.STRING, allowNull: false },
17       id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true },
18     }, {
19       classMethods: { ... },
20       instanceMethods: { ... }
21     });
22
23     Project.hasMany(Task)
24
25 ### Finders
26
27     Project.find(123).success (project) ->
28
29     Project.find({ where: {title: 'Hello'} })
30     Project.find({ where: {id: [1,3,4]} })
31     Project.find({ where: ["id > ?", 25] })
32
33     Project.find(
34       where: {title: 'a'}
35       attributes: ['id', ['name', 'title']]
36     )
37
38     .findOrCreate(...)
39
40     .findAll
41     .findAll({ where: ... })
42     .findAll({ order: 'title DESC' })
43     .findAll({ limit: 10 })
44     .findAll({ offset: 10, limit: 2 })
45
46     .count()
47
48
49 ### Build
50
51     item = Project.build({ ... })
52
53     item.title = '...'
54
55     item.save().success (item) ->
56
57     item.updateAttributes({ title: '...' })
58
59     item.destroy().success ->
60
61     item.values
62