-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolder-db.js
More file actions
94 lines (83 loc) · 3.81 KB
/
folder-db.js
File metadata and controls
94 lines (83 loc) · 3.81 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
const fs = require('fs');
const path = require('path');
// Chooses the folder where the database will be stored
function init(dbName) {
if (!fs.existsSync(dbName)) {
fs.mkdirSync(dbName);
console.log(`Database ${dbName} created!`)
}
return {
createTable(tableName) {
const tablePath = path.join(dbName, tableName);
if (!fs.existsSync(tablePath)) {
fs.mkdirSync(tablePath);
}
return {
insert(recordName, data) {
const recordPath = path.join(dbName, tableName, `${recordName}.json`);
const tempRecordPath = recordPath + '.tmp';
try {
fs.writeFileSync(tempRecordPath, JSON.stringify(data, null, 2));
const writtenData = JSON.parse(fs.readFileSync(tempRecordPath, 'utf-8'));
if (JSON.stringify(data) !== JSON.stringify(writtenData)) {
throw new Error('Data verification failed after write');
}
fs.renameSync(tempRecordPath, recordPath);
} catch (error) {
console.error('Failed to insert record:', error);
if (fs.existsSync(tempRecordPath)) {
fs.unlinkSync(tempRecordPath);
}
throw error;
}
},
read(recordName) {
const recordPath = path.join(dbName, tableName, `${recordName}.json`);
if (fs.existsSync(recordPath)) {
const data = fs.readFileSync(recordPath, 'utf-8');
return JSON.parse(data);
} else {
console.error(`Error: Record not found in database: ${dbName}, table: ${tableName}, record: ${recordName}`)
return null;
}
},
update(recordName, data) {
const recordPath = path.join(__dirname, dbName, tableName, `${recordName}.json`);
if (fs.existsSync(recordPath)) {
const existingDataRaw = fs.readFileSync(recordPath, 'utf-8');
const existingData = JSON.parse(existingDataRaw);
const updatedData = { ...existingData, ...data };
fs.writeFileSync(recordPath, JSON.stringify(updatedData, null, 2));
} else {
console.log(`Record ${recordName} does not exist.`);
}
},
delete(recordName) {
const recordPath = path.join(dbName, tableName, `${recordName}.json`);
if (fs.existsSync(recordPath)) {
fs.unlinkSync(recordPath);
}
},
exists(recordName) {
const itemPath = path.join(dbName, tableName, `${recordName}.json`);
return fs.existsSync(itemPath);
},
list() {
const records = fs.readdirSync(path.join(dbName, tableName)).filter(file => file.endsWith('.json'));
return records.map(record => record.replace('.json', ''));
}
}
},
tableExists(tableName) {
const tablePath = path.join(dbName, tableName);
return fs.existsSync(tablePath);
},
listTables() {
const tables = fs.readdirSync(dbName).filter(file => fs.statSync(path.join(dbName, file)).isDirectory());
return tables;
}
}
}
module.exports = {
init
};