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
218
219
220
221
222
223
224
225
226
|
import {
and,
eq,
exists,
gt,
gte,
like,
lt,
lte,
notExists,
notLike,
} from "drizzle-orm";
import {
bookmarkLinks,
bookmarkLists,
bookmarks,
bookmarksInLists,
bookmarkTags,
tagsOnBookmarks,
} from "@hoarder/db/schema";
import { Matcher } from "@hoarder/shared/types/search";
import { AuthedContext } from "..";
interface BookmarkQueryReturnType {
id: string;
}
function intersect(
vals: BookmarkQueryReturnType[][],
): BookmarkQueryReturnType[] {
if (!vals || vals.length === 0) {
return [];
}
if (vals.length === 1) {
return [...vals[0]];
}
const countMap = new Map<string, number>();
const map = new Map<string, BookmarkQueryReturnType>();
for (const arr of vals) {
for (const item of arr) {
countMap.set(item.id, (countMap.get(item.id) ?? 0) + 1);
map.set(item.id, item);
}
}
const result: BookmarkQueryReturnType[] = [];
for (const [id, count] of countMap) {
if (count === vals.length) {
result.push(map.get(id)!);
}
}
return result;
}
function union(vals: BookmarkQueryReturnType[][]): BookmarkQueryReturnType[] {
if (!vals || vals.length === 0) {
return [];
}
const uniqueIds = new Set<string>();
const map = new Map<string, BookmarkQueryReturnType>();
for (const arr of vals) {
for (const item of arr) {
uniqueIds.add(item.id);
map.set(item.id, item);
}
}
const result: BookmarkQueryReturnType[] = [];
for (const id of uniqueIds) {
result.push(map.get(id)!);
}
return result;
}
async function getIds(
db: AuthedContext["db"],
userId: string,
matcher: Matcher,
): Promise<BookmarkQueryReturnType[]> {
switch (matcher.type) {
case "tagName": {
const comp = matcher.inverse ? notExists : exists;
return db
.selectDistinct({ id: bookmarks.id })
.from(bookmarks)
.where(
and(
eq(bookmarks.userId, userId),
comp(
db
.select()
.from(tagsOnBookmarks)
.innerJoin(
bookmarkTags,
eq(tagsOnBookmarks.tagId, bookmarkTags.id),
)
.where(
and(
eq(tagsOnBookmarks.bookmarkId, bookmarks.id),
eq(bookmarkTags.userId, userId),
eq(bookmarkTags.name, matcher.tagName),
),
),
),
),
);
}
case "listName": {
const comp = matcher.inverse ? notExists : exists;
return db
.selectDistinct({ id: bookmarks.id })
.from(bookmarks)
.where(
and(
eq(bookmarks.userId, userId),
comp(
db
.select()
.from(bookmarksInLists)
.innerJoin(
bookmarkLists,
eq(bookmarksInLists.listId, bookmarkLists.id),
)
.where(
and(
eq(bookmarksInLists.bookmarkId, bookmarks.id),
eq(bookmarkLists.userId, userId),
eq(bookmarkLists.name, matcher.listName),
),
),
),
),
);
}
case "archived": {
return db
.select({ id: bookmarks.id })
.from(bookmarks)
.where(
and(
eq(bookmarks.userId, userId),
eq(bookmarks.archived, matcher.archived),
),
);
}
case "url": {
const comp = matcher.inverse ? notLike : like;
return db
.select({ id: bookmarkLinks.id })
.from(bookmarkLinks)
.leftJoin(bookmarks, eq(bookmarks.id, bookmarkLinks.id))
.where(
and(
eq(bookmarks.userId, userId),
comp(bookmarkLinks.url, `%${matcher.url}%`),
),
);
}
case "favourited": {
return db
.select({ id: bookmarks.id })
.from(bookmarks)
.where(
and(
eq(bookmarks.userId, userId),
eq(bookmarks.favourited, matcher.favourited),
),
);
}
case "dateAfter": {
const comp = matcher.inverse ? lt : gte;
return db
.select({ id: bookmarks.id })
.from(bookmarks)
.where(
and(
eq(bookmarks.userId, userId),
comp(bookmarks.createdAt, matcher.dateAfter),
),
);
}
case "dateBefore": {
const comp = matcher.inverse ? gt : lte;
return db
.select({ id: bookmarks.id })
.from(bookmarks)
.where(
and(
eq(bookmarks.userId, userId),
comp(bookmarks.createdAt, matcher.dateBefore),
),
);
}
case "and": {
const vals = await Promise.all(
matcher.matchers.map((m) => getIds(db, userId, m)),
);
return intersect(vals);
}
case "or": {
const vals = await Promise.all(
matcher.matchers.map((m) => getIds(db, userId, m)),
);
return union(vals);
}
default: {
throw new Error("Unknown matcher type");
}
}
}
export async function getBookmarkIdsFromMatcher(
ctx: AuthedContext,
matcher: Matcher,
): Promise<string[]> {
const results = await getIds(ctx.db, ctx.user.id, matcher);
return results.map((r) => r.id);
}
|