refacto: use @db/sqlite for better usage
This commit is contained in:
@@ -1,33 +1,32 @@
|
||||
import { DB } from "https://deno.land/x/sqlite/mod.ts";
|
||||
import { Database } from "@db/sqlite";
|
||||
import type { Note } from "./note.ts";
|
||||
|
||||
export const db = new DB(Deno.env.get("SQLITE_PATH") ?? "notes.db");
|
||||
export const db = new Database(Deno.env.get("SQLITE_PATH") ?? "notes.db");
|
||||
try {
|
||||
const [[journalMode]] = db.query<[string]>("PRAGMA journal_mode=WAL");
|
||||
db.query("PRAGMA busy_timeout=5000");
|
||||
console.log(`[db] journal_mode=${journalMode}, busy_timeout=5000`);
|
||||
db.exec("PRAGMA journal_mode=WAL");
|
||||
db.exec("PRAGMA busy_timeout=5000");
|
||||
const [row] = db.prepare("PRAGMA journal_mode").all<{ journal_mode: string }>();
|
||||
console.log(`[db] journal_mode=${row.journal_mode}, busy_timeout=5000`);
|
||||
} catch (e) {
|
||||
console.error("[db] failed to set PRAGMAs:", e);
|
||||
}
|
||||
|
||||
export const getNotes = (cursor?: string, limit = 20) => {
|
||||
const rows = cursor
|
||||
? db.query<[string, string, string, string, string]>(
|
||||
"SELECT did, rkey, title, publishedAt, createdAt FROM note WHERE rkey < ? ORDER BY rkey DESC LIMIT ?",
|
||||
[cursor, limit],
|
||||
)
|
||||
: db.query<[string, string, string, string, string]>(
|
||||
"SELECT did, rkey, title, publishedAt, createdAt FROM note ORDER BY rkey DESC LIMIT ?",
|
||||
[limit],
|
||||
);
|
||||
type NoteRow = {
|
||||
did: string;
|
||||
rkey: string;
|
||||
title: string;
|
||||
publishedAt: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
const notes = rows.map(([did, rkey, title, publishedAt, createdAt]) => ({
|
||||
did,
|
||||
rkey,
|
||||
title,
|
||||
publishedAt,
|
||||
createdAt,
|
||||
}));
|
||||
export const getNotes = (cursor?: string, limit = 20) => {
|
||||
const notes = cursor
|
||||
? db.prepare(
|
||||
"SELECT did, rkey, title, publishedAt, createdAt FROM note WHERE rkey < ? ORDER BY rkey DESC LIMIT ?",
|
||||
).all<NoteRow>(cursor, limit)
|
||||
: db.prepare(
|
||||
"SELECT did, rkey, title, publishedAt, createdAt FROM note ORDER BY rkey DESC LIMIT ?",
|
||||
).all<NoteRow>(limit);
|
||||
|
||||
return {
|
||||
notes,
|
||||
@@ -36,23 +35,13 @@ export const getNotes = (cursor?: string, limit = 20) => {
|
||||
};
|
||||
|
||||
export const getNotesByDid = (did: string, cursor?: string, limit = 20) => {
|
||||
const rows = cursor
|
||||
? db.query<[string, string, string, string, string]>(
|
||||
const notes = cursor
|
||||
? db.prepare(
|
||||
"SELECT did, rkey, title, publishedAt, createdAt FROM note WHERE did = ? AND rkey < ? ORDER BY rkey DESC LIMIT ?",
|
||||
[did, cursor, limit],
|
||||
)
|
||||
: db.query<[string, string, string, string, string]>(
|
||||
).all<NoteRow>(did, cursor, limit)
|
||||
: db.prepare(
|
||||
"SELECT did, rkey, title, publishedAt, createdAt FROM note WHERE did = ? ORDER BY rkey DESC LIMIT ?",
|
||||
[did, limit],
|
||||
);
|
||||
|
||||
const notes = rows.map(([did, rkey, title, publishedAt, createdAt]) => ({
|
||||
did,
|
||||
rkey,
|
||||
title,
|
||||
publishedAt,
|
||||
createdAt,
|
||||
}));
|
||||
).all<NoteRow>(did, limit);
|
||||
|
||||
return {
|
||||
notes,
|
||||
@@ -61,35 +50,26 @@ export const getNotesByDid = (did: string, cursor?: string, limit = 20) => {
|
||||
};
|
||||
|
||||
export const deleteNote = ({ did, rkey }: { did: string; rkey: string }) => {
|
||||
db.query("DELETE FROM note WHERE did = ? AND rkey = ?", [did, rkey]);
|
||||
db.exec("DELETE FROM note WHERE did = ? AND rkey = ?", did, rkey);
|
||||
};
|
||||
|
||||
export const getCursor = (): string | undefined => {
|
||||
const rows = db.query<[string]>(
|
||||
const row = db.prepare(
|
||||
"SELECT value FROM state WHERE key = 'cursor'",
|
||||
);
|
||||
return rows[0]?.[0];
|
||||
).get<{ value: string }>();
|
||||
return row?.value;
|
||||
};
|
||||
|
||||
export const saveCursor = (cursor: number) => {
|
||||
db.query(
|
||||
db.exec(
|
||||
"INSERT OR REPLACE INTO state (key, value) VALUES ('cursor', ?)",
|
||||
[String(cursor)],
|
||||
String(cursor),
|
||||
);
|
||||
};
|
||||
|
||||
export const upsertNote = (note: Note) => {
|
||||
const now = new Date().toISOString();
|
||||
const params = [
|
||||
note.title,
|
||||
note.publishedAt ? new Date(note.publishedAt).toISOString() : now,
|
||||
note.createdAt ? new Date(note.createdAt).toISOString() : now,
|
||||
note.did,
|
||||
note.rkey,
|
||||
];
|
||||
console.log(`[db] upsertNote params: ${JSON.stringify(params)}`);
|
||||
|
||||
db.query(
|
||||
db.exec(
|
||||
`
|
||||
INSERT INTO note (
|
||||
title,
|
||||
@@ -104,6 +84,10 @@ export const upsertNote = (note: Note) => {
|
||||
title = excluded.title,
|
||||
publishedAt = excluded.publishedAt
|
||||
`,
|
||||
params,
|
||||
note.title,
|
||||
note.publishedAt ? new Date(note.publishedAt).toISOString() : now,
|
||||
note.createdAt ? new Date(note.createdAt).toISOString() : now,
|
||||
note.did,
|
||||
note.rkey,
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user