add simple line break
This commit is contained in:
1 parent
4fb41de0f1
commit
e4e364ae2a
8 files changed
+123
-73
No files matched your search
+54
-18
@@ -47,7 +47,7 @@ export class Database {
|
||||
priority INT,
|
||||
-- q.title
|
||||
[image] TEXT,
|
||||
-- q.tag.split(/ /g)
|
||||
-- m2o files_tag
|
||||
-- q.category
|
||||
-- q.content
|
||||
excerptHtml TEXT NOT NULL,
|
||||
@@ -58,6 +58,12 @@ export class Database {
|
||||
CREATE INDEX IF NOT EXISTS files_createdAt ON files(createdAt);
|
||||
CREATE INDEX IF NOT EXISTS files_updatedAt ON files(updatedAt);
|
||||
CREATE INDEX IF NOT EXISTS files_priority ON files(priority);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS files_tag (
|
||||
[path] TEXT NOT NULL,
|
||||
tag TEXT NOT NULL,
|
||||
PRIMARY KEY ([path], tag)
|
||||
);
|
||||
`)
|
||||
|
||||
this.db.exec(/* sql */ `
|
||||
@@ -77,6 +83,11 @@ export class Database {
|
||||
VALUES (@path, @date, @createdAt, @updatedAt, @priority, @image, @excerptHtml, @contentHtml)
|
||||
`)
|
||||
|
||||
const ftStmt = this.db.prepare(/* sql */ `
|
||||
INSERT INTO files_tag ([path], tag)
|
||||
VALUES (@path, @tag)
|
||||
`)
|
||||
|
||||
const qStmt = this.db.prepare(/* sql */ `
|
||||
INSERT INTO q ([path], title, tag, category, content)
|
||||
VALUES (@path, @title, @tag, @category, @content)
|
||||
@@ -118,6 +129,15 @@ export class Database {
|
||||
content,
|
||||
tag: tag ? tag.join(' ') : undefined,
|
||||
})
|
||||
|
||||
if (tag) {
|
||||
Array.from(new Set(tag)).map((t) => {
|
||||
ftStmt.run({
|
||||
path,
|
||||
tag: t,
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
})()
|
||||
}
|
||||
@@ -130,12 +150,14 @@ export class Database {
|
||||
[date],
|
||||
title,
|
||||
[image],
|
||||
tag,
|
||||
GROUP_CONCAT(ft.tag, ' ') tag,
|
||||
category,
|
||||
contentHtml content
|
||||
FROM files f
|
||||
JOIN q ON q.path = f.path
|
||||
JOIN q ON q.path = f.path
|
||||
LEFT JOIN files_tag ft ON ft.path = f.path
|
||||
WHERE f.path = @id
|
||||
GROUP BY f.path
|
||||
`
|
||||
)
|
||||
.get({ id })
|
||||
@@ -163,6 +185,7 @@ export class Database {
|
||||
opts: {
|
||||
offset: number
|
||||
limit: number
|
||||
tag?: string
|
||||
}
|
||||
) {
|
||||
return this._search(q, {
|
||||
@@ -171,17 +194,11 @@ export class Database {
|
||||
})
|
||||
}
|
||||
|
||||
public by(
|
||||
q: string,
|
||||
opts: {
|
||||
offset: number
|
||||
limit: number
|
||||
}
|
||||
) {
|
||||
return this._search(q, {
|
||||
public by(opts: { tag?: string; offset: number; limit: number }) {
|
||||
return this._search('', {
|
||||
...opts,
|
||||
orderedBy: 'priority DESC, [date] DESC, createdAt DESC, updatedAt DESC',
|
||||
where: '[date] IS NOT NULL',
|
||||
where: ['[date] IS NOT NULL'],
|
||||
})
|
||||
}
|
||||
|
||||
@@ -191,15 +208,36 @@ export class Database {
|
||||
offset: number
|
||||
limit: number
|
||||
orderedBy: string
|
||||
where?: string
|
||||
tag?: string
|
||||
where?: string[]
|
||||
}
|
||||
) {
|
||||
const $where: string[] = []
|
||||
if (q) {
|
||||
$where.push('q = @q')
|
||||
}
|
||||
|
||||
if (opts.tag) {
|
||||
$where.push('ft.tag = @tag')
|
||||
}
|
||||
|
||||
if (opts.where) {
|
||||
$where.push(...opts.where)
|
||||
}
|
||||
|
||||
const fromSegment = /* sql */ `
|
||||
FROM files f
|
||||
JOIN q ON q.path = f.path
|
||||
WHERE ${q ? `q = @q` : 'TRUE'} ${opts.where ? `AND ${opts.where}` : ''}
|
||||
${opts.tag ? `LEFT JOIN files_tag ft ON ft.path = f.path` : ''}
|
||||
WHERE ${$where.join(' AND ') || 'TRUE'}
|
||||
${opts.tag ? 'GROUP BY f.path' : ''}
|
||||
`
|
||||
|
||||
const cond = {
|
||||
q,
|
||||
tag: opts.tag,
|
||||
}
|
||||
|
||||
const count =
|
||||
this.db
|
||||
.prepare(
|
||||
@@ -208,7 +246,7 @@ export class Database {
|
||||
${fromSegment}
|
||||
`
|
||||
)
|
||||
.get({ q })?.count || 0
|
||||
.get(cond)?.count || 0
|
||||
|
||||
const result = this.db
|
||||
.prepare(
|
||||
@@ -218,7 +256,6 @@ export class Database {
|
||||
[date],
|
||||
title,
|
||||
[image],
|
||||
tag,
|
||||
category,
|
||||
excerptHtml excerpt
|
||||
${fromSegment}
|
||||
@@ -226,12 +263,11 @@ export class Database {
|
||||
LIMIT ${opts.limit} OFFSET ${opts.offset}
|
||||
`
|
||||
)
|
||||
.all({ q }) as {
|
||||
.all(cond) as {
|
||||
path: string
|
||||
date?: Date
|
||||
title: string
|
||||
image?: string
|
||||
tag?: string[]
|
||||
category?: string
|
||||
excerpt: string
|
||||
}[]
|
||||
|
||||
@@ -42,18 +42,20 @@ app.get('/q', (req, res) => {
|
||||
|
||||
if (q) {
|
||||
res.json(
|
||||
db.q(`${q} ${tag ? `tag:${tag}` : ''}`, {
|
||||
db.q(q, {
|
||||
offset,
|
||||
limit: perPage,
|
||||
tag,
|
||||
})
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
res.json(
|
||||
db.by(tag ? `tag:${tag}` : '', {
|
||||
db.by({
|
||||
offset,
|
||||
limit: perPage,
|
||||
tag,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user