umzug-hyper-storage
Version:
A zero-dependency Storage Adapter for Umzug that uses a Hyper Data Service to track migrations
3 lines (2 loc) • 4.22 kB
JavaScript
function t(){return t=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},t.apply(this,arguments)}function e(t){return e=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},e(t)}function r(t,e){return r=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},r(t,e)}function n(t,e,o){return n=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(t){return!1}}()?Reflect.construct:function(t,e,n){var o=[null];o.push.apply(o,e);var i=new(Function.bind.apply(t,o));return n&&r(i,n.prototype),i},n.apply(null,arguments)}function o(t){var i="function"==typeof Map?new Map:void 0;return o=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==i){if(i.has(t))return i.get(t);i.set(t,o)}function o(){return n(t,arguments,e(this).constructor)}return o.prototype=Object.create(t.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),r(o,t)},o(t)}function i(t,e){try{var r=t()}catch(t){return e(t)}return r&&r.then?r.then(void 0,e):r}var c=/*#__PURE__*/function(t){var e,n;function o(e,r){return void 0===r&&(r="An Error Occurred"),t.call(this,e.msg||r)||this}return n=t,(e=o).prototype=Object.create(n.prototype),e.prototype.constructor=e,r(e,n),o}(/*#__PURE__*/o(Error)),u=/*#__PURE__*/function(){function e(t){var e;if(this.hyper=void 0,this.docId=void 0,this.docType=void 0,this.docTypeField=void 0,this.createdField=void 0,this.updatedField=void 0,!t.hyper)throw new Error("hyper is a required option and must be an instance of hyper-connect");t.doc=null!=(e=t.doc)?e:{};var r=t.doc,n=(r=void 0===r?{}:r).id,o=r.type,i=r.typeField,c=r.createdField,u=r.updatedField;this.hyper=t.hyper,this.docType=null!=o?o:"__scripts",this.docId=null!=n?n:"hyper-scripts-meta",this.docTypeField=null!=i?i:"type",this.updatedField=null!=u?u:"updatedAt",this.createdField=null!=c?c:"createdAt"}var r=e.prototype;return r.logMigration=function(t){var e=t.name;try{var r=this;return Promise.resolve(i(function(){return Promise.resolve(r._findOrCreateMigrationDoc()).then(function(t){return t.migrations=[].concat(t.migrations,[e]),Promise.resolve(r._updateMigrationDoc(t)).then(function(){})})},function(t){throw new Error("Could not log migration "+e+": "+t.message)}))}catch(t){return Promise.reject(t)}},r.unlogMigration=function(t){var e=t.name;try{var r=this;return Promise.resolve(i(function(){return Promise.resolve(r._findOrCreateMigrationDoc()).then(function(t){var n=t.migrations.indexOf(e);return t.migrations=[].concat(t.migrations),t.migrations.splice(n,1),Promise.resolve(r._updateMigrationDoc(t)).then(function(){})})},function(t){throw new Error("Could not unlog migration "+e+": "+t.message)}))}catch(t){return Promise.reject(t)}},r.executed=function(){try{return Promise.resolve(this._findOrCreateMigrationDoc()).then(function(t){return t.migrations})}catch(t){return Promise.reject(t)}},r._findOrCreateMigrationDoc=function(){try{var t=this;return Promise.resolve(t.hyper.data.get(t.docId)).then(function(e){var r,n;if(!1===e.ok){if(404!==e.status)throw new c(e,"Could not retrieve migration doc");n=void 0}else n=e.doc||e;if(n)return n;var o=((r={_id:t.docId})[t.docTypeField]=t.docType,r.migrations=[],r[t.createdField]=new Date,r[t.updatedField]=new Date,r);return Promise.resolve(t.hyper.data.add(o)).then(function(t){if(!t.ok&&t.status&&t.status>=400)throw new c(t,"Could not create migration doc");return o})})}catch(t){return Promise.reject(t)}},r._updateMigrationDoc=function(e){try{var r,n=t({},e,((r={})[this.updatedField]=new Date,r));return Promise.resolve(this.hyper.data.update(e._id,n)).then(function(t){if(!t.ok)throw new c(t,"Could not update migration doc");return n})}catch(t){return Promise.reject(t)}},e}();exports.FromHyperError=c,exports.HyperStorage=u;
//# sourceMappingURL=index.cjs.map