yashsingh1 commited on
Commit
3a82ffe
·
verified ·
1 Parent(s): d33f560

feat: upload orm code

Browse files
Files changed (1) hide show
  1. orm.ts +122 -0
orm.ts ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Bunch of functions to make accessing rocksdb easier
2
+
3
+ import RocksDB from "rocksdb";
4
+
5
+ let db: RocksDB;
6
+
7
+ export type RocksDBBatch = ReturnType<RocksDB["batch"]>;
8
+
9
+ export async function initDB(path: string) {
10
+ db = new RocksDB(path);
11
+ await new Promise((resolve, reject) => {
12
+ db.open(
13
+ {
14
+ createIfMissing: true,
15
+ },
16
+ (err) => {
17
+ if (err) {
18
+ reject(err);
19
+ process.exit(1);
20
+ } else {
21
+ resolve(undefined);
22
+ }
23
+ }
24
+ );
25
+ });
26
+ }
27
+
28
+ export function getDB() {
29
+ return db;
30
+ }
31
+
32
+ export function cleanDB() {
33
+ db.close(console.info);
34
+ }
35
+
36
+ export function startBatch() {
37
+ return db.batch();
38
+ }
39
+
40
+ interface Category {
41
+ id: number;
42
+ name: string;
43
+ parentId: number | null;
44
+ url: string | null; // null if api used to scrape
45
+ }
46
+
47
+ export function insertCategory(category: Category, batch: RocksDBBatch) {
48
+ return batch.put(`category:${category.id}`, JSON.stringify(category));
49
+ }
50
+
51
+ interface Forum {
52
+ id: number;
53
+ name: string;
54
+ categoryId: number | null;
55
+ url: string | null; // null if api used to scrape
56
+ }
57
+
58
+ export function insertForum(forum: Forum, batch: RocksDBBatch) {
59
+ return batch.put(`forum:${forum.id}`, JSON.stringify(forum));
60
+ }
61
+
62
+ interface Thread {
63
+ id: number;
64
+ title: string;
65
+ url: string;
66
+ date: string;
67
+ lastEditDate: string | null; // null if api used to scrape
68
+ pages: number;
69
+ categoryId: number | null;
70
+ forumId: number | null;
71
+ authorId: string | null; // null if api used to scrape and author is anon
72
+ tags: { url: string | null; name: string }[] | null; // null if android api used to scrape otherwise url is null if web api
73
+ }
74
+
75
+ export function insertThread(thread: Thread, batch: RocksDBBatch) {
76
+ return batch.put(`thread:meta:${thread.id}`, JSON.stringify(thread));
77
+ }
78
+
79
+ interface Page {
80
+ url: string;
81
+ posts: number[];
82
+ pageNumber: number;
83
+ }
84
+
85
+ export function insertPage(page: Page, threadId: number, batch: RocksDBBatch) {
86
+ return batch.put(
87
+ `thread:page:${threadId}:${page.pageNumber}`,
88
+ JSON.stringify(page)
89
+ );
90
+ }
91
+
92
+ interface Post {
93
+ id: number;
94
+ content: string;
95
+ content_markdown?: string; // only used if api used to scrape
96
+ creationDate: string;
97
+ lastEditDate: string | null; // null if api used to scrape
98
+ authorId: string | null; // null if api used to scrape and post author is anon
99
+ pageNumber: number;
100
+ }
101
+
102
+ export function insertPost(post: Post, batch: RocksDBBatch) {
103
+ return batch.put(`thread:post:${post.id}`, JSON.stringify(post));
104
+ }
105
+
106
+ interface User {
107
+ id: number | null;
108
+ username: string;
109
+ url: string | null;
110
+ anonymous: boolean;
111
+ anonPFPStyles: string | null;
112
+ }
113
+
114
+ export function insertUser(user: User, batch: RocksDBBatch) {
115
+ let userKey;
116
+ if (user.anonymous) {
117
+ userKey = `user:anon:${[user.username, user.anonPFPStyles].join("|")}`;
118
+ } else {
119
+ userKey = `user:auth:${user.username}`;
120
+ }
121
+ return batch.put(userKey, JSON.stringify(user));
122
+ }