UNPKG

umzug-hyper-storage

Version:

A zero-dependency Storage Adapter for Umzug that uses a Hyper Data Service to track migrations

3 lines (2 loc) 2.16 kB
function t(){return t=Object.assign||function(t){for(var i=1;i<arguments.length;i++){var e=arguments[i];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t},t.apply(this,arguments)}class i extends Error{constructor({msg:t},i="An Error Occurred"){super(t||i)}}class e{constructor(t){var i;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!=(i=t.doc)?i:{};const{hyper:e,doc:{id:r,type:o,typeField:a,createdField:n,updatedField:d}={}}=t;this.hyper=e,this.docType=null!=o?o:"__scripts",this.docId=null!=r?r:"hyper-scripts-meta",this.docTypeField=null!=a?a:"type",this.updatedField=null!=d?d:"updatedAt",this.createdField=null!=n?n:"createdAt"}async logMigration({name:t}){try{const i=await this._findOrCreateMigrationDoc();i.migrations=[...i.migrations,t],await this._updateMigrationDoc(i)}catch(i){throw new Error(`Could not log migration ${t}: ${i.message}`)}}async unlogMigration({name:t}){try{const i=await this._findOrCreateMigrationDoc(),e=i.migrations.indexOf(t);i.migrations=[...i.migrations],i.migrations.splice(e,1),await this._updateMigrationDoc(i)}catch(i){throw new Error(`Could not unlog migration ${t}: ${i.message}`)}}async executed(){return(await this._findOrCreateMigrationDoc()).migrations}async _findOrCreateMigrationDoc(){const t=await this.hyper.data.get(this.docId);let e;if(!1===t.ok){if(404!==t.status)throw new i(t,"Could not retrieve migration doc");e=void 0}else e=t.doc||t;if(e)return e;const r={_id:this.docId,[this.docTypeField]:this.docType,migrations:[],[this.createdField]:new Date,[this.updatedField]:new Date};let o=await this.hyper.data.add(r);if(!o.ok&&o.status&&o.status>=400)throw new i(o,"Could not create migration doc");return r}async _updateMigrationDoc(e){const r=t({},e,{[this.updatedField]:new Date}),o=await this.hyper.data.update(e._id,r);if(!o.ok)throw new i(o,"Could not update migration doc");return r}}export{i as FromHyperError,e as HyperStorage}; //# sourceMappingURL=index.modern.js.map