feat: init db
This commit is contained in:
43
src/data/db.ts
Normal file
43
src/data/db.ts
Normal 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
8
src/data/note.ts
Normal 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
15
src/migrations/init.ts
Normal 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();
|
||||
Reference in New Issue
Block a user