216 lines
5.8 KiB
TypeScript
216 lines
5.8 KiB
TypeScript
import { SEO_REGISTRY, type SeoRegistryEntry } from '../data/seo-registry'
|
|
import { nowStr } from './auth'
|
|
import { execInit, execute, query, queryInit, queryOne, queryOneInit } from './mysql'
|
|
|
|
export type PageSeoRow = {
|
|
id: number
|
|
route_key: string
|
|
locale: string
|
|
page_label: string
|
|
title: string
|
|
description: string
|
|
keywords: string | null
|
|
og_title: string | null
|
|
og_description: string | null
|
|
og_image: string | null
|
|
robots: string | null
|
|
og_type: string | null
|
|
updated_at: string
|
|
updated_by: number | null
|
|
}
|
|
|
|
export type PageSeoRecord = {
|
|
id: string
|
|
routeKey: string
|
|
locale: 'en' | 'zh'
|
|
pageLabel: string
|
|
title: string
|
|
description: string
|
|
keywords?: string
|
|
ogTitle?: string
|
|
ogDescription?: string
|
|
ogImage?: string
|
|
robots?: string
|
|
ogType?: 'website' | 'article'
|
|
updatedAt: string
|
|
updatedBy?: string
|
|
}
|
|
|
|
export type PageSeoUpdateInput = {
|
|
title: string
|
|
description: string
|
|
keywords?: string
|
|
ogTitle?: string
|
|
ogDescription?: string
|
|
ogImage?: string
|
|
robots?: string
|
|
ogType?: 'website' | 'article'
|
|
}
|
|
|
|
function rowToRecord(row: PageSeoRow): PageSeoRecord {
|
|
return {
|
|
id: String(row.id),
|
|
routeKey: row.route_key,
|
|
locale: row.locale as 'en' | 'zh',
|
|
pageLabel: row.page_label,
|
|
title: row.title,
|
|
description: row.description,
|
|
keywords: row.keywords ?? undefined,
|
|
ogTitle: row.og_title ?? undefined,
|
|
ogDescription: row.og_description ?? undefined,
|
|
ogImage: row.og_image ?? undefined,
|
|
robots: row.robots ?? undefined,
|
|
ogType: (row.og_type as 'website' | 'article' | null) ?? undefined,
|
|
updatedAt: row.updated_at,
|
|
updatedBy: row.updated_by != null ? String(row.updated_by) : undefined,
|
|
}
|
|
}
|
|
|
|
function entryToValues(entry: SeoRegistryEntry, now: string) {
|
|
return [
|
|
entry.routeKey,
|
|
entry.locale,
|
|
entry.pageLabel,
|
|
entry.title,
|
|
entry.description,
|
|
entry.keywords ?? null,
|
|
entry.ogTitle ?? null,
|
|
entry.ogDescription ?? null,
|
|
entry.ogImage ?? null,
|
|
entry.robots ?? 'index,follow',
|
|
entry.ogType ?? 'website',
|
|
now,
|
|
null,
|
|
]
|
|
}
|
|
|
|
export async function initPageSeoSchema() {
|
|
await execInit(`
|
|
CREATE TABLE IF NOT EXISTS page_seo (
|
|
id BIGINT PRIMARY KEY AUTO_INCREMENT,
|
|
route_key VARCHAR(512) NOT NULL,
|
|
locale VARCHAR(8) NOT NULL,
|
|
page_label VARCHAR(255) NOT NULL,
|
|
title TEXT NOT NULL,
|
|
description TEXT NOT NULL,
|
|
keywords TEXT,
|
|
og_title TEXT,
|
|
og_description TEXT,
|
|
og_image TEXT,
|
|
robots VARCHAR(64) DEFAULT 'index,follow',
|
|
og_type VARCHAR(32) DEFAULT 'website',
|
|
updated_at VARCHAR(32) NOT NULL,
|
|
updated_by BIGINT,
|
|
UNIQUE KEY uk_page_seo_route_locale (route_key, locale)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
|
`)
|
|
}
|
|
|
|
export async function seedPageSeoIfEmpty() {
|
|
const count = await queryOneInit<{ c: number }>('SELECT COUNT(*) AS c FROM page_seo')
|
|
if ((count?.c ?? 0) > 0) return
|
|
|
|
const now = nowStr()
|
|
for (const entry of SEO_REGISTRY) {
|
|
await execInit(
|
|
`INSERT INTO page_seo (
|
|
route_key, locale, page_label, title, description,
|
|
keywords, og_title, og_description, og_image, robots, og_type,
|
|
updated_at, updated_by
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
entryToValues(entry, now)
|
|
)
|
|
}
|
|
}
|
|
|
|
export async function syncMissingPageSeo() {
|
|
const now = nowStr()
|
|
const existingRows = await queryInit<{ route_key: string; locale: string }>(
|
|
'SELECT route_key, locale FROM page_seo'
|
|
)
|
|
const existing = new Set(existingRows.map((r) => `${r.route_key}::${r.locale}`))
|
|
|
|
for (const entry of SEO_REGISTRY) {
|
|
const routeLocaleKey = `${entry.routeKey}::${entry.locale}`
|
|
if (existing.has(routeLocaleKey)) continue
|
|
|
|
await execInit(
|
|
`INSERT INTO page_seo (
|
|
route_key, locale, page_label, title, description,
|
|
keywords, og_title, og_description, og_image, robots, og_type,
|
|
updated_at, updated_by
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
entryToValues(entry, now)
|
|
)
|
|
}
|
|
}
|
|
|
|
export async function listPageSeo(params?: { locale?: string; keyword?: string }) {
|
|
let sql = 'SELECT * FROM page_seo WHERE 1=1'
|
|
const args: unknown[] = []
|
|
|
|
if (params?.locale === 'en' || params?.locale === 'zh') {
|
|
sql += ' AND locale = ?'
|
|
args.push(params.locale)
|
|
}
|
|
|
|
if (params?.keyword) {
|
|
sql += ' AND (page_label LIKE ? OR route_key LIKE ? OR title LIKE ?)'
|
|
const kw = `%${params.keyword}%`
|
|
args.push(kw, kw, kw)
|
|
}
|
|
|
|
sql += ' ORDER BY route_key ASC, locale ASC'
|
|
const rows = await query<PageSeoRow>(sql, args)
|
|
return rows.map(rowToRecord)
|
|
}
|
|
|
|
export async function getPageSeoByRoute(routeKey: string, locale: 'en' | 'zh') {
|
|
const row = await queryOne<PageSeoRow>(
|
|
'SELECT * FROM page_seo WHERE route_key = ? AND locale = ?',
|
|
[routeKey, locale]
|
|
)
|
|
return row ? rowToRecord(row) : null
|
|
}
|
|
|
|
export async function getPageSeoById(id: string | number) {
|
|
const row = await queryOne<PageSeoRow>('SELECT * FROM page_seo WHERE id = ?', [id])
|
|
return row ? rowToRecord(row) : null
|
|
}
|
|
|
|
export async function updatePageSeo(id: string | number, input: PageSeoUpdateInput, updatedBy?: string) {
|
|
const existing = await getPageSeoById(id)
|
|
if (!existing) return null
|
|
|
|
const now = nowStr()
|
|
await execute(
|
|
`UPDATE page_seo SET
|
|
title = ?,
|
|
description = ?,
|
|
keywords = ?,
|
|
og_title = ?,
|
|
og_description = ?,
|
|
og_image = ?,
|
|
robots = ?,
|
|
og_type = ?,
|
|
updated_at = ?,
|
|
updated_by = ?
|
|
WHERE id = ?`,
|
|
[
|
|
input.title,
|
|
input.description,
|
|
input.keywords ?? null,
|
|
input.ogTitle ?? null,
|
|
input.ogDescription ?? null,
|
|
input.ogImage ?? null,
|
|
input.robots ?? 'index,follow',
|
|
input.ogType ?? 'website',
|
|
now,
|
|
updatedBy ? Number(updatedBy) || null : null,
|
|
id,
|
|
]
|
|
)
|
|
|
|
return getPageSeoById(id)
|
|
}
|