feat: add endpoints to get notes

This commit is contained in:
Julien Calixte
2026-02-09 14:07:17 +01:00
parent ed157cb4df
commit 6e22fd2f56
3 changed files with 68 additions and 2 deletions

View File

@@ -3,6 +3,58 @@ import type { Note } from "./note.ts";
export const db = new DB(Deno.env.get("SQLITE_PATH") ?? "notes.db");
export const getNotes = (cursor?: string, limit = 20) => {
const rows = cursor
? db.query<[string, string, string, string, string, string]>(
"SELECT did, rkey, title, content, publishedAt, createdAt FROM note WHERE rkey < ? ORDER BY rkey DESC LIMIT ?",
[cursor, limit],
)
: db.query<[string, string, string, string, string, string]>(
"SELECT did, rkey, title, content, publishedAt, createdAt FROM note ORDER BY rkey DESC LIMIT ?",
[limit],
);
const notes = rows.map(([did, rkey, title, content, publishedAt, createdAt]) => ({
did,
rkey,
title,
content,
publishedAt,
createdAt,
}));
return {
notes,
cursor: notes.length === limit ? notes[notes.length - 1].rkey : undefined,
};
};
export const getNotesByDid = (did: string, cursor?: string, limit = 20) => {
const rows = cursor
? db.query<[string, string, string, string, string, string]>(
"SELECT did, rkey, title, content, publishedAt, createdAt FROM note WHERE did = ? AND rkey < ? ORDER BY rkey DESC LIMIT ?",
[did, cursor, limit],
)
: db.query<[string, string, string, string, string, string]>(
"SELECT did, rkey, title, content, publishedAt, createdAt FROM note WHERE did = ? ORDER BY rkey DESC LIMIT ?",
[did, limit],
);
const notes = rows.map(([did, rkey, title, content, publishedAt, createdAt]) => ({
did,
rkey,
title,
content,
publishedAt,
createdAt,
}));
return {
notes,
cursor: notes.length === limit ? notes[notes.length - 1].rkey : undefined,
};
};
export const upsertNote = (note: Note) => {
db.query(
`