twin-db 1.0.2 → 1.1.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 ADDED
@@ -0,0 +1,88 @@
1
+ ### Before All
2
+ Make sure you're using `"type": "module"` in package.json.<br>
3
+ Make sure to pass just values that JSON accepts, if not, the values will be changed to `null`.
4
+ # How to Use?
5
+ To create a new database make the following step:
6
+ ```js
7
+ import TwinDB from 'twin-db';
8
+ const database = new TwinDB('db');
9
+ ```
10
+ And your database is done.
11
+ ### You can make various databases too:
12
+ ```js
13
+ import TwinDB from 'twin-db';
14
+ const database = new TwinDB('db');
15
+ const coolDatabase = new TwinDB('cool');
16
+ ```
17
+ # Database Methods
18
+ Now we going to explain you the database methods.<br>
19
+ Imagine the following data from a random database:
20
+ ```json
21
+ {
22
+ "name": "Daniel",
23
+ "surname": "Costa",
24
+ "age": 60,
25
+ "cool": false,
26
+ "hobbies": ["cs", "pizza"],
27
+ "address": {
28
+ "city": "I dont",
29
+ "state": "ES",
30
+ "country": "Brazil"
31
+ }
32
+ }
33
+ ```
34
+ ### 1. Set Method
35
+ This one changes the value of a path to the value you passed.
36
+ ```js
37
+ database.set('name', 'De'); // now the name is "De".
38
+ database.set('age', 50); // now the age is 50.
39
+ ```
40
+ - To go through the object, make sure to use ".", like `address.city`.
41
+ ### 2. Get Method
42
+ Get a value from a given path.
43
+ ```js
44
+ database.get('surname'); // returns "Costa".
45
+ ```
46
+ ### 3. Delete Method
47
+ Deletes a value from data.
48
+ ```js
49
+ database.delete('cool') // now the cool value no longer exists.
50
+ database.delete('address.country') // now the country no longer exists too.
51
+ ```
52
+ ### 4. Sum Method
53
+ Sum the current value of the given path with the given value.
54
+ ```js
55
+ database.sum('age', 30); // now the age is 80.
56
+ ```
57
+ ### 5. Sub Method
58
+ Subtract the current value of the given path with the given value.
59
+ ```js
60
+ database.sub('age', 10); // now the age is 70.
61
+ ```
62
+ ### 6. Concat Method
63
+ Concatenate the current value of the given path with the given value.
64
+ ```js
65
+ database.concat('address.city', ' know'); // now the city is "I dont know".
66
+ ```
67
+ ### 7. Push Method
68
+ Push the given values into the current value array.
69
+ ```js
70
+ database.push('hobbies', 'sleep', 'valorant'); // now the hobbies is ["cs", "pizza", "sleep", "valorant"].
71
+ ```
72
+ ### 8. Pull Method
73
+ ```js
74
+ database.pull('hobbies', 'cs', 'sleep') // now the hobbies is ["pizza", "valorant"].
75
+ ```
76
+ ### Now the data object will looks like that:
77
+ ```json
78
+ {
79
+ "name": "De",
80
+ "surname": "Costa",
81
+ "age": 70,
82
+ "hobbies": ["pizza", "valorant"],
83
+ "address": {
84
+ "city": "I dont know",
85
+ "state": "ES"
86
+ }
87
+ }
88
+ ```
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { TwinDB } from './structures/database.js';
1
+ import { TwinDB } from './structures/database';
2
2
  export default TwinDB;
@@ -33,7 +33,7 @@ export class TwinDB {
33
33
  if (typeof searchedValue !== 'object')
34
34
  await eval('this.data' + mappedKeys.join('') + ' = {}');
35
35
  } // percorre todos os objetos definindo eles como {} para não dar erro abaixo
36
- await eval(`this.data` +
36
+ await eval('this.data' +
37
37
  keys.map((key) => `['${key}']`).join('') +
38
38
  ` = ${JSON.stringify(value)}`);
39
39
  writeFileSync(this.path, JSON.stringify(this.data, null, 2), 'utf8');
@@ -82,9 +82,9 @@ export class TwinDB {
82
82
  const isSum = type === 'sum';
83
83
  if (!value || typeof value !== 'number')
84
84
  throw new Error(`The value to ${isSum ? 'sum' : 'sub'} must be a number`);
85
- const currentValue = this.get(path);
85
+ let currentValue = (this.get(path) || 0);
86
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`);
87
+ currentValue = 0;
88
88
  return this.update(path, isSum ? currentValue + value : currentValue - value);
89
89
  }
90
90
  sum(path, value) {
@@ -108,9 +108,9 @@ export class TwinDB {
108
108
  throw new Error(pathErrorMessage);
109
109
  if (!values || values.length === 0)
110
110
  throw new Error('You must provide a value to update');
111
- const currentValue = this.get(path);
111
+ let currentValue = (this.get(path) || []);
112
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');
113
+ currentValue = [];
114
114
  currentValue.push(...values);
115
115
  return this.update(path, currentValue);
116
116
  }
@@ -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 current value of this path is not an array or the path does not exists');
124
+ throw new Error('The value to pull 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.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "A usefully local database.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",