Skip to content

Commit

Permalink
Ready for Beta 1 ✔
Browse files Browse the repository at this point in the history
  • Loading branch information
r-rajaneesh committed Aug 11, 2022
1 parent 5991351 commit 2dd374c
Show file tree
Hide file tree
Showing 8 changed files with 531 additions and 52 deletions.
41 changes: 39 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> # No proper Readme yet, try again later 😁
1 change: 1 addition & 0 deletions databases/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is the folder where the database files will be stored by default
40 changes: 40 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
128 changes: 106 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit 2dd374c

Please sign in to comment.