Last active
January 13, 2016 11:43
-
-
Save yukidarake/cdb59532b12062e62b7a to your computer and use it in GitHub Desktop.
coの使い方
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const async = require('async'); | |
const co = require('co'); | |
const MongoClient = require('mongodb').MongoClient; | |
const MONGO_URL = 'mongodb://localhost:27017/test'; | |
function* doAsync() { | |
return new Promise((resolve, reject) => { | |
let db; | |
async.series([ | |
// CONNECT | |
(next) => { | |
MongoClient.connect(MONGO_URL, (err, _db) => { | |
db = _db; | |
next(err); | |
}); | |
}, | |
// REMOVE | |
(next) => { | |
db.collection('test').removeMany({ _id: /^async793/ }, next); | |
}, | |
// UPDATE x 10 | |
(next) => { | |
async.timesSeries(10, (n, next) => { | |
const id = `async793-${n}`; | |
const update = { | |
$set: { | |
_id: id, | |
name: `なかむらasync${n}`, | |
}, | |
}; | |
const opts = { | |
upsert: true, | |
}; | |
db.collection('test').updateOne({ _id: id }, update, opts, next); | |
}, next); | |
}, | |
// FIND x 10 | |
(next) => { | |
async.timesSeries(10, (n, next) => { | |
const id = `async793-${n}`; | |
db.collection('test').findOne({ _id: id }, (err, result) => { | |
console.log(result); | |
next(err); | |
}); | |
}, next); | |
}, | |
], (err, result) => { | |
if (err) { | |
console.log(err); | |
} | |
db && db.close(); | |
resolve(); | |
}); | |
}); | |
} | |
function* doCo() { | |
return new Promise((resolve, reject) => { | |
co(function* () { | |
const filter = { _id: 'test-co' }; | |
// CONNECT | |
const db = yield MongoClient.connect(MONGO_URL); | |
// REMOVE | |
yield db.collection('test').removeMany({ _id: /^co793/ }); | |
// UPDATE | |
const opts = { | |
upsert: true, | |
}; | |
for (let i = 0; i < 10; i++) { | |
const id = `co793-${i}`; | |
const update = { | |
$set: { | |
_id: id, | |
name: `なかむらco${i}`, | |
}, | |
}; | |
yield db.collection('test').updateOne({ _id: id }, update, opts); | |
} | |
// FIND | |
for (let i = 0; i < 10; i++) { | |
const id = `co793-${i}`; | |
const result = yield db.collection('test').findOne({ _id: id }); | |
console.log(result); | |
} | |
db.close(); | |
}).catch((err) => { | |
console.error(err.stack); | |
}); | |
}); | |
} | |
co(function* () { | |
yield doAsync(); | |
yield doCo(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "cdb59532b12062e62b7a", | |
"version": "1.0.0", | |
"description": "", | |
"main": "main.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+ssh://[email protected]/cdb59532b12062e62b7a.git" | |
}, | |
"author": "", | |
"license": "MIT", | |
"bugs": { | |
"url": "https://gist.github.com/cdb59532b12062e62b7a" | |
}, | |
"homepage": "https://gist.github.com/cdb59532b12062e62b7a", | |
"dependencies": { | |
"async": "^1.5.2", | |
"co": "^4.6.0", | |
"mongodb": "^2.1.4" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const async = require('async'); | |
const co = require('co'); | |
const MongoClient = require('mongodb').MongoClient; | |
const MONGO_URL = 'mongodb://localhost:27017/test'; | |
function* doAsync() { | |
return new Promise((resolve, reject) => { | |
let db; | |
async.series([ | |
// CONNECT | |
(next) => { | |
MongoClient.connect(MONGO_URL, (err, _db) => { | |
db = _db; | |
next(err); | |
}); | |
}, | |
// REMOVE | |
(next) => { | |
db.collection('test').removeMany({ _id: /^async793/ }, next); | |
}, | |
// UPDATE | |
(next) => { | |
async.each(['a', 'b', 'c'], (i, next) => { | |
const filter = { _id: `async793-${i}` }; | |
const update = { | |
$set: { | |
_id: filter._id, | |
name: `なかむらasync-${i}`, | |
}, | |
}; | |
const opts = { | |
upsert: true, | |
}; | |
db.collection('test').updateOne(filter, update, opts, next); | |
}, next); | |
}, | |
// FIND parallel | |
(next) => { | |
async.parallel({ | |
a: (next) => { | |
db.collection('test').findOne({ _id: `async793-a` }, next); | |
}, | |
b: (next) => { | |
db.collection('test').findOne({ _id: `async793-b` }, next); | |
}, | |
c: (next) => { | |
db.collection('test').findOne({ _id: `async793-c` }, next); | |
}, | |
}, (err, result) => { | |
console.log(result); | |
next(err); | |
}); | |
}, | |
], (err, result) => { | |
if (err) { | |
console.log(err); | |
} | |
db && db.close(); | |
resolve(); | |
}); | |
}); | |
} | |
function* doCo() { | |
return new Promise((resolve, reject) => { | |
co(function* () { | |
const filter = { _id: 'test-co' }; | |
// CONNECT | |
const db = yield MongoClient.connect(MONGO_URL); | |
// REMOVE | |
yield db.collection('test').removeMany({ _id: /^co793/ }); | |
// UPDATE | |
for (let i of ['a', 'b', 'c']) { | |
const id = `co793-${i}`; | |
const update = { | |
$set: { | |
_id: id, | |
name: `なかむらco-${i}`, | |
}, | |
}; | |
const opts = { | |
upsert: true, | |
}; | |
yield db.collection('test').updateOne({ _id: id }, update, opts); | |
} | |
// FIND parallel | |
const result = yield { | |
a: db.collection('test').findOne({ _id: `co793-a` }), | |
b: db.collection('test').findOne({ _id: `co793-b` }), | |
c: db.collection('test').findOne({ _id: `co793-c` }), | |
}; | |
console.log(result); | |
db.close(); | |
}).catch((err) => { | |
console.error(err.stack); | |
}); | |
}); | |
} | |
co(function* () { | |
yield doAsync(); | |
yield doCo(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const async = require('async'); | |
const co = require('co'); | |
const MongoClient = require('mongodb').MongoClient; | |
const MONGO_URL = 'mongodb://localhost:27017/test'; | |
(function() { | |
const filter = { _id: 'test-async' }; | |
let db; | |
async.series([ | |
// CONNECT | |
(next) => { | |
MongoClient.connect(MONGO_URL, (err, _db) => { | |
db = _db; | |
next(err); | |
}); | |
}, | |
// REMOVE | |
(next) => { | |
db.collection('test').removeOne(filter, next); | |
}, | |
// UPDATE | |
(next) => { | |
const update = { | |
$set: { | |
_id: filter._id, | |
name: 'async793', | |
}, | |
}; | |
const opts = { | |
upsert: true, | |
}; | |
db.collection('test').updateOne(filter, update, opts, next); | |
}, | |
// FIND | |
(next) => { | |
db.collection('test').findOne(filter, (err, result) => { | |
console.log(result); | |
next(err, result); | |
}); | |
}, | |
], (err, result) => { | |
if (err) { | |
console.error(err); | |
} | |
db && db.close(); | |
}); | |
})(); | |
co(function* () { | |
const filter = { _id: 'test-co' }; | |
// CONNECT | |
const db = yield MongoClient.connect(MONGO_URL); | |
// REMOVE | |
yield db.collection('test').removeOne(filter); | |
// UPDATE | |
const update = { | |
$set: { | |
_id: filter._id, | |
name: 'co793', | |
}, | |
}; | |
const opts = { | |
upsert: true, | |
}; | |
yield db.collection('test').updateOne(filter, update, opts); | |
// FIND | |
const result = yield db.collection('test').findOne(filter); | |
console.log(result); | |
db.close(); | |
}).catch((err) => { | |
console.error(err.stack); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment