File size: 2,892 Bytes
3a82ffe | 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 | // Bunch of functions to make accessing rocksdb easier
import RocksDB from "rocksdb";
let db: RocksDB;
export type RocksDBBatch = ReturnType<RocksDB["batch"]>;
export async function initDB(path: string) {
db = new RocksDB(path);
await new Promise((resolve, reject) => {
db.open(
{
createIfMissing: true,
},
(err) => {
if (err) {
reject(err);
process.exit(1);
} else {
resolve(undefined);
}
}
);
});
}
export function getDB() {
return db;
}
export function cleanDB() {
db.close(console.info);
}
export function startBatch() {
return db.batch();
}
interface Category {
id: number;
name: string;
parentId: number | null;
url: string | null; // null if api used to scrape
}
export function insertCategory(category: Category, batch: RocksDBBatch) {
return batch.put(`category:${category.id}`, JSON.stringify(category));
}
interface Forum {
id: number;
name: string;
categoryId: number | null;
url: string | null; // null if api used to scrape
}
export function insertForum(forum: Forum, batch: RocksDBBatch) {
return batch.put(`forum:${forum.id}`, JSON.stringify(forum));
}
interface Thread {
id: number;
title: string;
url: string;
date: string;
lastEditDate: string | null; // null if api used to scrape
pages: number;
categoryId: number | null;
forumId: number | null;
authorId: string | null; // null if api used to scrape and author is anon
tags: { url: string | null; name: string }[] | null; // null if android api used to scrape otherwise url is null if web api
}
export function insertThread(thread: Thread, batch: RocksDBBatch) {
return batch.put(`thread:meta:${thread.id}`, JSON.stringify(thread));
}
interface Page {
url: string;
posts: number[];
pageNumber: number;
}
export function insertPage(page: Page, threadId: number, batch: RocksDBBatch) {
return batch.put(
`thread:page:${threadId}:${page.pageNumber}`,
JSON.stringify(page)
);
}
interface Post {
id: number;
content: string;
content_markdown?: string; // only used if api used to scrape
creationDate: string;
lastEditDate: string | null; // null if api used to scrape
authorId: string | null; // null if api used to scrape and post author is anon
pageNumber: number;
}
export function insertPost(post: Post, batch: RocksDBBatch) {
return batch.put(`thread:post:${post.id}`, JSON.stringify(post));
}
interface User {
id: number | null;
username: string;
url: string | null;
anonymous: boolean;
anonPFPStyles: string | null;
}
export function insertUser(user: User, batch: RocksDBBatch) {
let userKey;
if (user.anonymous) {
userKey = `user:anon:${[user.username, user.anonPFPStyles].join("|")}`;
} else {
userKey = `user:auth:${user.username}`;
}
return batch.put(userKey, JSON.stringify(user));
}
|