added all files to project

This commit is contained in:
2022-03-10 10:36:59 +01:00
parent 09dd957b33
commit 46a936d7de
25351 changed files with 3883356 additions and 0 deletions
+101
View File
@@ -0,0 +1,101 @@
import { Config } from "./lib/JsonDBConfig";
export declare type FindCallback = (entry: any, index: number | string) => boolean;
export declare class JsonDB {
private loaded;
private data;
private readonly config;
/**
* JSONDB Constructor
* @param filename where to save the "DB". Can also be used to give the whole configuration
* @param saveOnPush save the database at each push command into the json file
* @param humanReadable the JSON file will be readable easily by a human
* @param separator what to use as separator
*/
constructor(filename: string | Config, saveOnPush?: boolean, humanReadable?: boolean, separator?: string);
/**
* Process datapath into different parts
* @param dataPath
*/
private processDataPath;
private retrieveData;
private getParentData;
/**
* Get the wanted data
* @param dataPath path of the data to retrieve
*/
getData(dataPath: string): any;
/**
* Same as getData only here it's directly typed to your object
* @param dataPath path of the data to retrieve
*/
getObject<T>(dataPath: string): T;
/**
* Check for existing datapath
* @param dataPath
*/
exists(dataPath: string): boolean;
/**
* Returns the number of element which constitutes the array
* @param dataPath
*/
count(dataPath: string): number;
/**
* Returns the index of the object that meets the criteria submitted. Returns -1, if no match is found.
* @param dataPath base dataPath from where to start searching
* @param searchValue value to look for in the dataPath
* @param propertyName name of the property to look for searchValue
*/
getIndex(dataPath: string, searchValue: (string | number), propertyName?: string): number;
/**
* Return the index of the value inside the array. Returns -1, if no match is found.
* @param dataPath base dataPath from where to start searching
* @param searchValue value to look for in the dataPath
*/
getIndexValue(dataPath: string, searchValue: (string | number)): number;
private getArrayData;
/**
* Find all specific entry in an array/object
* @param rootPath base dataPath from where to start searching
* @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
*/
filter<T>(rootPath: string, callback: FindCallback): T[] | undefined;
/**
* Find a specific entry in an array/object
* @param rootPath base dataPath from where to start searching
* @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
*/
find<T>(rootPath: string, callback: FindCallback): T | undefined;
/**
* Pushing data into the database
* @param dataPath path leading to the data
* @param data data to push
* @param override overriding or not the data, if not, it will merge them
*/
push(dataPath: string, data: any, override?: boolean): void;
/**
* Delete the data
* @param dataPath path leading to the data
*/
delete(dataPath: string): void;
/**
* Only use this if you know what you're doing.
* It reset the full data of the database.
* @param data
*/
resetData(data: any): void;
/**
* Reload the database from the file
*/
reload(): void;
/**
* Manually load the database
* It is automatically called when the first getData is done
*/
load(): void;
/**
* Manually save the database
* By default you can't save the database if it's not loaded
* @param force force the save of the database
*/
save(force?: boolean): void;
}
+347
View File
@@ -0,0 +1,347 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonDB = void 0;
const Utils_1 = require("./lib/Utils");
const FS = require("fs");
const path = require("path");
const mkdirp = require("mkdirp");
const Errors_1 = require("./lib/Errors");
const DBParentData_1 = require("./lib/DBParentData");
const ArrayInfo_1 = require("./lib/ArrayInfo");
const JsonDBConfig_1 = require("./lib/JsonDBConfig");
class JsonDB {
loaded = false;
data = {};
config;
/**
* JSONDB Constructor
* @param filename where to save the "DB". Can also be used to give the whole configuration
* @param saveOnPush save the database at each push command into the json file
* @param humanReadable the JSON file will be readable easily by a human
* @param separator what to use as separator
*/
constructor(filename, saveOnPush = true, humanReadable = false, separator = '/') {
if (filename instanceof JsonDBConfig_1.Config) {
this.config = filename;
}
else {
this.config = new JsonDBConfig_1.Config(filename, saveOnPush, humanReadable, separator);
}
if (!FS.existsSync(this.config.filename)) {
const dirname = path.dirname(this.config.filename);
mkdirp.sync(dirname);
this.save(true);
this.loaded = true;
}
}
/**
* Process datapath into different parts
* @param dataPath
*/
processDataPath(dataPath) {
if (dataPath === undefined || !dataPath.trim()) {
throw new Errors_1.DataError("The Data Path can't be empty", 6);
}
if (dataPath == this.config.separator) {
return [];
}
dataPath = (0, Utils_1.removeTrailingChar)(dataPath, this.config.separator);
const path = dataPath.split(this.config.separator);
path.shift();
return path;
}
retrieveData(dataPath, create = false) {
this.load();
const thisDb = this;
const recursiveProcessDataPath = (data, index) => {
let property = dataPath[index];
/**
* Find the wanted Data or create it.
*/
function findData(isArray = false) {
if (data.hasOwnProperty(property)) {
data = data[property];
}
else if (create) {
if (isArray) {
data[property] = [];
}
else {
data[property] = {};
}
data = data[property];
}
else {
throw new Errors_1.DataError(`Can't find dataPath: ${thisDb.config.separator}${dataPath.join(thisDb.config.separator)}. Stopped at ${property}`, 5);
}
}
const arrayInfo = ArrayInfo_1.ArrayInfo.processArray(property);
if (arrayInfo) {
property = arrayInfo.property;
findData(true);
if (!Array.isArray(data)) {
throw new Errors_1.DataError(`DataPath: ${thisDb.config.separator}${dataPath.join(thisDb.config.separator)}. ${property} is not an array.`, 11);
}
const arrayIndex = arrayInfo.getIndex(data, true);
if (!arrayInfo.append && data.hasOwnProperty(arrayIndex)) {
data = data[arrayIndex];
}
else if (create) {
if (arrayInfo.append) {
data.push({});
data = data[data.length - 1];
}
else {
data[arrayIndex] = {};
data = data[arrayIndex];
}
}
else {
throw new Errors_1.DataError(`DataPath: ${thisDb.config.separator}${dataPath.join(thisDb.config.separator)}. . Can't find index ${arrayInfo.index} in array ${property}`, 10);
}
}
else {
findData();
}
if (dataPath.length == ++index) {
return data;
}
return recursiveProcessDataPath(data, index);
};
if (dataPath.length === 0) {
return this.data;
}
return recursiveProcessDataPath(this.data, 0);
}
getParentData(dataPath, create) {
const path = this.processDataPath(dataPath);
const last = path.pop();
return new DBParentData_1.DBParentData(this.retrieveData(path, create), this, dataPath, last);
}
/**
* Get the wanted data
* @param dataPath path of the data to retrieve
*/
getData(dataPath) {
const path = this.processDataPath(dataPath);
return this.retrieveData(path, false);
}
/**
* Same as getData only here it's directly typed to your object
* @param dataPath path of the data to retrieve
*/
getObject(dataPath) {
return this.getData(dataPath);
}
/**
* Check for existing datapath
* @param dataPath
*/
exists(dataPath) {
try {
this.getData(dataPath);
return true;
}
catch (e) {
if (e instanceof Errors_1.DataError) {
return false;
}
throw e;
}
}
/**
* Returns the number of element which constitutes the array
* @param dataPath
*/
count(dataPath) {
const result = this.getData(dataPath);
if (!Array.isArray(result)) {
throw new Errors_1.DataError(`DataPath: ${dataPath} is not an array.`, 11);
}
const path = this.processDataPath(dataPath);
const data = this.retrieveData(path, false);
return data.length;
}
/**
* Returns the index of the object that meets the criteria submitted. Returns -1, if no match is found.
* @param dataPath base dataPath from where to start searching
* @param searchValue value to look for in the dataPath
* @param propertyName name of the property to look for searchValue
*/
getIndex(dataPath, searchValue, propertyName = 'id') {
const data = this.getArrayData(dataPath);
return data.map(function (element) { return element[propertyName]; }).indexOf(searchValue);
}
/**
* Return the index of the value inside the array. Returns -1, if no match is found.
* @param dataPath base dataPath from where to start searching
* @param searchValue value to look for in the dataPath
*/
getIndexValue(dataPath, searchValue) {
return this.getArrayData(dataPath).indexOf(searchValue);
}
getArrayData(dataPath) {
const result = this.getData(dataPath);
if (!Array.isArray(result)) {
throw new Errors_1.DataError(`DataPath: ${dataPath} is not an array.`, 11);
}
const path = this.processDataPath(dataPath);
return this.retrieveData(path, false);
}
/**
* Find all specific entry in an array/object
* @param rootPath base dataPath from where to start searching
* @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
*/
filter(rootPath, callback) {
const result = this.getData(rootPath);
if (Array.isArray(result)) {
return result.filter(callback);
}
if (result instanceof Object) {
const entries = Object.entries(result);
const found = entries.filter((entry) => {
return callback(entry[1], entry[0]);
});
if (!found || found.length < 1) {
return undefined;
}
return found.map((entry) => {
return entry[1];
});
}
throw new Errors_1.DataError("The entry at the path (" + rootPath + ") needs to be either an Object or an Array", 12);
}
/**
* Find a specific entry in an array/object
* @param rootPath base dataPath from where to start searching
* @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
*/
find(rootPath, callback) {
const result = this.getData(rootPath);
if (Array.isArray(result)) {
return result.find(callback);
}
if (result instanceof Object) {
const entries = Object.entries(result);
const found = entries.find((entry) => {
return callback(entry[1], entry[0]);
});
if (!found || found.length < 2) {
return undefined;
}
return found[1];
}
throw new Errors_1.DataError("The entry at the path (" + rootPath + ") needs to be either an Object or an Array", 12);
}
/**
* Pushing data into the database
* @param dataPath path leading to the data
* @param data data to push
* @param override overriding or not the data, if not, it will merge them
*/
push(dataPath, data, override = true) {
const dbData = this.getParentData(dataPath, true);
if (!dbData) {
throw new Error("Data not found");
}
let toSet = data;
if (!override) {
if (Array.isArray(data)) {
let storedData = dbData.getData();
if (storedData === undefined) {
storedData = [];
}
else if (!Array.isArray(storedData)) {
throw new Errors_1.DataError("Can't merge another type of data with an Array", 3);
}
toSet = storedData.concat(data);
}
else if (data === Object(data)) {
if (Array.isArray(dbData.getData())) {
throw new Errors_1.DataError("Can't merge an Array with an Object", 4);
}
toSet = (0, Utils_1.merge)(dbData.getData(), data);
}
}
dbData.setData(toSet);
if (this.config.saveOnPush) {
this.save();
}
}
/**
* Delete the data
* @param dataPath path leading to the data
*/
delete(dataPath) {
const dbData = this.getParentData(dataPath, true);
if (!dbData) {
return;
}
dbData.delete();
if (this.config.saveOnPush) {
this.save();
}
}
/**
* Only use this if you know what you're doing.
* It reset the full data of the database.
* @param data
*/
resetData(data) {
this.data = data;
}
/**
* Reload the database from the file
*/
reload() {
this.loaded = false;
this.load();
}
;
/**
* Manually load the database
* It is automatically called when the first getData is done
*/
load() {
if (this.loaded) {
return;
}
try {
const data = FS.readFileSync(this.config.filename, 'utf8');
this.data = JSON.parse(data);
this.loaded = true;
}
catch (err) {
const error = new Errors_1.DatabaseError("Can't Load Database", 1, err);
throw error;
}
}
/**
* Manually save the database
* By default you can't save the database if it's not loaded
* @param force force the save of the database
*/
save(force) {
force = force || false;
if (!force && !this.loaded) {
throw new Errors_1.DatabaseError("DataBase not loaded. Can't write", 7);
}
let data = "";
try {
if (this.config.humanReadable) {
data = JSON.stringify(this.data, null, 4);
}
else {
data = JSON.stringify(this.data);
}
FS.writeFileSync(this.config.filename, data, 'utf8');
}
catch (err) {
const error = new Errors_1.DatabaseError("Can't save the database", 2, err);
throw error;
}
}
}
exports.JsonDB = JsonDB;
//# sourceMappingURL=JsonDB.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,40 @@
import { KeyValue } from "./Utils";
export declare const arrayRegex: () => RegExp;
export declare class ArrayInfo {
readonly property: string;
readonly index: number;
readonly append: boolean;
constructor(property: string, index: any);
/**
* Check if the property want to access an Array
* @param property
*/
static processArray(property?: string): ArrayInfo | null;
/**
* Get the index for the array
* @param data
* @param avoidProperty
*/
getIndex(data: KeyValue, avoidProperty?: boolean): number;
/**
* Get the Data
* @param data
*/
getData(data: KeyValue): any;
/**
* Set the data for the array
* @param data
* @param value
*/
setData(data: KeyValue, value: any): void;
/**
* Delete the index from the array
* @param data
*/
delete(data: KeyValue): void;
/**
* Check if the ArrayInfo is valid for the given data
* @param data
*/
isValid(data: KeyValue): boolean;
}
@@ -0,0 +1,112 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayInfo = exports.arrayRegex = void 0;
const Errors_1 = require("./Errors");
function isInt(value) {
return !isNaN(value) &&
Number(value) == value && !isNaN(parseInt(value, 10));
}
const arrayRegex = () => /^([\.0-9a-zA-Z_$\-][0-9a-zA-Z_\-$\.]*)\[((?!(\]|\[)).*|)\]$/gm;
exports.arrayRegex = arrayRegex;
const regexCache = {};
class ArrayInfo {
property;
index = 0;
append = false;
constructor(property, index) {
this.property = property;
this.append = index === "";
if (isInt(index)) {
this.index = parseInt(index);
}
else if (!this.append) {
throw new Errors_1.DataError("Only numerical values accepted for array index", 200);
}
}
/**
* Check if the property want to access an Array
* @param property
*/
static processArray(property) {
if (typeof property === 'undefined') {
return null;
}
if (regexCache[property]) {
return regexCache[property];
}
const arrayIndexRegex = (0, exports.arrayRegex)();
const match = arrayIndexRegex.exec(property.trim());
if (match != null) {
return (regexCache[property] = new ArrayInfo(match[1], match[2]));
}
return null;
}
/**
* Get the index for the array
* @param data
* @param avoidProperty
*/
getIndex(data, avoidProperty) {
if (avoidProperty === undefined) {
avoidProperty = false;
}
if (this.append) {
return -1;
}
const index = this.index;
if (index == -1) {
const dataIterable = avoidProperty ? data : data[this.property];
if (dataIterable.length === 0) {
return 0;
}
return dataIterable.length - 1;
}
return index;
}
/**
* Get the Data
* @param data
*/
getData(data) {
if (this.append) {
throw new Errors_1.DataError("Can't get data when appending", 100);
}
const index = this.getIndex(data);
return data[this.property][index];
}
/**
* Set the data for the array
* @param data
* @param value
*/
setData(data, value) {
if (this.append) {
data[this.property].push(value);
}
else {
const index = this.getIndex(data);
data[this.property][index] = value;
}
}
/**
* Delete the index from the array
* @param data
*/
delete(data) {
if (this.append) {
throw new Errors_1.DataError("Can't delete an appended data", 10);
}
const index = this.getIndex(data);
data[this.property].splice(index, 1);
}
/**
* Check if the ArrayInfo is valid for the given data
* @param data
*/
isValid(data) {
const index = this.getIndex(data);
return data[this.property].hasOwnProperty(index);
}
}
exports.ArrayInfo = ArrayInfo;
//# sourceMappingURL=ArrayInfo.js.map
@@ -0,0 +1 @@
{"version":3,"file":"ArrayInfo.js","sourceRoot":"","sources":["../../src/lib/ArrayInfo.ts"],"names":[],"mappings":";;;AAAA,qCAAkC;AAGlC,SAAS,KAAK,CAAC,KAAU;IACrB,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC;QAChB,MAAM,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAA;AAC7D,CAAC;AAEM,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC,+DAA+D,CAAA;AAAlF,QAAA,UAAU,cAAwE;AAC/F,MAAM,UAAU,GAAG,EAAc,CAAA;AAEjC,MAAa,SAAS;IACT,QAAQ,CAAQ;IAChB,KAAK,GAAW,CAAC,CAAA;IACjB,MAAM,GAAY,KAAK,CAAA;IAGhC,YAAY,QAAgB,EAAE,KAAU;QACpC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,MAAM,GAAG,KAAK,KAAK,EAAE,CAAA;QAC1B,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;YACd,IAAI,CAAC,KAAK,GAAG,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0,0 +1,27 @@
import { KeyValue } from "./Utils";
import { JsonDB } from "../JsonDB";
export declare class DBParentData {
readonly parent?: string;
readonly data: KeyValue;
readonly db: JsonDB;
readonly dataPath: string;
constructor(data: any, db: JsonDB, dataPath: string, parent?: string);
/**
* Check if it's an array
* @param deletion
*/
private checkArray;
/**
* Get the data linked to this path
*/
getData(): any;
/**
* Set the data to the wanted path
* @param toSet
*/
setData(toSet: any): void;
/**
* Delete the data of the current path
*/
delete(): void;
}
@@ -0,0 +1,84 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DBParentData = void 0;
const ArrayInfo_1 = require("./ArrayInfo");
const Errors_1 = require("./Errors");
class DBParentData {
parent;
data;
db;
dataPath;
constructor(data, db, dataPath, parent) {
this.parent = parent;
this.data = data;
this.db = db;
this.dataPath = dataPath;
}
/**
* Check if it's an array
* @param deletion
*/
checkArray(deletion = false) {
const arrayInfo = ArrayInfo_1.ArrayInfo.processArray(this.parent);
if (arrayInfo && (!arrayInfo.append || deletion) && !arrayInfo.isValid(this.data)) {
throw new Errors_1.DataError("DataPath: /" + this.dataPath + ". Can't find index " + arrayInfo.index + " in array " + arrayInfo.property, 10);
}
return arrayInfo;
}
/**
* Get the data linked to this path
*/
getData() {
if (this.parent === undefined) {
return this.data;
}
const arrayInfo = this.checkArray();
if (arrayInfo) {
return arrayInfo.getData(this.data);
}
else {
return this.data[this.parent];
}
}
/**
* Set the data to the wanted path
* @param toSet
*/
setData(toSet) {
if (this.parent === undefined) {
this.db.resetData(toSet);
return;
}
const arrayInfo = ArrayInfo_1.ArrayInfo.processArray(this.parent);
if (arrayInfo) {
if (!this.data.hasOwnProperty(arrayInfo.property)) {
this.data[arrayInfo.property] = [];
}
else if (!Array.isArray(this.data[arrayInfo.property])) {
throw new Errors_1.DataError("DataPath: /" + this.dataPath + ". " + arrayInfo.property + " is not an array.", 11);
}
arrayInfo.setData(this.data, toSet);
}
else {
this.data[this.parent] = toSet;
}
}
/**
* Delete the data of the current path
*/
delete() {
if (this.parent === undefined) {
this.db.resetData({});
return;
}
const arrayInfo = this.checkArray(true);
if (arrayInfo) {
arrayInfo.delete(this.data);
}
else {
delete this.data[this.parent];
}
}
}
exports.DBParentData = DBParentData;
//# sourceMappingURL=DBParentData.js.map
@@ -0,0 +1 @@
{"version":3,"file":"DBParentData.js","sourceRoot":"","sources":["../../src/lib/DBParentData.ts"],"names":[],"mappings":";;;AAAA,2CAAqC;AACrC,qCAAkC;AAIlC,MAAa,YAAY;IACZ,MAAM,CAAS;IACf,IAAI,CAAU;IACd,EAAE,CAAQ;IACV,QAAQ,CAAQ;IAGzB,YAAY,IAAS,EAAE,EAAU,EAAE,QAAgB,EAAE,MAAe;QAChE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAA;QACZ,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAC5B,CAAC;IAED;;;OAGG;IACK,UAAU,CAAC,WAAoB,KAAK;QACxC,MAAM,SAAS,GAAG,qBAAS,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACrD,IAAI,SAAS,IAAI,CAAC,CAAC,SAAS,CAAC,MAAM,IAAI,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YAC/E,MAAM,IAAI,kBAAS,CAAC,aAAa,GAAG,IAAI,CAAC,QAAQ,GAAG,qBAAqB,GAAG,SAAS,CAAC,KAAK,GAAG,YAAY,GAAG,SAAS,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA;SACvI;QAED,OAAO,SAAS,CAAA;IACpB,CAAC;IAED;;OAEG;IACI,OAAO;QACV,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,EAAE;YAC3B,OAAO,IAAI,CAAC,IAAI,CAAA;SACnB;QACD,MAAM,SAAS,GAAG,IAAI,CAAC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,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QACvC,IAAI,SAAS,EAAE;YACX,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;SAC9B;aAAM;YACH,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;SAChC;IACL,CAAC;CACJ;AAjFD,oCAiFC"}
@@ -0,0 +1,10 @@
export declare abstract class NestedError extends Error {
readonly inner?: Error;
readonly id: Number;
constructor(message: string, id: Number, inner?: Error);
toString(): string;
}
export declare class DatabaseError extends NestedError {
}
export declare class DataError extends NestedError {
}
+28
View File
@@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataError = exports.DatabaseError = exports.NestedError = void 0;
class NestedError extends Error {
inner;
id;
constructor(message, id, inner) {
super(message);
this.inner = inner;
this.id = id;
this.name = this.constructor.name;
}
toString() {
const string = this.name + ": " + this.message;
if (this.inner) {
return string + ':\n' + this.inner;
}
return string;
}
}
exports.NestedError = NestedError;
class DatabaseError extends NestedError {
}
exports.DatabaseError = DatabaseError;
class DataError extends NestedError {
}
exports.DataError = DataError;
//# sourceMappingURL=Errors.js.map
@@ -0,0 +1 @@
{"version":3,"file":"Errors.js","sourceRoot":"","sources":["../../src/lib/Errors.ts"],"names":[],"mappings":";;;AAAA,MAAsB,WAAY,SAAQ,KAAK;IAClC,KAAK,CAAQ;IACb,EAAE,CAAQ;IAGnB,YAAmB,OAAe,EAAE,EAAU,EAAE,KAAa;QACzD,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAA;QACZ,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA;IACrC,CAAC;IAGD,QAAQ;QACJ,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC,OAAO,CAAA;QAC9C,IAAI,IAAI,CAAC,KAAK,EAAE;YACZ,OAAO,MAAM,GAAG,KAAK,GAAG,IAAI,CAAC,KAAK,CAAA;SACrC;QACD,OAAO,MAAM,CAAA;IACjB,CAAC;CACJ;AApBD,kCAoBC;AAED,MAAa,aAAc,SAAQ,WAAW;CAC7C;AADD,sCACC;AAED,MAAa,SAAU,SAAQ,WAAW;CACzC;AADD,8BACC"}
@@ -0,0 +1,13 @@
export interface JsonDBConfig {
filename: string;
saveOnPush: boolean;
humanReadable: boolean;
separator: string;
}
export declare class Config implements JsonDBConfig {
filename: string;
humanReadable: boolean;
saveOnPush: boolean;
separator: string;
constructor(filename: string, saveOnPush?: boolean, humanReadable?: boolean, separator?: string);
}
@@ -0,0 +1,22 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Config = void 0;
const path = require("path");
class Config {
filename;
humanReadable;
saveOnPush;
separator;
constructor(filename, saveOnPush = true, humanReadable = false, separator = '/') {
this.filename = filename;
// Force json if no extension
if (path.extname(filename) === "") {
this.filename += ".json";
}
this.humanReadable = humanReadable;
this.saveOnPush = saveOnPush;
this.separator = separator;
}
}
exports.Config = Config;
//# sourceMappingURL=JsonDBConfig.js.map
@@ -0,0 +1 @@
{"version":3,"file":"JsonDBConfig.js","sourceRoot":"","sources":["../../src/lib/JsonDBConfig.ts"],"names":[],"mappings":";;;AAAA,6BAA6B;AAS7B,MAAa,MAAM;IACjB,QAAQ,CAAQ;IAChB,aAAa,CAAS;IACtB,UAAU,CAAS;IACnB,SAAS,CAAQ;IAGjB,YAAY,QAAgB,EAAE,aAAsB,IAAI,EAAE,gBAAyB,KAAK,EAAE,YAAoB,GAAG;QAC/G,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QAExB,6BAA6B;QAC7B,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE;YACjC,IAAI,CAAC,QAAQ,IAAI,OAAO,CAAA;SACzB;QAED,IAAI,CAAC,aAAa,GAAG,aAAa,CAAA;QAClC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;IAC5B,CAAC;CACF;AAnBD,wBAmBC"}
@@ -0,0 +1,5 @@
export interface KeyValue {
[key: string]: any;
}
export declare const merge: (...arrays: KeyValue[]) => KeyValue;
export declare const removeTrailingChar: (dataPath: string, trailing: string) => string;
+36
View File
@@ -0,0 +1,36 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeTrailingChar = exports.merge = void 0;
const merge = (...arrays) => {
const destination = {};
arrays.forEach(function (source) {
var prop;
for (prop in source) {
if (prop in destination && destination[prop] === null) {
destination[prop] = source[prop];
}
else if (prop in destination && Array.isArray(destination[prop])) {
// Concat Arrays
destination[prop] = destination[prop].concat(source[prop]);
}
else if (prop in destination && typeof destination[prop] === "object") {
// Merge Objects
destination[prop] = (0, exports.merge)(destination[prop], source[prop]);
}
else {
// Set new values
destination[prop] = source[prop];
}
}
});
return destination;
};
exports.merge = merge;
const removeTrailingChar = (dataPath, trailing) => {
if (dataPath.length > 1 && dataPath.endsWith(trailing)) {
return dataPath.substring(0, dataPath.length - 1);
}
return dataPath;
};
exports.removeTrailingChar = removeTrailingChar;
//# sourceMappingURL=Utils.js.map
@@ -0,0 +1 @@
{"version":3,"file":"Utils.js","sourceRoot":"","sources":["../../src/lib/Utils.ts"],"names":[],"mappings":";;;AAIO,MAAM,KAAK,GAAG,CAAC,GAAG,MAAkB,EAAY,EAAE;IACrD,MAAM,WAAW,GAAG,EAAc,CAAA;IAClC,MAAM,CAAC,OAAO,CAAC,UAAU,MAAM;QAC3B,IAAI,IAAI,CAAA;QACR,KAAK,IAAI,IAAI,MAAM,EAAE;YAEjB,IAAI,IAAI,IAAI,WAAW,IAAI,WAAW,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE;gBACnD,WAAW,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,CAA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