Skip to content

Commit

Permalink
v3.0.3: fix database directory path
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTRdiamond committed May 8, 2024
1 parent e290a25 commit 6115119
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
19 changes: 9 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Variables = new Map();
class ZanixonDB {
constructor(options){
const defaultOptions = {
directory: options.directory || "./database",
directory: options.directory || "/database",
tables: { "main": "/main/database.json" },
showLogs: options.showLogs || false
};
Expand All @@ -32,7 +32,7 @@ class ZanixonDB {
// create database files
const tables = this.tables;
for(let table in tables) {
const filePath = path.join(__dirname, this.options.directory, tables[table]);
const filePath = path.join(this.options.directory, tables[table]);
const isCreated = fs.existsSync(filePath);
if(!isCreated) {
if(!filePath.endsWith(".json")) throw new Error(`Invalid database file at ${table} ${tables[table]}`);
Expand All @@ -51,7 +51,7 @@ class ZanixonDB {

async isExists(table) {
if(!table) throw new Error(`Failed check the table, table name is not defined!`);
return await fs.existsSync(path.join(__dirname, this.options.directory, `${this.tables[table]}`));
return await fs.existsSync(path.join(this.options.directory, `${this.tables[table]}`));
}

variables(data, table = "main") {
Expand All @@ -62,7 +62,7 @@ class ZanixonDB {

async setKey(oldKey, newKey, table = "main") {
if(!this.tables[table]) throw new Error(`Invalid table, table with name "${table}" is not found!`);
const filePath = path.join(__dirname, this.options.directory, this.tables[table]);
const filePath = path.join(this.options.directory, this.tables[table]);

// checking params
if(!this.isEmpty(oldKey)) throw new Error(`oldKey parameter value is undefined!`)
Expand All @@ -80,22 +80,21 @@ class ZanixonDB {

async set(key, value, table = "main") {
if(!this.tables[table]) throw new Error(`Invalid table, table with name "${table}" is not found!`);
const filePath = path.join(__dirname, this.options.directory, this.tables[table]);
const filePath = path.join(this.options.directory, this.tables[table]);

if(!this.isEmpty(key)) throw new Error(`Key parameter value is undefined`)
if(!this.isEmpty(value)) throw new Error(`Value parameter value is undefined`)
if(!this.isExists(table)) throw new Error(`Table with name "${table}" at ${this.tables[table]} is not exists!`);

let content = await this.getContent(filePath);
content = Lodash.set(content, key, value);
console.log(content)
await this.setContent(filePath, content)
return Lodash.get(content, key);
}

async get(key, table = "main") {
if(!this.tables[table]) throw new Error(`Invalid table, table with name "${table}" is not found!`);
const filePath = path.join(__dirname, this.options.directory, this.tables[table]);
const filePath = path.join(this.options.directory, this.tables[table]);

if(!this.isEmpty(key)) throw new Error(`Key parameter value is undefined`)
if(!this.isExists(table)) throw new Error(`Table with name "${table}" at ${this.tables[table]} is not exists!`);
Expand All @@ -107,7 +106,7 @@ class ZanixonDB {

async delete(key, table = "main") {
if(!this.tables[table]) throw new Error(`Invalid table, table with name "${table}" is not found!`);
const filePath = path.join(__dirname, this.options.directory, this.tables[table]);
const filePath = path.join(this.options.directory, this.tables[table]);

if(!this.isEmpty(key)) throw new Error(`Key parameter value is undefined`)
if(!this.isExists(table)) throw new Error(`Table with name "${table}" at ${this.tables[table]} is not exists!`);
Expand All @@ -121,7 +120,7 @@ class ZanixonDB {

async has(key, table = "main") {
if(!this.tables[table]) throw new Error(`Invalid table, table with name "${table}" is not found!`);
const filePath = path.join(__dirname, this.options.directory, this.tables[table]);
const filePath = path.join(this.options.directory, this.tables[table]);

if(!this.isEmpty(key)) throw new Error(`Key parameter value is undefined`)
if(!this.isExists(table)) throw new Error(`Table with name "${table}" at ${this.tables[table]} is not exists!`);
Expand All @@ -134,7 +133,7 @@ class ZanixonDB {

async all(table = "main") {
if(!this.tables[table]) throw new Error(`Invalid table, table with name "${table}" is not found!`);
const filePath = path.join(__dirname, this.options.directory, this.tables[table]);
const filePath = path.join(this.options.directory, this.tables[table]);
return await this.getContent(filePath);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zanixon.db",
"version": "3.0.0",
"version": "3.0.3",
"description": "A simple JSON database for javacsript node.js",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 6115119

Please sign in to comment.