@alwatr/nitrobase-engine
Version:
Nitrobase is a blazingly fast, lightweight database built on JSON. It stores data entirely in memory for lightning-quick access, while also providing a JSON file backup for persistence. You can easily serve your data over the web using our high-performanc
6 lines (4 loc) • 5.88 kB
JavaScript
/* 📦 @alwatr/nitrobase-engine v10.0.0 */
import{delay as H}from"@alwatr/delay";import{exitHook as k}from"@alwatr/exit-hook";import{getStoreId as U,getStorePath as X}from"@alwatr/nitrobase-helper";import{CollectionReference as V,DocumentReference as Y}from"@alwatr/nitrobase-reference";import{StoreFileType as O,StoreFileExtension as L,Region as D}from"@alwatr/nitrobase-types";import{existsSync as A,readJson as $,resolve as W,unlink as J,writeJson as j}from"@alwatr/node-fs";import{createLogger as G}from"@alwatr/logger";var B=G("@alwatr/nitrobase-engine");B.logFileModule?.("alwatr-nitrobase");class Z{config;static version="10.0.0";static rootDbStat__={name:".nitrobase",region:D.Secret,type:O.Collection,extension:L.Json,changeDebounce:40};rootDb__;cacheReferences__={};constructor(q){this.config=q;this.storeChanged_=this.storeChanged_.bind(this),B.logMethodArgs?.("new",q),this.config.defaultChangeDebounce??=40,this.rootDb__=this.loadRootDb__(),k(this.exitHook__.bind(this))}hasStore(q){let z=U(q),K=this.rootDb__.hasItem(z);return B.logMethodFull?.("hasStore",z,K),K}newDocument(q,z){return B.logMethodArgs?.("newDocument",q),this.newStoreFile__({...q,type:O.Document},z)}newCollection(q){return B.logMethodArgs?.("newCollection",q),this.newStoreFile__({...q,type:O.Collection})}newStoreFile__(q,z){B.logMethodArgs?.("newStoreFile__",q),q.changeDebounce??=this.config.defaultChangeDebounce;let K;if(q.type===O.Document){if(z===void 0)throw B.accident("newStoreFile__","document_data_required",q),Error("document_data_required",{cause:q});K=Y.newRefFromData(q,z,this.storeChanged_)}else if(q.type===O.Collection)K=V.newRefFromData(q,this.storeChanged_);else throw B.accident("newStoreFile__","store_file_type_not_supported",q),Error("store_file_type_not_supported",{cause:q});if(this.rootDb__.hasItem(K.id))throw B.accident("newStoreFile__","store_file_already_defined",q),Error("store_file_already_defined",{cause:q});this.rootDb__.addItem(K.id,q),this.cacheReferences__[K.id]=K,this.storeChanged_(K)}async openDocument(q){let z=U(q);if(B.logMethodArgs?.("openDocument",z),Object.hasOwn(this.cacheReferences__,z)){if(!(this.cacheReferences__[z]instanceof Y))throw B.accident("openDocument","document_wrong_type",z),Error("document_wrong_type",{cause:z});return this.cacheReferences__[z]}if(!this.rootDb__.hasItem(z))throw B.accident("openDocument","document_not_found",z),Error("document_not_found",{cause:z});let K=this.rootDb__.getItemData(z);if(K.type!=O.Document)throw B.accident("openDocument","document_wrong_type",z),Error("document_wrong_type",{cause:z});let Q=await this.readContext__(K),M=Y.newRefFromContext(Q,this.storeChanged_);return this.cacheReferences__[z]=M,M}async openCollection(q){let z=U(q);if(B.logMethodArgs?.("openCollection",z),Object.hasOwn(this.cacheReferences__,z)){if(!(this.cacheReferences__[z]instanceof V))throw B.accident("openCollection","collection_wrong_type",z),Error("collection_wrong_type",{cause:z});return this.cacheReferences__[z]}if(!this.rootDb__.hasItem(z))throw B.accident("openCollection","collection_not_found",z),Error("collection_not_found",{cause:z});let K=this.rootDb__.getItemData(z);if(K.type!=O.Collection)throw B.accident("openCollection","collection_wrong_type",z),Error("collection_not_found",{cause:z});let Q=await this.readContext__(K),M=V.newRefFromContext(Q,this.storeChanged_);return this.cacheReferences__[z]=M,M}unloadStore(q){let z=U(q);B.logMethodArgs?.("unloadStore",z);let K=this.cacheReferences__[z];if(K===void 0)return;if(K.hasUnprocessedChanges_===!0)K.updateDelayed_=!1,this.storeChanged_(K);delete this.cacheReferences__[z]}async removeStore(q){let z=U(q);if(B.logMethodArgs?.("removeStore",z),!this.rootDb__.hasItem(z))throw B.accident("removeStore","document_not_found",z),Error("document_not_found",{cause:z});let K=this.cacheReferences__[z];if(K!==void 0)K.freeze=!0,K.updateDelayed_=!1,K.hasUnprocessedChanges_=!1,delete this.cacheReferences__[z];let Q=X(this.rootDb__.getItemData(z));this.rootDb__.removeItem(z),await H.by(0);try{await J(W(this.config.rootPath,Q))}catch(M){B.error("removeStore","remove_file_failed",M,{id:q,path:Q})}}async saveAll(){B.logMethod?.("saveAll");for(let q of Object.values(this.cacheReferences__))if(q.hasUnprocessedChanges_===!0&&q.freeze!==!0)q.updateDelayed_=!1,await this.storeChanged_(q)}async readContext__(q){if(typeof q!=="string")q=X(q);B.logMethodArgs?.("readContext__",q),B.time?.(`readContext__time(${q})`);let z=await $(W(this.config.rootPath,q));return B.timeEnd?.(`readContext__time(${q})`),z}writeContext__(q,z){if(typeof q!=="string")q=X(q);return B.logMethodArgs?.("writeContext__",q),j(W(this.config.rootPath,q),z)}async storeChanged_(q){B.logMethodArgs?.("storeChanged__",q.id);let z=q.getStoreMeta().rev;try{if(await this.writeContext__(q.path,q.getFullContext_()),z===q.getStoreMeta().rev)q.hasUnprocessedChanges_=!1}catch(K){B.error("storeChanged__","write_context_failed",{id:q.id,error:K})}}loadRootDb__(){B.logMethod?.("loadRootDb__");let q=W(this.config.rootPath,X(Z.rootDbStat__));if(!A(q)){if(this.config.errorWhenNotInitialized===!0)throw Error("store_not_found",{cause:"Nitrobase not initialized"});return B.banner("Initialize new alwatr-nitrobase"),V.newRefFromData(Z.rootDbStat__,this.storeChanged_)}let z=$(q,!0);return V.newRefFromContext(z,this.storeChanged_,"root-db")}exitHook__(){B.logMethod?.("exitHook__");for(let q of Object.values(this.cacheReferences__))if(B.logProperty?.(`StoreFile.${q.id}.hasUnprocessedChanges`,q.hasUnprocessedChanges_),q.hasUnprocessedChanges_===!0&&q.freeze!==!0)B.incident?.("exitHook__","rescue_unsaved_context",{id:q.id}),j(W(this.config.rootPath,q.path),q.getFullContext_(),!0),q.hasUnprocessedChanges_=!1}getStoreList(){return B.logMethod?.("getStoreList"),this.rootDb__.values()}}export{Z as AlwatrNitrobase};
//# debugId=4655BB1D91E5512A64756E2164756E21
//# sourceMappingURL=main.js.map