Насторил работу блога через Backend
This commit is contained in:
parent
014439d565
commit
edd730b438
33 changed files with 1019 additions and 200 deletions
59
scripts/create-posts-collection.ts
Normal file
59
scripts/create-posts-collection.ts
Normal 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
82
scripts/migrate-posts.ts
Normal 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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue