@hedystia/db 1.3.7 → 1.4.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.
Files changed (4) hide show
  1. package/index.d.ts +53 -50
  2. package/index.js +46 -10
  3. package/package.json +1 -1
  4. package/readme.md +39 -13
package/index.d.ts CHANGED
@@ -1,50 +1,53 @@
1
- interface Table {
2
- columns: string[];
3
- records: {
4
- [key: string]: string;
5
- }[];
6
- }
7
- interface QueueItem {
8
- method: string;
9
- table: string;
10
- record?: {
11
- [key: string]: string;
12
- };
13
- query?: {
14
- [key: string]: string;
15
- };
16
- newData?: {
17
- [key: string]: string;
18
- };
19
- }
20
- export default class DataBase {
21
- queue: QueueItem[];
22
- password: string;
23
- filePath: string;
24
- tables: {
25
- [key: string]: Table;
26
- };
27
- constructor(filePath: string, password: string);
28
- createTable(tableName: string, columns: string[]): void;
29
- deleteTable(tableName: string): void;
30
- insert(tableName: string, record: {
31
- [key: string]: string;
32
- }): void;
33
- update(tableName: string, query: {
34
- [key: string]: string;
35
- }, newData: {
36
- [key: string]: string;
37
- }): void;
38
- select(tableName: string, query: {
39
- [key: string]: string;
40
- }): unknown;
41
- delete(tableName: string, query: {
42
- [key: string]: string;
43
- }): void;
44
- private processQueue;
45
- private insertTable;
46
- private updateTable;
47
- private deleteFromTable;
48
- private saveToFile;
49
- private readFromFile;
50
- }
1
+ interface Table {
2
+ columns: string[];
3
+ records: {
4
+ [key: string]: any;
5
+ }[];
6
+ }
7
+ interface QueueItem {
8
+ method: string;
9
+ table: string;
10
+ record?: {
11
+ [key: string]: any;
12
+ };
13
+ query?: {
14
+ [key: string]: any;
15
+ };
16
+ newData?: {
17
+ [key: string]: any;
18
+ };
19
+ }
20
+ export default class DataBase {
21
+ queue: QueueItem[];
22
+ password: string;
23
+ filePath: string;
24
+ tables: {
25
+ [key: string]: Table;
26
+ };
27
+ constructor(filePath: string, password: string);
28
+ createTable(tableName: string, columns: string[]): void;
29
+ deleteTable(tableName: string): void;
30
+ addColumn(tableName: string, column: string, defaultValue: any): void;
31
+ deleteColumn(tableName: string, column: string): void;
32
+ insert(tableName: string, record: {
33
+ [key: string]: any;
34
+ }): void;
35
+ update(tableName: string, query: {
36
+ [key: string]: any;
37
+ }, newData: {
38
+ [key: string]: any;
39
+ }): void;
40
+ select(tableName: string, query: {
41
+ [key: string]: any;
42
+ }): unknown;
43
+ delete(tableName: string, query: {
44
+ [key: string]: any;
45
+ }): void;
46
+ private processQueue;
47
+ private insertTable;
48
+ private updateTable;
49
+ private deleteFromTable;
50
+ private saveToFile;
51
+ private readFromFile;
52
+ }
53
+ export {};
package/index.js CHANGED
@@ -18,7 +18,7 @@ const Database = class {
18
18
  throw new Error(`Table "${tableName}" already exists.`);
19
19
  }
20
20
 
21
- this.tables[tableName] = {columns, records: []};
21
+ this.tables[tableName] = { columns, records: [] };
22
22
  this.saveToFile();
23
23
  }
24
24
 
@@ -32,15 +32,48 @@ const Database = class {
32
32
  this.saveToFile();
33
33
  }
34
34
 
35
+ addColumn(tableName, column, defaultValue) {
36
+ this.readFromFile();
37
+ if (!this.tables[tableName]) {
38
+ throw new Error(`Table "${tableName}" does not exist.`);
39
+ }
40
+ if (this.tables[tableName].columns.includes(column)) {
41
+ throw new Error(`Column "${column}" already exists in table "${tableName}".`);
42
+ }
43
+
44
+ this.tables[tableName].columns.push(column);
45
+ for (const record of this.tables[tableName].records) {
46
+ record[column] = defaultValue ?? null;
47
+ }
48
+ this.saveToFile();
49
+ }
50
+
51
+ deleteColumn(tableName, column) {
52
+ this.readFromFile();
53
+ if (!this.tables[tableName]) {
54
+ throw new Error(`Table "${tableName}" does not exist.`);
55
+ }
56
+ if (!this.tables[tableName].columns.includes(column)) {
57
+ throw new Error(`Column "${column}" does not exist in table "${tableName}".`);
58
+ }
59
+
60
+ const columnIndex = this.tables[tableName].columns.indexOf(column);
61
+ this.tables[tableName].columns.splice(columnIndex, 1);
62
+ for (const record of this.tables[tableName].records) {
63
+ delete record[column];
64
+ }
65
+ this.saveToFile();
66
+ }
67
+
35
68
  insert(tableName, record) {
36
- this.queue.push({method: "insert", table: tableName, record});
69
+ this.queue.push({ method: "insert", table: tableName, record });
37
70
  if (this.queue.length === 1) {
38
71
  this.processQueue();
39
72
  }
40
73
  }
41
74
 
42
75
  update(tableName, query, newData) {
43
- this.queue.push({method: "update", table: tableName, query, newData});
76
+ this.queue.push({ method: "update", table: tableName, query, newData });
44
77
  if (this.queue.length === 1) {
45
78
  this.processQueue();
46
79
  }
@@ -55,7 +88,7 @@ const Database = class {
55
88
  }
56
89
 
57
90
  delete(tableName, query = {}) {
58
- this.queue.push({method: "delete", table: tableName, query});
91
+ this.queue.push({ method: "delete", table: tableName, query });
59
92
  if (this.queue.length === 1) {
60
93
  this.processQueue();
61
94
  }
@@ -83,7 +116,7 @@ const Database = class {
83
116
  }
84
117
 
85
118
  const table = this.tables[tableName];
86
- const formattedRecord = table.columns.reduce((obj, column) => ({...obj, [column]: record[column] || null}), {});
119
+ const formattedRecord = table.columns.reduce((obj, column) => ({ ...obj, [column]: record[column] || null }), {});
87
120
  table.records.push(formattedRecord);
88
121
  this.saveToFile();
89
122
  this.queue.shift();
@@ -98,12 +131,15 @@ const Database = class {
98
131
  throw new Error(`Table "${tableName}" does not exist.`);
99
132
  }
100
133
  const table = this.tables[tableName];
101
- table.records = table.records.map((record) => {
102
- if (Object.entries(query).every(([column, value]) => record[column] === value)) {
103
- return {...record, ...newData};
104
- }
134
+ const updatedRecords = table.records.map((record) => {
135
+ Object.entries(newData).forEach(([column, value]) => {
136
+ if (table.columns.includes(column) && Object.entries(query).every(([qColumn, qValue]) => record[qColumn] === qValue)) {
137
+ record[column] = value;
138
+ }
139
+ });
105
140
  return record;
106
141
  });
142
+ table.records = updatedRecords;
107
143
  this.saveToFile();
108
144
  this.queue.shift();
109
145
  if (this.queue.length > 0) {
@@ -118,7 +154,7 @@ const Database = class {
118
154
  }
119
155
 
120
156
  this.tables[tableName].records = this.tables[tableName].records.filter(
121
- (record) => !Object.entries(query).every(([column, value]) => record[column] === value)
157
+ (record) => !Object.entries(query).every(([column, value]) => record[column] === value),
122
158
  );
123
159
  this.saveToFile();
124
160
  this.queue.shift();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hedystia/db",
3
- "version": "1.3.7",
3
+ "version": "1.4.0",
4
4
  "description": "A database made by the company hedystia, easy to use",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/readme.md CHANGED
@@ -27,9 +27,9 @@ const database = new Database("./database.ht", "password");
27
27
  // You can only use it once to create the table after that you can no longer use it.
28
28
  database.createTable("users", ["id", "name", "email"]);
29
29
 
30
- database.insert("users", {id: "1", name: "John Doe", email: "jdoe@example.com"});
30
+ database.insert("users", { id: "1", name: "John Doe", email: "jdoe@example.com" });
31
31
 
32
- database.insert("users", {id: "2", name: "María", email: "maria@example.com"});
32
+ database.insert("users", { id: "2", name: "María", email: "maria@example.com" });
33
33
 
34
34
  const users = database.select("users");
35
35
 
@@ -37,30 +37,56 @@ console.log("----------------------------------");
37
37
 
38
38
  console.log(users);
39
39
 
40
- const userJohn = database.select("users", {name: "John Doe"});
40
+ database.addColumn("users", "phone");
41
+
42
+ database.addColumn("users", "lang", "en-US");
43
+
44
+ const newUsersPhone = database.select("users");
45
+
41
46
  console.log("----------------------------------");
47
+
48
+ console.log(newUsersPhone);
49
+
50
+ database.deleteColumn("users", "phone");
51
+
52
+ const oldUsersPhone = database.select("users");
53
+
54
+ console.log("----------------------------------");
55
+
56
+ console.log(oldUsersPhone);
57
+
58
+ const userJohn = database.select("users", { name: "John Doe" });
59
+
60
+ console.log("----------------------------------");
61
+
42
62
  console.log(userJohn);
43
63
 
44
- database.delete("users", {name: "María"});
64
+ database.delete("users", { name: "María" });
45
65
 
46
66
  const users2 = database.select("users");
67
+
47
68
  console.log("----------------------------------");
69
+
48
70
  console.log(users2);
49
71
 
50
- database.update("users", {id: "1"}, {name: "Jane Doe"});
72
+ database.update("users", { id: "1" }, { name: "Jane Doe", lang: "es-ES" });
51
73
 
52
74
  const users3 = database.select("users");
75
+
53
76
  console.log("----------------------------------");
77
+
54
78
  console.log(users3);
55
79
  ```
56
80
 
57
81
  ## Functions
58
82
 
59
- | Function | Description |
60
- | ------------- | ------------------------------------ |
61
- | `createTable` | To create a table |
62
- | `deleteTable` | To delete a table |
63
- | `insert` | To insert a data in the table |
64
- | `update` | To update a data in the table |
65
- | `select` | To search for information in a table |
66
- | `delete` | To delete a data from the table |
83
+ | Function | Description |
84
+ | -------------- | ---------------------------------------------- |
85
+ | `createTable` | To create a table |
86
+ | `deleteTable` | To delete a table |
87
+ | `addColumn` | To add a column to an already created table |
88
+ | `deleteColumn` | To remove a column to an already created table |
89
+ | `insert` | To insert a data in the table |
90
+ | `update` | To update a data in the table |
91
+ | `select` | To search for information in a table |
92
+ | `delete` | To delete a data from the table |