diff --git a/.gitignore b/.gitignore index 22b754c..36f6bba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,40 @@ -package-lock.json +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies /node_modules -/databases +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.pnpm-debug.log* + +# local env files +.env*.local + +# vercel +.vercel + +# typescript +*.tsbuildinfo + +# custom +index1.js +*.dx +test.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..8c4b585 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +> # No proper Readme yet, try again later 😁 diff --git a/databases/README.md b/databases/README.md new file mode 100644 index 0000000..2e0807b --- /dev/null +++ b/databases/README.md @@ -0,0 +1 @@ +# This is the folder where the database files will be stored by default diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..7f2e6da --- /dev/null +++ b/index.d.ts @@ -0,0 +1,40 @@ +interface setDocParams { + [key]: any; +} +interface construct { + path: string; + uniqueKey: boolean; + serverpath: string; +} +export default class DB { + constructor(options: construct); + private #save(): void; + /** + * Create a new collection + */ + public createCollection(collectioName: string, cb: Function): void; + /** + * Delete a collection + */ + public deleteCollection(collectioName: string, cb: Function): void; + /** + * Delete a document in a collection + */ + public deleteDoc(fn: Function, cb: Function): void; + /** + * Find a document in a collection + */ + public findDoc(fn: Function, cb: Function): void; + /** + * Get the key-value of a entire collection + */ + public getCollection(collectioName: string, cb: Function): void; + /** + * Check if a collection has a document + */ + public hasDoc(fn: Function, cb: Function): void; + /** + * Set a new document(s) + */ + public setDoc(params: setDocParams[] | setDocParams, cb: Function): void; +} diff --git a/index.js b/index.js index 91c8220..984eb4b 100644 --- a/index.js +++ b/index.js @@ -1,37 +1,121 @@ +import axios from "axios"; import fs from "fs-extra"; import _ from "lodash"; -class DB extends Map { - constructor(options = {}) { +import zlib from "zlib"; +class DB { + constructor(options = { path: "./databases/index.dx", uniqueKey: true }) { options = _.defaultsDeep(options, { - path: "./databases/index.json", + path: "./databases/index.dx", + uniqueKey: true, + serverPath: "", }); - super(); + this.server = options.serverPath !== "" ? true : false; this.options = options; + this.collectionName = "index"; this.tmp = {}; + fs.ensureFileSync(`${this.options.path}`); - this.save = () => { - fs.writeJsonSync(`${this.options.path}`, this.tmp); - }; - process.on("beforeExit", () => this.save()); + try { + this.tmp = JSON.parse(zlib.inflateRawSync(fs.readFileSync(`${this.options.path}`))) ?? {}; + } catch { + fs.writeFileSync(`${this.options.path}`, zlib.deflateRawSync(Buffer.from(JSON.stringify({})))); + this.tmp = JSON.parse(zlib.inflateRawSync(fs.readFileSync(`${this.options.path}`))) ?? {}; + } + const events = [ + "beforeExit", + "rejectionHandled", + "uncaughtException", + "unhandledRejection", + "exit", + "beforeExit", + "SIGHUP", + "SIGTERM", + "SIGINT", + "SIGBREAK", + "SIGKILL", + ]; + + events.forEach((eventName) => { + process.on(eventName, (...args) => { + Promise.resolve(this.#save()); + process.exit(0); + }); + }); + setInterval(() => { + this.#save(); + }, 5000); + } + #save() { + if (!this.server) fs.writeFileSync(`${this.options.path}`, zlib.deflateRawSync(Buffer.from(JSON.stringify(this.tmp)))); + + this.#sendRequest(); } - createCollection(name) { - this.tmp[name] = {}; - this.save(); + async #sendRequest() { + if (!this.server) return; + await axios.post(`${this.options.serverPath}`, { data: JSON.stringify(this.tmp) }, { method: "POST" }).catch((error) => { + console.log(`E: ${error}`); + }); } - createData(collectionName, key, value) { - this.tmp[collectionName][key] = value; - this.save(); + createCollection(collectionName, cb) { + if (this.tmp[collectionName]) return; + this.tmp[collectionName] = []; + if (cb) cb(collectionName, this.tmp[collectionName]); + return this.tmp[collectionName]; } - getData(collectionName, key) { - return this.tmp[collectionName][key]; + deleteCollection(collectionName, cb) { + delete this.tmp[collectionName]; + if (cb) cb(); + return; + } + deleteDoc(fn, cb) { + this.tmp[this.collectionName].forEach((doc, index) => { + const vals = Object.values(doc); + vals.forEach((val) => { + if (fn(val)) { + this.tmp[this.collectionName].splice(index, 1); + } + }); + }); + if (cb) cb(true); } - deleteData(collectionName, key) { - delete this.tmp[collectionName][key]; - this.save(); + findDoc(fn, cb) { + let op; + this.tmp[this.collectionName].forEach((doc, index) => { + const vals = Object.values(doc); + vals.forEach((val) => { + if (fn(val)) op = doc; + }); + }); + if (cb) cb(op); + return op; + } + getCollection(collectionName, cb) { + this.collectionName = collectionName; + if (cb) cb(this.tmp[collectionName]); + + return this.tmp[collectionName]; + } + hasDoc(fn, cb) { + this.tmp[this.collectionName].forEach((doc, index) => { + const vals = Object.values(doc); + vals.forEach((val) => { + if (fn(val)) { + op = doc; + has = true; + } + }); + }); + if (cb) cb(has); + return has; } - setExpiry(collectionName, key, expiry) { - this.tmp[collectionName][key][expiry]; - this.save(); + setDoc(params, cb) { + if (!Array.isArray(params)) this.tmp[this.collectionName].push(params); + else + params.forEach((param) => { + this.tmp[this.collectionName].push(param); + }); + if (cb) cb(this.tmp[this.collectionName]); + return this.tmp[this.collectionName]; } } export default DB; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..d68b9cb --- /dev/null +++ b/package-lock.json @@ -0,0 +1,297 @@ +{ + "name": "@rajaneesh_r/json-db", + "version": "0.0.1-beta.1", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "@rajaneesh_r/json-db", + "version": "0.0.1-beta.1", + "dependencies": { + "axios": "^0.27.2", + "fs-extra": "^10.1.0", + "lodash": "^4.17.21" + }, + "devDependencies": { + "@types/fs-extra": "^9.0.13", + "@types/lodash": "^4.14.182", + "@types/node": "^18.6.1" + } + }, + "node_modules/@types/fs-extra": { + "version": "9.0.13", + "resolved": "http://localhost:4873/@types%2ffs-extra/-/fs-extra-9.0.13.tgz", + "integrity": "sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/lodash": { + "version": "4.14.182", + "resolved": "http://localhost:4873/@types%2flodash/-/lodash-4.14.182.tgz", + "integrity": "sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/node": { + "version": "18.6.1", + "resolved": "http://localhost:4873/@types%2fnode/-/node-18.6.1.tgz", + "integrity": "sha512-z+2vB6yDt1fNwKOeGbckpmirO+VBDuQqecXkgeIqDlaOtmKn6hPR/viQ8cxCfqLU4fTlvM3+YjM367TukWdxpg==", + "dev": true, + "license": "MIT" + }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "http://localhost:4873/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", + "license": "MIT" + }, + "node_modules/axios": { + "version": "0.27.2", + "resolved": "http://localhost:4873/axios/-/axios-0.27.2.tgz", + "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.14.9", + "form-data": "^4.0.0" + } + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "http://localhost:4873/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "http://localhost:4873/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/follow-redirects": { + "version": "1.15.1", + "resolved": "http://localhost:4873/follow-redirects/-/follow-redirects-1.15.1.tgz", + "integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "license": "MIT", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "http://localhost:4873/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/fs-extra": { + "version": "10.1.0", + "resolved": "http://localhost:4873/fs-extra/-/fs-extra-10.1.0.tgz", + "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", + "license": "MIT", + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/graceful-fs": { + "version": "4.2.10", + "resolved": "http://localhost:4873/graceful-fs/-/graceful-fs-4.2.10.tgz", + "integrity": "sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==", + "license": "ISC" + }, + "node_modules/jsonfile": { + "version": "6.1.0", + "resolved": "http://localhost:4873/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "license": "MIT", + "dependencies": { + "universalify": "^2.0.0" + }, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "http://localhost:4873/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "license": "MIT" + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "http://localhost:4873/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "http://localhost:4873/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/universalify": { + "version": "2.0.0", + "resolved": "http://localhost:4873/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", + "license": "MIT", + "engines": { + "node": ">= 10.0.0" + } + } + }, + "dependencies": { + "@types/fs-extra": { + "version": "9.0.13", + "resolved": "http://localhost:4873/@types%2ffs-extra/-/fs-extra-9.0.13.tgz", + "integrity": "sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, + "@types/lodash": { + "version": "4.14.182", + "resolved": "http://localhost:4873/@types%2flodash/-/lodash-4.14.182.tgz", + "integrity": "sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==", + "dev": true + }, + "@types/node": { + "version": "18.6.1", + "resolved": "http://localhost:4873/@types%2fnode/-/node-18.6.1.tgz", + "integrity": "sha512-z+2vB6yDt1fNwKOeGbckpmirO+VBDuQqecXkgeIqDlaOtmKn6hPR/viQ8cxCfqLU4fTlvM3+YjM367TukWdxpg==", + "dev": true + }, + "asynckit": { + "version": "0.4.0", + "resolved": "http://localhost:4873/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" + }, + "axios": { + "version": "0.27.2", + "resolved": "http://localhost:4873/axios/-/axios-0.27.2.tgz", + "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", + "requires": { + "follow-redirects": "^1.14.9", + "form-data": "^4.0.0" + } + }, + "combined-stream": { + "version": "1.0.8", + "resolved": "http://localhost:4873/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "http://localhost:4873/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" + }, + "follow-redirects": { + "version": "1.15.1", + "resolved": "http://localhost:4873/follow-redirects/-/follow-redirects-1.15.1.tgz", + "integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==" + }, + "form-data": { + "version": "4.0.0", + "resolved": "http://localhost:4873/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, + "fs-extra": { + "version": "10.1.0", + "resolved": "http://localhost:4873/fs-extra/-/fs-extra-10.1.0.tgz", + "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", + "requires": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + }, + "graceful-fs": { + "version": "4.2.10", + "resolved": "http://localhost:4873/graceful-fs/-/graceful-fs-4.2.10.tgz", + "integrity": "sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==" + }, + "jsonfile": { + "version": "6.1.0", + "resolved": "http://localhost:4873/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "requires": { + "graceful-fs": "^4.1.6", + "universalify": "^2.0.0" + } + }, + "lodash": { + "version": "4.17.21", + "resolved": "http://localhost:4873/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + }, + "mime-db": { + "version": "1.52.0", + "resolved": "http://localhost:4873/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==" + }, + "mime-types": { + "version": "2.1.35", + "resolved": "http://localhost:4873/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "requires": { + "mime-db": "1.52.0" + } + }, + "universalify": { + "version": "2.0.0", + "resolved": "http://localhost:4873/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==" + } + } +} diff --git a/package.json b/package.json index eb8c531..c695203 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,28 @@ { - "name": "json-database", - "version": "1.0.0", - "description": "The best database", + "name": "@rajaneesh_r/json-db", + "version": "0.0.1-beta.1", + "type": "module", "main": "index.js", - "type":"module", - "keywords": [ - "Database", - "db" - ], - "author": "Rajaneesh.R", - "license": "ISC", + "typings": "index.d.ts", + "scripts": { + "test": "node test.js", + "publish-beta-public": "npm publish --access public --tag beta --registry=https://registry.npmjs.com", + "publish-beta-private": "npm publish --access public --tag beta", + "publish-public": "npm publish --access public", + "publish-private": "npm publish --access public --registry=https://registry.npmjs.com" + }, + "author": { + "name": "Rajaneesh.R", + "email": "rajaneeshr2006@gmail.com" + }, "dependencies": { + "axios": "^0.27.2", "fs-extra": "^10.1.0", "lodash": "^4.17.21" }, "devDependencies": { "@types/fs-extra": "^9.0.13", "@types/lodash": "^4.14.182", - "ts-node": "^10.8.1" - }, - "files": [ - "index.js", - "package.json" - ] + "@types/node": "^18.6.1" + } } diff --git a/test.js b/test.js index 1d3a4b1..b3bd904 100644 --- a/test.js +++ b/test.js @@ -1,13 +1,30 @@ -import DB from "./index"; +import fs from "fs-extra"; +import DB from "./index.js"; +import zlib from "zlib"; const db = new DB(); -db.createCollection("test"); -db.createData("test", "a", 1); -console.log(db.getData("test", "a")); -db.createData("test", "b", "123"); -db.save(); -db.createData("test", "3123", { hello: "world" }); -db.save(); -setTimeout(() => { - db.save(); -}, 2000); -db.createData("test", "eeeee", "eeee"); +// db.createCollection("a"); +// db.getCollection("a", (collection) => collection); +// db.setDoc({ hello: { not: "pog" }, world: "pog" }); +// db.setDoc({ dasdad: { noteee: "eeee" }, pog: "not pog" }); + +// db.findDoc((key) => { +// return `${key}` === `${{ not: "pog" }}`; +// }); +// db.deleteCollection("a"); +// db.deleteDoc( +// (val) => { +// return val === "not pog"; +// }, +// (e) => {}, +// ); +setInterval(() => { + db.getCollection("a", (collection) => console.log(collection)); +}, 1000); + +// zlib.deflate(Buffer.from(JSON.stringify({ hello: "world" })), (err, res) => { +// if (err) throw new Error(err); +// fs.writeFileSync("./mygzipfile.txt", res); +// }); +// zlib.inflate(fs.readFileSync("./mygzipfile.txt"), (err, res) => { +// console.log(JSON.parse(res.toString())); +// });