-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.js
34 lines (28 loc) · 894 Bytes
/
Database.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
import { date2int, shuffle } from "./Utils.js";
let database = undefined;
const Database = {};
Database.read = async () => {
if (!database) {
database = await fetch("/database/db.json").then(x => x.json());
}
return database;
};
Database.readPostsAsMap = async () => {
if (!database) await Database.read();
if (!database.postsMap) {
Database.postsMap = database.posts.reduce((acc, post) => {
acc[post.id] = post; return acc;
}, {});
}
return Database.postsMap;
};
Database.readSortedPosts = async () => {
if (!database) await Database.read();
const posts = [...database.posts];
return posts.sort((a, b) => date2int(b.lastUpdateDate) - date2int(a.lastUpdateDate));
};
Database.readRandomPosts = async () => {
if (!database) await Database.read();
return shuffle(database.posts);
}
export default Database;