Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
alexbeletsky / proc.js
Created Mar 22, 2014
Callbacks + Async
View proc.js
async.waterfall([
download,
process,
upload
], function (err, results) {
if (err) {
return console.error('processing failed, error: ', err);
}
console.log('processing completed, results: ', results);
});
View aggregate.js
function aggregate(callback) {
mongo.items.aggregate([
{
$match: {created: {$gte: timespan.from, $lt: timespan.to}}
},
{
$group: {
_id: {
url: '$source',
item: '$itemId'
@alexbeletsky
alexbeletsky / 1.js
Last active Aug 29, 2015
Which is better
View 1.js
// which is better?
function f() {
// returns object.. takes some cpu to complete
}
// this one?
return f().one || f().two;
// that one?
View collections.json
/* 0 */
{
"_id" : ObjectId("533be1949421f7c070000007"),
"followers" : [
{
"_id" : ObjectId("533be1949421f7c070000005"),
"email" : "1396433300606@tests.com"
}
],
"public" : false,
@alexbeletsky
alexbeletsky / patch.js
Created May 1, 2014
Using streams for patching db
View patch.js
var _ = require('underscore');
var through = require('through');
var moment = require('moment');
var logger = require('../source/utils/logger');
var log = require('single-line-log');
function run(db, callback) {
var started = moment();
var patched = 0;
View nostreams.js
var config = require('../config');
var db = require('./db')(config);
function readUsers(callback) {
db.users.find({unsubscribed: {$exists: false}, firstTimeUser: {$exists: false}}).forEach(function (err, user) {
if (err) {
throw callback(err);
}
if (!user) {
View auth.spec.js
var request = require('request');
var testUtils = require('../utils');
var moment = require('moment');
var crypto = require('crypto');
describe('auth.spec.js', function () {
var authUrl, url, payload, error, response, body;
beforeEach(function () {
authUrl = testUtils.getRootUrl() + '/api/auth';
View base64.js
function base64(data) {
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];
if (!data) {
return data;
}
data = utf8Encode(data);
View logger.js
require('colors');
var util = require('util');
var moment = require('moment');
var logentries = require('node-logentries');
var config = require('../../config');
var stub = require('./stub');
var log = logentries.logger({
token: config.logentries.token,
@alexbeletsky
alexbeletsky / modej.js
Created Nov 19, 2014
Simple Multi-tenancy for MongoDB
View modej.js
var tenancy = require('../utils/tenancy');
function shiftsModel(mongo) {
function create(user, shift, callback) {
mongo.shifts.save(tenancy(user, shift), tenancy(callback));
}
function getByWeek(user, week, callback) {
mongo.shifts.find(tenancy(user, {week: +week}), tenancy(callback));