/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@induct/core@0.2.4/dist/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var classValidator=require("class-validator"),express=require("express"),azureFunctionsTsEssentials=require("azure-functions-ts-essentials");class ValidationError extends Error{constructor(t){super(),this.message=t,this.name="InductValidationError"}}class QueryError extends Error{constructor(t){super(),this.message=t,this.name="InductQueryError"}}
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __awaiter(t,e,i,r){return new(i||(i=Promise))((function(s,o){function d(t){try{a(r.next(t))}catch(t){o(t)}}function n(t){try{a(r.throw(t))}catch(t){o(t)}}function a(t){var e;t.done?s(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(d,n)}a((r=r.apply(t,e||[])).next())}))}!function(t){t[t.OK=200]="OK",t[t.CREATED=201]="CREATED",t[t.ACCEPTED=202]="ACCEPTED",t[t.NO_CONTENT=204]="NO_CONTENT",t[t.BAD_REQUEST=400]="BAD_REQUEST",t[t.UNAUTHORIZED=401]="UNAUTHORIZED",t[t.FORBIDDEN=403]="FORBIDDEN",t[t.NOT_FOUND=404]="NOT_FOUND",t[t.CONFLICT=409]="CONFLICT",t[t.UNPROCESSABLE_ENTITY=422]="UNPROCESSABLE_ENTITY",t[t.TOO_MANY_REQUESTS=429]="TOO_MANY_REQUESTS",t[t.INTERNAL_SERVER_ERROR=500]="INTERNAL_SERVER_ERROR"}(exports.StatusCode||(exports.StatusCode={}));class Model{constructor(t,e){var i;t&&(this._model=new e.schema(t)),this._table_name=e.tableName,this._con=e.connection,this._id_field=e.idField,this.fields=null!==(i=e.fields)&&void 0!==i?i:"*"}get validated(){return this._validated}get options(){return this._options}get con(){return this._con}get table_name(){return this._table_name}get id_field(){return this._id_field}get model(){return this._model}get trx(){return this._trx}destroyConnection(){return __awaiter(this,void 0,void 0,(function*(){yield this._trx.destroy()}))}startTransaction(){return __awaiter(this,void 0,void 0,(function*(){return this._trx&&(yield this._trx.destroy()),this._trx=yield this._con.transaction(),this._trx}))}commitTransaction(){return __awaiter(this,void 0,void 0,(function*(){this._trx.commit()}))}rollbackTransaction(){return __awaiter(this,void 0,void 0,(function*(){this._trx.rollback()}))}get(t){return this._model[t]}set(t,e){this._model[t]=e}exists(t,e){return __awaiter(this,void 0,void 0,(function*(){return(yield this._con(this._table_name).where({[t]:e})).length>0}))}validate(){return __awaiter(this,void 0,void 0,(function*(){return classValidator.validate(this._model)}))}}class InductModel extends Model{constructor(t,e){super(t,e),this._qb=this._con(this._table_name)}findAll(){return __awaiter(this,void 0,void 0,(function*(){try{return yield this._qb.select(this.fields)}catch(t){throw new QueryError(`InductModel.findAll failed with error ${t}`)}}))}findOneById(t){return __awaiter(this,void 0,void 0,(function*(){try{const e=null!=t?t:this._model[this._id_field];return this._qb.select(this.fields).where(this._id_field,e)}catch(t){throw new QueryError(`InductModel.findOneById failed with error ${t}`)}}))}create(t){return __awaiter(this,void 0,void 0,(function*(){try{const e=null!=t?t:this._model;return yield this._qb.insert(e),this._model}catch(t){throw new QueryError(`InductModel.create failed with error ${t}`)}}))}update(t){return __awaiter(this,void 0,void 0,(function*(){try{const e=null!=t?t:this._model,i=this._model[this._id_field];return yield this._qb.update(e).where(this._id_field,i)}catch(t){throw new QueryError(`InductModel.update failed with error ${t}`)}}))}delete(t){return __awaiter(this,void 0,void 0,(function*(){try{const e=null!=t?t:this._model[this._id_field];return yield this._qb.where(this._id_field,e).del(),e}catch(t){throw new QueryError(`InductModel.delete failed with error ${t}`)}}))}}class ControllerResult{constructor(t){Object.assign(this,t)}send(){var t;return this.res.status(this.status).json({data:this.data,info:this.info,error:null===(t=this.error)||void 0===t?void 0:t.name,validationErrors:this.validationErrors})}redirect(t){return this.res.redirect(t)}}const inductModelFactory=t=>(e,i)=>__awaiter(void 0,void 0,void 0,(function*(){try{const{all:r,validate:s}=i,o=e?t.filter((t=>!!e[t])):[];if(t&&0===o.length&&!r)throw new TypeError("Lookup field or bulk option unspecified");const d=new InductModel(e,i);if(s){if((yield d.validate()).length>0)throw new ValidationError("Schema validation failed")}return d}catch(t){console.log(t)}}));class Induct{constructor(t){var e;this.connection=t.connection,this.schema=t.schema,this.idField=t.idField,this.tableName=t.tableName,this.idParam=t.idParam||"id",this.lookupFields=[t.idField,...null!==(e=t.additionalLookupFields)&&void 0!==e?e:[]],this.fieldsList=t.fieldsList,this.validate=t.validate,this.all=t.all,this.modelFactory=inductModelFactory(this.lookupFields)}_getModelOptions(t={}){const e=Object.entries(t),{all:i,validate:r,fieldsList:s,schema:o,connection:d,tableName:n,idField:a}=this,l={all:i,validate:r,fields:s,schema:o,connection:d,tableName:n,idField:a};for(const[t,i]of e)l[t]=i;return l}model(t,e){return __awaiter(this,void 0,void 0,(function*(){const i=this._getModelOptions(e),r=inductModelFactory([this.idField]);return yield r(t,i)}))}handler(t,e){return-1!==t.indexOf("find")?this.lookupHandler(t,e):this.modifyHandler(t,e)}lookupHandler(t,e){const i=this._getModelOptions(e);return(e,r)=>__awaiter(this,void 0,void 0,(function*(){let s;const o=Object.assign({},e.body);"findOneById"===t&&(o[this.idField]=e.params.id);try{const e=yield this.modelFactory(o,i);if(!e)return new ControllerResult({res:r,status:exports.StatusCode.BAD_REQUEST}).send();if("function"!=typeof e[t])throw new TypeError(`${t} is not a function of the supplied model`);const d=yield e[t]();if(0==d.length)s={res:r,status:exports.StatusCode.NOT_FOUND};else{const t=1===d.length?d[0]:d;s={res:r,status:exports.StatusCode.OK,data:t}}}catch(t){s={res:r,status:exports.StatusCode.INTERNAL_SERVER_ERROR,error:t}}return new ControllerResult(s).send()}))}modifyHandler(t,e){const i=this._getModelOptions(e);return(e,r)=>__awaiter(this,void 0,void 0,(function*(){let s;const o=Object.assign({},e.body);"update"!==t&&"delete"!==t||(o[this.idField]=e.params[this.idParam]);try{const e=yield this.modelFactory(o,i);if(!e)return new ControllerResult({res:r,status:exports.StatusCode.BAD_REQUEST}).send();if("function"!=typeof e[t])throw new TypeError(`${t} is not a function of the supplied model`);const d=yield e[t]();if(d){let e=exports.StatusCode.OK;"create"===t?e=exports.StatusCode.CREATED:"delete"===t&&(e=exports.StatusCode.NO_CONTENT),s={res:r,status:e,data:d}}else{const e="create"===t?exports.StatusCode.BAD_REQUEST:exports.StatusCode.NOT_FOUND;s={res:r,status:e}}}catch(t){s={res:r,status:exports.StatusCode.INTERNAL_SERVER_ERROR,error:t}}return new ControllerResult(s).send()}))}azureFunctionsRouter(t){const e=this._getModelOptions(t);return(t,i)=>__awaiter(this,void 0,void 0,(function*(){var t;const r=null===(t=i.params)||void 0===t?void 0:t.id,s=Object.assign({},i.body);let o,d;r&&(s[this.idField]=r);try{const t=yield this.modelFactory(s,e);if(t)switch(i.method){case azureFunctionsTsEssentials.HttpMethod.Get:o=r?yield t.findOneById():yield t.findAll(),d={status:200,body:o};break;case azureFunctionsTsEssentials.HttpMethod.Post:o=yield t.create(),d={status:201,body:o};break;case azureFunctionsTsEssentials.HttpMethod.Patch:o=yield t.update(),d=o?{status:200,body:o}:{status:400,body:"Bad request"};break;case azureFunctionsTsEssentials.HttpMethod.Delete:o=yield t.delete(),d=o?{status:204,body:o}:{status:404,body:"Resource not found"};break;default:d={status:405,body:"Method not allowed"}}else d={status:400,body:"Bad request"}}catch(t){d={status:500,body:`${t.name}`}}return d||{status:400,body:"Bad request"}}))}router(){const t=express.Router();return t.get("/",this.lookupHandler("findAll",{all:!0})),t.post("/",this.modifyHandler("create",{validate:!0})),t.patch("/:id",this.modifyHandler("update",{validate:!0})),t.get("/:id",this.lookupHandler("findOneById")),t.delete("/:id",this.modifyHandler("delete")),t}}exports.ControllerResult=ControllerResult,exports.InductBaseModel=Model,exports.InductModel=InductModel,exports.QueryError=QueryError,exports.ValidationError=ValidationError,exports.default=Induct,exports.inductModelFactory=inductModelFactory;
//# sourceMappingURL=/sm/3980d35b6cf4f0781e16ab2a05534703a17060075dfc3197a02ff9a6cb694161.map