Skip to content

Commit

Permalink
feat: json files generate in working directory!
Browse files Browse the repository at this point in the history
  • Loading branch information
khalby786 committed Mar 12, 2021
1 parent 8d17ef7 commit 2c411bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Khaleel Gibran
Copyright (c) 2021 Khaleel Gibran

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsoning",
"version": "0.9.18",
"version": "0.10.18",
"description": "A simple key-value JSON-based persistent lightweight database.",
"main": "src/index.js",
"scripts": {
Expand All @@ -10,8 +10,8 @@
"report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"docs:build": "node docma.js && bash createcname.sh",
"docs:serve": "docma serve",
"delete": "rm src/db.json",
"lint": "eslint src/jsonin.js"
"delete": "rm db.json",
"lint": "eslint src/jsoning.js"
},
"dependencies": {
"write-file-atomic": "^3.0.3"
Expand Down
30 changes: 15 additions & 15 deletions src/jsoning.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class Jsoning {
}

// use an existing database or create a new one
if (fs.existsSync(resolve(__dirname, database))) {
if (fs.existsSync(resolve(process.cwd(), database))) {
this.database = database;
} else {
fs.writeFileSync(resolve(__dirname, database), "{}");
fs.writeFileSync(resolve(process.cwd(), database), "{}");
this.database = database;
}
return true;
Expand Down Expand Up @@ -70,11 +70,11 @@ class Jsoning {
throw new TypeError("Invalid key/value for element");
}

var db = require(resolve(__dirname, this.database));
var db = require(resolve(process.cwd(), this.database));
db[key] = value;
try {
await writeFileAtomic(
resolve(__dirname, this.database),
resolve(process.cwd(), this.database),
JSON.stringify(db),
{ chown: { uid: 100, gid: 50 } }
);
Expand All @@ -99,7 +99,7 @@ class Jsoning {
*
*/
async all() {
let data = fs.readFileSync(resolve(__dirname, this.database), "utf-8");
let data = fs.readFileSync(resolve(process.cwd(), this.database), "utf-8");
data = JSON.parse(data);
return data;
}
Expand All @@ -124,14 +124,14 @@ class Jsoning {
}

let db = JSON.parse(
fs.readFileSync(resolve(__dirname, this.database), "utf-8")
fs.readFileSync(resolve(process.cwd(), this.database), "utf-8")
);
if (Object.prototype.hasOwnProperty.call(db, key)) {
try {
const removeProp = key;
const { [removeProp]: remove, ...rest } = db;
await writeFileAtomic(
resolve(__dirname, this.database),
resolve(process.cwd(), this.database),
JSON.stringify(rest)
);
return true;
Expand Down Expand Up @@ -163,7 +163,7 @@ class Jsoning {
throw new TypeError("Invalid key of element");
}

let db = fs.readFileSync(resolve(__dirname, this.database), "utf-8");
let db = fs.readFileSync(resolve(process.cwd(), this.database), "utf-8");
db = JSON.parse(db);
if (Object.prototype.hasOwnProperty.call(db, key)) {
let data = db[key];
Expand All @@ -189,7 +189,7 @@ class Jsoning {
let cleared = {};
try {
await writeFileAtomic(
resolve(__dirname, this.database),
resolve(process.cwd(), this.database),
JSON.stringify(cleared),
{ chown: { uid: 100, gid: 50 } }
);
Expand Down Expand Up @@ -243,7 +243,7 @@ class Jsoning {

// see if value exists
let db = JSON.parse(
fs.readFileSync(resolve(__dirname, this.database), "utf-8")
fs.readFileSync(resolve(process.cwd(), this.database), "utf-8")
);
if (Object.prototype.hasOwnProperty.call(db, key)) {
// key exists
Expand Down Expand Up @@ -277,7 +277,7 @@ class Jsoning {
db[key] = result;
try {
await writeFileAtomic(
resolve(__dirname, this.database),
resolve(process.cwd(), this.database),
JSON.stringify(db),
{ chown: { uid: 100, gid: 50 } }
);
Expand Down Expand Up @@ -315,7 +315,7 @@ class Jsoning {
throw new TypeError("Invalid key of element");
}

let db = fs.readFileSync(resolve(__dirname, this.database), "utf-8");
let db = fs.readFileSync(resolve(process.cwd(), this.database), "utf-8");
db = JSON.parse(db);

if (Object.prototype.hasOwnProperty.call(db, key)) {
Expand All @@ -341,7 +341,7 @@ class Jsoning {
*/
async push(key, value) {
// see if element exists
let db = fs.readFileSync(resolve(__dirname, this.database), "utf-8");
let db = fs.readFileSync(resolve(process.cwd(), this.database), "utf-8");
db = JSON.parse(db);

if (Object.prototype.hasOwnProperty.call(db, key)) {
Expand All @@ -355,7 +355,7 @@ class Jsoning {
db[key].push(value);
try {
await writeFileAtomic(
resolve(__dirname, this.database),
resolve(process.cwd(), this.database),
JSON.stringify(db),
{ chown: { uid: 100, gid: 50 } }
);
Expand All @@ -372,7 +372,7 @@ class Jsoning {
db[key].push(value);
try {
await writeFileAtomic(
resolve(__dirname, this.database),
resolve(process.cwd(), this.database),
JSON.stringify(db),
{ chown: { uid: 100, gid: 50 } }
);
Expand Down
2 changes: 1 addition & 1 deletion tests/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require("ava");

const jsoning = require('../src/index');
const db = new jsoning('db.json');
const db = new jsoning('./db.json');


test("Jsoning#set", async (t) => {
Expand Down

0 comments on commit 2c411bd

Please sign in to comment.