-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
217 lines (170 loc) · 4.67 KB
/
index.js
File metadata and controls
217 lines (170 loc) · 4.67 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"use strict";
const fs = require("node:fs");
const path = require("node:path");
const DEFAULT_OPTIONS = {
file: "data.json",
idKey: "id",
pretty: 2,
createIfMissing: true
};
function createDb(options = {}) {
const config = {
...DEFAULT_OPTIONS,
...options
};
if (typeof config.file !== "string" || config.file.trim() === "") {
throw new TypeError("`file` doit être une chaîne non vide.");
}
if (typeof config.idKey !== "string" || config.idKey.trim() === "") {
throw new TypeError("`idKey` doit être une chaîne non vide.");
}
const filePath = path.resolve(process.cwd(), config.file);
function ensureFile() {
if (!fs.existsSync(filePath)) {
if (!config.createIfMissing) {
throw new Error(`Fichier introuvable: ${filePath}`);
}
fs.writeFileSync(filePath, "[]\n", "utf8");
}
}
function read() {
ensureFile();
const raw = fs.readFileSync(filePath, "utf8").trim();
if (raw === "") {
return [];
}
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed)) {
throw new TypeError("Le contenu JSON doit être un tableau.");
}
return parsed;
}
function write(data) {
if (!Array.isArray(data)) {
throw new TypeError("Les données à écrire doivent être un tableau.");
}
const tmpPath = `${filePath}.tmp`;
const json = `${JSON.stringify(data, null, config.pretty)}\n`;
fs.writeFileSync(tmpPath, json, "utf8");
fs.renameSync(tmpPath, filePath);
return data;
}
function all() {
return read();
}
function find(query) {
const data = read();
if (typeof query === "function") {
return data.filter(query);
}
if (query && typeof query === "object" && !Array.isArray(query)) {
return data.filter((entry) =>
Object.keys(query).every((key) => entry[key] === query[key])
);
}
throw new TypeError("`find` attend un objet de filtre ou une fonction.");
}
function getById(id) {
const data = read();
return data.find((entry) => entry[config.idKey] === id) || null;
}
function insert(entry) {
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
throw new TypeError("`insert` attend un objet.");
}
if (!(config.idKey in entry)) {
throw new TypeError(`L'objet doit contenir la clé '${config.idKey}'.`);
}
const data = read();
const id = entry[config.idKey];
if (data.some((item) => item[config.idKey] === id)) {
throw new Error(`Une entrée avec ${config.idKey}=${id} existe déjà.`);
}
data.push(entry);
write(data);
return entry;
}
function upsert(entry) {
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
throw new TypeError("`upsert` attend un objet.");
}
if (!(config.idKey in entry)) {
throw new TypeError(`L'objet doit contenir la clé '${config.idKey}'.`);
}
const data = read();
const id = entry[config.idKey];
const index = data.findIndex((item) => item[config.idKey] === id);
if (index === -1) {
data.push(entry);
} else {
data[index] = entry;
}
write(data);
return entry;
}
function updateById(id, patch) {
if (!patch || typeof patch !== "object" || Array.isArray(patch)) {
throw new TypeError("`updateById` attend un objet patch.");
}
const data = read();
const index = data.findIndex((entry) => entry[config.idKey] === id);
if (index === -1) {
return null;
}
const next = {
...data[index],
...patch
};
if (next[config.idKey] !== id) {
throw new Error(`La clé '${config.idKey}' ne peut pas être modifiée.`);
}
data[index] = next;
write(data);
return next;
}
function replaceById(id, entry) {
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
throw new TypeError("`replaceById` attend un objet.");
}
if (entry[config.idKey] !== id) {
throw new Error(`L'objet doit contenir '${config.idKey}' égal à ${id}.`);
}
const data = read();
const index = data.findIndex((item) => item[config.idKey] === id);
if (index === -1) {
return null;
}
data[index] = entry;
write(data);
return entry;
}
function removeById(id) {
const data = read();
const index = data.findIndex((entry) => entry[config.idKey] === id);
if (index === -1) {
return false;
}
data.splice(index, 1);
write(data);
return true;
}
function clear() {
write([]);
return [];
}
return {
filePath,
all,
find,
getById,
insert,
upsert,
updateById,
replaceById,
removeById,
clear
};
}
module.exports = {
createDb
};