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

Commit 15977057 by Sascha Depold

Merge pull request #574 from Runnable/master

Provide a link to a live sandbox environment where sequelizse examples are available
2 parents b14ecc52 397e4d8f
...@@ -30,7 +30,7 @@ The Sequelize library provides easy access to MySQL, SQLite or PostgreSQL databa ...@@ -30,7 +30,7 @@ The Sequelize library provides easy access to MySQL, SQLite or PostgreSQL databa
- Associations - Associations
- Importing definitions from single files - Importing definitions from single files
## Documentation, Examples and Updates ## ## Documentation and Updates ##
You can find the documentation and announcements of updates on the [project's website](http://www.sequelizejs.com). You can find the documentation and announcements of updates on the [project's website](http://www.sequelizejs.com).
If you want to know about latest development and releases, follow me on [Twitter](http://twitter.com/sdepold). If you want to know about latest development and releases, follow me on [Twitter](http://twitter.com/sdepold).
...@@ -42,6 +42,11 @@ Also make sure to take a look at the examples in the repository. The website wil ...@@ -42,6 +42,11 @@ Also make sure to take a look at the examples in the repository. The website wil
- [Google Groups](https://groups.google.com/forum/#!forum/sequelize) - [Google Groups](https://groups.google.com/forum/#!forum/sequelize)
- [XING](https://www.xing.com/net/priec1b5cx/sequelize) (pretty much inactive, but you might want to name it on your profile) - [XING](https://www.xing.com/net/priec1b5cx/sequelize) (pretty much inactive, but you might want to name it on your profile)
## Running Examples
Instructions for running samples are located in the [example directory](https://github.com/sequelize/sequelize/tree/master/examples). Try these samples in a live sandbox environment:
<a href="https://runnable.com/sequelize" target="_blank"><img src="https://runnable.com/external/styles/assets/runnablebtn.png"></a>
## Roadmap ## Roadmap
A very basic roadmap. Chances aren't too bad, that not mentioned things are implemented as well. Don't panic :) A very basic roadmap. Chances aren't too bad, that not mentioned things are implemented as well. Don't panic :)
......
...@@ -5,11 +5,11 @@ ...@@ -5,11 +5,11 @@
First of all, Person is getting associated via many-to-many with other Person objects (e.g. Person.hasMany('brothers')). First of all, Person is getting associated via many-to-many with other Person objects (e.g. Person.hasMany('brothers')).
Afterwards a Person becomes associated with a 'father' and a mother using a one-to-one association created by hasOneAndBelongsTo. Afterwards a Person becomes associated with a 'father' and a mother using a one-to-one association created by hasOneAndBelongsTo.
The last association has the type many-to-one and is defined by the function hasManyAndBelongsTo. The last association has the type many-to-one and is defined by the function hasManyAndBelongsTo.
The rest of the example is about setting and getting the associated data. The rest of the example is about setting and getting the associated data.
*/ */
var Sequelize = require(__dirname + "/../../index") var Sequelize = require(__dirname + "/../../index")
, config = require(__dirname + "/../../test/config") , config = require(__dirname + "/../../spec/config/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false}) , sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
, Person = sequelize.define('Person', { name: Sequelize.STRING }) , Person = sequelize.define('Person', { name: Sequelize.STRING })
, Pet = sequelize.define('Pet', { name: Sequelize.STRING }) , Pet = sequelize.define('Pet', { name: Sequelize.STRING })
...@@ -36,13 +36,13 @@ sequelize.sync({force:true}).on('success', function() { ...@@ -36,13 +36,13 @@ sequelize.sync({force:true}).on('success', function() {
.add(brother.save()) .add(brother.save())
.add(sister.save()) .add(sister.save())
.add(pet.save()) .add(pet.save())
chainer.run().on('success', function() { chainer.run().on('success', function() {
person.setMother(mother).on('success', function() { person.getMother().on('success', function(mom) { person.setMother(mother).on('success', function() { person.getMother().on('success', function(mom) {
console.log('my mom: ', mom.name) console.log('my mom: ', mom.name)
})}) })})
person.setFather(father).on('success', function() { person.getFather().on('success', function(dad) { person.setFather(father).on('success', function() { person.getFather().on('success', function(dad) {
console.log('my dad: ', dad.name) console.log('my dad: ', dad.name)
})}) })})
person.setBrothers([brother]).on('success', function() { person.getBrothers().on('success', function(brothers) { person.setBrothers([brother]).on('success', function() { person.getBrothers().on('success', function(brothers) {
console.log("my brothers: " + brothers.map(function(b) { return b.name })) console.log("my brothers: " + brothers.map(function(b) { return b.name }))
......
var Sequelize = require(__dirname + "/../../index") var Sequelize = require(__dirname + "/../../index")
, config = require(__dirname + "/../../test/config") , config = require(__dirname + "/../../spec/config/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false}) , sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
var Person = sequelize.define('Person', { name: Sequelize.STRING }) var Person = sequelize.define('Person', { name: Sequelize.STRING })
...@@ -8,12 +8,12 @@ var Person = sequelize.define('Person', { name: Sequelize.STRING }) ...@@ -8,12 +8,12 @@ var Person = sequelize.define('Person', { name: Sequelize.STRING })
sequelize.sync({force: true}).on('success', function() { sequelize.sync({force: true}).on('success', function() {
var count = 10, var count = 10,
queries = [] queries = []
for(var i = 0; i < count; i++) for(var i = 0; i < count; i++)
chainer.add(Person.create({name: 'someone' + (i % 3)})) chainer.add(Person.create({name: 'someone' + (i % 3)}))
console.log("Begin to save " + count + " items!") console.log("Begin to save " + count + " items!")
chainer.run().on('success', function() { chainer.run().on('success', function() {
console.log("finished") console.log("finished")
Person.count().on('success', function(count) { Person.count().on('success', function(count) {
......
var Sequelize = require(__dirname + "/../../index") var Sequelize = require(__dirname + "/../../index")
, config = require(__dirname + "/../../test/config") , config = require(__dirname + "/../../spec/config/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false}) , sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
var Person = sequelize.define('Person', var Person = sequelize.define('Person',
{ name: Sequelize.STRING, { name: Sequelize.STRING,
age : Sequelize.INTEGER age : Sequelize.INTEGER
...@@ -12,12 +12,12 @@ var Person = sequelize.define('Person', ...@@ -12,12 +12,12 @@ var Person = sequelize.define('Person',
sequelize.sync({force: true}).on('success', function() { sequelize.sync({force: true}).on('success', function() {
var count = 10, var count = 10,
queries = [] queries = []
for(var i = 0; i < count; i++) for(var i = 0; i < count; i++)
chainer.add(Person.create({name: 'someone' + (i % 3), age : i+5})) chainer.add(Person.create({name: 'someone' + (i % 3), age : i+5}))
console.log("Begin to save " + count + " items!") console.log("Begin to save " + count + " items!")
chainer.run().on('success', function() { chainer.run().on('success', function() {
console.log("finished") console.log("finished")
Person.max('age').on('success', function(max) { Person.max('age').on('success', function(max) {
......
var Sequelize = require(__dirname + "/../../index") var Sequelize = require(__dirname + "/../../index")
, config = require("../../test/config") , config = require(__dirname + "/../../spec/config/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false, host: config.host}) , sequelize = new Sequelize(config.database, config.username, config.password, {logging: false, host: config.host})
, QueryChainer = Sequelize.Utils.QueryChainer , QueryChainer = Sequelize.Utils.QueryChainer
, sys = require("sys") , sys = require("sys")
...@@ -10,16 +10,16 @@ Person.sync({force: true}).on('success', function() { ...@@ -10,16 +10,16 @@ Person.sync({force: true}).on('success', function() {
var start = Date.now() var start = Date.now()
, count = 10000 , count = 10000
, done = 0 , done = 0
var createPerson = function() { var createPerson = function() {
Person.create({name: 'someone'}).on('success', function() { Person.create({name: 'someone'}).on('success', function() {
if(++done == count) { if(++done == count) {
var duration = (Date.now() - start) var duration = (Date.now() - start)
console.log("\nFinished creation of " + count + " people. Took: " + duration + "ms (avg: " + (duration/count) + "ms)") console.log("\nFinished creation of " + count + " people. Took: " + duration + "ms (avg: " + (duration/count) + "ms)")
start = Date.now() start = Date.now()
console.log("Will now read them from the database:") console.log("Will now read them from the database:")
Person.findAll().on('success', function(people) { Person.findAll().on('success', function(people) {
console.log("Reading " + people.length + " items took: " + (Date.now() - start) + "ms") console.log("Reading " + people.length + " items took: " + (Date.now() - start) + "ms")
}) })
...@@ -35,7 +35,7 @@ Person.sync({force: true}).on('success', function() { ...@@ -35,7 +35,7 @@ Person.sync({force: true}).on('success', function() {
for(var i = 0; i < count; i++) { for(var i = 0; i < count; i++) {
createPerson() createPerson()
} }
}).on('failure', function(err) { }).on('failure', function(err) {
console.log(err) console.log(err)
}) })
\ No newline at end of file
var fs = require("fs") /*
, Sequelize = require("sequelize") Title: Default values
, sequelize = new Sequelize('sequelize_test', 'root', null, {logging: false})
, Image = sequelize.define('Image', { data: Sequelize.TEXT })
Image.sync({force: true}).on('success', function() { This example demonstrates the use of default values for defined model fields. Instead of just specifying the datatype,
console.log("reading image") you have to pass a hash with a type and a default. You also might want to specify either an attribute can be null or not!
var image = fs.readFileSync(__dirname + '/source.png').toString("base64") */
console.log("done\n")
var Sequelize = require(__dirname + "/../../index")
console.log("creating database entry") , config = require(__dirname + "/../../spec/config/config")
Image.create({data: image}).on('success', function(img) { , sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
console.log("done\n")
var User = sequelize.define('User', {
console.log("writing file") name: { type: Sequelize.STRING, allowNull: false},
fs.writeFileSync(__dirname + '/target.png', img.data, "base64") isAdmin: { type: Sequelize.BOOLEAN, allowNull: false, defaultValue: false }
console.log("done\n") })
, user = User.build({ name: 'Someone' })
console.log("you might open the file ./target.png")
sequelize.sync({force: true}).on('success', function() {
user.save().on('success', function(user) {
console.log("user.isAdmin should be the default value (false): ", user.isAdmin)
user.updateAttributes({ isAdmin: true }).on('success', function(user) {
console.log("user.isAdmin was overwritten to true: " + user.isAdmin)
})
}) })
}).on('failure', function(err) {
console.log(err)
}) })
\ No newline at end of file
/* /*
Title: Defining class and instance methods Title: Defining class and instance methods
This example shows the usage of the classMethods and instanceMethods option for Models. This example shows the usage of the classMethods and instanceMethods option for Models.
*/ */
var Sequelize = require(__dirname + "/../../index") var Sequelize = require(__dirname + "/../../index")
, config = require(__dirname + "/../../test/config") , config = require(__dirname + "/../../spec/config/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false}) , sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
// model definition // model definition
var Task = sequelize.define("Task", { var Task = sequelize.define("Task", {
name: Sequelize.STRING, name: Sequelize.STRING,
deadline: Sequelize.DATE, deadline: Sequelize.DATE,
...@@ -51,7 +51,7 @@ Task.sync({force: true}).on('success', function() { ...@@ -51,7 +51,7 @@ Task.sync({force: true}).on('success', function() {
console.log("should be false: " + task1.passedDeadline()) console.log("should be false: " + task1.passedDeadline())
console.log("should be true: " + task2.passedDeadline()) console.log("should be true: " + task2.passedDeadline())
console.log("should be 10: " + task1.importance) console.log("should be 10: " + task1.importance)
Task.setImportance(30, function() { Task.setImportance(30, function() {
Task.findAll().on('success', function(tasks) { Task.findAll().on('success', function(tasks) {
tasks.forEach(function(task) { tasks.forEach(function(task) {
......
var Sequelize = require(__dirname + "/../../index") var Sequelize = require(__dirname + "/../../index")
, config = require(__dirname + "/../../test/config") , config = require(__dirname + "/../../spec/config/config")
, sequelize = new Sequelize(config.database, config.username, config.password, { , sequelize = new Sequelize(config.database, config.username, config.password, {
// use other database server or port // use other database server or port
host: 'my.srv.tld', host: 'my.srv.tld',
port: 12345, port: 12345,
// disable logging // disable logging
logging: false logging: false
}) })
......
var Sequelize = require(__dirname + "/../../index") var Sequelize = require(__dirname + "/../../index")
, config = require(__dirname + "/../../test/config") , config = require(__dirname + "/../../spec/config/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false}) , sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
, Project = sequelize.import(__dirname + "/Project") , Project = sequelize.import(__dirname + "/Project")
, Task = sequelize.import(__dirname + "/Task") , Task = sequelize.import(__dirname + "/Task")
Project.hasMany(Task) Project.hasMany(Task)
Task.belongsTo(Project) Task.belongsTo(Project)
sequelize.sync({force: true}).on('success', function() { sequelize.sync({force: true}).on('success', function() {
Project Project
.create({ name: 'Sequelize', description: 'A nice MySQL ORM for NodeJS' }) .create({ name: 'Sequelize', description: 'A nice MySQL ORM for NodeJS' })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!