Skip to content

Instantly share code, notes, and snippets.

@kpnemo
Created February 16, 2015 14:45
Show Gist options
  • Select an option

  • Save kpnemo/8c2ef8c21b0854381e2f to your computer and use it in GitHub Desktop.

Select an option

Save kpnemo/8c2ef8c21b0854381e2f to your computer and use it in GitHub Desktop.
var appEvetns = require('../inc/app_events'),
dbUtils = require('../inc/db_util'),
MongoDb = dbUtils.mongo.Db,
MongoBSON = dbUtils.mongo.pure().BSON,
MongoGridStore = dbUtils.mongo.GridStore,
MongoBinary = dbUtils.mongo.Binary,
MongoCode = dbUtils.mongo.Code,
MongoObjectID = dbUtils.mongo.ObjectID,
moment = require('moment'),
_ = require('lodash'),
config = require('../config/env.config.js'),
dbClient = null,
db = null,
collections = {};
dbUtils.dbConnect(function (err, mongoClient) {
if (err) {
console.log('api_util:: db connect error');
appEvetns.emit('dbConnectError', err);
} else {
console.log('api_util:: db connected');
dbClient = mongoClient;
db = mongoClient.db(config.get_db('name'));
appEvetns.emit('dbConnected', mongoClient);
}
});
var _cleanData = function (data) {
var filteredData = null;
if (_.isArray(data)) {
_.each(data, function (item, index) {
item.id = item._id.toHexString();
delete item._id;
if (filteredData == null) filteredData = [];
filteredData.push(item);
});
} else if (_.isObject(data)) {
if (filteredData == null) filteredData = {};
data.id = data._id.toHexString();
delete data._id;
filteredData = data;
} else if (_.isNull(data)) {
return null;
}
return filteredData;
};
var getIndex = function (collectionName, callback) {
var cb = callback || function () {};
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.find({}, {sort: [['_id', 'desc']]}).toArray(function (err, results) {
cb(err, _cleanData(results));
});
}
})
}
var add = function (collectionName, data, callback) {
var _data = data,
cb = callback || function () {},
timestamp = moment().unix();
data.created_at = timestamp;
if (data.id) delete data.id;
if (data._id) delete data._id;
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.insert(_data, function (err, result) {
cb(err, _cleanData(result[0]));
});
}
});
};
var addBatch = function (collectionName, data, callback) {
var _data = data,
cb = callback || function () {},
timestamp = moment().unix();
data.created_at = timestamp;
_.each(data, function (item) {
if (item.id) delete data.id;
if (item._id) delete data._id;
});
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.insert(_data, function (err, result) {
cb(err, _cleanData(result));
});
}
});
};
var getById = function (collectionName, id, callback) {
var cb = callback || function () {};
if (_.isString(id) && id.length != 24) {
cb('Id Not valid', null);
return;
}
var _id = (_.isObject(id)) ? id : new MongoObjectID(id);
if (!db) {
cb(true, 'No DB Connection');
return;
}
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.findOne({'_id': _id}, function (err, result) {
cb(err, _cleanData(result));
});
}
});
}
var get = function (collectionName, queryParams, params, callback) {
var cb = callback || function () {},
options = params || {};
if (queryParams && queryParams.id) {
queryParams._id = (_.isObject(queryParams.id)) ? queryParams.id : new MongoObjectID(queryParams.id);
delete queryParams.id;
}
else if (queryParams && queryParams._id) {
queryParams._id = (_.isObject(queryParams._id)) ? queryParams._id : new MongoObjectID(queryParams._id);
}
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.find(queryParams, options).toArray(function (err, results) {
cb(err, _cleanData(results));
});
}
});
}
var getNative = function (collectionName, queryParams, params, callback) {
var cb = callback || function () {},
options = params || {};
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.find(queryParams, options).toArray(function (err, results) {
cb(err, results);
});
}
});
}
var remove = function (collectionName, queryParams, callback) {
var cb = callback || function () {};
// console.log('api remove', queryParams, typeof(queryParams.id), typeof(queryParams._id));
if (queryParams && queryParams.id) {
queryParams._id = (_.isObject(queryParams.id)) ? queryParams.id : new MongoObjectID(queryParams.id);
delete queryParams.id;
}
else if (queryParams && queryParams._id) {
queryParams._id = (_.isObject(queryParams._id)) ? queryParams._id : new MongoObjectID(queryParams._id);
}
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.remove(queryParams, function (err, numOfDeleted) {
cb(err, numOfDeleted);
});
}
});
}
var update = function (collectionName, id, data, params, callback) {
var _data = data || {},
_id = (_.isObject(id)) ? id : new MongoObjectID(id),
cb = callback || function () {},
options = params || {},
timeStamp = moment().unix();
if (_data.id) delete data.id;
if (_data._id) delete data._id;
data.modified_at = timeStamp;
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.update({'_id': _id}, {$set: _data}, options, function (err, result) {
setTimeout(function () {
getById(collectionName, id, cb);
}, 5);
});
}
});
}
var findAndUpdate = function (collectionName, query, data, params, callback) {
var _data = data || {},
_query = query || {},
cb = callback || function () {},
options = params || {};
if (_data.id) delete data.id;
if (_data._id) delete data._id;
if (_query && _query.id) {
_query._id = (_.isObject(_query.id)) ? _query.id : new MongoObjectID(_query.id);
delete _query.id;
}
else if (_query && _query._id) {
_query._id = (_.isObject(_query._id)) ? _query._id : new MongoObjectID(_query._id);
}
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.update(_query, {$set: _data}, options, function (err, result) {
setTimeout(function () {
get(collectionName, _query, {}, cb);
}, 5);
});
}
});
}
var updateWithString = function (collectionName, id, updateString, params, callback) {
var _updateString = updateString,
_id = (_.isObject(id)) ? id : new MongoObjectID(id),
cb = callback || function () {},
options = params || {};
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.update({'_id': _id}, _updateString, options, function (err, result) {
setTimeout(function () {
getById(collectionName, id, cb);
}, 5);
});
}
});
}
var reset = function (collectionName, callback) {
var cb = callback || function () {};
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.remove(function (err, numOfDeleted) {
cb(err, numOfDeleted);
});
}
});
}
var count = function (collectionName, queryParams, callback) {
var cb = callback || function () {};
db.collection(collectionName, function (err, collection) {
if (err) {
cb(err, null);
} else {
collection.find(queryParams).count(function (err, count) {
cb(err, count);
});
}
});
}
module.exports = {
index: function (collectionName, callback) {
getIndex(collectionName, callback);
},
add: function (collectionName, data, callback) {
add(collectionName, data, callback);
},
addBatch: function (collectionName, data, callback) {
addBatch(collectionName, data, callback);
},
count: function (collectionName, queryParams, callback) {
count(collectionName, queryParams, callback);
},
getById: function (collectionName, id, callback) {
getById(collectionName, id, callback);
},
get: function (collectionName, queryParams, params, callback) {
get(collectionName, queryParams, params, callback);
},
getNative: function (collectionName, queryParams, params, callback) {
getNative(collectionName, queryParams, params, callback);
},
remove: function (collectionName, queryParams, callback) {
remove(collectionName, queryParams, callback);
},
update: function (collectionName, id, data, params, callback) {
update(collectionName, id, data, params, callback);
},
findAndUpdate: function (collectionName, query, data, params, callback) {
findAndUpdate(collectionName, query, data, params, callback);
},
updateWithString: function (collectionName, id, updateString, params, callback) {
updateWithString(collectionName, id, updateString, params, callback);
},
reset: function (collectionName, callback) {
reset(collectionName, callback);
},
dbUtils: function () {
return dbUtils;
},
dbClient: function () {
return dbClient;
},
db: function () {
return db;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment