index.ejs
7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<script type="text/javascript" charset="utf-8">
document.observe("dom:loaded", function() { buildNavigation([], []) })
</script>
<div>
<a name="sequelize"></a>
<h1>Sequelize</h1>
<p>
The Sequelize library provides easy access to a MySQL database by mapping database
entries to objects and vice versa. To put it in a nutshell... it's an ORM (Object-Relational-Mapper).
The library is written entirely in JavaScript and can be used in the Node.JS environment.
</p>
</div>
<div class="seperator"></div>
<div>
<a name="installation"></a>
<h2>Installation</h2>
<p>
Sequelize will have a Kiwi package in future. For now, you can install it via NPM or just download
the code from the git repository and require Sequelize.js:
<pre><%= partial("installation.ejs") %></pre>
This will make the class Sequelize available.
</p>
</div>
<div class="seperator"></div>
<div>
<a name="basicMapping"></a>
<h2>Basic Mapping</h2>
<p>
To get the ball rollin' you first have to create an instance of Sequelize. Use it the following way:
<pre><%- koala.render(".js", "var sequelize = new Sequelize('database', 'username', 'password')") %></pre>
This will save the passed database credentials and provide all further methods. Furthermore you can specify
a non-default host or port and some options:
<pre><%- koala.render(".js", partial("advancedInstantiation.ejs")) %></pre>
To define mappings between a class (Stop telling me that JavaScript don't know classes. Name it however you want to!)
and a table, use the define method:
<pre><%- koala.render(".js", partial("basicMapping1.ejs")) %></pre>
Sequelize currently supports the following datatypes:
<pre><%= partial("basicMapping2.ejs") %></pre>
You can also store your model definitions in a single file using the import method:
<pre><%- koala.render(".js", partial("basicMapping3.ejs")) %></pre>
Choose the name of the exported function the way you want. It doesn't matter at all. You can also specify multiple
models in one file. The import method will return a hash, which stores the result of sequelize.define under the key <i>Project</i>.
</p>
</div>
<div class="seperator"></div>
<div>
<a name="sync"></a>
<h2>Synchronize with database</h2>
<p>
When starting a new project you won't have a database structure and using Sequelize you won't need to. Just specify
your model structures and let the library do the rest.<br><br>
Currently supported is the creation and deletion of tables:
<pre><%- koala.render(".js", partial("sync1.ejs")) %></pre>
Because synchronizing and dropping all of your tables might be a lot of lines to write, you can also let
Sequelize do the work for you:
<pre><%- koala.render(".js", partial("sync2.ejs")) %></pre>
</p>
</div>
<div class="seperator"></div>
<div>
<a name="models"></a>
<h2>Creating and working with instances</h2>
<p>
In order to create instances of defined classes just do it as follows:
<pre><%- koala.render(".js", partial("models1.ejs")) %></pre>
To save it in the database use the save method and pass a callback to it, if needed:
<pre><%- koala.render(".js", partial("models2.ejs")) %></pre>
Now lets change some values and save changes to the database... There are two ways to do that:
<pre><%- koala.render(".js", partial("models3.ejs")) %></pre>
</p>
</div>
<div class="seperator"></div>
<div>
<a name="expandingModels"></a>
<h2>Expanding models</h2>
<p>
Sequelize allows you to pass custom class and instance methods. Just do the following:
<pre><%- koala.render(".js", partial("expand.ejs")) %></pre>
</p>
</div>
<div class="seperator"></div>
<div>
<a name="chainQueries"></a>
<h2>Chain queries</h2>
<p>
Because you will want to save several items at once and just go on after all of them are saved, Sequelize provides a handy helper for that:
<pre><%- koala.render(".js", partial("chainQueries1.ejs")) %></pre>
And a real example:
<pre><%- koala.render(".js", partial("chainQueries2.ejs")) %></pre>
You can also pass params to the method... and of course you can also call other methods, which trigger a callback:
<pre><%- koala.render(".js", partial("chainQueries3.ejs")) %></pre>
</p>
</div>
<div class="seperator"></div>
<div>
<a name="associations"></a>
<h2>Associations</h2>
<p>
With Sequelize you can also specify associations between multiple classes. Doing so will help you to easily
access and set those associated objects. The library therefore provides for each defined class the method
the following methods:
<pre><%- koala.render(".js", partial("associations1.ejs")) %></pre>
Because Sequelize is doing a lot of magic, you have to call Sequelize#sync after setting the associations!
Doing so will allow you the following:
<pre><%- koala.render(".js", partial("associations2.ejs")) %></pre>
To remove created associations you can just call the set method without a specific id:
<pre><%- koala.render(".js", partial("associations3.ejs")) %></pre>
You can also do it vice versa:
<pre><%- koala.render(".js", partial("associations4.ejs")) %></pre>
For hasOne its basically the same:
<pre><%- koala.render(".js", partial("associations5.ejs")) %></pre>
In order to specify many-to-many associations you can use the following syntax:
<pre><%- koala.render(".js", partial("associations6.ejs")) %></pre>
This will create a table, named according to the specified association names (= MembersProjects),
which just stores the id of a project and a member. Don't forget to call the sync method of the sequelize
instance.<br><br>
Since v0.4.1 all association tables and association keys are named as the passed association names:
<pre><%- koala.render(".js", partial("associations7.ejs")) %></pre>
Sequelize v0.4.2 introduced a new possibility to load the associated objects from the database.
The relevant method is called <i>fetchAssociations</i> and returns a hash:
<pre><%- koala.render(".js", partial("associations8.ejs")) %></pre>
</p>
</div>
<div class="seperator"></div>
<div>
<a name="finding"></a>
<h2>Finding some objects</h2>
<p>
OK... you can define classes and associations. You can save them. You would probably like to get them from
the database again :-) Easy:
<pre><%- koala.render(".js", partial("finding.ejs")) %></pre>
Since v0.4.3 there is an option for find methods, which forces the load of associated data. The resulting objects
will store the data in the <i>fetchedAssociations</i> attribute:
<pre><%- koala.render(".js", partial("finding2.ejs")) %></pre>
</p>
</div>
<div class="seperator"></div>
<div>
<a name="projects"></a>
<h2>Projects using Sequelize</h2>
<p>
You are using Sequelize? Let me know and get listed here! Just send me a private message on github :-)
</p>
</div>