-
Notifications
You must be signed in to change notification settings - Fork 0
/
dire.js
163 lines (145 loc) · 5.61 KB
/
dire.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const fs = require('fs').promises;
const path = require('path');
class Dire {
/**
* Creates an instance of Dire.
* @param {string} [directoryPath] - The path to the directory where JSON files are stored.
* @param {number} [saveInterval] - The interval (in milliseconds) at which the database is saved to JSON files.
*/
constructor(directoryPath = __dirname, saveInterval = 60000) {
this.directoryPath = directoryPath;
this.saveInterval = saveInterval;
this.data = {};
this.isLoaded = false;
// Check if saveInterval is greater than 0 before starting the timer
if (this.saveInterval > 0) {
this.timer = setInterval(() => this.save(), this.saveInterval);
}
this.modifiedCollections = new Set();
this.load();
}
// Add a method to mark a collection as modified
markCollectionAsModified(collectionName) {
this.modifiedCollections.add(collectionName);
}
/**
* Asynchronously loads collections from JSON files in the directory.
*/
async load() {
try {
const files = await fs.readdir(this.directoryPath);
for (const file of files) {
if (file.endsWith('.json')) {
const collectionName = path.basename(file, '.json');
const filePath = path.join(this.directoryPath, file);
const fileData = JSON.parse(await fs.readFile(filePath, 'utf-8'));
this.data[collectionName] = fileData[collectionName];
// console.log(`Loaded collection ${collectionName} from file: ${filePath}`);
}
}
this.isLoaded = true;
} catch (error) {
console.error('Error loading collections:', error.message);
}
}
/**
* Asynchronously saves modified collections to their respective JSON files.
*/
async save() {
try {
// Iterate through the modified collections
for (const collectionName of this.modifiedCollections) {
const filePath = path.join(this.directoryPath, `${collectionName}.json`);
// Write the modified collection to its JSON file
await fs.writeFile(filePath, JSON.stringify({ [collectionName]: this.data[collectionName] }, null, 0));
}
// Clear the set of modified collections after saving
this.modifiedCollections.clear();
} catch (error) {
console.error('Error saving modified collections:', error.message);
}
}
/**
* Asynchronously adds a new collection with the provided data.
* @param {string} collectionName - The name of the collection.
* @param {Array} items - The data to be added to the collection.
*/
async add(collectionName, items = []) {
this.data[collectionName] = items;
this.markCollectionAsModified(collectionName);
}
/**
* Asynchronously drops a collection and its associated file.
* @param {string} collectionName - The name of the collection to be dropped.
*/
async drop(collectionName) {
delete this.data[collectionName];
const filePath = path.join(this.directoryPath, `${collectionName}.json`);
try {
await fs.unlink(filePath);
} catch (error) {
console.error('Error deleting file:', error.message);
}
}
/**
* Asynchronously inserts one or more items into the collection with unique IDs.
* @param {string} collectionName - The name of the collection.
* @param {object|object[]} items - The item or items to insert into the collection.
*/
async insert(collectionName, items) {
const collection = this.data[collectionName];
if (!Array.isArray(items)) {
items = [items];
}
items.forEach((item) => {
collection.push(item);
});
this.markCollectionAsModified(collectionName);
}
/**
* Asynchronously updates items in the collection based on a filter and update function.
* @param {string} collectionName - The name of the collection.
* @param {function} filterFunction - The filter function to match items for updating.
* @param {function} updateFunction - The function to update matched items.
*/
async update(collectionName, filterFunction = () => true, updateFunction) {
const collection = this.data[collectionName];
collection.forEach((item) => {
if (filterFunction(item)) {
updateFunction(item);
}
});
this.markCollectionAsModified(collectionName);
}
/**
* Asynchronously finds items in the collection based on a filter function.
* @param {string} collectionName - The name of the collection.
* @param {function} filterFunction - The filter function to match items.
* @returns {object[]} - An array of matched items.
*/
async find(collectionName, filterFunction = () => true) {
const collection = this.data[collectionName];
return collection.filter(filterFunction);
}
/**
* Asynchronously finds one item in the collection based on a filter function.
* @param {string} collectionName - The name of the collection.
* @param {function} filterFunction - The filter function to match an item.
* @returns {object|null} - The matched item or null if not found.
*/
async findOne(collectionName, filterFunction) {
const collection = this.data[collectionName];
return collection.find(filterFunction) || null;
}
/**
* Asynchronously deletes items from the collection based on a filter.
* @param {string} collectionName - The name of the collection.
* @param {function} filterFunction - The filter function to match items for deletion.
*/
async delete(collectionName, filterFunction = () => true) {
const collection = this.data[collectionName];
this.data[collectionName] = collection.filter((item) => !filterFunction(item));
this.markCollectionAsModified(collectionName);
}
}
module.exports = Dire;