-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5991351
commit 2dd374c
Showing
8 changed files
with
531 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
> # No proper Readme yet, try again later 😁 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.