@narodb/naro
Version:
A simple, fast, and lightweight NoSQL database
3 lines (2 loc) • 11.3 kB
JavaScript
var e=require("lodash"),t=require("notepack.io"),r=require("fs-jetpack"),n=require("redaxios");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=/*#__PURE__*/o(e),c=/*#__PURE__*/o(r),u=/*#__PURE__*/o(n);function s(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 l(){return l=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},l.apply(null,arguments)}var a=/*#__PURE__*/function(){function e(){}return e.ensureDirectory=function(e){c.default.dir(e)},e.readBinaryFile=function(e){var r=c.default.read(e,"buffer");return r?t.decode(r):[]},e.writeBinaryFile=function(e,r){return c.default.write(e,t.encode(r))},e.listDirectories=function(e){var t=c.default.inspectTree(e);return t?t.children.filter(function(e){return"dir"===e.type}).map(function(e){return e.name}):[]},e.removeDirectory=function(e){return c.default.remove(e)},e}(),f=/*#__PURE__*/function(){function e(e){this.rootPath=void 0,this.collections={},this.logFileName="data.bin",this.rootPath=e}var t=e.prototype;return t.initialize=function(){a.ensureDirectory(this.rootPath),this.loadCollections()},t.getStructuredCollections=function(){return this.collections},t.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 s(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)?s(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.")}(a.listDirectories(this.rootPath));!(e=t()).done;){var r=e.value,n=this.rootPath+"/"+r+"/"+this.logFileName;c.default.file(n),this.collections[r]=a.readBinaryFile(n)}return this.collections},t.getCollection=function(e){return this.collections[e]?this.collections[e]:this.collections[e]=[]},t.updateCollection=function(e,t){this.collections[e]=i.default.cloneDeep(t)},t.writeCollections=function(){var e=this;Object.keys(this.collections).forEach(function(t){a.writeBinaryFile(e.rootPath+"/"+t+"/"+e.logFileName,e.collections[t])})},t.writeCollection=function(e){var t=this.rootPath+"/"+e;a.ensureDirectory(t),a.writeBinaryFile(t+"/"+this.logFileName,this.collections[e])},t.removeCollection=function(e){a.removeDirectory(this.rootPath+"/"+e),delete this.collections[e]},e}(),h=/*#__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}(),d=/*#__PURE__*/function(){function e(){}return e.generate=function(){return Date.now().toString(36)+Math.random().toString(36).slice(2,11)},e}();exports.Naro=/*#__PURE__*/function(){function e(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 f(i),this.core.initialize(),this.registerProcessListeners()}}var t=e.prototype;return t.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)})},t.writeToDisk=function(){try{return this.core.writeCollections(),Promise.resolve()}catch(e){return Promise.reject(e)}},t.add=function(e,t){try{var r,n=function(e){if(r)return e;var n=o.core.getCollection(c),u=d.generate(),s={id:u,createdAt:Date.now(),path:c+"/"+u},l=Object.assign(t,s);return n.push(l),o.core.updateCollection(c,n),i.default.cloneDeep(l)},o=this,c=h.validate(e).collectionName,u=function(){if(o.host)return Promise.resolve(o.serverRequest("add",[e,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)}},t.serverRequest=function(e,t){var r=this;return Promise.resolve(function(){try{var n=Promise.resolve(u.default.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}())},t.set=function(e,t){try{var r,n=function(e){if(r)return e;var n=o.core.getCollection(u),c={path:u+"/"+s,createdAt:Date.now(),id:s},l=Object.assign(t,c),a=i.default.findIndex(n,function(e){return e.id===s});return-1!==a?n[a]=l:n.push(l),o.core.updateCollection(u,n),i.default.cloneDeep(l)},o=this,c=h.validate(e),u=c.collectionName,s=c.collectionId,l=c.subCollectionName,a=c.subCollectionId;if(!s)throw new Error("Collection ID is required");if(l)throw new Error("Sub-collection is not supported in set method");if(a)throw new Error("Sub-collection ID is not supported in set method");var f=function(){if(o.host)return Promise.resolve(o.serverRequest("set",[e,t])).then(function(e){return r=1,e})}();return Promise.resolve(f&&f.then?f.then(n):n(f))}catch(e){return Promise.reject(e)}},t.getAll=function(e,t){void 0===t&&(t={});try{var r,n=function(e){if(r)return e;var n=i.default.cloneDeep(o.core.getCollection(c)),u=t.limit,s=t.populate,l=t.offset,a=void 0===l?0:l,f=n;return f=(f=o.filterCollection(f,t.filters||[])).slice(a),f=o.limitDocuments(f,u),Promise.resolve(o.populateCollection(f,s)).then(function(e){return f=e})},o=this,c=h.validate(e).collectionName,u=function(){if(o.host)return Promise.resolve(o.serverRequest("getAll",[e,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)}},t.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}})})},t.populate=function(e,t){try{var r,n=function(n){return r?n:Promise.resolve(Promise.all(t.map(function(t){try{var r=e[t],n=function(){if("string"==typeof r)return Promise.resolve(o.get(r)).then(function(r){r&&(e[t]=r)})}();return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}}))).then(function(){return i.default.cloneDeep(e)})},o=this;if(!t)return Promise.resolve(e);if(!t.length)throw new Error("Populate fields cannot be an empty array");var c=function(){if(o.host)return Promise.resolve(o.serverRequest("populate",[e,t])).then(function(e){return r=1,e})}();return Promise.resolve(c&&c.then?c.then(n):n(c))}catch(e){return Promise.reject(e)}},t.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)}},t.limitDocuments=function(e,t){return void 0===t?e:e.slice(0,t)},t.get=function(e){try{var t,r=function(e){if(t)return e;var r=n.core.getCollection(c);return i.default.find(r,function(e){return e.id===u})||void 0},n=this,o=h.validate(e),c=o.collectionName,u=o.collectionId,s=function(){if(n.host)return Promise.resolve(n.serverRequest("get",[e])).then(function(e){return t=1,e})}();return Promise.resolve(s&&s.then?s.then(r):r(s))}catch(e){return Promise.reject(e)}},t.update=function(e,t){try{var r,n=function(e){if(r)return e;var n=o.core.getCollection(u),c=i.default.findIndex(n,function(e){return e.id===s});if(-1===c)throw new Error("Item not found");var a=n[c],f=i.default.omit(t,["id","path","createdAt"]);return n[c]=l({},a,f),o.core.updateCollection(u,n),n[c]},o=this,c=h.validate(e),u=c.collectionName,s=c.collectionId;if(!s)throw new Error("Collection ID is required");var a=function(){if(o.host)return Promise.resolve(o.serverRequest("update",[e,t])).then(function(e){return r=1,e})}();return Promise.resolve(a&&a.then?a.then(n):n(a))}catch(e){return Promise.reject(e)}},t.delete=function(e){try{var t,r=function(e){if(t)return e;var r=n.core.getCollection(c),o=i.default.findIndex(r,function(e){return e.id===u});-1!==o&&r[o]&&(r.splice(o,1),n.core.updateCollection(c,r))},n=this,o=h.validate(e),c=o.collectionName,u=o.collectionId;if(!u)throw new Error("Collection ID is required");var s=function(){if(n.host)return Promise.resolve(n.serverRequest("delete",[e])).then(function(e){return t=1,e})}();return Promise.resolve(s&&s.then?s.then(r):r(s))}catch(e){return Promise.reject(e)}},t.exists=function(e){try{var t,r=function(e){if(t)return e;var r=n.core.getCollection(c);return i.default.some(r,function(e){return e.id===u})},n=this,o=h.validate(e),c=o.collectionName,u=o.collectionId;if(!u)throw new Error("Collection ID is required");var s=function(){if(n.host)return Promise.resolve(n.serverRequest("exists",[e])).then(function(e){return t=1,e})}();return Promise.resolve(s&&s.then?s.then(r):r(s))}catch(e){return Promise.reject(e)}},t.count=function(e){try{var t,r=function(e){return t?e:n.core.getCollection(o).length},n=this,o=h.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)}},t.clear=function(e){try{var t,r=function(e){if(t)return e;n.core.removeCollection(i)},n=this,o=h.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)}},t.getStructuredCollections=function(){try{var e=this;return Promise.resolve(e.host?e.serverRequest("getStructuredCollections",[]):e.core.getStructuredCollections())}catch(e){return Promise.reject(e)}},e}();
//# sourceMappingURL=index.js.map