/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/modelizer@0.5.5/lib/modelizer.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
var Model=require("./model"),Q=require("q"),assert=require("./microlibs").assert,check=require("./microlibs").check,isEmptyObject=require("./microlibs").isEmptyObject,ObjectId=require("./objectid");Model.MongoConnector=function(e){var t=e;return function(e){var r=t.collection(e.modelName);return r.callOperation=function(t,r,o){var n,s=Q.defer();return e.operations.hasOwnProperty(t)?n=e.operationImpls[t](r,o):e.factorys.hasOwnProperty(t)?n=e.factoryImpls[t](r,o):assert(!1,"Call '"+t+"' was not defined"),null!=n&&"promise"==n.name?n:(s.resolve(n),s.promise)},r}},Model.prototype.express=function(e){this.app=e},Model.prototype.serve=function(){var e=this;this.app.all("*",(function(e,t,r){if(t.header("Access-Control-Allow-Credentials",!0),t.header("Access-Control-Allow-Origin",e.headers.origin),t.header("Access-Control-Allow-Methods","OPTIONS,GET,POST,PUT,DELETE"),t.header("Access-Control-Allow-Headers","Content-Type, Authorization, X-Requested-With"),"OPTIONS"==e.method)return t.send(200);r()})),this.app.get("/"+this.modelName+"/all",(function(t,r){r.setHeader("Content-Type","application/json"),e.filtered_allQ(t).then((function(t){for(var o=0;o<t.length;o++)e._transform(e,t[o],"pack");r.send(JSON.stringify(t,null,2))})).fail((function(e){e.statusCode||console.log("Error in HTTP GET - all()\n",e.message,"\nStack:\n",e.stack),r.send(e.statusCode||500,{error:e.message})})).done()})),this.app.post("/"+this.modelName+"/find",(function(t,r){r.setHeader("Content-Type","application/json"),e.filtered_findQ(t.body,t).then((function(t){for(var o=0;o<t.length;o++)e._transform(e,t[o],"pack");r.send(JSON.stringify(t,null,2))})).fail((function(e){e.statusCode||console.log("Error in HTTP GET - find()\n",e.message,"\nStack:\n",e.stack),r.send(e.statusCode||500,{error:e.message})})).done()})),this.app.get("/"+this.modelName+"/:id",(function(t,r){r.setHeader("Content-Type","application/json"),e.filtered_getQ(t.params.id,t).then((function(t){e._transform(e,t,"pack"),r.send(JSON.stringify(t,null,2))})).fail((function(e){e.statusCode||console.log("Error in HTTP GET - get()\n",e.message,"\nStack:\n",e.stack),r.send(e.statusCode||500,{error:e.message})})).done()})),this.app.put("/"+this.modelName,(function(t,r){if(assert(null!=t.body,"request body missing! Use bodyParser!"),r.setHeader("Content-Type","application/json"),null!=t.body._id)try{t.body._id=ObjectId(t.body._id)}catch(e){return void r.send(err.statusCode||400,{error:"Invalid ObjectId Format"})}e._transform(e,t.body,"unpack"),e.filtered_saveQ(t.body,t).then((function(e){r.send(JSON.stringify(e,null,2))})).fail((function(e){e.statusCode||console.log("Error in HTTP GET - save()\n",e.message,"\nStack:\n",e.stack),r.send(e.statusCode||500,{error:e.message})})).done()})),this.app.delete("/"+this.modelName+"/:id",(function(t,r){r.setHeader("Content-Type","application/json"),e.filtered_removeQ(t.params.id,t).then((function(){r.send(200,{status:"OK"})})).fail((function(e){e.statusCode||console.log("Error in HTTP GET - remove()\n",e.message,"\nStack:\n",e.stack),r.send(e.statusCode||500,{error:e.message})})).done()})),this.app.put("/"+this.modelName+"/:op",(function(t,r){r.setHeader("Content-Type","application/json"),assert(null!=t.body,"No body in request!"),e.filtered_callOpQ(t.params.op,t.body,t).then((function(e){"object"!=typeof e&&(e={result:e}),r.send(200,e)})).fail((function(e){e.statusCode||console.log("Error in HTTP GET - operation()\n",e.message,"\nStack:\n",e.stack),r.send(e.statusCode||500,{error:e.message})})).done()}))},Model.prototype.filtered_getQ=function(e,t){var r;try{r=ObjectId(e)}catch(e){var o=Q.defer();return o.reject(new Error("Invalid ObjectId Format")),o.promise}return this.filtered_findQ({_id:r},t).then((function(e){if(1!=e.length){var t=Q.defer();return t.reject(new Error("Object not found!")),t.promise}return e[0]}))},Model.prototype.filtered_findQ=function(e,t){var r=this;return this._getReadFilter(t).then((function(t){if(!1===t){var o=new Error("Access denied!");throw o.statusCode=401,o}for(var n in t)e[n]=t[n];return r.findQ(e)}))},Model.prototype.filtered_allQ=function(e){return this.filtered_findQ({},e)},Model.prototype.filtered_saveQ=function(e,t){var r=this;return this._getWriteFilter(e,t).then((function(t){if(!1===t){var o=new Error("Access denied!");throw o.statusCode=401,o}return r.saveQ(e)}))},Model.prototype.filtered_removeQ=function(e,t){var r,o=this,n=Q.defer();try{r=ObjectId(e)}catch(e){return n.reject(new Error("Invalid ObjectId Format")),n.promise}return o.getQ(r).then((function(e){return Q(o._getWriteFilter(e,t))})).then((function(e){if(!1===e){var t=new Error("Access denied!");throw t.statusCode=401,t}return o.removeQ(r)}))},Model.prototype.filtered_callOpQ=function(e,t,r){return this.callOpQ(e,t,r)},Model.runSimpleServer=function(e,t,r){var o=require("mongojs")(r),n=require("express"),s=n();s.use(n.logger()),s.use(n.bodyParser()),s.use(n.cookieParser()),s.use(n.session({secret:"Session Secret",store:new n.session.MemoryStore})),s.use(n.static(__dirname)),s.use(n.static(e)),s.set("json spaces",2),s.listen(t),Model.simpleServer={},Model.simpleServer.app=s,Model.simpleServer.connector=Model.MongoConnector(o),console.log("Server setup at Port",t)},module.exports=Model;
//# sourceMappingURL=/sm/aff38990210979eaf5f8589ddf15a6ae1573d046971463331819bfc77d2b5b60.map