add query page

This commit is contained in:
Pacharapol Withayasakpunt committed 2020-02-28 04:30:25 +07:00
1 parent c2ae07be06
commit 3bc2a943bd
31 files changed
+380 -164

No files matched your search

+1
View File
@@ -10,6 +10,7 @@
"dependencies": {
"@patarapolw/qsearch": "^1.1.1",
"cors": "^2.8.5",
"dayjs": "^1.8.21",
"express": "^4.17.1",
"fast-glob": "^3.2.2",
"gray-matter": "^4.0.2",
+21
View File
@@ -4,6 +4,7 @@ import crypto from 'crypto'
import matter from 'gray-matter'
import glob from 'fast-glob'
import DataStore from 'nedb-promises'
import dayjs from 'dayjs'
import MakeHtml from '../src/utils/make-html'
@@ -44,6 +45,7 @@ const db = new DataStore('assets/data.nedb')
await db.insert({
...data,
date: formatDate(data.date),
filename: f,
h,
excerpt: computedExcerpt,
@@ -64,8 +66,13 @@ const db = new DataStore('assets/data.nedb')
computedContent = computedContent.substr(computedExcerpt.length)
}
if (data.date) {
console.log(formatDate(data.date))
}
await db.insert({
...data,
date: formatDate(data.date),
filename: f,
h,
excerpt: computedExcerpt,
@@ -77,3 +84,17 @@ const db = new DataStore('assets/data.nedb')
}
}))
})().catch(console.error)
const tzOffset = new Date().getTimezoneOffset()
function formatDate (d?: string | Date) {
if (d) {
if (typeof d === 'string') {
return dayjs(d).toDate()
} else {
return dayjs(d).add(tzOffset, 'minute').toDate()
}
}
return d
}
+12 -9
View File
@@ -26,26 +26,29 @@ dataRouter.get('/*', (req, res) => {
dataRouter.post('/', async (req, res, next) => {
try {
const { q, project, offset, limit } = req.body
let cond = q
if (typeof q === 'string') {
const { q, cond = {}, project, offset, limit } = req.body
const $and = [
{ filename: { $regex: /^posts\// } },
cond
] as any[]
if (q) {
const parser = new QSearch({
dialect: 'nedb',
schema: {
title: {},
filename: {},
excerpt: {},
date: { type: 'date' }
}
})
cond = parser.parse(q)
$and.push(parser.parse(q))
}
const count = await db.count(cond)
const count = await db.count({ $and })
let c = db.find({ $and }, project).skip(offset || 0)
let c = db.find(cond).skip(offset || 0)
if (project) {
c = c.projection(project)
}
if (limit !== null) {
c = c.limit(limit || 5)
}
+5
View File
@@ -420,6 +420,11 @@ dateformat@~1.0.4-1.2.3:
get-stdin "^4.0.1"
meow "^3.3.0"
dayjs@^1.8.21:
version "1.8.21"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.8.21.tgz#98299185b72b9b679f31c7ed987b63923c961552"
integrity sha512-1kbWK0hziklUHkGgiKr7xm59KwAg/K3Tp7H/8X+f58DnNCwY3pKYjOCJpIlVs125FRBukGVZdKZojC073D0IeQ==
debounce@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/debounce/-/debounce-1.2.0.tgz#44a540abc0ea9943018dc0eaa95cce87f65cd131"