UNPKG

@narodb/naro

Version:

A simple, fast, and lightweight NoSQL database

3 lines (2 loc) 11.6 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("lodash"),require("notepack.io"),require("fs-jetpack"),require("redaxios")):"function"==typeof define&&define.amd?define(["exports","lodash","notepack.io","fs-jetpack","redaxios"],t):t((e||self).naro={},e.lodash,e.notepack_io,e.fsJetpack,e.redaxios)}(this,function(e,t,r,n,o){function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var c=/*#__PURE__*/i(t),s=/*#__PURE__*/i(n),u=/*#__PURE__*/i(o);function l(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 a(){return a=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},a.apply(null,arguments)}var f=/*#__PURE__*/function(){function e(){}return e.ensureDirectory=function(e){s.default.dir(e)},e.readBinaryFile=function(e){var t=s.default.read(e,"buffer");return t?r.decode(t):[]},e.writeBinaryFile=function(e,t){return s.default.write(e,r.encode(t))},e.listDirectories=function(e){var t=s.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 s.default.remove(e)},e}(),h=/*#__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(){f.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 l(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)?l(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.")}(f.listDirectories(this.rootPath));!(e=t()).done;){var r=e.value,n=this.rootPath+"/"+r+"/"+this.logFileName;s.default.file(n),this.collections[r]=f.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]=c.default.cloneDeep(t)},t.writeCollections=function(){var e=this;Object.keys(this.collections).forEach(function(t){f.writeBinaryFile(e.rootPath+"/"+t+"/"+e.logFileName,e.collections[t])})},t.writeCollection=function(e){var t=this.rootPath+"/"+e;f.ensureDirectory(t),f.writeBinaryFile(t+"/"+this.logFileName,this.collections[e])},t.removeCollection=function(e){f.removeDirectory(this.rootPath+"/"+e),delete this.collections[e]},e}(),d=/*#__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}(),v=/*#__PURE__*/function(){function e(){}return e.generate=function(){return Date.now().toString(36)+Math.random().toString(36).slice(2,11)},e}();e.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 h(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(i),s=v.generate(),u={id:s,createdAt:Date.now(),path:i+"/"+s},l=Object.assign(t,u);return n.push(l),o.core.updateCollection(i,n),c.default.cloneDeep(l)},o=this,i=d.validate(e).collectionName,s=function(){if(o.host)return Promise.resolve(o.serverRequest("add",[e,t])).then(function(e){return r=1,e})}();return Promise.resolve(s&&s.then?s.then(n):n(s))}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(s),i={path:s+"/"+u,createdAt:Date.now(),id:u},l=Object.assign(t,i),a=c.default.findIndex(n,function(e){return e.id===u});return-1!==a?n[a]=l:n.push(l),o.core.updateCollection(s,n),c.default.cloneDeep(l)},o=this,i=d.validate(e),s=i.collectionName,u=i.collectionId,l=i.subCollectionName,a=i.subCollectionId;if(!u)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=c.default.cloneDeep(o.core.getCollection(i)),s=t.limit,u=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,s),Promise.resolve(o.populateCollection(f,u)).then(function(e){return f=e})},o=this,i=d.validate(e).collectionName,s=function(){if(o.host)return Promise.resolve(o.serverRequest("getAll",[e,t])).then(function(e){return r=1,e})}();return Promise.resolve(s&&s.then?s.then(n):n(s))}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 c.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 i=function(){if(o.host)return Promise.resolve(o.serverRequest("populate",[e,t])).then(function(e){return r=1,e})}();return Promise.resolve(i&&i.then?i.then(n):n(i))}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(i);return c.default.find(r,function(e){return e.id===s})||void 0},n=this,o=d.validate(e),i=o.collectionName,s=o.collectionId,u=function(){if(n.host)return Promise.resolve(n.serverRequest("get",[e])).then(function(e){return t=1,e})}();return Promise.resolve(u&&u.then?u.then(r):r(u))}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(s),i=c.default.findIndex(n,function(e){return e.id===u});if(-1===i)throw new Error("Item not found");var l=n[i],f=c.default.omit(t,["id","path","createdAt"]);return n[i]=a({},l,f),o.core.updateCollection(s,n),n[i]},o=this,i=d.validate(e),s=i.collectionName,u=i.collectionId;if(!u)throw new Error("Collection ID is required");var l=function(){if(o.host)return Promise.resolve(o.serverRequest("update",[e,t])).then(function(e){return r=1,e})}();return Promise.resolve(l&&l.then?l.then(n):n(l))}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(i),o=c.default.findIndex(r,function(e){return e.id===s});-1!==o&&r[o]&&(r.splice(o,1),n.core.updateCollection(i,r))},n=this,o=d.validate(e),i=o.collectionName,s=o.collectionId;if(!s)throw new Error("Collection ID is required");var u=function(){if(n.host)return Promise.resolve(n.serverRequest("delete",[e])).then(function(e){return t=1,e})}();return Promise.resolve(u&&u.then?u.then(r):r(u))}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(i);return c.default.some(r,function(e){return e.id===s})},n=this,o=d.validate(e),i=o.collectionName,s=o.collectionId;if(!s)throw new Error("Collection ID is required");var u=function(){if(n.host)return Promise.resolve(n.serverRequest("exists",[e])).then(function(e){return t=1,e})}();return Promise.resolve(u&&u.then?u.then(r):r(u))}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=d.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=d.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.umd.js.map