@narodb/naro
Version:
A simple, fast, and lightweight NoSQL database
3 lines (2 loc) • 11.1 kB
JavaScript
import e from"lodash";import{decode as t,encode as r}from"notepack.io";import n from"fs-jetpack";import o from"redaxios";function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function c(){return c=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},c.apply(null,arguments)}var s=/*#__PURE__*/function(){function e(){}return e.ensureDirectory=function(e){n.dir(e)},e.readBinaryFile=function(e){var r=n.read(e,"buffer");return r?t(r):[]},e.writeBinaryFile=function(e,t){return n.write(e,r(t))},e.listDirectories=function(e){var t=n.inspectTree(e);return t?t.children.filter(function(e){return"dir"===e.type}).map(function(e){return e.name}):[]},e.removeDirectory=function(e){return n.remove(e)},e}(),u=/*#__PURE__*/function(){function t(e){this.rootPath=void 0,this.collections={},this.logFileName="data.bin",this.rootPath=e}var r=t.prototype;return r.initialize=function(){s.ensureDirectory(this.rootPath),this.loadCollections()},r.getStructuredCollections=function(){return this.collections},r.loadCollections=function(){for(var e,t=function(e){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,t){if(e){if("string"==typeof e)return i(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?i(e,t):void 0}}(e))){t&&(e=t);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.")}(s.listDirectories(this.rootPath));!(e=t()).done;){var r=e.value,o=this.rootPath+"/"+r+"/"+this.logFileName;n.file(o),this.collections[r]=s.readBinaryFile(o)}return this.collections},r.getCollection=function(e){return this.collections[e]?this.collections[e]:this.collections[e]=[]},r.updateCollection=function(t,r){this.collections[t]=e.cloneDeep(r)},r.writeCollections=function(){var e=this;Object.keys(this.collections).forEach(function(t){s.writeBinaryFile(e.rootPath+"/"+t+"/"+e.logFileName,e.collections[t])})},r.writeCollection=function(e){var t=this.rootPath+"/"+e;s.ensureDirectory(t),s.writeBinaryFile(t+"/"+this.logFileName,this.collections[e])},r.removeCollection=function(e){s.removeDirectory(this.rootPath+"/"+e),delete this.collections[e]},t}(),l=/*#__PURE__*/function(){function e(){}return e.validate=function(e){if(/[^a-zA-Z0-9/_-]/.test(e)||e.includes("//"))throw new Error("Path contains invalid characters or consecutive slashes.");if(e.endsWith("/"))throw new Error("Path should not end with a '/'.");if(!e)throw new Error("Empty path provided.");var t=e.split("/");if(t.length>4)throw new Error("Invalid path format. Ensure the path is in the format 'collectionName/id/subCollectionName/id'.");return{collectionName:t[0],collectionId:t[1],subCollectionName:t[2],subCollectionId:t[3]}},e}(),a=/*#__PURE__*/function(){function e(){}return e.generate=function(){return Date.now().toString(36)+Math.random().toString(36).slice(2,11)},e}(),f=/*#__PURE__*/function(){function t(e,t){if(this.dbName=void 0,this.core=void 0,this.host=void 0,this.orgId=void 0,this.projectId=void 0,null!=t&&t.URI){var r=t.URI.split(";"),n=r[1],o=r[2];if(this.host=r[0],this.orgId=n,this.projectId=o,!this.host||!this.orgId||!this.projectId)throw new Error("Invalid URI format.")}else{if(e.includes("/"))throw new Error("dbName cannot contain '/'");this.dbName=e;var i=process.cwd()+"/data/"+this.dbName;this.core=new u(i),this.core.initialize(),this.registerProcessListeners()}}var r=t.prototype;return r.registerProcessListeners=function(){var e=this,t=function(t){try{return e.core.writeCollections(),process.removeAllListeners("exit"),process.removeAllListeners("SIGINT"),process.removeAllListeners("SIGTERM"),void 0!==t&&process.exit(t),Promise.resolve()}catch(e){return Promise.reject(e)}};process.setMaxListeners(Infinity),process.on("exit",t),process.on("SIGINT",function(){return t(0)}),process.on("SIGTERM",function(){return t(0)})},r.writeToDisk=function(){try{return this.core.writeCollections(),Promise.resolve()}catch(e){return Promise.reject(e)}},r.add=function(t,r){try{var n,o=function(t){if(n)return t;var o=i.core.getCollection(c),s=a.generate(),u={id:s,createdAt:Date.now(),path:c+"/"+s},l=Object.assign(r,u);return o.push(l),i.core.updateCollection(c,o),e.cloneDeep(l)},i=this,c=l.validate(t).collectionName,s=function(){if(i.host)return Promise.resolve(i.serverRequest("add",[t,r])).then(function(e){return n=1,e})}();return Promise.resolve(s&&s.then?s.then(o):o(s))}catch(e){return Promise.reject(e)}},r.serverRequest=function(e,t){var r=this;return Promise.resolve(function(){try{var n=Promise.resolve(o.post(r.host,{orgId:r.orgId,projectId:r.projectId,method:e,params:t})).then(function(e){return e.data})}catch(e){return e}return n&&n.then?n.then(void 0,function(e){return e}):n}())},r.set=function(t,r){try{var n,o=function(t){if(n)return t;var o=i.core.getCollection(s),c={path:s+"/"+u,createdAt:Date.now(),id:u},l=Object.assign(r,c),a=e.findIndex(o,function(e){return e.id===u});return-1!==a?o[a]=l:o.push(l),i.core.updateCollection(s,o),e.cloneDeep(l)},i=this,c=l.validate(t),s=c.collectionName,u=c.collectionId,a=c.subCollectionName,f=c.subCollectionId;if(!u)throw new Error("Collection ID is required");if(a)throw new Error("Sub-collection is not supported in set method");if(f)throw new Error("Sub-collection ID is not supported in set method");var h=function(){if(i.host)return Promise.resolve(i.serverRequest("set",[t,r])).then(function(e){return n=1,e})}();return Promise.resolve(h&&h.then?h.then(o):o(h))}catch(e){return Promise.reject(e)}},r.getAll=function(t,r){void 0===r&&(r={});try{var n,o=function(t){if(n)return t;var o=e.cloneDeep(i.core.getCollection(c)),s=r.limit,u=r.populate,l=r.offset,a=void 0===l?0:l,f=o;return f=(f=i.filterCollection(f,r.filters||[])).slice(a),f=i.limitDocuments(f,s),Promise.resolve(i.populateCollection(f,u)).then(function(e){return f=e})},i=this,c=l.validate(t).collectionName,s=function(){if(i.host)return Promise.resolve(i.serverRequest("getAll",[t,r])).then(function(e){return n=1,e})}();return Promise.resolve(s&&s.then?s.then(o):o(s))}catch(e){return Promise.reject(e)}},r.filterCollection=function(e,t){if(!t.every(function(e){return["==","!=","<","<=",">",">="].includes(e.operator)}))throw new Error("Invalid operator in filter");return e.filter(function(e){return t.every(function(t){var r=t.value,n=e[t.field];switch(t.operator){case"==":return n==r;case"!=":return n!=r;case"<":return"number"==typeof n&&"number"==typeof r&&!Number.isNaN(n)&&!Number.isNaN(r)&&n<r;case"<=":return"number"==typeof n&&"number"==typeof r&&!Number.isNaN(n)&&!Number.isNaN(r)&&n<=r;case">":return"number"==typeof n&&"number"==typeof r&&!Number.isNaN(n)&&!Number.isNaN(r)&&n>r;case">=":return"number"==typeof n&&"number"==typeof r&&!Number.isNaN(n)&&!Number.isNaN(r)&&n>=r}})})},r.populate=function(t,r){try{var n,o=function(o){return n?o:Promise.resolve(Promise.all(r.map(function(e){try{var r=t[e],n=function(){if("string"==typeof r)return Promise.resolve(i.get(r)).then(function(r){r&&(t[e]=r)})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return e.cloneDeep(t)})},i=this;if(!r)return Promise.resolve(t);if(!r.length)throw new Error("Populate fields cannot be an empty array");var c=function(){if(i.host)return Promise.resolve(i.serverRequest("populate",[t,r])).then(function(e){return n=1,e})}();return Promise.resolve(c&&c.then?c.then(o):o(c))}catch(e){return Promise.reject(e)}},r.populateCollection=function(e,t){try{var r=this;return t&&0!==t.length?Promise.all(e.map(function(e){return r.populate(e,t)})):Promise.resolve(e)}catch(e){return Promise.reject(e)}},r.limitDocuments=function(e,t){return void 0===t?e:e.slice(0,t)},r.get=function(t){try{var r,n=function(t){if(r)return t;var n=o.core.getCollection(c);return e.find(n,function(e){return e.id===s})||void 0},o=this,i=l.validate(t),c=i.collectionName,s=i.collectionId,u=function(){if(o.host)return Promise.resolve(o.serverRequest("get",[t])).then(function(e){return r=1,e})}();return Promise.resolve(u&&u.then?u.then(n):n(u))}catch(e){return Promise.reject(e)}},r.update=function(t,r){try{var n,o=function(t){if(n)return t;var o=i.core.getCollection(u),s=e.findIndex(o,function(e){return e.id===a});if(-1===s)throw new Error("Item not found");var l=o[s],f=e.omit(r,["id","path","createdAt"]);return o[s]=c({},l,f),i.core.updateCollection(u,o),o[s]},i=this,s=l.validate(t),u=s.collectionName,a=s.collectionId;if(!a)throw new Error("Collection ID is required");var f=function(){if(i.host)return Promise.resolve(i.serverRequest("update",[t,r])).then(function(e){return n=1,e})}();return Promise.resolve(f&&f.then?f.then(o):o(f))}catch(e){return Promise.reject(e)}},r.delete=function(t){try{var r,n=function(t){if(r)return t;var n=o.core.getCollection(c),i=e.findIndex(n,function(e){return e.id===s});-1!==i&&n[i]&&(n.splice(i,1),o.core.updateCollection(c,n))},o=this,i=l.validate(t),c=i.collectionName,s=i.collectionId;if(!s)throw new Error("Collection ID is required");var u=function(){if(o.host)return Promise.resolve(o.serverRequest("delete",[t])).then(function(e){return r=1,e})}();return Promise.resolve(u&&u.then?u.then(n):n(u))}catch(e){return Promise.reject(e)}},r.exists=function(t){try{var r,n=function(t){if(r)return t;var n=o.core.getCollection(c);return e.some(n,function(e){return e.id===s})},o=this,i=l.validate(t),c=i.collectionName,s=i.collectionId;if(!s)throw new Error("Collection ID is required");var u=function(){if(o.host)return Promise.resolve(o.serverRequest("exists",[t])).then(function(e){return r=1,e})}();return Promise.resolve(u&&u.then?u.then(n):n(u))}catch(e){return Promise.reject(e)}},r.count=function(e){try{var t,r=function(e){return t?e:n.core.getCollection(o).length},n=this,o=l.validate(e).collectionName,i=function(){if(n.host)return Promise.resolve(n.serverRequest("count",[e])).then(function(e){return t=1,e})}();return Promise.resolve(i&&i.then?i.then(r):r(i))}catch(e){return Promise.reject(e)}},r.clear=function(e){try{var t,r=function(e){if(t)return e;n.core.removeCollection(i)},n=this,o=l.validate(e),i=o.collectionName;if(o.collectionId)throw new Error("Collection ID detected. Use delete method instead.");var c=function(){if(n.host)return Promise.resolve(n.serverRequest("clear",[e])).then(function(e){return t=1,e})}();return Promise.resolve(c&&c.then?c.then(r):r(c))}catch(e){return Promise.reject(e)}},r.getStructuredCollections=function(){try{var e=this;return Promise.resolve(e.host?e.serverRequest("getStructuredCollections",[]):e.core.getStructuredCollections())}catch(e){return Promise.reject(e)}},t}();export{f as Naro};
//# sourceMappingURL=index.modern.mjs.map