UNPKG

monguito

Version:

MongoDB Abstract Repository implementation for Node.js

3 lines (2 loc) 12.7 kB
var e=require("mongoose"),t=require("typescript-optional");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=/*#__PURE__*/n(e);function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function o(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(n)return(n=n.call(e)).next.bind(n);if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return i(e,t);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?i(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function u(){return u=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},u.apply(null,arguments)}function s(e){return s=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},s(e)}function a(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,l(e,t)}function c(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(c=function(){return!!e})()}function l(e,t){return l=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},l(e,t)}function d(e){var t="function"==typeof Map?new Map:void 0;return d=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,n)}function n(){return function(e,t,n){if(c())return Reflect.construct.apply(null,arguments);var r=[null];r.push.apply(r,t);var i=new(e.bind.apply(e,r));return n&&l(i,n.prototype),i}(e,arguments,s(this).constructor)}return n.prototype=Object.create(e.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),l(n,e)},d(e)}var f=function(e){return e&&"createdAt"in e&&"updatedAt"in e&&"createdBy"in e&&"updatedBy"in e},h=/*#__PURE__*/function(e){function t(t,n){var r;return(r=e.call(this,t,{cause:n})||this).name=r.constructor.name,Error.captureStackTrace&&Error.captureStackTrace(r,r.constructor),r}return a(t,e),t}(/*#__PURE__*/d(Error)),p=/*#__PURE__*/function(e){function t(t,n){return e.call(this,t,n)||this}return a(t,e),t}(h),v=/*#__PURE__*/function(e){function t(t,n){return e.call(this,t,n)||this}return a(t,e),t}(h),y=/*#__PURE__*/function(e){function t(t,n){return e.call(this,t,n)||this}return a(t,e),t}(h),m=/*#__PURE__*/function(e){function t(t,n){var r;return(r=e.call(this,t,n)||this).cause=void 0,r.cause=n,r}a(t,e);var n=t.prototype;return n.getInvalidFields=function(){return Object.keys(this.cause.errors)},n.getInvalidRequiredFields=function(){return this.getInvalidFieldsOfKind("required")},n.getInvalidUniqueFields=function(){return this.getInvalidFieldsOfKind("unique")},n.getInvalidFieldsOfKind=function(e){var t=[];for(var n in this.cause.errors){var r=this.cause.errors[n];r.kind===e&&t.push(r.path)}return t},t}(h),b=/*#__PURE__*/function(){function e(t){if(this.type=void 0,this.schema=void 0,this.subtypes=void 0,!t.type||!t.schema)throw new p("The given domain model must specify a type and a schema");this.type=t.type,this.schema=t.schema,this.subtypes=[];for(var n,r=o(null!=(i=t.subtypes)?i:[]);!(n=r()).done;){var i;this.subtypes.push(new e(n.value))}}var t=e.prototype;return t.getSubtypeTree=function(){return this.subtypes||[]},t.getSubtypeData=function(e){var t;return null==(t=this.subtypes)?void 0:t.find(function(t){return t.type.name===e})},t.getSubtypeConstructor=function(e){var t=this.getSubtypeData(e);return null==t?void 0:t.type},t.getSupertypeConstructor=function(){return this.type},t.getSupertypeName=function(){return this.type.name},t.has=function(e){return e===this.getSupertypeName()||!!this.getSubtypeData(e)},e}();function g(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var w=/*#__PURE__*/function(){function e(e,t){this.connection=void 0,this.domainTree=void 0,this.entityModel=void 0,this.connection=t,this.domainTree=new b(e),this.entityModel=this.createEntityModel(t)}var n=e.prototype;return n.createEntityModel=function(e){var t;t=e?e.model(this.domainTree.type.name,this.domainTree.schema):r.default.model(this.domainTree.type.name,this.domainTree.schema);for(var n,i=o(this.domainTree.getSubtypeTree());!(n=i()).done;){var u=n.value;t.discriminator(u.type.name,u.schema)}return t},n.findById=function(e,n){try{var r,i=this;if(!e)throw new p("The given ID must be valid");return Promise.resolve(i.entityModel.findById(e).session(null!=(r=null==n?void 0:n.session)?r:null).exec()).then(function(e){return t.Optional.ofNullable(i.instantiateFrom(e))})}catch(e){return Promise.reject(e)}},n.findOne=function(e){try{var n,r,i=this;return Promise.resolve(i.entityModel.findOne(null!=(n=null==e?void 0:e.filters)?n:void 0).session(null!=(r=null==e?void 0:e.session)?r:null).exec()).then(function(e){return t.Optional.ofNullable(i.instantiateFrom(e))})}catch(e){return Promise.reject(e)}},n.findAll=function(e){try{var t,n,r,i,o,u,s,a,c=this;if(null!=e&&null!=(t=e.pageable)&&t.pageNumber&&(null==e||null==(n=e.pageable)?void 0:n.pageNumber)<0)throw new p("The given page number must be a positive number");if(null!=e&&null!=(r=e.pageable)&&r.offset&&(null==e||null==(i=e.pageable)?void 0:i.offset)<0)throw new p("The given page offset must be a positive number");var l=null!=(o=null==e||null==(u=e.pageable)?void 0:u.offset)?o:0,d=null!=(s=null==e||null==(a=e.pageable)?void 0:a.pageNumber)?s:0;return Promise.resolve(g(function(){var t;return Promise.resolve(c.entityModel.find(null==e?void 0:e.filters).skip(d>0?(d-1)*l:0).limit(l).sort(null==e?void 0:e.sortBy).session(null!=(t=null==e?void 0:e.session)?t:null).exec()).then(function(e){return e.map(function(e){return c.instantiateFrom(e)})})},function(e){throw new p("The given optional parameters must be valid",e)}))}catch(e){return Promise.reject(e)}},n.save=function(e,t){try{if(!e)throw new p("The given entity cannot be null or undefined");return Promise.resolve(e.id?this.update(e,t):this.insert(e,t))}catch(e){return Promise.reject(e)}},n.insert=function(e,t){try{var n=this;if(!e)throw new p("The given entity cannot be null or undefined");return Promise.resolve(g(function(){var r=n.createDocumentForInsertion(e,t);return Promise.resolve(r.save({session:null==t?void 0:t.session})).then(function(e){return n.instantiateFrom(e)})},function(e){throw e instanceof r.default.Error.ValidationError?new m("One or more fields of the given entity do not specify valid values",e):e}))}catch(e){return Promise.reject(e)}},n.createDocumentForInsertion=function(e,t){this.setDiscriminatorKeyOnEntity(e);var n=new this.entityModel(e);return this.setAuditableDataOnDocumentToInsert(n,e,null==t?void 0:t.userId),n},n.setDiscriminatorKeyOnEntity=function(e){var t=e.constructor.name;if(!this.domainTree.has(t))throw new p("The entity with name "+t+" is not included in the setup of the custom repository");t!==this.domainTree.getSupertypeName()&&!("__t"in e)&&(e.__t=t)},n.setAuditableDataOnDocumentToInsert=function(e,t,n){f(t)&&(n&&(e.$locals.userId=n),e.__v=0)},n.update=function(e,t){try{var n,i=this;if(!e)throw new p("The given entity must be valid");return Promise.resolve(i.entityModel.findById(e.id).session(null!=(n=null==t?void 0:t.session)?n:null)).then(function(n){var o;function u(t){if(o)return t;throw new p("There is no document matching the given ID '"+e.id+"'")}var s=function(){if(n)return g(function(){return n.set(e),i.setAuditableDataOnDocumentToUpdate(n,null==t?void 0:t.userId),Promise.resolve(n.save({session:null==t?void 0:t.session})).then(function(e){var t=i.instantiateFrom(e);return o=1,t})},function(e){throw e instanceof r.default.Error.ValidationError?new m("One or more fields of the given entity do not specify valid values",e):e})}();return s&&s.then?s.then(u):u(s)})}catch(e){return Promise.reject(e)}},n.setAuditableDataOnDocumentToUpdate=function(e,t){var n;e.isNew=!1,f(e)&&(t&&(e.$locals.userId=t),e.__v=(null!=(n=e.__v)?n:0)+1)},n.deleteById=function(e,t){try{if(!e)throw new p("The given ID must be valid");return Promise.resolve(this.entityModel.findByIdAndDelete(e,{session:null==t?void 0:t.session})).then(function(e){return!!e})}catch(e){return Promise.reject(e)}},n.instantiateFrom=function(e){if(!e)return null;var t=e.get("__t"),n=t?this.domainTree.getSubtypeConstructor(t):this.domainTree.getSupertypeConstructor();if(n)try{return new n(e.toObject())}catch(t){throw new v("An error occurred while instantiating an entity with ID "+e.id,t)}throw new y("There is no registered instance constructor for the document with ID "+e.id+" or the corresponding entity type is abstract")},e}(),P=function(e,t,n){return Promise.resolve(function(e){try{return Promise.resolve(e?e.startSession():r.default.connection.startSession())}catch(e){return Promise.reject(e)}}(n)).then(function(r){return r.startTransaction(),function(i,o){try{var u=function(t,n){try{var i=Promise.resolve(e(r)).then(function(e){return Promise.resolve(r.commitTransaction()).then(function(){return e})})}catch(e){return n(e)}return i&&i.then?i.then(void 0,n):i}(0,function(i){return Promise.resolve(r.abortTransaction()).then(function(){if(function(e){return e.message.includes("does not match any in-progress transactions")}(i)&&t<I)return P(e,++t,n);throw i})})}catch(e){return o(!0,e)}return u&&u.then?u.then(o.bind(null,!1),o.bind(null,!0)):o(!1,u)}(0,function(e,t){if(r.endSession(),e)throw t;return t})})},T=function(e,t){try{return Promise.resolve(null!=t&&t.session?e(t.session):P(e,0,null==t?void 0:t.connection))}catch(e){return Promise.reject(e)}},I=3,O=/*#__PURE__*/function(e){function t(t,n){return e.call(this,t,n)||this}a(t,e);var n=t.prototype;return n.saveAll=function(e,t){try{var n=this;return Promise.resolve(T(function(r){try{return Promise.resolve(Promise.all(e.map(function(e){try{return Promise.resolve(n.save(e,{userId:null==t?void 0:t.userId,session:r}))}catch(e){return Promise.reject(e)}})))}catch(e){return Promise.reject(e)}},u({},t,{connection:n.connection})))}catch(e){return Promise.reject(e)}},n.deleteAll=function(e){try{var t=this;return Promise.resolve(T(function(n){try{return Promise.resolve(t.entityModel.deleteMany(null==e?void 0:e.filters,{session:n})).then(function(e){return e.deletedCount})}catch(e){return Promise.reject(e)}},u({},e,{connection:t.connection})))}catch(e){return Promise.reject(e)}},n.update=function(t,n){try{var r=e.prototype.update.bind(this);return Promise.resolve(T(function(e){try{var i;return Promise.resolve(r(t,{userId:null==n?void 0:n.userId,session:null!=(i=null==n?void 0:n.session)?i:e}))}catch(e){return Promise.reject(e)}},{connection:this.connection}))}catch(e){return Promise.reject(e)}},t}(w),j=new e.Schema({},{toObject:{transform:function(e,t){t.id=e.id,delete t._id}}}),S=A(j,{createdBy:{type:String},updatedBy:{type:String}},{timestamps:!0,toObject:{transform:function(e,t){t.id=e.id,t.version=e.__v,delete t._id,delete t.__v}},plugins:[{fn:function(e){e.pre("save",function(e){this.$locals.userId&&(this.createdBy||(this.createdBy=this.$locals.userId),this.updatedBy=this.$locals.userId),delete this.$locals.userId,e()})}}]});function A(t,n,r){var i=n instanceof e.Schema,o=new e.Schema(u({},t.obj,i?n.obj:n),u({},t.options,i?n.options:r));return function(t,n,r,i){n.plugins.forEach(function(e){t.plugin(e.fn,e.options)}),r instanceof e.Schema&&r.plugins.forEach(function(e){t.plugin(e.fn,e.options)}),null!=i&&i.plugins&&i.plugins.forEach(function(e){t.plugin(e.fn,e.options)})}(o,t,n,r),o}exports.AuditableClass=function(e){this.createdAt=void 0,this.createdBy=void 0,this.updatedAt=void 0,this.updatedBy=void 0,this.version=void 0,this.createdAt=e.createdAt?new Date(e.createdAt):void 0,this.createdBy=e.createdBy,this.updatedAt=e.updatedAt?new Date(e.updatedAt):void 0,this.updatedBy=e.updatedBy,this.version=e.version},exports.AuditableSchema=S,exports.BaseSchema=j,exports.IllegalArgumentException=p,exports.MongooseRepository=w,exports.MongooseTransactionalRepository=O,exports.UndefinedConstructorException=y,exports.ValidationException=m,exports.extendSchema=A,exports.isAuditable=f,exports.runInTransaction=T; //# sourceMappingURL=repo.cjs.map