Permalink
Browse files

added module

  • Loading branch information...
1 parent a8f112d commit 421a21198f3184fd0aba4e4972fc0b43fd65f1ef mikelis committed Nov 2, 2012
Showing with 136 additions and 0 deletions.
  1. +1 −0 index.js
  2. +108 −0 lib/api.js
  3. +3 −0 lib/validators.js
  4. +24 −0 package.json
View
@@ -0,0 +1 @@
+module.exports = require('./lib/api');
View
@@ -0,0 +1,108 @@
+var schemas = {};
+
+exports.addSchema = function(name, Schema) {
+ schemas[name] = Schema;
+};
+
+exports.create = function(Schema, data, callback) {
+ if ('string' == typeof Schema) {
+ new schemas[Schema](data).save(callback);
+ } else {
+ new Schema(data).save(callback);
+ }
+};
+
+exports.validateAndCreate = function(Schema, data, Validator, callback) {
+ Validator(data, function(err, res){
+ if (err) {
+ callback(err, null);
+ } else {
+ new Schema(data).save(callback);
+ }
+ });
+};
+
+exports.list = function(Schema, callback) {
+ if ('string' == typeof Schema) {
+ schemas[Schema].find(callback);
+ } else {
+ Schema.find(callback);
+ }
+};
+
+exports.remove = function(Schema, filter, callback) {
+ if ('string' == typeof Schema) {
+ schemas[Schema].remove(filter, callback);
+ } else {
+ Schema.remove(filter, callback);
+ }
+}
+
+exports.find = function(Schema, filter, callback) {
+ if ('string' == typeof Schema) {
+ console.log(schemas[Schema]);
+ schemas[Schema].find(filter, callback);
+ } else {
+ Schema.find(filter, callback);
+ }
+};
+
+exports.findByID = function(Schema, id, callback) {
+ if ('string' == typeof Schema) {
+ schemas[Schema].findById(id, callback);
+ } else {
+ Schema.findById(id, callback);
+ }
+};
+
+exports.findOne = function(Schema, filter, callback) {
+ if ('string' == typeof Schema) {
+ schemas[Schema].findOne(filter, callback);
+ } else {
+ Schema.findOne(filter, callback);
+ }
+};
+
+exports.paginate = function(Schema, pageSize, pageNumber, callback) {
+ if ('string' == typeof Schema) {
+ schemas[Schema].find().skip(pageSize * (pageNumber - 1)).limit(pageSize).exec(callback);
+ } else {
+ Schema.find().skip(pageSize * (pageNumber - 1)).limit(pageSize).exec(callback);
+ }
+};
+
+exports.count = function(Schema, filter, callback) {
+ if ('string' == typeof Schema) {
+ schemas[Schema].find(filter).count().exec(callback);
+ } else {
+ Schema.find(filter).count().exec(callback);
+ }
+};
+
+exports.exists = function(Schema, filter, callback) {
+ if ('string' == typeof Schema) {
+ schemas[Schema].findOne(filter, function(err, doc){
+ if (err) {
+ callback("error occured: "+err, null);
+ } else {
+ if (doc) {
+ callback(null, true);
+ } else {
+ callback(null, false);
+ }
+ }
+ });
+ } else {
+ Schema.findOne(filter, function(err, doc){
+ if (err) {
+ callback("error occured: "+err, null);
+ } else {
+ if (doc) {
+ callback(null, true);
+ } else {
+ callback(null, false);
+ }
+ }
+ });
+ }
+};
View
@@ -0,0 +1,3 @@
+exports.email = function(data, callback) {
+
+};
View
@@ -0,0 +1,24 @@
+{
+ "name": "mongoose.helper",
+ "description": "API for mongoose scheme",
+ "url": "",
+ "keywords": [
+ "Mongoose",
+ "mongoDB",
+ "API"
+ ],
+ "author": {
+ "name": "Mikelis Asnins",
+ "email": "mikelis.asnins@yahoo.com"
+ },
+ "contributors": [],
+ "lib": ".",
+ "main": "./index.js",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/mikelsis/mongoose.helper.git"
+ },
+ "version": "0.0.1",
+ "readme": "",
+ "_id": "mongoose.api@0.0.1"
+}

0 comments on commit 421a211

Please sign in to comment.