@hedystia/db 1.3.7 → 1.5.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/index.d.ts +55 -50
- package/index.js +62 -10
- package/package.json +1 -1
- package/readme.md +41 -13
package/index.d.ts
CHANGED
|
@@ -1,50 +1,55 @@
|
|
|
1
|
-
interface Table {
|
|
2
|
-
columns: string[];
|
|
3
|
-
records: {
|
|
4
|
-
[key: string]:
|
|
5
|
-
}[];
|
|
6
|
-
}
|
|
7
|
-
interface QueueItem {
|
|
8
|
-
method: string;
|
|
9
|
-
table: string;
|
|
10
|
-
record?: {
|
|
11
|
-
[key: string]:
|
|
12
|
-
};
|
|
13
|
-
query?: {
|
|
14
|
-
[key: string]:
|
|
15
|
-
};
|
|
16
|
-
newData?: {
|
|
17
|
-
[key: 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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
private
|
|
49
|
-
private
|
|
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
|
+
createTableIfNotExists(tableName: string, columns: string[]): void;
|
|
30
|
+
deleteTable(tableName: string): void;
|
|
31
|
+
deleteTableIfExists(tableName: string): void;
|
|
32
|
+
addColumn(tableName: string, column: string, defaultValue: any): void;
|
|
33
|
+
deleteColumn(tableName: string, column: string): void;
|
|
34
|
+
insert(tableName: string, record: {
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
}): void;
|
|
37
|
+
update(tableName: string, query: {
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
}, newData: {
|
|
40
|
+
[key: string]: any;
|
|
41
|
+
}): void;
|
|
42
|
+
select(tableName: string, query: {
|
|
43
|
+
[key: string]: any;
|
|
44
|
+
}): unknown;
|
|
45
|
+
delete(tableName: string, query: {
|
|
46
|
+
[key: string]: any;
|
|
47
|
+
}): void;
|
|
48
|
+
private processQueue;
|
|
49
|
+
private insertTable;
|
|
50
|
+
private updateTable;
|
|
51
|
+
private deleteFromTable;
|
|
52
|
+
private saveToFile;
|
|
53
|
+
private readFromFile;
|
|
54
|
+
}
|
|
55
|
+
export {};
|
package/index.js
CHANGED
|
@@ -18,10 +18,18 @@ 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
|
|
|
25
|
+
createTableIfNotExists(tableName, columns) {
|
|
26
|
+
this.readFromFile();
|
|
27
|
+
if (!this.tables[tableName]) {
|
|
28
|
+
this.tables[tableName] = { columns, records: [] };
|
|
29
|
+
this.saveToFile();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
25
33
|
deleteTable(tableName) {
|
|
26
34
|
this.readFromFile();
|
|
27
35
|
if (!this.tables[tableName]) {
|
|
@@ -32,15 +40,56 @@ const Database = class {
|
|
|
32
40
|
this.saveToFile();
|
|
33
41
|
}
|
|
34
42
|
|
|
43
|
+
deleteTableIfExists(tableName) {
|
|
44
|
+
this.readFromFile();
|
|
45
|
+
if (this.tables[tableName]) {
|
|
46
|
+
delete this.tables[tableName];
|
|
47
|
+
this.saveToFile();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
addColumn(tableName, column, defaultValue) {
|
|
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}" already exists in table "${tableName}".`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this.tables[tableName].columns.push(column);
|
|
61
|
+
for (const record of this.tables[tableName].records) {
|
|
62
|
+
record[column] = defaultValue ?? null;
|
|
63
|
+
}
|
|
64
|
+
this.saveToFile();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
deleteColumn(tableName, column) {
|
|
68
|
+
this.readFromFile();
|
|
69
|
+
if (!this.tables[tableName]) {
|
|
70
|
+
throw new Error(`Table "${tableName}" does not exist.`);
|
|
71
|
+
}
|
|
72
|
+
if (!this.tables[tableName].columns.includes(column)) {
|
|
73
|
+
throw new Error(`Column "${column}" does not exist in table "${tableName}".`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const columnIndex = this.tables[tableName].columns.indexOf(column);
|
|
77
|
+
this.tables[tableName].columns.splice(columnIndex, 1);
|
|
78
|
+
for (const record of this.tables[tableName].records) {
|
|
79
|
+
delete record[column];
|
|
80
|
+
}
|
|
81
|
+
this.saveToFile();
|
|
82
|
+
}
|
|
83
|
+
|
|
35
84
|
insert(tableName, record) {
|
|
36
|
-
this.queue.push({method: "insert", table: tableName, record});
|
|
85
|
+
this.queue.push({ method: "insert", table: tableName, record });
|
|
37
86
|
if (this.queue.length === 1) {
|
|
38
87
|
this.processQueue();
|
|
39
88
|
}
|
|
40
89
|
}
|
|
41
90
|
|
|
42
91
|
update(tableName, query, newData) {
|
|
43
|
-
this.queue.push({method: "update", table: tableName, query, newData});
|
|
92
|
+
this.queue.push({ method: "update", table: tableName, query, newData });
|
|
44
93
|
if (this.queue.length === 1) {
|
|
45
94
|
this.processQueue();
|
|
46
95
|
}
|
|
@@ -55,7 +104,7 @@ const Database = class {
|
|
|
55
104
|
}
|
|
56
105
|
|
|
57
106
|
delete(tableName, query = {}) {
|
|
58
|
-
this.queue.push({method: "delete", table: tableName, query});
|
|
107
|
+
this.queue.push({ method: "delete", table: tableName, query });
|
|
59
108
|
if (this.queue.length === 1) {
|
|
60
109
|
this.processQueue();
|
|
61
110
|
}
|
|
@@ -83,7 +132,7 @@ const Database = class {
|
|
|
83
132
|
}
|
|
84
133
|
|
|
85
134
|
const table = this.tables[tableName];
|
|
86
|
-
const formattedRecord = table.columns.reduce((obj, column) => ({...obj, [column]: record[column] || null}), {});
|
|
135
|
+
const formattedRecord = table.columns.reduce((obj, column) => ({ ...obj, [column]: record[column] || null }), {});
|
|
87
136
|
table.records.push(formattedRecord);
|
|
88
137
|
this.saveToFile();
|
|
89
138
|
this.queue.shift();
|
|
@@ -98,12 +147,15 @@ const Database = class {
|
|
|
98
147
|
throw new Error(`Table "${tableName}" does not exist.`);
|
|
99
148
|
}
|
|
100
149
|
const table = this.tables[tableName];
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
150
|
+
const updatedRecords = table.records.map((record) => {
|
|
151
|
+
Object.entries(newData).forEach(([column, value]) => {
|
|
152
|
+
if (table.columns.includes(column) && Object.entries(query).every(([qColumn, qValue]) => record[qColumn] === qValue)) {
|
|
153
|
+
record[column] = value;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
105
156
|
return record;
|
|
106
157
|
});
|
|
158
|
+
table.records = updatedRecords;
|
|
107
159
|
this.saveToFile();
|
|
108
160
|
this.queue.shift();
|
|
109
161
|
if (this.queue.length > 0) {
|
|
@@ -118,7 +170,7 @@ const Database = class {
|
|
|
118
170
|
}
|
|
119
171
|
|
|
120
172
|
this.tables[tableName].records = this.tables[tableName].records.filter(
|
|
121
|
-
(record) => !Object.entries(query).every(([column, value]) => record[column] === value)
|
|
173
|
+
(record) => !Object.entries(query).every(([column, value]) => record[column] === value),
|
|
122
174
|
);
|
|
123
175
|
this.saveToFile();
|
|
124
176
|
this.queue.shift();
|
package/package.json
CHANGED
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,58 @@ console.log("----------------------------------");
|
|
|
37
37
|
|
|
38
38
|
console.log(users);
|
|
39
39
|
|
|
40
|
-
|
|
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
|
|
60
|
-
|
|
|
61
|
-
| `createTable`
|
|
62
|
-
| `deleteTable`
|
|
63
|
-
| `
|
|
64
|
-
| `
|
|
65
|
-
| `
|
|
66
|
-
| `
|
|
83
|
+
| Function | Description |
|
|
84
|
+
| ------------------------ | ---------------------------------------------- |
|
|
85
|
+
| `createTable` | To create a table |
|
|
86
|
+
| `deleteTable` | To delete a table |
|
|
87
|
+
| `createTableIfNotExists` | To create a table if it does not exist |
|
|
88
|
+
| `deleteTableIfExists` | To delete a table if it exists |
|
|
89
|
+
| `addColumn` | To add a column to an already created table |
|
|
90
|
+
| `deleteColumn` | To remove a column to an already created table |
|
|
91
|
+
| `insert` | To insert a data in the table |
|
|
92
|
+
| `update` | To update a data in the table |
|
|
93
|
+
| `select` | To search for information in a table |
|
|
94
|
+
| `delete` | To delete a data from the table |
|