twin-db 1.1.1 → 1.2.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 CHANGED
@@ -86,3 +86,10 @@ database.pull('hobbies', 'cs', 'sleep') // now the hobbies is ["pizza", "valoran
86
86
  }
87
87
  }
88
88
  ```
89
+ ## Updates
90
+ `MM/DD/YYYY`<br>
91
+ 04/09/2026 - 1.1.*
92
+ - Now **sum**, **sub** and **push** sets the value to the value you passed in execution when the path does not exists.
93
+
94
+ 05/03/2026 - 1.2.*
95
+ - Now package doesn't uses eval anymore, providing better security.
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { TwinDB } from './structures/database';
1
+ import { TwinDB } from './structures/database.js';
2
2
  export default TwinDB;
@@ -1,18 +1,18 @@
1
1
  export declare class TwinDB {
2
- data: object;
2
+ data: Record<string, unknown>;
3
3
  path: string;
4
4
  name: string;
5
5
  constructor(name?: string);
6
6
  private load;
7
7
  private update;
8
- set(path: string, value: unknown): Promise<object>;
8
+ set(path: string, value: unknown): Record<string, unknown>;
9
9
  get(path: string): unknown | null;
10
- delete(path: string): Promise<object>;
10
+ delete(path: string): Record<string, unknown>;
11
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>;
12
+ sum(path: string, value: number): Record<string, unknown>;
13
+ sub(path: string, value: number): Record<string, unknown>;
14
+ concat(path: string, value: string): Record<string, unknown>;
15
+ push(path: string, ...values: unknown[]): Record<string, unknown>;
16
+ pull(path: string, ...values: unknown[]): Record<string, unknown>;
17
17
  }
18
18
  export type SumOrSub = 'sum' | 'sub';
@@ -7,11 +7,12 @@ export class TwinDB {
7
7
  constructor(name = 'db') {
8
8
  this.path = 'database/' + name + '.json';
9
9
  this.name = name;
10
+ this.data = {};
10
11
  if (!existsSync('database'))
11
12
  mkdirSync('database');
12
13
  this.load();
13
14
  }
14
- async load() {
15
+ load() {
15
16
  let data;
16
17
  try {
17
18
  data = readFileSync(this.path, 'utf8');
@@ -23,19 +24,20 @@ export class TwinDB {
23
24
  if (!data)
24
25
  writeFileSync(this.path, JSON.stringify(this.data, null, 2), 'utf8');
25
26
  }
26
- async update(path /*user.info.name*/, value) {
27
+ update(path /*user.info.name*/, value) {
27
28
  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)}`);
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
+ }
39
41
  writeFileSync(this.path, JSON.stringify(this.data, null, 2), 'utf8');
40
42
  return this.data;
41
43
  }
@@ -61,16 +63,14 @@ export class TwinDB {
61
63
  current = current[key];
62
64
  }
63
65
  }
64
- async delete(path) {
66
+ delete(path) {
65
67
  if (!path || typeof path !== 'string')
66
68
  throw new Error(pathErrorMessage);
67
69
  const keys = path.split('.');
68
70
  const pathExists = this.get(path);
69
71
  if (pathExists === null)
70
72
  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');
73
+ this.update(path, null);
74
74
  writeFileSync(this.path, JSON.stringify(this.data, null, 2), 'utf8');
75
75
  return this.data;
76
76
  }
@@ -121,7 +121,7 @@ export class TwinDB {
121
121
  throw new Error('You must provide a value to update');
122
122
  const currentValue = this.get(path);
123
123
  if (!Array.isArray(currentValue))
124
- throw new Error('The value to pull is not an array or the path does not exists');
124
+ throw new Error('The current value of this path is not an array or the path does not exists');
125
125
  for (const value of values) {
126
126
  const index = currentValue.indexOf(value);
127
127
  if (index < 0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twin-db",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "A usefully local database.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -29,6 +29,7 @@
29
29
  ],
30
30
  "license": "MIT",
31
31
  "devDependencies": {
32
+ "@types/node": "^25.6.0",
32
33
  "prettier": "^3.8.1",
33
34
  "tsx": "^4.21.0",
34
35
  "typescript": "^6.0.2"