move database helpers into their own files

v1.18.x
psychobunny 11 years ago
parent ae1ecb490f
commit 290e348851

@ -13,7 +13,6 @@
var winston = require('winston'), var winston = require('winston'),
nconf = require('nconf'), nconf = require('nconf'),
path = require('path'), path = require('path'),
async = require('async'),
express = require('express'), express = require('express'),
utils = require('./../../public/src/utils.js'), utils = require('./../../public/src/utils.js'),
levelup, levelup,
@ -81,22 +80,6 @@
db.close(callback); db.close(callback);
}; };
var helpers = {};
helpers.iterator = function(fn, keys, value, callback) {
var results = [];
async.each(keys, function(key, next) {
module[fn](key, value, function(err, result) {
results.push(result);
next();
});
}, function(err) {
callback(err, results);
});
};
module.helpers = module.helpers || {}; module.helpers = module.helpers || {};
module.helpers.level = helpers; module.helpers.level = require('./level/helpers');
}(exports)); }(exports));

@ -0,0 +1,19 @@
"use strict";
var helpers = {},
async = require('async');
helpers.iterator = function(fn, keys, value, callback) {
var results = [];
async.each(keys, function(key, next) {
module[fn](key, value, function(err, result) {
results.push(result);
next();
});
}, function(err) {
callback(err, results);
});
};
module.exports = helpers;

@ -109,56 +109,7 @@
db.close(); db.close();
}; };
var helpers = {};
helpers.findItem = function(data, key) {
if(!data) {
return null;
}
for(var i=0; i<data.length; ++i) {
if(data[i]._key === key) {
var item = data.splice(i, 1);
if(item && item.length) {
return item[0];
} else {
return null;
}
}
}
return null;
};
helpers.fieldToString = function(field) {
if(field === null || field === undefined) {
return field;
}
if(typeof field !== 'string') {
field = field.toString();
}
// if there is a '.' in the field name it inserts subdocument in mongo, replace '.'s with \uff0E
field = field.replace(/\./g, '\uff0E');
return field;
};
helpers.valueToString = function(value) {
if(value === null || value === undefined) {
return value;
}
return value.toString();
};
helpers.done = function(cb) {
return function(err, result) {
if (typeof cb === 'function') {
cb(err, result);
}
};
};
module.helpers = module.helpers || {}; module.helpers = module.helpers || {};
module.helpers.mongo = helpers; module.helpers.level = require('./mongo/helpers');
}(exports)); }(exports));

@ -0,0 +1,52 @@
"use strict";
var helpers = {};
helpers.findItem = function(data, key) {
if(!data) {
return null;
}
for(var i=0; i<data.length; ++i) {
if(data[i]._key === key) {
var item = data.splice(i, 1);
if(item && item.length) {
return item[0];
} else {
return null;
}
}
}
return null;
};
helpers.fieldToString = function(field) {
if(field === null || field === undefined) {
return field;
}
if(typeof field !== 'string') {
field = field.toString();
}
// if there is a '.' in the field name it inserts subdocument in mongo, replace '.'s with \uff0E
field = field.replace(/\./g, '\uff0E');
return field;
};
helpers.valueToString = function(value) {
if(value === null || value === undefined) {
return value;
}
return value.toString();
};
helpers.done = function(cb) {
return function(err, result) {
if (typeof cb === 'function') {
cb(err, result);
}
};
};
module.exports = helpers;

@ -0,0 +1,6 @@
"use strict";
var helpers = {};
module.exports = helpers;
Loading…
Cancel
Save