@hedystia/db 1.3.6
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 +50 -0
- package/index.js +153 -0
- package/package.json +43 -0
- package/readme.md +66 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const cryptoJS = require("crypto-js");
|
|
3
|
+
|
|
4
|
+
const AES = cryptoJS.AES;
|
|
5
|
+
const enc = cryptoJS.enc;
|
|
6
|
+
|
|
7
|
+
const Database = class {
|
|
8
|
+
constructor(filePath, password) {
|
|
9
|
+
this.tables = {};
|
|
10
|
+
this.filePath = filePath || "./database.ht";
|
|
11
|
+
this.password = password;
|
|
12
|
+
this.queue = [];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
createTable(tableName, columns = []) {
|
|
16
|
+
this.readFromFile();
|
|
17
|
+
if (this.tables[tableName]) {
|
|
18
|
+
throw new Error(`Table "${tableName}" already exists.`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
this.tables[tableName] = {columns, records: []};
|
|
22
|
+
this.saveToFile();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
deleteTable(tableName) {
|
|
26
|
+
this.readFromFile();
|
|
27
|
+
if (!this.tables[tableName]) {
|
|
28
|
+
throw new Error(`Table "${tableName}" does not exist.`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
delete this.tables[tableName];
|
|
32
|
+
this.saveToFile();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
insert(tableName, record) {
|
|
36
|
+
this.queue.push({method: "insert", table: tableName, record});
|
|
37
|
+
if (this.queue.length === 1) {
|
|
38
|
+
this.processQueue();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
update(tableName, query, newData) {
|
|
43
|
+
this.queue.push({method: "update", table: tableName, query, newData});
|
|
44
|
+
if (this.queue.length === 1) {
|
|
45
|
+
this.processQueue();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
select(tableName, query = {}) {
|
|
50
|
+
this.readFromFile();
|
|
51
|
+
if (!this.tables[tableName]) {
|
|
52
|
+
throw new Error(`Table "${tableName}" does not exist.`);
|
|
53
|
+
}
|
|
54
|
+
return this.tables[tableName].records.filter((record) => Object.entries(query).every(([column, value]) => record[column] === value));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
delete(tableName, query = {}) {
|
|
58
|
+
this.queue.push({method: "delete", table: tableName, query});
|
|
59
|
+
if (this.queue.length === 1) {
|
|
60
|
+
this.processQueue();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
processQueue() {
|
|
65
|
+
const request = this.queue[0];
|
|
66
|
+
switch (request.method) {
|
|
67
|
+
case "insert":
|
|
68
|
+
this.insertTable(request.table, request.record);
|
|
69
|
+
break;
|
|
70
|
+
case "update":
|
|
71
|
+
this.updateTable(request.table, request.query, request.newData);
|
|
72
|
+
break;
|
|
73
|
+
case "delete":
|
|
74
|
+
this.deleteFromTable(request.table, request.query);
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
insertTable(tableName, record) {
|
|
80
|
+
this.readFromFile();
|
|
81
|
+
if (!this.tables[tableName]) {
|
|
82
|
+
throw new Error(`Table "${tableName}" does not exist.`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const table = this.tables[tableName];
|
|
86
|
+
const formattedRecord = table.columns.reduce((obj, column) => ({...obj, [column]: record[column] || null}), {});
|
|
87
|
+
table.records.push(formattedRecord);
|
|
88
|
+
this.saveToFile();
|
|
89
|
+
this.queue.shift();
|
|
90
|
+
if (this.queue.length > 0) {
|
|
91
|
+
this.processQueue();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
updateTable(tableName, query, newData) {
|
|
96
|
+
this.readFromFile();
|
|
97
|
+
if (!this.tables[tableName]) {
|
|
98
|
+
throw new Error(`Table "${tableName}" does not exist.`);
|
|
99
|
+
}
|
|
100
|
+
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
|
+
}
|
|
105
|
+
return record;
|
|
106
|
+
});
|
|
107
|
+
this.saveToFile();
|
|
108
|
+
this.queue.shift();
|
|
109
|
+
if (this.queue.length > 0) {
|
|
110
|
+
this.processQueue();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
deleteFromTable(tableName, query) {
|
|
115
|
+
this.readFromFile();
|
|
116
|
+
if (!this.tables[tableName]) {
|
|
117
|
+
throw new Error(`Table "${tableName}" does not exist.`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
this.tables[tableName].records = this.tables[tableName].records.filter(
|
|
121
|
+
(record) => !Object.entries(query).every(([column, value]) => record[column] === value)
|
|
122
|
+
);
|
|
123
|
+
this.saveToFile();
|
|
124
|
+
this.queue.shift();
|
|
125
|
+
if (this.queue.length > 0) {
|
|
126
|
+
this.processQueue();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
saveToFile() {
|
|
131
|
+
if (!this.filePath.endsWith(".ht")) {
|
|
132
|
+
throw new Error(`File path must include '.ht': ${this.filePath}`);
|
|
133
|
+
}
|
|
134
|
+
const data = JSON.stringify(this.tables);
|
|
135
|
+
const encrypted = AES.encrypt(data, this.password).toString();
|
|
136
|
+
fs.writeFileSync(this.filePath, encrypted);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
readFromFile() {
|
|
140
|
+
if (!fs.existsSync(this.filePath)) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const encrypted = fs.readFileSync(this.filePath, "utf8");
|
|
144
|
+
const data = AES.decrypt(encrypted, this.password).toString(enc.Utf8);
|
|
145
|
+
try {
|
|
146
|
+
this.tables = JSON.parse(data);
|
|
147
|
+
} catch {
|
|
148
|
+
this.tables = {};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
module.exports = Database;
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hedystia/db",
|
|
3
|
+
"version": "1.3.6",
|
|
4
|
+
"description": "A database made by the company hedystia, easy to use",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"private": false,
|
|
8
|
+
"keywords": [
|
|
9
|
+
"Database",
|
|
10
|
+
"Hedystia",
|
|
11
|
+
"Discord",
|
|
12
|
+
"node",
|
|
13
|
+
"db",
|
|
14
|
+
"discord.js",
|
|
15
|
+
"typescript"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc"
|
|
19
|
+
},
|
|
20
|
+
"author": "contact@hedystia.com",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/Zastinian/hedystia.db.git"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/Zastinian/hedystia.db/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://docs.hedystia.com/db/start",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"crypto-js": "^4.2.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/crypto-js": "^4.2.2",
|
|
35
|
+
"@types/node": "^20.11.8"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=18.0.0"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
## Installation
|
|
2
|
+
|
|
3
|
+
```
|
|
4
|
+
npm i hedystia.db
|
|
5
|
+
|
|
6
|
+
yarn add hedystia.db
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Nodejs Version
|
|
10
|
+
|
|
11
|
+
- `v18.0.0` or higher
|
|
12
|
+
|
|
13
|
+
## Links
|
|
14
|
+
|
|
15
|
+
- [Discord](https://discord.gg/aXvuUpvRQs) [Hedystia Discord]
|
|
16
|
+
- [Discord_Bot](https://hedystia.com) [Hedystia Bot]
|
|
17
|
+
- [Docs](https://docs.hedystia.com/db/start) [Hedystia Docs]
|
|
18
|
+
|
|
19
|
+
## Example
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const Database = require("hedystia.db");
|
|
23
|
+
|
|
24
|
+
// Create a file named database.ht and enter the password
|
|
25
|
+
const database = new Database("./database.ht", "password");
|
|
26
|
+
|
|
27
|
+
// You can only use it once to create the table after that you can no longer use it.
|
|
28
|
+
database.createTable("users", ["id", "name", "email"]);
|
|
29
|
+
|
|
30
|
+
database.insert("users", {id: "1", name: "John Doe", email: "jdoe@example.com"});
|
|
31
|
+
|
|
32
|
+
database.insert("users", {id: "2", name: "María", email: "maria@example.com"});
|
|
33
|
+
|
|
34
|
+
const users = database.select("users");
|
|
35
|
+
|
|
36
|
+
console.log("----------------------------------");
|
|
37
|
+
|
|
38
|
+
console.log(users);
|
|
39
|
+
|
|
40
|
+
const userJohn = database.select("users", {name: "John Doe"});
|
|
41
|
+
console.log("----------------------------------");
|
|
42
|
+
console.log(userJohn);
|
|
43
|
+
|
|
44
|
+
database.delete("users", {name: "María"});
|
|
45
|
+
|
|
46
|
+
const users2 = database.select("users");
|
|
47
|
+
console.log("----------------------------------");
|
|
48
|
+
console.log(users2);
|
|
49
|
+
|
|
50
|
+
database.update("users", {id: "1"}, {name: "Jane Doe"});
|
|
51
|
+
|
|
52
|
+
const users3 = database.select("users");
|
|
53
|
+
console.log("----------------------------------");
|
|
54
|
+
console.log(users3);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Functions
|
|
58
|
+
|
|
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 |
|