generated from phildenhoff/logseq-plugin-template-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.ts
44 lines (39 loc) · 1.09 KB
/
notes.ts
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
import { DB } from "https://deno.land/x/sqlite@v3.8/mod.ts";
type UserID = string;
type Note = {
id: number;
content: string;
};
export const userIdFromLogin = (login: string): UserID => {
return `user::${login}`;
};
export const allUserNotes = (userId: UserID, db: DB): Note[] => {
const rows = db.query<[number, string]>(
"SELECT id, content FROM notes WHERE user_id = :user_id;",
{ user_id: userId }
);
return rows.map((row) => {
return {
id: row[0],
content: row[1],
};
});
};
export const allUserUnprocessedNotes = (userId: UserID, db: DB): Note[] => {
const rows = db.query<[number, string]>(
"UPDATE notes SET times_read = times_read + 1, times_processed = times_processed + 1 WHERE (user_id = :user_id) AND (times_processed < 1) RETURNING id, content;",
{ user_id: userId }
);
return rows.map((row) => {
return {
id: row[0],
content: row[1],
};
});
};
export const addNote = (userId: UserID, content: string, db: DB) => {
db.query(`INSERT INTO notes (content, user_id) VALUES (?, ?)`, [
content,
userId,
]);
};