Насторил работу блога через Backend

This commit is contained in:
Web-serfer 2026-04-15 02:12:25 +05:00
parent 014439d565
commit edd730b438
33 changed files with 1019 additions and 200 deletions

View file

@ -0,0 +1,59 @@
import PocketBase from 'pocketbase';
const PB_URL = process.env.POCKETBASE_URL || 'http://127.0.0.1:8090';
const ADMIN_EMAIL = process.env.PB_ADMIN_EMAIL;
const ADMIN_PASSWORD = process.env.PB_ADMIN_PASSWORD;
async function createCollection() {
if (!ADMIN_EMAIL || !ADMIN_PASSWORD) {
console.error('❌ Укажите PB_ADMIN_EMAIL и PB_ADMIN_PASSWORD в .env');
process.exit(1);
}
const pb = new PocketBase(PB_URL);
try {
await pb.admins.authWithPassword(ADMIN_EMAIL, ADMIN_PASSWORD);
console.log('✅ Подключено к PocketBase');
} catch (e) {
console.error('❌ Ошибка авторизации');
process.exit(1);
}
try {
const collection = await pb.collections.create({
name: 'posts',
type: 'base',
system: false,
schema: [
{ name: 'title', type: 'text', required: true },
{ name: 'description', type: 'text', required: true },
{ name: 'author', type: 'text', required: false },
{ name: 'category', type: 'text', required: false },
{ name: 'categoryColor', type: 'text', required: false },
{ name: 'date', type: 'date', required: true },
{ name: 'readTime', type: 'text', required: false },
{ name: 'imageUrl', type: 'text', required: false },
{ name: 'slug', type: 'text', required: true },
{ name: 'draft', type: 'bool', required: false },
{ name: 'content', type: 'editor', required: false },
],
listRule: '',
viewRule: '',
createRule: '@request.auth.id != ""',
updateRule: '@request.auth.id != ""',
deleteRule: '@request.auth.id != ""',
});
console.log('✅ Коллекция "posts" создана');
console.log(' ID:', collection.id);
} catch (e: any) {
if (e.data?.message?.includes('already exists')) {
console.log(' Коллекция "posts" уже существует');
} else {
console.error('❌ Ошибка:', e.data || e.message);
}
}
}
createCollection();

82
scripts/migrate-posts.ts Normal file
View file

@ -0,0 +1,82 @@
import PocketBase from 'pocketbase';
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
const PB_URL = process.env.POCKETBASE_URL || 'http://127.0.0.1:8090';
const ADMIN_EMAIL = process.env.PB_ADMIN_EMAIL;
const ADMIN_PASSWORD = process.env.PB_ADMIN_PASSWORD;
interface PostFrontmatter {
title: string;
description: string;
author: string;
category: string;
categoryColor: string;
date: string;
readTime: string;
imageUrl: string;
draft: boolean;
}
async function migrate() {
console.log('🔄 Миграция постов в PocketBase...\n');
if (!ADMIN_EMAIL || !ADMIN_PASSWORD) {
console.error('❌ Укажите PB_ADMIN_EMAIL и PB_ADMIN_PASSWORD в .env');
process.exit(1);
}
const pb = new PocketBase(PB_URL);
try {
await pb.admins.authWithPassword(ADMIN_EMAIL, ADMIN_PASSWORD);
console.log('✅ Подключено к PocketBase');
} catch (e) {
console.error('❌ Ошибка авторизации в PocketBase');
process.exit(1);
}
const blogDir = path.join(process.cwd(), 'frontend/src/content/blog');
const files = fs.readdirSync(blogDir).filter(f => f.endsWith('.mdx'));
console.log(`📂 Найдено ${files.length} постов в ${blogDir}\n`);
let migrated = 0;
let skipped = 0;
for (const file of files) {
const filePath = path.join(blogDir, file);
const fileContent = fs.readFileSync(filePath, 'utf-8');
const { data, content } = matter(fileContent);
const frontmatter = data as PostFrontmatter;
const slug = file.replace('.mdx', '');
try {
const record = await pb.collection('posts').create({
title: frontmatter.title,
description: frontmatter.description,
author: frontmatter.author,
category: frontmatter.category,
categoryColor: frontmatter.categoryColor,
date: frontmatter.date,
readTime: frontmatter.readTime,
imageUrl: frontmatter.imageUrl,
slug: slug,
draft: frontmatter.draft ?? false,
content: content,
});
console.log(`✅ Мигрирован: ${slug}`);
migrated++;
} catch (e: any) {
console.error(`❌ Ошибка миграции ${slug}:`, e.response?.data || e.message);
}
}
console.log(`\n📊 Готово! Мигрировано: ${migrated}, пропущено: ${skipped}`);
}
migrate().catch(console.error);