不要怂,就是干,撸起袖子干!

Commit 48bd2610 by Sascha Depold

moved current website into doc_old and created a new one under doc

1 parent e4f870d1
Showing with 950 additions and 311 deletions
/**
* Module dependencies.
*/
require.paths.unshift(__dirname + "/lib/node")
var express = require('express'),
connect = require('connect'),
fs = require('fs'),
http = require('http'),
koala = require('koala'),
sys = require('sys'),
ChangelogImporter = require(__dirname + "/lib/sequelizejs/ChangelogImporter"),
ExampleImporter = require(__dirname + "/lib/sequelizejs/ExampleImporter"),
changelogImporter = new ChangelogImporter("github.com", "/sdepold/sequelize/raw/master/changelog.md", false),
exampleImporter = new ExampleImporter(false)
var app = module.exports = express.createServer(
connect.logger()
)
var express = require('express')
var app = module.exports = express.createServer()
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views')
app.use(connect.bodyDecoder())
app.use(connect.methodOverride())
app.use(connect.compiler({ src: __dirname + '/public', enable: ['less'] }))
app.set('view engine', 'ejs')
app.helpers(require("express-view-helpers"))
app.helpers({
koala: require("koala").render
})
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(require('connect').compiler({ src: __dirname + '/public', enable: ['less'] }))
app.use(app.router)
app.use(connect.staticProvider(__dirname + '/public'))
app.use(express.static(__dirname + '/public'))
})
app.configure('development', function(){
app.use(connect.errorHandler({ dumpExceptions: true, showStack: true }))
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
})
app.configure('production', function(){
app.use(connect.errorHandler())
app.use(express.errorHandler())
})
// Routes
app.get('/', function(req, res){
res.render('index.ejs', {
locals: { koala: koala }
})
})
app.get("/changelog", function(req, res) {
res.render('changelog.ejs')
})
app.get("/examples/:example?", function(req, res) {
var examples = fs.readdirSync(__dirname + "/views/examples"),
example = req.params.example
if (typeof example != "undefined") {
console.log(example)
if(examples.indexOf(example + ".ejs") > -1)
res.render("examples/" + example + ".ejs", {
locals: { examples: examples }
})
else
res.redirect("/examples")
} else {
res.render("examples/" + examples[0], {
locals: { examples: examples }
})
}
})
app.get("/background", function(req, res) {
fs.readdir(__dirname + "/public/images/", function(err, files) {
require("fs").readdir(__dirname + "/public/images/", function(err, files) {
if(err) sys.log(err)
else {
if(files[0] == ".DS_Store") files.shift()
......@@ -79,19 +36,26 @@ app.get("/background", function(req, res) {
})
})
// Only listen on $ node app.js
if (!module.parent) {
app.listen(4000)
sys.log("Server is now listening on port 4000")
var runImporters = function() {
try {
changelogImporter.run()
exampleImporter.run()
} catch(e) {
console.log(e)
}
app.get('/', function(req, res){
var navigation = {
"installation": 'Installation',
"basic-mapping": 'Basic Mapping',
"sync-with-db": 'Synchronize with database',
"instances": "Creating and working with instances",
"expanding-models": "Expanding models",
"chain-queries": "Chain queries",
"associations": "Associations",
"find-objects": "Finding objects",
"projects": "Sequelize-based projects"
}
setInterval(runImporters, 1000*60*60)
runImporters()
res.render('index', {
navigation: navigation,
active: req.param('active') || 'installation'
})
})
if (!module.parent) {
app.listen(3000)
console.log("Express server listening on port %d", app.address().port)
}
\ No newline at end of file
{
"name": "sequelizejs.com",
"description": "The official sequelize webite.",
"version": "0.4.3",
"homepage": "http://sequelizejs.com",
"repository": {
"type": "git",
"url": "git://github.com/sdepold/sequelize.git"
},
"author": "Sascha Depold <sascha@depold.com> (http://depold.com)",
"directories": {
"lib": "lib"
},
"engines": {
"node": "*"
},
"dependencies": {
"koala": ">=0.1.2",
"markdown": ">=0.2.1",
"ejs": ">=0.3.1",
"less": ">=1.0.41",
"express-view-helpers": ">=0.0.2"
}
}
\ No newline at end of file
No preview for this file type
......@@ -3,6 +3,7 @@ body {
font-size: 14px;
font-family: "Helvetica Neue", Verdana;
line-height: 1.5;
background: url(/background) repeat top left fixed;
}
h2 {
margin: 0px;
......
......@@ -3,6 +3,7 @@ body {
font-size: 14px;
font-family: "Helvetica Neue", Verdana;
line-height: 1.5;
background: url(/background) repeat top left fixed;
}
h2 {
......
<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>
\ No newline at end of file
<%- partial('index/intro') %>
<% for(var key in navigation) { %>
<div class="seperator"></div>
<div>
<a name="<%= key %>"></a>
<h2><%= navigation[key] %></h2>
<p><%- partial('index/'+key) %></p>
</div>
<% } %>
\ No newline at end of file
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(".js", partial("code/associations/associations-1.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(".js", partial("code/associations/associations-2.ejs")) %></pre>
To remove created associations you can just call the set method without a specific id:
<pre><%- koala(".js", partial("code/associations/associations-3.ejs")) %></pre>
You can also do it vice versa:
<pre><%- koala(".js", partial("code/associations/associations-4.ejs")) %></pre>
For hasOne its basically the same:
<pre><%- koala(".js", partial("code/associations/associations-5.ejs")) %></pre>
In order to specify many-to-many associations you can use the following syntax:
<pre><%- koala(".js", partial("code/associations/associations-6.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(".js", partial("code/associations/associations-7.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(".js", partial("code/associations/associations-8.ejs")) %></pre>
\ No newline at end of file
To get the ball rollin' you first have to create an instance of Sequelize. Use it the following way:
<pre><%- koala(".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(".js", partial("code/basic-mapping/advanced-instantiation.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(".js", partial("code/basic-mapping/basic-mapping-1.ejs")) %></pre>
Sequelize currently supports the following datatypes:
<pre><%= partial("code/basic-mapping/basic-mapping-2.ejs") %></pre>
You can also store your model definitions in a single file using the import method:
<pre><%- koala(".js", partial("code/basic-mapping/basic-mapping-3.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>.
\ No newline at end of file
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(".js", partial("code/chain-queries/chain-queries-1.ejs")) %></pre>
And a real example:
<pre><%- koala(".js", partial("code/chain-queries/chain-queries-2.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(".js", partial("code/chain-queries/chain-queries-3.ejs")) %></pre>
No preview for this file type
Sequelize allows you to pass custom class and instance methods. Just do the following:
<pre><%- koala(".js", partial("code/expanding-models.ejs")) %></pre>
\ No newline at end of file
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(".js", partial("code/find-objects/finding-1.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(".js", partial("code/find-objects/finding-2.ejs")) %></pre>
\ No newline at end of file
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("code/installation.ejs") %></pre>
This will make the class Sequelize available.
\ No newline at end of file
In order to create instances of defined classes just do it as follows:
<pre><%- koala(".js", partial("code/instances/models-1.ejs")) %></pre>
To save it in the database use the save method and pass a callback to it, if needed:
<pre><%- koala(".js", partial("code/instances/models-2.ejs")) %></pre>
Now lets change some values and save changes to the database... There are two ways to do that:
<pre><%- koala(".js", partial("code/instances/models-3.ejs")) %></pre>
<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>
\ No newline at end of file
You are using Sequelize? Let me know and get listed here! Just send me a private message on GitHub :-)
\ No newline at end of file
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(".js", partial("code/sync-with-db/sync-1.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(".js", partial("code/sync-with-db/sync-2.ejs")) %></pre>
......@@ -4,34 +4,7 @@
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Sequelize &raquo; A MySQL Object-Relational-Mapper for NodeJS</title>
<link rel="stylesheet" href="/stylesheets/style.css">
<style type="text/css" media="screen">
body { background: url(/background) repeat top left fixed; }
</style>
<script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js"></script>
<!-- <script type="text/javascript" charset="utf-8" src="/javascripts/prototype.js"></script> -->
<script type="text/javascript" charset="utf-8">
var highlightNavigation = function(e) {
$$("#sidebar a").each(function(anchor) { anchor.removeClassName("active") })
$(e).addClassName("active")
}
var buildNavigation = function(additionalBefore, additionalAfter, options) {
options = options || {}
options.seperator = options.seperator || "<br />"
var navigationContent = "<h2>Navigation</h2><br />",
headline = null
navigationContent += additionalBefore.join(options.seperator)
$$("#content div a").each(function(e) {
if(headline = e.up().down("h2"))
navigationContent += "<a href='#" + e.name + "' onclick='highlightNavigation(this)'>" + headline.innerHTML + "</a>"
})
navigationContent += additionalAfter.join(options.seperator)
$("sidebar").update(navigationContent)
}
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9039631-4']);
......@@ -43,9 +16,21 @@
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript" charset="utf-8">
document.observe("dom:loaded", function() {
setTimeout(function() {
$$("[name='<%= active %>']").first().scrollTo()
}, 50)
})
</script>
</head>
<body>
<div id="sidebar" class="bordered"></div>
<div id="sidebar" class="bordered">
<h2>Navigation</h2><br/>
<% for(var key in navigation) { %>
<%- linkTo(navigation[key], "?active=" + key, {class: (active==key) ? "active" : ""}) %>
<% } %>
</div>
<div id="wrapper">
<div id="navigation" class="bordered">
<table width="100%">
......
/**
* Module dependencies.
*/
require.paths.unshift(__dirname + "/lib/node")
var express = require('express')
, connect = require('connect')
, fs = require('fs')
, http = require('http')
, koala = require('koala')
, sys = require('sys')
, ChangelogImporter = require(__dirname + "/lib/sequelizejs/ChangelogImporter")
, ExampleImporter = require(__dirname + "/lib/sequelizejs/ExampleImporter")
, changelogImporter = new ChangelogImporter("github.com", "/sdepold/sequelize/raw/master/changelog.md", false)
, exampleImporter = new ExampleImporter(false)
var app = module.exports = express.createServer(
connect.logger()
)
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views')
app.use(connect.bodyDecoder())
app.use(connect.methodOverride())
app.use(connect.compiler({ src: __dirname + '/public', enable: ['less'] }))
app.use(app.router)
app.use(connect.staticProvider(__dirname + '/public'))
})
app.configure('development', function(){
app.use(connect.errorHandler({ dumpExceptions: true, showStack: true }))
})
app.configure('production', function(){
app.use(connect.errorHandler())
})
// Routes
app.get('/', function(req, res){
res.render('index.ejs', {
locals: { koala: koala }
})
})
app.get("/changelog", function(req, res) {
res.render('changelog.ejs')
})
app.get("/examples/:example?", function(req, res) {
var examples = fs.readdirSync(__dirname + "/views/examples"),
example = req.params.example
if (typeof example != "undefined") {
console.log(example)
if(examples.indexOf(example + ".ejs") > -1)
res.render("examples/" + example + ".ejs", {
locals: { examples: examples }
})
else
res.redirect("/examples")
} else {
res.render("examples/" + examples[0], {
locals: { examples: examples }
})
}
})
app.get("/background", function(req, res) {
fs.readdir(__dirname + "/public/images/", function(err, files) {
if(err) sys.log(err)
else {
if(files[0] == ".DS_Store") files.shift()
var i = Math.round(Math.random() * (files.length - 1))
res.sendfile(__dirname + "/public/images/" + files[i])
}
})
})
// Only listen on $ node app.js
if (!module.parent) {
app.listen(4000)
sys.log("Server is now listening on port 4000")
var runImporters = function() {
try {
changelogImporter.run()
exampleImporter.run()
} catch(e) {
console.log(e)
}
}
setInterval(runImporters, 1000*60*60)
runImporters()
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ module.exports = function(host, filename, forceFetch) {
}
module.exports.prototype = {
markdown: require(__dirname + '/../node/markdown'),
markdown: require('markdown'),
changelogFile: __dirname + "/../../views/partials/changelog.ejs",
fs: require("fs"),
......@@ -13,7 +13,6 @@ module.exports.prototype = {
var self = this
, https = require("https")
, changelog = []
https.get({ host: this.host, path: this.filename }, function(res) {
res.setEncoding('utf8')
......
{
"name": "sequelizejs.com",
"description": "The official sequelize webite.",
"version": "0.4.3",
"homepage": "http://sequelizejs.com",
"repository": {
"type": "git",
"url": "git://github.com/sdepold/sequelize.git"
},
"author": "Sascha Depold <sascha@depold.com> (http://depold.com)",
"directories": {
"lib": "lib"
},
"engines": {
"node": "*"
},
"dependencies": {
"koala": ">=0.1.2",
"markdown": ">=0.2.1"
}
}
\ No newline at end of file
body {
padding: 50px;
font-size: 14px;
font-family: "Helvetica Neue", Verdana;
line-height: 1.5;
}
h2 {
margin: 0px;
}
.active {
text-decoration: underline !important;
}
.bordered {
-moz-border-radius: 15px;
border-radius: 15px;
padding: 20px;
border: 1px solid #888;
color: white;
background-color: rgba(0, 0, 0, 0.7);
box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
-o-box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
-webkit-box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
-moz-box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
}
#wrapper {
text-align: center;
padding: 0 200px;
}
#navigation, #content, #footer {
width: 800px;
text-align: justify;
margin: 0px auto;
}
#footer {
margin-top: 15px;
color: white;
text-align: center;
}
#footer a {
color: white;
}
#navigation {
margin-bottom: 15px;
padding: 10px 20px;
}
#navigation a {
color: white;
display: block;
text-decoration: none;
font-size: 16px;
font-weight: bold;
}
#navigation a:hover {
text-decoration: underline;
}
#sidebar {
float: left;
position: fixed;
width: 140px;
}
#sidebar a {
color: white;
padding: 5px 0px;
display: block;
text-decoration: none;
}
#sidebar a:hover {
text-decoration: underline;
}
pre, .pre_like {
border: 1px solid #888;
-moz-border-radius: 5px;
border-radius: 5px;
padding: 5px;
background-color: rgba(43, 32, 28, 0.7);
}
.comment {
color: grey;
}
.string {
color: #04d80d;
}
.regexp {
color: #2fe420;
}
.number {
color: #44aa43;
}
.class, .this {
color: #8ac9f2;
}
.inst.variable, .class.variable, .global.variable {
color: #2f5fe0;
}
.keyword {
font-weight: bold;
color: #43a8ed;
}
.seperator {
border-top: 1px solid white;
height: 1px;
overflow: hidden;
margin-bottom: 10px;
}
#forkBadge {
position: fixed;
top: 0px;
right: 0px;
}
#forkBadge img {
border: none;
}
#changelog p {
margin: 0px;
}
#changelog h3 {
margin: 5px 0px;
}
#changelog .pre_like {
margin: 10px 0px;
}
#changelog ul {
margin: 5px 0px;
padding-left: 30px;
padding-bottom: 0px;
}
body {
padding: 50px;
font-size: 14px;
font-family: "Helvetica Neue", Verdana;
line-height: 1.5;
}
h2 {
margin: 0px
}
.active {
text-decoration: underline !important;
}
.bordered {
-moz-border-radius: 15px;
border-radius: 15px;
padding: 20px;
border: 1px solid #888;
color: white;
background-color: rgba(0, 0, 0, 0.7);
box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
-o-box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
-webkit-box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
-moz-box-shadow: 0 2px 16px #000, 0 0 1px #000, 0 0 1px #000;
}
#wrapper{ text-align:center; padding:0 200px; }
#navigation, #content, #footer {
width: 800px;
text-align: justify;
margin:0px auto;
}
#footer {
margin-top: 15px;
color: white;
text-align: center;
a { color: white; }
}
#navigation {
margin-bottom: 15px;
padding: 10px 20px;
a {
color: white;
display: block;
text-decoration: none;
font-size: 16px;
font-weight: bold;
&:hover {
text-decoration: underline;
}
}
}
#sidebar {
float: left;
position: fixed;
width: 140px;
a {
color: white;
padding: 5px 0px;
display: block;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
}
pre, .pre_like {
border: 1px solid #888;
-moz-border-radius: 5px;
border-radius: 5px;
padding: 5px;
background-color: rgba(43, 32, 28, 0.7);
}
.comment { color: grey }
.string { color: #04D80D }
.regexp { color: #2FE420 }
.number { color: #44AA43 }
.class, .this { color: #8AC9F2 }
.inst.variable, .class.variable, .global.variable { color: #2F5FE0 }
.keyword { font-weight: bold; color: #43A8ED }
.seperator {
border-top: 1px solid white;
height: 1px;
overflow: hidden;
margin-bottom: 10px
}
#forkBadge {
position: fixed;
top: 0px;
right: 0px;
img {
border: none
}
}
#changelog {
p { margin: 0px }
h3 { margin: 5px 0px }
.pre_like { margin: 10px 0px }
ul {
margin: 5px 0px;
padding-left: 30px;
padding-bottom: 0px;
}
}
\ No newline at end of file
<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>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Sequelize &raquo; A MySQL Object-Relational-Mapper for NodeJS</title>
<link rel="stylesheet" href="/stylesheets/style.css">
<style type="text/css" media="screen">
body { background: url(/background) repeat top left fixed; }
</style>
<script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js"></script>
<!-- <script type="text/javascript" charset="utf-8" src="/javascripts/prototype.js"></script> -->
<script type="text/javascript" charset="utf-8">
var highlightNavigation = function(e) {
$$("#sidebar a").each(function(anchor) { anchor.removeClassName("active") })
$(e).addClassName("active")
}
var buildNavigation = function(additionalBefore, additionalAfter, options) {
options = options || {}
options.seperator = options.seperator || "<br />"
var navigationContent = "<h2>Navigation</h2><br />",
headline = null
navigationContent += additionalBefore.join(options.seperator)
$$("#content div a").each(function(e) {
if(headline = e.up().down("h2"))
navigationContent += "<a href='#" + e.name + "' onclick='highlightNavigation(this)'>" + headline.innerHTML + "</a>"
})
navigationContent += additionalAfter.join(options.seperator)
$("sidebar").update(navigationContent)
}
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9039631-4']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div id="sidebar" class="bordered"></div>
<div id="wrapper">
<div id="navigation" class="bordered">
<table width="100%">
<tr>
<td align="center" width="33%"><a href="/">Documentation</a></td>
<td align="center" width="33%"><a href="/examples">Examples</a></td>
<td align="center" width="33%"><a href="/changelog">Changelog</a></td>
</tr>
</table>
</div>
<div id="content" class="bordered"><%- body %></div>
<div id="footer" class="bordered">
&copy; 2010 Layout | Code by <a href="http://depold.com" target="_blank">Sascha Depold</a>;
Website based on <a href="http://nodejs.org" target="_blank" rel="nofollow">NodeJS</a> and
<a href="http://expressjs.com" target="_blank" rel="nofollow">ExpressJS</a>.
</div>
</div>
<a href="http://github.com/sdepold/sequelize" id="forkBadge">
<img src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" id="ribbon" alt="Fork me on GitHub">
</a>
</body>
</html>
\ No newline at end of file
var sequelize = new Sequelize('database', 'username', 'password', {
host: "my.server.tld",
port: 12345
})
// No need for password?
var sequelize = new Sequelize('database', 'username'[, null])
// Want no logging? Use that:
var sequelize = new Sequelize('database', 'username', 'password', {
disableLogging: true
})
// Since v0.4.3 you can disable the pluralization of table names:
var sequelize = new Sequelize('database', 'username', 'password', {
disableTableNameModification: true
})
sequelize.define('person', { /* ... */})
// will result in a table called >person< if that option is specified
// will result in a table called >people< if that option is not specified
\ No newline at end of file
// many to many association:
Class.hasMany('association name', AssociationClass, 'back association name')
// example
Project.hasMany('tasks', Task, 'projects')
// many to many association on the same class
Class.hasMany('association name')
// example
Person.hasMany('friends')
// one-to-one association
Class.hasOneAndBelongsTo('association name', AssociationClass)
// example
Person.hasOneAndBelongsTo('house', House, 'owner')
// one-to-many association
Class.hasManyAndBelongsTo('association name', AssociationClass, 'back association name')
// example
Person.hasManyAndBelongsTo('cars', Car, 'owner')
// one way associations
Class.hasMany('association name', AssociationClass)
Class.hasOne('association name', AssociationClass)
\ No newline at end of file
Project.hasManyAndBelongsTo('tasks', Task, 'project')
var project = new Project...
var task1 = new Task...
var task2 = new Task
// save them... and then:
project.setTasks([task1, task2], function(associatedTasks) {
// the associatedTasks are the very same as task1 and task2
})
// ok now they are save... how do I get them later on?
project.getTasks(function(associatedTasks) {
// bam
})
\ No newline at end of file
// remove the association with task1
project.setTasks([task2], function(associatedTasks) {
// you will get task2 only
})
// remove 'em all
projects.setTasks([], function(associatedTasks) {
// you will get an empty array
})
// project is associated with task1 and task2
task2.setProject(null, function(associatedProject) {
// will return no associations
})
\ No newline at end of file
Task.hasOne('author', Author)
Task.setAuthor(anAuthor)
\ No newline at end of file
Project.hasMany('members', Member, 'projects')
\ No newline at end of file
Person.hasOne('father', Person) // will create a foreign key 'fatherId'
Person.hasOne('mother', Person) // will create a foreign key 'motherId'
Person.hasMany('friends') // will create a table 'FriendsPeople' with friendId and personId
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!