-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsdb.js
More file actions
149 lines (129 loc) · 4.2 KB
/
msdb.js
File metadata and controls
149 lines (129 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const fs = require('fs');
const path = require('path');
/**
* Creates and initializes a database with the given name.
* @param {string} databaseName - the name of the database to create
* @returns {function} - a function that creates and initializes a table in the database
*/
function initializeDatabase(databaseName) {
const databaseFolderPath = './Database';
if (!fs.existsSync(databaseFolderPath)) {
fs.mkdirSync(databaseFolderPath);
}
const fullDatabaseFolderPath = path.join(databaseFolderPath, databaseName);
if (!fs.existsSync(fullDatabaseFolderPath)) {
fs.mkdirSync(fullDatabaseFolderPath);
}
return function initializeTable(tableName) {
const tableFilename = path.join(fullDatabaseFolderPath, `${tableName}.json`);
let tableData = {};
if (fs.existsSync(tableFilename)) {
const data = fs.readFileSync(tableFilename, 'utf8');
tableData = JSON.parse(data);
}
/**
* Saves an entry to the table.
* @param {string} id - the unique ID of the entry
* @param {object} data - the data to save
*/
function saveTable(id, data) {
const entryId = id || generateUniqueId();
tableData[entryId] = {
id: entryId,
value: data.value || data || null,
};
const jsonData = JSON.stringify(tableData, null, 2);
fs.writeFileSync(tableFilename, jsonData, 'utf8');
}
/**
* Updates the table data in the file system.
*/
function updateJson() {
const jsonData = JSON.stringify(tableData, null, 2);
fs.writeFileSync(tableFilename, jsonData, 'utf8');
}
/**
* Removes an entry from the table.
* @param {string} key - the unique ID of the entry to remove
*/
function removeFromTable(key) {
if (tableData.hasOwnProperty(key)) {
delete tableData[key];
updateJson();
}
}
/**
* Adds an entry to the table.
* @param {string} id - the unique ID of the entry
* @param {object} data - the data to add
*/
function addToTable(id, data) {
saveTable(id, data);
}
/**
* Retrieves an entry from the table.
* @param {string} key - the unique ID of the entry
* @returns {object} - the entry with the given ID, or null if no entry with the given ID exists
*/
function getFromTable(key) {
return tableData[key];
}
/**
* Retrieves a random entry from the table.
* @returns {object} - a random entry from the table, or null if the table is empty
*/
function getRandomEntry() {
const keys = Object.keys(tableData);
if (keys.length === 0) {
return null;
}
const randomKey = keys[Math.floor(Math.random() * keys.length)];
return tableData[randomKey];
}
/**
* Retrieves all entries from the table.
* @param {string} orderBy - the order to return the entries in ('asc' for ascending, 'desc' for descending)
* @returns {object[]} - an array of all entries in the table, sorted according to the given order
*/
function getAllEntries(orderBy = 'asc') {
const entries = Object.values(tableData);
if (orderBy === 'desc') {
entries.sort((a, b) => (a.id > b.id ? -1 : 1));
} else {
entries.sort((a, b) => (a.id > b.id ? 1 : -1));
}
return entries;
}
/**
* Retrieves entries that match the given condition.
* @param {object} condition - the condition to match
* @returns {object[]} - an array of entries that match the given condition
*/
function getWhere(condition) {
const entries = Object.entries(tableData);
const results = [];
for (const [key, value] of entries) {
if (value.value && value.value.good == condition.good) {
results.push(value.value);
}
}
return results;
}
return {
find: getFromTable,
save: addToTable,
remove: removeFromTable,
random: getRandomEntry,
getAll: getAllEntries,
getWhere: getWhere
};
};
}
/**
* Generates a unique ID.
* @returns {string} - a unique ID
*/
function generateUniqueId() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}
module.exports = initializeDatabase;