Created
August 31, 2017 03:23
-
-
Save boris/83a0143db760ffddb8044a93800bf6a6 to your computer and use it in GitHub Desktop.
Usage of webtask.io
This file contains 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
module.exports = function (ctx, done) { | |
var MongoClient = require('mongodb').MongoClient; | |
var assert = require('assert'); | |
var ObjectId = require('mongodb').ObjectID; | |
var url = ctx.data.MONGO_URL; | |
var insertValue = function(db) { | |
db.collection('cups').insertOne({ | |
'date': new Date(), | |
'coffee': 1 | |
}, function(err, result) { | |
assert.equal(err, null); | |
done(null, "New coffee added!"); | |
}); | |
}; | |
MongoClient.connect(url, function(err, db) { | |
assert.equal(null, err); | |
insertValue(db, function() { | |
db.close(); | |
}); | |
}); | |
}; |
This file contains 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
module.exports = | |
function (context, cb) { | |
var passwd = ""; | |
var candidates = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
for (var i = 0; i < 15; i++) | |
passwd += candidates.charAt(Math.floor(Math.random() * candidates.length)); | |
cb(null, passwd); | |
} |
This file contains 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
module.exports = function (ctx, done) { | |
var MongoClient = require('mongodb').MongoClient; | |
var assert = require('assert'); | |
var ObjectId = require('mongodb').ObjectID; | |
var url = ctx.data.MONGO_URL; | |
var getValue = function(db) { | |
var start = new Date(); | |
var end = new Date(); | |
start.setHours(0,0,0,0); | |
end.setHours(23,59,59,999); | |
db.collection('cups').count({ | |
date: {$gte: start, $lt: end} | |
}, function(err, result) { | |
assert.equal(err, null); | |
done(null, "Cups of coffee today: " + result); | |
}); | |
}; | |
MongoClient.connect(url, function(err, db) { | |
getValue(db, function() { | |
db.close(); | |
}) | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment