twin-db 1.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/LICENSE.md +21 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/structures/database.d.ts +18 -0
- package/dist/structures/database.js +133 -0
- package/package.json +33 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jaelson Kaciel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class TwinDB {
|
|
2
|
+
data: object;
|
|
3
|
+
path: string;
|
|
4
|
+
name: string;
|
|
5
|
+
constructor(name?: string);
|
|
6
|
+
private load;
|
|
7
|
+
private update;
|
|
8
|
+
set(path: string, value: unknown): Promise<object>;
|
|
9
|
+
get(path: string): unknown | null;
|
|
10
|
+
delete(path: string): Promise<object>;
|
|
11
|
+
private sumOrSub;
|
|
12
|
+
sum(path: string, value: number): Promise<object>;
|
|
13
|
+
sub(path: string, value: number): Promise<object>;
|
|
14
|
+
concat(path: string, value: string): Promise<object>;
|
|
15
|
+
push(path: string, ...values: unknown[]): Promise<object>;
|
|
16
|
+
pull(path: string, ...values: unknown[]): Promise<object>;
|
|
17
|
+
}
|
|
18
|
+
export type SumOrSub = 'sum' | 'sub';
|
|
@@ -0,0 +1,133 @@
|
|
|
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
|
+
if (!existsSync('database'))
|
|
11
|
+
mkdirSync('database');
|
|
12
|
+
this.load();
|
|
13
|
+
}
|
|
14
|
+
async load() {
|
|
15
|
+
let data;
|
|
16
|
+
try {
|
|
17
|
+
data = readFileSync(this.path, 'utf8');
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
data = '';
|
|
21
|
+
}
|
|
22
|
+
this.data = data ? JSON.parse(data) : {};
|
|
23
|
+
if (!data)
|
|
24
|
+
writeFileSync(this.path, JSON.stringify(this.data, null, 2), 'utf8');
|
|
25
|
+
}
|
|
26
|
+
async update(path /*user.info.name*/, value) {
|
|
27
|
+
const keys = path.split('.');
|
|
28
|
+
if (keys.length > 1)
|
|
29
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
30
|
+
const thisKeys = keys.slice(0, i + 1);
|
|
31
|
+
const mappedKeys = thisKeys.map((key) => `['${key}']`);
|
|
32
|
+
const searchedValue = await eval('this.data' + mappedKeys.join(''));
|
|
33
|
+
if (typeof searchedValue !== 'object')
|
|
34
|
+
await eval('this.data' + mappedKeys.join('') + ' = {}');
|
|
35
|
+
} // percorre todos os objetos definindo eles como {} para não dar erro abaixo
|
|
36
|
+
await eval(`this.data` +
|
|
37
|
+
keys.map((key) => `['${key}']`).join('') +
|
|
38
|
+
` = ${JSON.stringify(value)}`);
|
|
39
|
+
writeFileSync(this.path, JSON.stringify(this.data, null, 2), 'utf8');
|
|
40
|
+
return this.data;
|
|
41
|
+
}
|
|
42
|
+
set(path, value) {
|
|
43
|
+
if (!path || typeof path !== 'string')
|
|
44
|
+
throw new Error(pathErrorMessage);
|
|
45
|
+
if (value === undefined)
|
|
46
|
+
throw new Error('You must provide a value to update');
|
|
47
|
+
return this.update(path, value);
|
|
48
|
+
}
|
|
49
|
+
get(path) {
|
|
50
|
+
if (!path || typeof path !== 'string')
|
|
51
|
+
throw new Error(pathErrorMessage);
|
|
52
|
+
const keys = path.split('.');
|
|
53
|
+
let current = this.data;
|
|
54
|
+
let i = 0;
|
|
55
|
+
for (const key of keys) {
|
|
56
|
+
i++;
|
|
57
|
+
if (i === keys.length)
|
|
58
|
+
return current[key] ?? null;
|
|
59
|
+
if (typeof current[key] !== 'object')
|
|
60
|
+
return null;
|
|
61
|
+
current = current[key];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async delete(path) {
|
|
65
|
+
if (!path || typeof path !== 'string')
|
|
66
|
+
throw new Error(pathErrorMessage);
|
|
67
|
+
const keys = path.split('.');
|
|
68
|
+
const pathExists = this.get(path);
|
|
69
|
+
if (pathExists === null)
|
|
70
|
+
throw new Error('The path does not exists or its value is null');
|
|
71
|
+
const success = await eval('delete this.data' + keys.map((key) => `['${key}']`).join(''));
|
|
72
|
+
if (!success)
|
|
73
|
+
throw new Error('Could not delete the value');
|
|
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
|
+
const currentValue = this.get(path);
|
|
86
|
+
if (typeof currentValue !== 'number')
|
|
87
|
+
throw new Error(`The value to ${isSum ? 'sum' : 'sub'} is not a number or the path does not exists`);
|
|
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
|
+
const currentValue = this.get(path);
|
|
112
|
+
if (!Array.isArray(currentValue))
|
|
113
|
+
throw new Error('The current value of this path is not an array or the path does not exists');
|
|
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
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "twin-db",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A usefully local database.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": ["dist"],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "tsx src/index.ts",
|
|
11
|
+
"prettier": "npx prettier --write \"**/*.{js,ts}\"",
|
|
12
|
+
"build": "npx tsc"
|
|
13
|
+
},
|
|
14
|
+
"author": {
|
|
15
|
+
"name": "Jaelson Kaciel",
|
|
16
|
+
"email": "jaelsonkaciel4@gmail.com",
|
|
17
|
+
"url": "https://github.com/toddy007"
|
|
18
|
+
},
|
|
19
|
+
"keywords": ["db", "database", "local", "localdb", "localdatabase", "twin"],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"prettier": "^3.8.1",
|
|
23
|
+
"tsx": "^4.21.0",
|
|
24
|
+
"typescript": "^6.0.2"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/toddy007/twin-db.git"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=21.6.2"
|
|
32
|
+
}
|
|
33
|
+
}
|