Skip to content

Instantly share code, notes, and snippets.

View r01010010's full-sized avatar

Paloma Jiménez r01010010

  • Barcelona
View GitHub Profile
/**
* Dependencies
*/
fs = require('fs');
/**
* DB Initializer
*/
var i_mongoose = {
var sinon = require('sinon');
var chai = require('chai');
expect = chai.expect;
should = chai.should();
assert = chai.assert;
var clock;
beforeEach(function () {
clock = sinon.useFakeTimers();
var mongodb = require('mongodb');
// Connection URL
var url = 'mongodb://localhost:27017/scjs';
// Use connect method to connect to the Server
mongodb.MongoClient.connect(url, function(err, db) {
if(err) { throw err } else {
console.log('yahooo');
}
});
This file has been truncated, but you can view the full file.
db.provinces.insert(
[ { _id: ObjectId("54715c1442d576600a9965fc"),
name: 'Araba/Álava',
country: 'spain',
towns:
[ { _id: ObjectId("54715c1442d576600a996630"),
name: 'Alegría-Dulantzi',
province_id: ObjectId("54715c1442d576600a9965fc") },
{ _id: ObjectId("54715c1442d576600a996631"),
name: 'Amurrio',
var data = fs.readFileSync('/etc/passwd');
/* This code would start 1000 simultaneous asynchronous file reads, and run the do_next_part()
function immediately. This has several problems: first, we’d like to wait until
all the file reads are done until going further.*/
for(var i = 1; i <= 1000; i++) {
fs.readFile('./'+i+'.txt', function() {
// do something with the file
});
for(j = 0; j < provinces.length; j++){
var province = provinces[j];
var province_name = _s.strLeft(province.provincia, '/');
mongo_provinces[j] = {
_id: new mongodb.ObjectID()
, slug: _s.slugify(province_name)
, name: province_name
, country: "spain"
var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true, safe: true});
var db = new mongodb.Db('scjs', server);
db.open(function(err, db) { if(err) throw err;
async.eachSeries(
// Collection
mongo_provinces
@r01010010
r01010010 / SuperSchema.js
Last active August 29, 2015 14:11
reusable validation function
/*** Dependencies ***/
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ValidationError = require(g.wizco.dirs.libErrors + '/ValidationError');
/**
* SuperSchema
* Schema mongoose based SuperClass
*/
function SuperSchema(schema, _ev)
@r01010010
r01010010 / why.js
Last active August 29, 2015 14:12
Calling a defined in file var from a callback... undefined!
require('another_file.js');
var whatever = 8;
function foo(){
// calling a function from another file
foo_with_callback(function(returned_value){
console.log(whatever) // whatever is undefined <--- WHY ???
});
@r01010010
r01010010 / require_scopes.js
Last active August 29, 2015 14:12
Require scopes
/** WHY IS THIS IN NODE.JS ? **/
// file1.js
var _ = require('underscore');
require('./routes/document'); // _ will be NOT VISIBLE in document
// file2.js
_ = require('underscore');
require('./routes/document'); // _ will be VISIBLE in document