wio.db
Advanced tools
Comparing version 4.0.21 to 4.0.22
{ | ||
"name": "wio.db", | ||
"version": "4.0.21", | ||
"version": "4.0.22", | ||
"description": "Gözle okunabilir database modülü.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
const DatabaseError = require("./Error"); | ||
const path = require('path'); | ||
const { | ||
existsSync, | ||
mkdirSync, | ||
writeFileSync, | ||
readFileSync, | ||
unlinkSync | ||
} = require("fs"); | ||
const path = require("path"); | ||
const { existsSync, mkdirSync, writeFileSync, readFileSync, unlinkSync } = require("fs"); | ||
const { set, get, unset } = require("lodash"); | ||
/** | ||
@@ -26,28 +11,15 @@ * @type {JsonDatabase<V>} | ||
class JsonDatabase { | ||
/** | ||
* @private | ||
* @type {object} | ||
*/ | ||
#cache = {}; | ||
/** | ||
* @param {import("./Types/IOptions").IOptions} options | ||
* @constructor | ||
*/ | ||
constructor({ | ||
databasePath = "databases/db.json", | ||
maxDataSize = null | ||
} = {}) { | ||
constructor({ databasePath = "databases/db.json", maxDataSize = null } = {}) { | ||
if (maxDataSize !== null && typeof maxDataSize !== "number") { | ||
throw new DatabaseError("The maximum limit must be in number type!"); | ||
} | ||
if (maxDataSize !== null && maxDataSize < 1) { | ||
throw new DatabaseError("Inappropriate range for the limit!"); | ||
} | ||
let basePath = process.cwd(); | ||
@@ -66,3 +38,2 @@ if (databasePath.startsWith(basePath)) { | ||
if (!databasePath.endsWith(".json")) { | ||
@@ -79,3 +50,3 @@ if (databasePath.endsWith(path.sep)) { | ||
const dirNames = databasePath.split(path.sep).slice(1); | ||
const length = dirNames.length; | ||
@@ -98,3 +69,3 @@ | ||
const currentPath = `${targetDirPath}${path.sep}${dirName}`; | ||
if (!existsSync(currentPath)) { | ||
@@ -112,6 +83,4 @@ mkdirSync(currentPath); | ||
writeFileSync(this.path, "{}"); | ||
} else { | ||
this.#cache = JSON.parse(readFileSync(this.path, "utf-8")); | ||
} | ||
/** | ||
@@ -126,3 +95,3 @@ * @type {number} | ||
/** | ||
* Veri kaydedersiniz. | ||
* | ||
* @param {string} key Key | ||
@@ -133,4 +102,3 @@ * @param {V} value Value | ||
*/ | ||
set(key, value, autoWrite=true) { | ||
set(key, value, autoWrite = true) { | ||
if (key === "" || typeof key !== "string") { | ||
@@ -157,7 +125,8 @@ throw new DatabaseError("Unapproved key!"); | ||
set(this.#cache, key, value); | ||
const jsonData = this.toJSON(); | ||
if (autoWrite) | ||
writeFileSync(this.path, JSON.stringify(this.#cache, null, 4)); | ||
set(jsonData, key, value); | ||
if (autoWrite) writeFileSync(this.path, JSON.stringify(jsonData, null, 4)); | ||
this.size++; | ||
@@ -169,3 +138,3 @@ | ||
/** | ||
* Veri çekersiniz. | ||
* | ||
* @param {string} key Key | ||
@@ -177,13 +146,14 @@ * @param {V} [defaultValue=null] If there is no value, the default value to return. | ||
get(key, defaultValue = null) { | ||
if (key === "" || typeof key !== "string") { | ||
throw new DatabaseError("Unapproved key!"); | ||
} | ||
const data = get(this.#cache, key); | ||
return data === undefined ? defaultValue : data; | ||
const jsonData = this.toJSON(); | ||
const data = get(jsonData, key); | ||
return data === undefined ? defaultValue : data; | ||
} | ||
/** | ||
* Veri çekersiniz. | ||
* | ||
* @param {string} key Key | ||
@@ -199,3 +169,3 @@ * @param {V} [defaultValue=null] If there is no value, the default value to return. | ||
/** | ||
* Veri var mı yok mu kontrol eder. | ||
* | ||
* @param {string} key Key | ||
@@ -206,7 +176,7 @@ * @returns {boolean} | ||
exists(key) { | ||
return this.#cache.hasOwnProperty(key); | ||
return this.toJSON().hasOwnProperty(key); | ||
} | ||
/** | ||
* Veri var mı yok mu kontrol eder. | ||
* | ||
* @param {string} key Key | ||
@@ -221,3 +191,3 @@ * @returns {boolean} | ||
/** | ||
* Belirtilen miktarda veri döner. | ||
* | ||
* @param {number} limit Limit | ||
@@ -228,11 +198,13 @@ * @returns {Array<Schema<V>>}>} | ||
all(limit = 0) { | ||
if(typeof limit !== "number") { | ||
if (typeof limit !== "number") { | ||
throw new DatabaseError("Must be of limit number type!"); | ||
} | ||
const jsonData = JSON.parse(readFileSync(this.path, "utf-8")); | ||
const arr = []; | ||
for (const key in this.#cache) { | ||
for (const key in jsonData) { | ||
arr.push({ | ||
ID: key, | ||
data: this.#cache[key] | ||
data: jsonData[key] | ||
}); | ||
@@ -245,3 +217,3 @@ } | ||
/** | ||
* Belirtilen miktarda veri döner. | ||
* | ||
* @param {number} [limit] Limit | ||
@@ -256,3 +228,3 @@ * @returns {Array<Schema<V>>} | ||
/** | ||
* Belirtilen miktarda Object tipinde verileri döner. | ||
* | ||
* @param {number} [limit] Limit | ||
@@ -275,3 +247,3 @@ * @returns {{[key:string]:V}} | ||
/** | ||
* Veri siler. | ||
* | ||
* @param {string} key Key | ||
@@ -283,3 +255,2 @@ * @param {boolean} autoWrite Automatic write setting. | ||
delete(key, autoWrite = true) { | ||
if (key === "" || typeof key !== "string") { | ||
@@ -293,7 +264,8 @@ throw new DatabaseError("Unapproved key!"); | ||
const jsonData = this.toJSON(); | ||
this.size--; | ||
unset(this.#cache, key); | ||
unset(jsonData, key); | ||
if (autoWrite) | ||
writeFileSync(this.path, JSON.stringify(this.#cache, null, 4)); | ||
if (autoWrite) writeFileSync(this.path, JSON.stringify(jsonData, null, 4)); | ||
return; | ||
@@ -303,3 +275,3 @@ } | ||
/** | ||
* Verilerin hepsini siler. | ||
* | ||
* @returns {void} | ||
@@ -311,3 +283,2 @@ * @example db.deleteAll(); | ||
this.size = 0; | ||
this.#cache = {}; | ||
return; | ||
@@ -328,3 +299,3 @@ } | ||
/** | ||
* Array'den veri siler. | ||
* | ||
* @param {string} key Key | ||
@@ -344,4 +315,3 @@ * @param {boolean} multiple Whether to target multiple targets. | ||
} | ||
if (thisArg) | ||
callbackfn = callbackfn.bind(thisArg); | ||
if (thisArg) callbackfn = callbackfn.bind(thisArg); | ||
@@ -352,3 +322,3 @@ const length = data.length; | ||
const newArray = []; | ||
for (let i = 0; i < length; i++) { | ||
@@ -365,3 +335,3 @@ if (!callbackfn(data[i], i, data)) { | ||
} | ||
return this.set(key, data); | ||
@@ -371,3 +341,3 @@ } | ||
/** | ||
* Value'leri array şeklinde döner. | ||
* | ||
* @returns {V[]} Values[] | ||
@@ -382,3 +352,3 @@ * @example db.valueArray(); | ||
/** | ||
* ID'leri array şeklinde döner. | ||
* | ||
* @returns {string[]} ID[] | ||
@@ -391,5 +361,5 @@ * @example db.keyArray(); | ||
} | ||
/** | ||
* Matematik işlemleri yapar. | ||
* | ||
* @param {string} key Key | ||
@@ -403,3 +373,2 @@ * @param {"+" | "-" | "*" | "/" | "%"} operator Operator | ||
math(key, operator, value, goToNegative = false) { | ||
// @ts-ignore | ||
@@ -409,3 +378,3 @@ if (Array.isArray(value) || isNaN(value)) { | ||
} | ||
if (value <= 0) throw new DatabaseError(`Value cannot be less than 1.`); | ||
@@ -452,3 +421,3 @@ value = Number(value); | ||
/** | ||
* Toplama işlemi yapar. | ||
* | ||
* @param {string} key Key | ||
@@ -464,3 +433,3 @@ * @param {number} value Value | ||
/** | ||
* Çıkarma işlemi yapar. | ||
* | ||
* @param {string} key Key | ||
@@ -475,5 +444,5 @@ * @param {number} value Value | ||
} | ||
/** | ||
* Array'a veri ekler. | ||
* | ||
* @param {string} key Key | ||
@@ -500,3 +469,3 @@ * @param {any} value Value | ||
/** | ||
* Database'de ID'lerin içinde belirtilen veri varsa o verileri getirir. | ||
* | ||
* @param {string} key Key | ||
@@ -511,3 +480,3 @@ * @returns {Array<Schema<V>>} | ||
/** | ||
* Database'de ID'leri belirtilen veri ile başlayan verileri getirir. | ||
* | ||
* @param {string} key Key | ||
@@ -540,3 +509,3 @@ * @returns {Array<Schema<V>>} | ||
/** | ||
* İsmi belirtilen database dosyasını siler. | ||
* | ||
* @returns {void} | ||
@@ -549,3 +518,3 @@ */ | ||
/** | ||
* Çagrılan fonksiyon true değer dönerse onunla bağlantılı olan verileri siler. | ||
* | ||
* @param {(element:{ID:string,data:V},provider:this) => boolean} callbackfn | ||
@@ -571,3 +540,3 @@ * @param {any} [thisArg] | ||
size: this.size, | ||
version:"4.0.21" | ||
version: "4.0.21" | ||
}; | ||
@@ -577,18 +546,4 @@ } | ||
module.exports = JsonDatabase; | ||
/** | ||
@@ -599,2 +554,2 @@ * @template T | ||
* @prop {T} data | ||
*/ | ||
*/ |
const DatabaseError = require("./Error"); | ||
const path = require('path'); | ||
const { | ||
existsSync, | ||
mkdirSync, | ||
writeFileSync, | ||
readFileSync, | ||
unlinkSync | ||
} = require("fs"); | ||
const { | ||
set, | ||
get, | ||
unset | ||
} = require("lodash"); | ||
const yaml = require('yaml'); | ||
const path = require("path"); | ||
const { existsSync, mkdirSync, writeFileSync, readFileSync, unlinkSync } = require("fs"); | ||
const { set, get, unset } = require("lodash"); | ||
const yaml = require("yaml"); | ||
/** | ||
@@ -30,28 +12,15 @@ * @type {JsonDatabase<V>} | ||
class JsonDatabase { | ||
/** | ||
* @private | ||
* @type {object} | ||
*/ | ||
#cache = {}; | ||
/** | ||
* @param {import("./Types/IOptions").IOptions} options | ||
* @constructor | ||
*/ | ||
constructor({ | ||
databasePath = "db.yml", | ||
maxDataSize = null | ||
} = {}) { | ||
constructor({ databasePath = "db.yml", maxDataSize = null } = {}) { | ||
if (maxDataSize !== null && typeof maxDataSize !== "number") { | ||
throw new DatabaseError("The maximum limit must be in number type!"); | ||
} | ||
if (maxDataSize !== null && maxDataSize < 1) { | ||
throw new DatabaseError("Inappropriate range for the limit!"); | ||
} | ||
let basePath = process.cwd(); | ||
@@ -70,8 +39,8 @@ | ||
} | ||
if (!databasePath.endsWith(".yml")) { | ||
if (databasePath.endsWith(path.sep)) { | ||
databasePath+="db.yml"; | ||
databasePath += "db.yml"; | ||
} else { | ||
databasePath+=".yml"; | ||
databasePath += ".yml"; | ||
} | ||
@@ -83,3 +52,3 @@ } | ||
const dirNames = databasePath.split(path.sep).slice(1); | ||
const length = dirNames.length; | ||
@@ -102,3 +71,3 @@ | ||
const currentPath = `${targetDirPath}${path.sep}${dirName}`; | ||
if (!existsSync(currentPath)) { | ||
@@ -116,6 +85,4 @@ mkdirSync(currentPath); | ||
writeFileSync(this.path, ""); | ||
} else { | ||
this.#cache = yaml.parse(readFileSync(this.path, "utf-8")); | ||
} | ||
/** | ||
@@ -130,3 +97,3 @@ * @type {number} | ||
/** | ||
* Veri kaydedersiniz. | ||
* | ||
* @param {string} key Key | ||
@@ -137,4 +104,3 @@ * @param {V} value Value | ||
*/ | ||
set(key, value, autoWrite=true) { | ||
set(key, value, autoWrite = true) { | ||
if (key === "" || typeof key !== "string") { | ||
@@ -161,7 +127,8 @@ throw new DatabaseError("Unapproved key!"); | ||
set(this.#cache, key, value); | ||
const yamlData = this.toJSON(); | ||
if (autoWrite) | ||
writeFileSync(this.path, yaml.stringify(this.#cache, { indent: 4 })); | ||
set(yamlData, key, value); | ||
if (autoWrite) writeFileSync(this.path, yaml.stringify(yamlData, { indent: 4 })); | ||
this.size++; | ||
@@ -173,3 +140,3 @@ | ||
/** | ||
* Veri çekersiniz. | ||
* | ||
* @param {string} key Key | ||
@@ -181,3 +148,4 @@ * @param {V} [defaultValue=null] If there is no value, the default value to return. | ||
get(key, defaultValue = null) { | ||
const yamlData = this.toJSON(); | ||
if (key === "" || typeof key !== "string") { | ||
@@ -187,8 +155,8 @@ throw new DatabaseError("Unapproved key!"); | ||
const data = get(this.#cache, key); | ||
return data === undefined ? defaultValue : data; | ||
const data = get(yamlData, key); | ||
return data === undefined ? defaultValue : data; | ||
} | ||
/** | ||
* Veri çekersiniz. | ||
* | ||
* @param {string} key Key | ||
@@ -204,3 +172,3 @@ * @param {V} [defaultValue=null] If there is no value, the default value to return. | ||
/** | ||
* Veri var mı yok mu kontrol eder. | ||
* | ||
* @param {string} key Key | ||
@@ -216,3 +184,3 @@ * @returns {boolean} | ||
/** | ||
* Veri var mı yok mu kontrol eder. | ||
* | ||
* @param {string} key Key | ||
@@ -227,3 +195,3 @@ * @returns {boolean} | ||
/** | ||
* Belirtilen miktarda veri döner. | ||
* | ||
* @param {number} limit Limit | ||
@@ -234,11 +202,13 @@ * @returns {Array<Schema<V>>}>} | ||
all(limit = 0) { | ||
if(typeof limit !== "number") { | ||
const yamlData = yaml.parse(readFileSync(this.path, "utf-8")); | ||
if (typeof limit !== "number") { | ||
throw new DatabaseError("Must be of limit number type!"); | ||
} | ||
const arr = []; | ||
for (const key in this.#cache) { | ||
for (const key in yamlData) { | ||
arr.push({ | ||
ID: key, | ||
data: this.#cache[key] | ||
data: yamlData[key] | ||
}); | ||
@@ -251,3 +221,3 @@ } | ||
/** | ||
* Belirtilen miktarda veri döner. | ||
* | ||
* @param {number} [limit] Limit | ||
@@ -262,3 +232,3 @@ * @returns {Array<Schema<V>>} | ||
/** | ||
* Belirtilen miktarda Object tipinde verileri döner. | ||
* | ||
* @param {number} [limit] Limit | ||
@@ -281,3 +251,3 @@ * @returns {{[key:string]:V}} | ||
/** | ||
* Veri siler. | ||
* | ||
* @param {string} key Key | ||
@@ -289,2 +259,3 @@ * @param {boolean} autoWrite Automatic write setting. | ||
delete(key, autoWrite = true) { | ||
const yamlData = this.toJSON(); | ||
@@ -300,6 +271,5 @@ if (key === "" || typeof key !== "string") { | ||
this.size--; | ||
unset(this.#cache, key); | ||
unset(yamlData, key); | ||
if (autoWrite) | ||
writeFileSync(this.path, yaml.stringify(this.#cache, { indent: 4 })); | ||
if (autoWrite) writeFileSync(this.path, yaml.stringify(yamlData, { indent: 4 })); | ||
return; | ||
@@ -309,3 +279,3 @@ } | ||
/** | ||
* Verilerin hepsini siler. | ||
* | ||
* @returns {void} | ||
@@ -317,3 +287,2 @@ * @example db.deleteAll(); | ||
this.size = 0; | ||
this.#cache = {}; | ||
return; | ||
@@ -334,3 +303,3 @@ } | ||
/** | ||
* Array'den veri siler. | ||
* | ||
* @param {string} key Key | ||
@@ -350,4 +319,3 @@ * @param {boolean} multiple Whether to target multiple targets. | ||
} | ||
if (thisArg) | ||
callbackfn = callbackfn.bind(thisArg); | ||
if (thisArg) callbackfn = callbackfn.bind(thisArg); | ||
@@ -358,3 +326,3 @@ const length = data.length; | ||
const newArray = []; | ||
for (let i = 0; i < length; i++) { | ||
@@ -371,3 +339,3 @@ if (!callbackfn(data[i], i, data)) { | ||
} | ||
return this.set(key, data); | ||
@@ -377,3 +345,3 @@ } | ||
/** | ||
* Value'leri array şeklinde döner. | ||
* | ||
* @returns {V[]} Values[] | ||
@@ -388,3 +356,3 @@ * @example db.valueArray(); | ||
/** | ||
* ID'leri array şeklinde döner. | ||
* | ||
* @returns {string[]} ID[] | ||
@@ -397,5 +365,5 @@ * @example db.keyArray(); | ||
} | ||
/** | ||
* Matematik işlemleri yapar. | ||
* | ||
* @param {string} key Key | ||
@@ -409,3 +377,2 @@ * @param {"+" | "-" | "*" | "/" | "%"} operator Operator | ||
math(key, operator, value, goToNegative = false) { | ||
// @ts-ignore | ||
@@ -415,3 +382,3 @@ if (Array.isArray(value) || isNaN(value)) { | ||
} | ||
if (value <= 0) throw new DatabaseError(`Value cannot be less than 1.`); | ||
@@ -458,3 +425,3 @@ value = Number(value); | ||
/** | ||
* Toplama işlemi yapar. | ||
* | ||
* @param {string} key Key | ||
@@ -470,3 +437,3 @@ * @param {number} value Value | ||
/** | ||
* Çıkarma işlemi yapar. | ||
* | ||
* @param {string} key Key | ||
@@ -481,5 +448,5 @@ * @param {number} value Value | ||
} | ||
/** | ||
* Array'a veri ekler. | ||
* | ||
* @param {string} key Key | ||
@@ -506,3 +473,3 @@ * @param {any} value Value | ||
/** | ||
* Database'de ID'lerin içinde belirtilen veri varsa o verileri getirir. | ||
* | ||
* @param {string} key Key | ||
@@ -517,3 +484,3 @@ * @returns {Array<Schema<V>>} | ||
/** | ||
* Database'de ID'leri belirtilen veri ile başlayan verileri getirir. | ||
* | ||
* @param {string} key Key | ||
@@ -542,3 +509,3 @@ * @returns {Array<Schema<V>>} | ||
/** | ||
* İsmi belirtilen database dosyasını siler. | ||
* | ||
* @returns {void} | ||
@@ -551,3 +518,3 @@ */ | ||
/** | ||
* Çagrılan fonksiyon true değer dönerse onunla bağlantılı olan verileri siler. | ||
* | ||
* @param {(element:{ID:string,data:V},provider:this) => boolean} callbackfn | ||
@@ -571,3 +538,3 @@ * @returns {number} | ||
size: this.size, | ||
version:"4.0.21" | ||
version: "4.0.21" | ||
}; | ||
@@ -577,19 +544,4 @@ } | ||
module.exports = JsonDatabase; | ||
/** | ||
@@ -600,2 +552,2 @@ * @template T | ||
* @prop {T} data | ||
*/ | ||
*/ |
16
48810
1097