/* @alwatr/storage-engine v4.2.1 */ "use strict";var f=Object.create;var l=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var c=Object.getPrototypeOf,w=Object.prototype.hasOwnProperty;var A=(t,e,r)=>e in t?l(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var D=(t,e)=>{for(var r in e)l(t,r,{get:e[r],enumerable:!0})},u=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of y(e))!w.call(t,n)&&n!==r&&l(t,n,{get:()=>e[n],enumerable:!(s=v(e,n))||s.enumerable});return t};var S=(t,e,r)=>(r=t!=null?f(c(t)):{},u(e||!t||!t.__esModule?l(r,"default",{value:t,enumerable:!0}):r,t)),T=t=>u(l({},"__esModule",{value:!0}),t);var a=(t,e,r)=>(A(t,typeof e!="symbol"?e+"":e,r),r);var M={};D(M,{AlwatrStorageEngine:()=>g,AlwatrStorageEngineProvider:()=>d});module.exports=T(M);var m=require("path"),h=require("@alwatr/logger"),i=require("@alwatr/util/node.js"),_=S(require("exit-hook"),1);(0,h.definePackage)("storage-engine","4.*");var o=class o{constructor(e){a(this,"name");a(this,"storagePath");a(this,"saveDebounce");a(this,"saveBeautiful");a(this,"hasUnsavedChanges",!1);a(this,"_storage");a(this,"_logger");a(this,"_keys",null);a(this,"_saveTimer",null);this._logger=(0,h.createLogger)(`alwatr-storage:${e.name}`,e.devMode),this._logger.logMethodArgs?.("constructor",e),this._$save=this._$save.bind(this),this.name=e.name,this.storagePath=(0,m.resolve)(`${e.path??"./db"}/${e.name}.json`),this.saveDebounce=e.saveDebounce??1e3,this.saveBeautiful=e.saveBeautiful||!1,(0,_.default)(()=>this._$save(!0)),this._storage=this.load(),this._storage.meta?.formatVersion!==o.formatVersion&&(this._migrateStorage(),this.save())}get keys(){return this._keys===null&&(this._keys=Object.keys(this._storage.data)),this._keys}get length(){return this.keys.length}_nextAutoIncrementId(){this._storage.meta.lastAutoId;do this._storage.meta.lastAutoId++;while(this._storage.data[this._storage.meta.lastAutoId.toString()]!=null);return this._storage.meta.lastAutoId.toString()}get _newStorage(){return{ok:!0,meta:{id:this.name,formatVersion:o.formatVersion,reversion:0,lastUpdated:Date.now(),lastAutoId:-1},data:{}}}load(){this._logger.logMethodArgs?.("load",{name:this.name,path:this.storagePath});let e=(0,i.readJsonFileSync)(this.storagePath);if(e===null)return this._logger.incident?.("load","file_not_found","Storage path not found, empty storage loaded",{path:this.storagePath}),this.save(),this._newStorage;if(e.ok!==!0)throw this._logger.error("load","invalid_storage_data",{storage:e}),new Error("invalid_storage_data");return e}_migrateStorage(){if(this._storage.meta==null&&(this._storage.meta={id:this.name,formatVersion:5,reversion:0,lastUpdated:Date.now(),lastAutoId:-1}),this._storage.meta.formatVersion===4&&(this._storage.meta.id=this.name,this._storage.meta.formatVersion=5),this._storage.meta.formatVersion!==o.formatVersion)throw this._logger.error("load","storage_version_incompatible",{storageMeta:this._storage.meta}),new Error("storage_version_incompatible")}has(e){return this._storage.data[e]!=null}get(e,r){this._logger.logMethodArgs?.("get",e);let s=this._storage.data[e];return typeof s=="string"?this.get(s):s==null?null:r?s:JSON.parse(JSON.stringify(s))}set(e,r){this._logger.logMethodArgs?.("set",e.id),r!==!0&&(e=JSON.parse(JSON.stringify(e))),e.id==="auto_increment"&&(e.id=this._nextAutoIncrementId());let s=this._storage.data[e.id];return s==null&&(this._keys=null),e.meta??(e.meta={rev:0,updated:0,created:0}),e.meta.updated=Date.now(),e.meta.created=s?.meta?.created??e.meta.updated,e.meta.rev=(s?.meta?.rev??0)+1,this._storage.meta.lastUpdated=e.meta.updated,this._storage.data[e.id]=e,this.save(),e}delete(e){return this._logger.logMethodArgs?.("delete",e),this._storage.data[e]==null?!1:(delete this._storage.data[e],this._keys=null,this.save(),!0)}*allObject(){for(let e of this.keys){let r=this.get(e);r!=null&&(yield r)}this.save()}save(){this._logger.logMethod?.("save"),this.hasUnsavedChanges=!0,this._saveTimer==null&&(this._saveTimer=setTimeout(this._$save,this.saveDebounce))}_$save(e=!1){if(this._logger.logMethod?.("_$save"),this._saveTimer!=null&&(clearTimeout(this._saveTimer),this._saveTimer=null),this.hasUnsavedChanges)return this.hasUnsavedChanges=!1,this._storage.meta.reversion++,e?(0,i.writeJsonFileSync)(this.storagePath,this._storage,"rename",this.saveBeautiful?2:void 0):(0,i.writeJsonFile)(this.storagePath,this._storage,"replace",this.saveBeautiful?2:void 0)}unload(){this._logger.logMethod?.("unload"),this._$save(),this._keys=null,this._storage=this._newStorage}};a(o,"formatVersion",5);var g=o;var p=require("@alwatr/logger"),d=class{constructor(e){this._config=e;a(this,"_logger",(0,p.createLogger)("alwatr/storage-provider",this._config.devMode));a(this,"_list",{});this._logger.logMethodArgs?.("constructor",e)}get(e){return this._logger.logMethodArgs?.("get",{name:e.name}),this._list[e.name]||(this._logger.incident?.("get","new_storage","Create new storage engine",{name:e.name}),this._list[e.name]=new g({...this._config,...e}),console.log("Memory usage: %sMB",Math.round(process.memoryUsage.rss()/1e5)/10)),this._list[e.name]}unload(e){if(this._logger.logMethodArgs?.("unload",{name:e}),this._list[e]==null){this._logger.accident("unload","storage_not_found","Storage not defined or unloaded before",{name:e});return}this._list[e].unload(),delete this._list[e]}unloadAll(){for(let e in this._list)Object.prototype.hasOwnProperty.call(this._list,e)&&(this._list[e].unload(),delete this._list[e]);this._list={}}};0&&(module.exports={AlwatrStorageEngine,AlwatrStorageEngineProvider}); //# sourceMappingURL=main.cjs.map