public
Last active

  • Download Gist
10873199.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
 
var mongoose = require('./../mongoose');
var Schema = mongoose.Schema;
var sleep = 1000;
var timer;
var db = mongoose.createConnection();
 
db.on('error', function () {
// error on startup
console.error('conn error', arguments);
 
// limit our retries
connect.time += sleep;
if (connect.time > 22000) return console.error('giving up');
db.db.close();
timer = setTimeout(connect, sleep);
});
 
db.on('open', function () {
clearTimeout(timer);
console.error('opening');
setTimeout(function(){
find(true);
insert();
}, 1000);
});
 
function connect () {
db.open('localhost', 'testing_10873199');
}
connect.time = 0;
connect();
 
 
var schema = new Schema({
name: String
});
var A = db.model('A', schema);
 
console.error('creating');
var a = new A({ name: '10873199' });
A.create({name:'asdf'},{name:'dddd9'}, function(){});
A.create({name:'asdf2'},{name:'dddd92'}, function(){});
 
function find (show) {
console.error('finding');
A.find(function (err, docs) {
console.error('found');
if (show)
console.error(err, docs);
else
console.error('%d docs', docs.length);
});
}
 
var time = 0;
function insert () {
find(false);
console.error('inserting');
var a = new A({ key: 'asdfasdasdf' });
a.save(function(err){
console.error('inserted');
if (err) return console.error('save error', err);
});
time += sleep;
if (time > 20000) return;
insertTimer = setTimeout(insert, sleep);
}
 
process.on('SIGINT', drop);
 
function drop () {
db.db.dropDatabase(function () {
console.error('closing');
db.close();
});
process.exit(0);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.