36 lines
1004 B
TypeScript
36 lines
1004 B
TypeScript
import type { User } from "@markone/core/user"
|
|
import type { Bookmark } from "@markone/core/bookmark"
|
|
import { db } from "~/database.ts"
|
|
import { DEMO_BOOKMARKS } from "./demo-bookmarks.ts"
|
|
|
|
function insertDemoBookmarks(user: User) {
|
|
const query = db.query(`
|
|
INSERT OR IGNORE INTO bookmarks (id, user_id, kind, title, url)
|
|
VALUES ($id, $userId, $kind, $title, $url)
|
|
`)
|
|
const insert = db.transaction((bookmarks) => {
|
|
for (const bookmark of bookmarks) {
|
|
query.run({
|
|
id: bookmark.id,
|
|
userId: user.id,
|
|
kind: bookmark.kind,
|
|
title: bookmark.title,
|
|
url: bookmark.url,
|
|
})
|
|
}
|
|
})
|
|
insert(DEMO_BOOKMARKS)
|
|
}
|
|
|
|
function findBookmarkHtml(id: string, user: User): string | null {
|
|
const query = db.query("SELECT content_html FROM bookmarks WHERE id = $id AND user_id = $userId")
|
|
const row = query.get({ id, userId: user.id })
|
|
if (!row) {
|
|
return null
|
|
}
|
|
const { content_html } = row as { content_html: string }
|
|
return content_html
|
|
}
|
|
|
|
export { insertDemoBookmarks, findBookmarkHtml }
|