feat: init db

This commit is contained in:
Julien Calixte
2026-02-08 23:26:52 +01:00
parent ab1be0efb6
commit 2312240d10
9 changed files with 109 additions and 270 deletions

43
src/data/db.ts Normal file
View File

@@ -0,0 +1,43 @@
import { DB } from "https://deno.land/x/sqlite/mod.ts";
import type { Note } from "./note"
export const db = new DB("notes.db");
export const createNote = async (note: Note) => {
return db.query(
`
INSERT INTO note (
title, content, publishedAt, createdAt, did, rkey
) VALUES (?, ?, ?, ?, ?, ?)
`,
[
note.title,
note.content,
new Date(note.publishedAt).toISOString(), // publishedAt
new Date(note.createdAt).toISOString(), // createdAt
note.did,
note.rkey,
],
);
}
export const updateNote = async (note: Note) => {
db.query(
`
UPDATE note
SET
title = ?,
content = ?,
publishedAt = ?
WHERE did = ?
AND rkey = ?
`,
[
note.title,
note.content,
note.publishedAt, // publishedAt
note.did,
note.rkey,
],
);
}

8
src/data/note.ts Normal file
View File

@@ -0,0 +1,8 @@
export type Note = {
did: string
rkey: string
title: string
content: string
publishedAt: Date
createdAt: Date
}

15
src/migrations/init.ts Normal file
View File

@@ -0,0 +1,15 @@
import { db } from "../data/db"
db.execute(`
CREATE TABLE IF NOT EXISTS note (
title TEXT NOT NULL,
content TEXT NOT NULL,
publishedAt DATETIME,
createdAt DATETIME NOT NULL,
did TEXT NOT NULL,
rkey TEXT NOT NULL,
PRIMARY KEY (did, rkey)
);
`);
db.close();