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

Commit c2ffae57 by Mick Hansen

[ci-skip] docs on docs on docs

1 parent 9c89747c
## Installation
You have two options to install Sequelize:
1. Install it via NPM:
```bash
# Use npm on the commandline:
$ npm install sequelize
// Then require the installed library in your application code:
var Sequelize = require("sequelize")
```
2. Download the code from the git repository and require it's entry file index.js:
```bash
# Checkout the current code from the repository using the commandline
$ cd path/to/lib
$ git clone git://github.com/sequelize/sequelize.git
// Then require the installed library in your application code:
var Sequelize = require(__dirname + "/lib/sequelize/index")
```
This will make the class `Sequelize` available.
\ No newline at end of file
## Utils / Lodash.js
Sequelize comes with some handy utils including references to `lodash` as well as some individual helpers. You can access them via `Sequelize.Utils`.
You can access all the methods of lodash like this:
```js
Sequelize.Utils._.each(/* ... */)
Sequelize.Utils._.map(/* ... */)
Sequelize.Utils._...
```
Check out the [Lodash][0] page for further information.
[0]: http://lodash.com/
## Compatibility
Sequelize is compatible to the following versions of Node.JS:
* 0.8.x
* 0.10.x
## Asynchronicity
Since`v1.3.0`there are multiple ways of adding listeners to asynchronous requests. First of all, each time you call a finder method or save an object, sequelize triggers asynchronous logic. To react to the success or the failure (or both) of the request, you can do the following:
// the old, pre-v1.3.0 way
Model.findAll().on('success', function(models) { /* foo */ })
Model.findAll().on('failure', function(err) { /* bar */ })
 
// the new, >=v1.3.0 way
// each one is valid
Model.findAll().on('success', function(models) { /* foo */ })
Model.findAll().success(function(models) { /* foo */ })
Model.findAll().ok(function(models) { /* foo */ })
 
// Model.findAll().on('failure', function(err) { /* bar */ }) ==> invalid since v1.5.0
Model.findAll().on('error', function(err) { /* bar */ }) // ==> new since v1.5.0
Model.findAll().error(function(err) { /* bar */ })
Model.findAll().failure(function(err) { /* bar */ })
Model.findAll().fail(function(err) { /* bar */ })
 
Model.findAll().complete(function(err, result) { /* bar */ })
Model.findAll().done(function(err, result) { /* bar */ })
 
// As of 1.7.0 we support Promises/A
var self = User
 
user.find(1).then(function(user1) {
return user1.increment(['aNumber'], 2)
}).then(function(user2) {
return user.find(1)
}).then(function(user3) {
console.log(user3.aNumber) // 2
}, function(err) {
// err...
})
 
 
// For functions with multiple success values (e.g. findOrCreate) there is also spread
 
user.findOrCreate(...).spread(function(user, wasCreated) {
// all arguments are passed
})
user.findOrCreate(...).then(function(user) {
// only the first argument is passed
})
**Please notice:**Since v1.5.0 the 'error' event is used to notify about errors. If such events aren't caught however, Node.JS will throw an error. So you would probably like to catch them :D
### Additional links
If you want to keep track about latest development of sequelize or to just discuss things with other sequelize users you might want to take a look at the following resources:
* [Twitter: @sdepold][0]
* [Twitter: @sequelizejs][1]
* [ADN: @sdepold][2]
* [IRC: sequelizejs@freenode.net][3]
* [XING][4]
* [Facebook][5]
## Companies & Projects
Here is a list of companies and projects that are using Sequelize in real world applications:
### [Shutterstock][6]
Shutterstock Images LLC is a leading global provider of high-quality stock footage, stock photography, vectors and illustrations to creative industry professionals around the world. Shutterstock works closely with its growing contributor community of artists, photographers, videographers and illustrators to curate a global marketplace for royalty-free, top-quality imagery. Shutterstock adds tens of thousands of rights-cleared images and footage clips each week, with more than 18 million files currently available.
### [Clevertech][7]
Clevertech builds web and mobile applications for startups using Lean and Agile methodologies. Clevertech relies on Sequelize for its applications, its speed, versatility and data flexibility. Clevertech contributes back to open source development and its expert developers support the continuing effort to make sequelize the best ORM for Node projects.
### [Metamarkets][8]
Metamarkets enables buyers and sellers of digital media to visualize insights and make decisions with real-time pricing, performance, and audience data.
### [filsh][10]
filsh allows you to download online videos from various video
portals like YouTube, Vimeo and Dailymotion in a format you like.
No software required.
### [Using sequelize?][11]
If you want to get listed here, just drop me a line or send me a pull-request on Github!
[0]: http://twitter.com/sdepold
[1]: http://twitter.com/sequelizejs
[2]: https://alpha.app.net/sdepold
[3]: irc://irc.freenode.net/sequelizejs
[4]: https://www.xing.com/net/priec1b5cx/sequelize
[5]: https://www.facebook.com/sequelize
[6]: http://www.shutterstock.com
[7]: http://www.clevertech.biz
[8]: http://metamarkets.com/
[9]: https://innofluence.com
[10]: http://filsh.net
[11]: https://github.com/sequelize/sequelize/tree/gh-pages
\ No newline at end of file
## Transactions
```js
sequelize.transaction(function(t) {
// we just opened a new connection to the database which is transaction exclusive.
// also we send some first transaction queries to the database.
// do some async stuff ...
Sequelize supports two ways of using transactions, one will automatically commit or rollback the transaction based on a promise chain and the other leaves it up to the user.
The key difference is that the managed transaction uses a callback that expects a promise to be returned to it while the unmanaged transaction returns a promise.
// if everything is ok ... commit the transaction
t.commit().success(function() {})
### Auto commit/rollback
```js
return sequelize.transaction(t) {
return User.create({
firstName: 'Abraham',
lastName: 'Lincoln'
}, {transaction: t}).then(function (user) {
return user.setShooter({
firstName: 'John',
lastName: 'Boothe'
}, {transction: t});
}).then(function () {
// Transaction has been committed
}).catch(function (err) {
// Transaction has been rolled back
// err is whatever rejected the promise chain returned to the transaction callback
});
});
```
// if something failed ... rollback the transaction
t.rollback().success(function() {})
### Handled by user
```js
return sequelize.transaction().function (t) {
return User.create({
firstName: 'Homer',
lastName: 'Simpson'
}, {transaction: t}).then(function (user) {
return user.addSibling({
firstName: 'Lisa',
lastName: 'Simpson'
}, {transction: t});
}).then(function () {
t.commit();
}).catch(function (err) {
t.rollback();
});
});
```
// the commit / rollback will emit events which can be observed via:
t.done(function() {
/* we will be here once the transaction
has been committed / reverted */
})
})
### Using transactions with other sequelize methods
sequelize.transaction(function(t) {
User.create({ username: 'foo' }, { transaction: t }).success(function() {
// this user will only be available inside the session
User.all({ transaction: t }) // will return the user
User.all() // will not return the user
})
})
```
\ No newline at end of file
The `transaction` option goes with most other options, which are usually the first argument of a method.
For methods that take values, like `.create`, `.update()`, `.updateAttributes()` and more `transaction` should be passed to the option in the second argument.
If unsure, refer to the api documentation for the method you are using to be sure of the signature.
\ No newline at end of file
The Sequelize library provides easy access to MySQL, MariaDB, SQLite or PostgreSQL databases 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.
## Easy installation
## Installation
```bash
$ npm install sequelize
$ npm install mysql
$ npm install mysql|pg|sqlite
```
## Example usage
......
......@@ -10,7 +10,6 @@ extra_javascript:
pages:
- ['index.md', 'Home']
- ['docs/installation.md', 'Docs', 'Installation']
- ['docs/usage.md', 'Docs', 'Usage']
- ['docs/models.md', 'Docs', 'Models']
- ['docs/instances.md', 'Docs', 'Instances']
......@@ -18,7 +17,6 @@ pages:
- ['docs/hooks.md', 'Docs', 'Hooks']
- ['docs/transactions.md', 'Docs', 'Transactions']
- ['docs/migrations.md', 'Docs', 'Migrations']
- ['docs/misc.md', 'Docs', 'Misc']
- ['api/sequelize.md', 'API', 'Sequelize']
- ['api/model.md', 'API', 'Model']
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!