twin-db 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -18
- package/dist/index.cjs +402 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +80 -0
- package/dist/index.d.ts +80 -2
- package/dist/index.js +362 -2
- package/dist/index.js.map +1 -0
- package/package.json +22 -4
- package/dist/structures/database.d.ts +0 -18
- package/dist/structures/database.js +0 -133
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs';
|
|
2
|
-
const pathErrorMessage = 'The path must be a string or you dont provide a path';
|
|
3
|
-
export class TwinDB {
|
|
4
|
-
data;
|
|
5
|
-
path;
|
|
6
|
-
name;
|
|
7
|
-
constructor(name = 'db') {
|
|
8
|
-
this.path = 'database/' + name + '.json';
|
|
9
|
-
this.name = name;
|
|
10
|
-
this.data = {};
|
|
11
|
-
if (!existsSync('database'))
|
|
12
|
-
mkdirSync('database');
|
|
13
|
-
this.load();
|
|
14
|
-
}
|
|
15
|
-
load() {
|
|
16
|
-
let data;
|
|
17
|
-
try {
|
|
18
|
-
data = readFileSync(this.path, 'utf8');
|
|
19
|
-
}
|
|
20
|
-
catch (e) {
|
|
21
|
-
data = '';
|
|
22
|
-
}
|
|
23
|
-
this.data = data ? JSON.parse(data) : {};
|
|
24
|
-
if (!data)
|
|
25
|
-
writeFileSync(this.path, JSON.stringify(this.data, null, 2), 'utf8');
|
|
26
|
-
}
|
|
27
|
-
update(path /*user.info.name*/, value) {
|
|
28
|
-
const keys = path.split('.');
|
|
29
|
-
let current = this.data;
|
|
30
|
-
for (let i = 0; i < keys.length; i++) {
|
|
31
|
-
let key = keys[i];
|
|
32
|
-
if (i === keys.length - 1) {
|
|
33
|
-
current[key] = value;
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
if (typeof current[key] !== 'object' || Array.isArray(current[key]) || !current[key]) {
|
|
37
|
-
current[key] = {};
|
|
38
|
-
}
|
|
39
|
-
current = current[key];
|
|
40
|
-
}
|
|
41
|
-
writeFileSync(this.path, JSON.stringify(this.data, null, 2), 'utf8');
|
|
42
|
-
return this.data;
|
|
43
|
-
}
|
|
44
|
-
set(path, value) {
|
|
45
|
-
if (!path || typeof path !== 'string')
|
|
46
|
-
throw new Error(pathErrorMessage);
|
|
47
|
-
if (value === undefined)
|
|
48
|
-
throw new Error('You must provide a value to update');
|
|
49
|
-
return this.update(path, value);
|
|
50
|
-
}
|
|
51
|
-
get(path) {
|
|
52
|
-
if (!path || typeof path !== 'string')
|
|
53
|
-
throw new Error(pathErrorMessage);
|
|
54
|
-
const keys = path.split('.');
|
|
55
|
-
let current = this.data;
|
|
56
|
-
let i = 0;
|
|
57
|
-
for (const key of keys) {
|
|
58
|
-
i++;
|
|
59
|
-
if (i === keys.length)
|
|
60
|
-
return current[key] ?? null;
|
|
61
|
-
if (typeof current[key] !== 'object')
|
|
62
|
-
return null;
|
|
63
|
-
current = current[key];
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
delete(path) {
|
|
67
|
-
if (!path || typeof path !== 'string')
|
|
68
|
-
throw new Error(pathErrorMessage);
|
|
69
|
-
const keys = path.split('.');
|
|
70
|
-
const pathExists = this.get(path);
|
|
71
|
-
if (pathExists === null)
|
|
72
|
-
throw new Error('The path does not exists or its value is null');
|
|
73
|
-
this.update(path, null);
|
|
74
|
-
writeFileSync(this.path, JSON.stringify(this.data, null, 2), 'utf8');
|
|
75
|
-
return this.data;
|
|
76
|
-
}
|
|
77
|
-
sumOrSub(path, value, type) {
|
|
78
|
-
if (!path || typeof path !== 'string')
|
|
79
|
-
throw new Error(pathErrorMessage);
|
|
80
|
-
if (!type || !['sum', 'sub'].includes(type))
|
|
81
|
-
throw new Error('The type must be "sum" or "sub"');
|
|
82
|
-
const isSum = type === 'sum';
|
|
83
|
-
if (!value || typeof value !== 'number')
|
|
84
|
-
throw new Error(`The value to ${isSum ? 'sum' : 'sub'} must be a number`);
|
|
85
|
-
let currentValue = (this.get(path) || 0);
|
|
86
|
-
if (typeof currentValue !== 'number')
|
|
87
|
-
currentValue = 0;
|
|
88
|
-
return this.update(path, isSum ? currentValue + value : currentValue - value);
|
|
89
|
-
}
|
|
90
|
-
sum(path, value) {
|
|
91
|
-
return this.sumOrSub(path, value, 'sum');
|
|
92
|
-
}
|
|
93
|
-
sub(path, value) {
|
|
94
|
-
return this.sumOrSub(path, value, 'sub');
|
|
95
|
-
}
|
|
96
|
-
concat(path, value) {
|
|
97
|
-
if (!path || typeof path !== 'string')
|
|
98
|
-
throw new Error(pathErrorMessage);
|
|
99
|
-
if (!value || typeof value !== 'string')
|
|
100
|
-
throw new Error('You must provide a string value to update');
|
|
101
|
-
const currentValue = this.get(path);
|
|
102
|
-
if (typeof currentValue !== 'string')
|
|
103
|
-
throw new Error('The value to concat is not a string or the path does not exists');
|
|
104
|
-
return this.update(path, currentValue + value);
|
|
105
|
-
}
|
|
106
|
-
push(path, ...values) {
|
|
107
|
-
if (!path || typeof path !== 'string')
|
|
108
|
-
throw new Error(pathErrorMessage);
|
|
109
|
-
if (!values || values.length === 0)
|
|
110
|
-
throw new Error('You must provide a value to update');
|
|
111
|
-
let currentValue = (this.get(path) || []);
|
|
112
|
-
if (!Array.isArray(currentValue))
|
|
113
|
-
currentValue = [];
|
|
114
|
-
currentValue.push(...values);
|
|
115
|
-
return this.update(path, currentValue);
|
|
116
|
-
}
|
|
117
|
-
pull(path, ...values) {
|
|
118
|
-
if (!path || typeof path !== 'string')
|
|
119
|
-
throw new Error(pathErrorMessage);
|
|
120
|
-
if (!values || values.length === 0)
|
|
121
|
-
throw new Error('You must provide a value to update');
|
|
122
|
-
const currentValue = this.get(path);
|
|
123
|
-
if (!Array.isArray(currentValue))
|
|
124
|
-
throw new Error('The current value of this path is not an array or the path does not exists');
|
|
125
|
-
for (const value of values) {
|
|
126
|
-
const index = currentValue.indexOf(value);
|
|
127
|
-
if (index < 0)
|
|
128
|
-
continue;
|
|
129
|
-
currentValue.splice(index, 1);
|
|
130
|
-
}
|
|
131
|
-
return this.update(path, currentValue);
|
|
132
|
-
}
|
|
133
|
-
}
|