UNPKG

2.48 kBJavaScriptView Raw
1"use strict";
2/*!
3 * Simple Node Mobile Portal
4 * Copyright(c) 2012-2017 Faisal(admin@simpleportaljs.com)
5 *
6 * MIT Licensed
7 */
8var url=require("url"),dispatch=require("dispatch"),StorageService=require("./../service/storageservice"),util=require("./../util"),DbUtil=require("./../util/db");module.exports=function restfulmongo(a){var b=[];b[a+"/mongo/((\\w+)+)/count"]=function(h,g,i,d,c,l){var k=h.url.split("/");if(k.length>3){var j=k[3];h.url=h.url.replace("/mongo/"+j,"/mongo/");var f=h.dbid||"default";var e=new StorageService({dbid:f,collectionName:j});e.count({},function(n,o){var m={count:o||0};util.sendServiceResponse(g,n,m)})}else{i()}};b[a+"/mongo/((\\w+)+)/duplicates"]=function(h,g,i,d,c,p){console.log("Simpleportal -restfulmongo duplicated session being served.");var l=url.parse(h.url,true);var n=l.pathname.split("/");if(n.length>3){var k=n[3];h.url=h.url.replace("/mongo/"+k,"/"+k);var f=h.dbid||"default";var o="id";if(h.query&&h.query.id){o=h.query.id}var e=new StorageService({dbid:f,collectionName:k});query={};query[o]={$exists:true};var j;var m=[];e.find(query,function(r,q){if(!r&&q){q.forEach(function(s){if(j&&s[o]==j[o]){if(!m[j]){m.push(j)}m.push(s)}j=s})}util.sendServiceResponse(g,r,m)},{sorting:{sort:[[o,1]]}})}else{i()}};b[a+"/mongo/((\\w+)+)/search"]=function(h,g,i,d,c,o){var k=url.parse(h.url,true);var l=k.pathname.split("/");if(l.length>3){var j=l[3];h.url=h.url.replace("/mongo/"+j,"/"+j);var f=h.dbid||"default";var n="id";var m=h.query||{};var e=new StorageService({dbid:f,collectionName:j});e.find(m,function(q,p){util.sendServiceResponse(g,q,p)})}else{i()}};b[a+"/mongo/((\\w+/\\w+)+)"]=function(i,h,j,d,c,o){var l=url.parse(i.url,true);var m=l.pathname.split("/");if(m.length>4){var k=m[3];var e=m[4];i.url=i.url.replace("/mongo/"+k,"/mongo/");var g=i.dbid||"default";var f=new StorageService({dbid:g,collectionName:k});var n={id:e};f.findOne({id:e},function(p,q){if(p&&!result&&(e.length==12||e.length==24)){try{n={_id:DbUtil.getObjectId(e)};f.findOne(n,function(r,s){util.sendServiceResponse(h,r,s)})}catch(p){util.sendServiceResponse(h,p,{})}}else{util.sendServiceResponse(h,p,q)}})}else{j()}};b[a+"/mongo/(\\w+)"]=function(h,g,i,d,c,m){var k=url.parse(h.url,true);var l=k.pathname.split("/");if(l.length>3){var j=l[3];h.url=h.url.replace("/mongo/"+j,"/mongo/");var f=h.dbid||"default";var e=new StorageService({dbid:f,collectionName:j});e.find({},function(n,o){util.sendServiceResponse(g,null,o)})}else{i()}};return dispatch(b)};
\No newline at end of file