Initial reader-api backend extracted from reader
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
const { Client } = require('pg');
|
||||
require('dotenv').config({ path: '.env.local' });
|
||||
|
||||
async function checkAndCreateDatabase() {
|
||||
const dbUrl = process.env.DATABASE_URL;
|
||||
if (!dbUrl) {
|
||||
console.error('DATABASE_URL is not set in .env.local');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Phân tích DATABASE_URL
|
||||
const match = dbUrl.match(/postgresql:\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/([^?]+)/);
|
||||
if (!match) {
|
||||
console.error('Invalid DATABASE_URL format');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const [_, user, password, host, port, database] = match;
|
||||
|
||||
// Kết nối đến database mặc định 'postgres'
|
||||
const client = new Client({
|
||||
user,
|
||||
password: decodeURIComponent(password),
|
||||
host,
|
||||
port: parseInt(port),
|
||||
database: 'postgres',
|
||||
});
|
||||
|
||||
try {
|
||||
await client.connect();
|
||||
|
||||
const res = await client.query(`SELECT 1 FROM pg_database WHERE datname = $1`, [database]);
|
||||
|
||||
if (res.rowCount === 0) {
|
||||
console.log(`Bắt đầu tạo database "${database}"...`);
|
||||
await client.query(`CREATE DATABASE "${database}"`);
|
||||
console.log(`Đã tạo thành công database "${database}".`);
|
||||
} else {
|
||||
console.log(`Database "${database}" đã tồn tại, bỏ qua tạo mới.`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Lỗi khi kiểm tra/tạo database:', error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
}
|
||||
|
||||
checkAndCreateDatabase();
|
||||
@@ -0,0 +1,60 @@
|
||||
const { PrismaClient } = require('@prisma/client')
|
||||
const mongoose = require('mongoose')
|
||||
require('dotenv').config({ path: '.env.local' })
|
||||
require('dotenv').config()
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
async function main() {
|
||||
console.log('Connecting to MongoDB...')
|
||||
// Connect to MongoDB using MONGODB_URI
|
||||
const mongoUri = process.env.MONGODB_URI
|
||||
if (!mongoUri) {
|
||||
throw new Error('MONGODB_URI is not defined in env')
|
||||
}
|
||||
await mongoose.connect(mongoUri)
|
||||
|
||||
// Wipe MongoDB Chapters
|
||||
console.log('Wiping chapters from MongoDB...')
|
||||
try {
|
||||
const chapterSchema = new mongoose.Schema({}, { strict: false })
|
||||
const Chapter = mongoose.models.Chapter || mongoose.model('Chapter', chapterSchema, 'chapters')
|
||||
const res = await Chapter.deleteMany({})
|
||||
console.log(`Deleted ${res.deletedCount} chapters.`)
|
||||
} catch (e) {
|
||||
console.error('Error wiping mongo chapters', e)
|
||||
}
|
||||
|
||||
// Wipe PostgreSQL Content
|
||||
console.log('Wiping Novels, Genres, Comments, Bookmarks from PostgreSQL...')
|
||||
try {
|
||||
// Delete in order to respect foreign keys if Cascade isn't perfect, but Cascade is set on most.
|
||||
await prisma.comment.deleteMany({})
|
||||
console.log('Deleted all comments.')
|
||||
|
||||
await prisma.bookmark.deleteMany({})
|
||||
console.log('Deleted all bookmarks.')
|
||||
|
||||
await prisma.novelGenre.deleteMany({})
|
||||
console.log('Deleted all novel_genres.')
|
||||
|
||||
await prisma.genre.deleteMany({})
|
||||
console.log('Deleted all genres.')
|
||||
|
||||
await prisma.novel.deleteMany({})
|
||||
console.log('Deleted all novels.')
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error wiping postgres', error)
|
||||
}
|
||||
|
||||
console.log('Cleanup complete.')
|
||||
}
|
||||
|
||||
main()
|
||||
.catch(console.error)
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect()
|
||||
await mongoose.disconnect()
|
||||
process.exit(0)
|
||||
})
|
||||
Reference in New Issue
Block a user