use Next.js

This commit is contained in:
polv committed 2020-12-17 23:15:28 +07:00
1 parent cf94c66807
commit 20a00d5ab3
34 files changed
+7117 -1

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
import { dbPost, idx, initDatabase } from './db'
export async function doSearch ({
q,
tag,
page = 1
}: {
q?: string
tag?: string
page?: number
}) {
await initDatabase()
const perPage = 5
const offset = (page - 1) * perPage
let cond = dbPost.chain().find({
date: { $exists: true } as any
})
if (q) {
cond = cond.find({
path: { $in: idx.search(q).map(({ ref }) => ref) }
})
}
if (tag) {
cond = cond.find({
tag: { $contains: tag }
})
}
cond = cond.simplesort('date', { desc: true })
const count = cond.branch().count()
const result = cond.offset(offset).limit(perPage).data()
return {
count,
result
}
}