refactor: update all scripts to use CouchDB instead of MongoDB
- Updated backend/scripts/seedBadges.js to use only CouchDB - Removed MongoDB dependencies and conditional logic - Created new scripts/seedBadges.js for root directory usage - Updated scripts/migrate-production.js to make MongoDB migration optional - Fixed module path resolution for all scripts to work from any directory - Cleaned up scripts/migrate-to-couchdb.js imports - All scripts now work with CouchDB service without MongoDB dependencies 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
@@ -1,23 +1,11 @@
|
||||
require("dotenv").config();
|
||||
const Nano = require("nano");
|
||||
|
||||
// Check if we should use CouchDB or MongoDB
|
||||
const useCouchDB = process.env.COUCHDB_URL && !process.env.MONGO_URI;
|
||||
|
||||
let db;
|
||||
let Badge;
|
||||
|
||||
if (useCouchDB) {
|
||||
// CouchDB setup
|
||||
const couchdbUrl = process.env.COUCHDB_URL;
|
||||
const dbName = process.env.COUCHDB_DB_NAME || 'adopt-a-street';
|
||||
const nano = Nano(couchdbUrl);
|
||||
db = nano.use(dbName);
|
||||
} else {
|
||||
// MongoDB setup (legacy)
|
||||
const mongoose = require("mongoose");
|
||||
Badge = require("../models/Badge");
|
||||
}
|
||||
// CouchDB setup
|
||||
const couchdbUrl = process.env.COUCHDB_URL || 'http://localhost:5984';
|
||||
const dbName = process.env.COUCHDB_DB_NAME || 'adopt-a-street';
|
||||
const nano = Nano(couchdbUrl);
|
||||
const db = nano.use(dbName);
|
||||
|
||||
/**
|
||||
* Initial badge definitions
|
||||
@@ -247,8 +235,12 @@ const badges = [
|
||||
/**
|
||||
* Seed badges into CouchDB
|
||||
*/
|
||||
async function seedBadgesCouchDB() {
|
||||
async function seedBadges() {
|
||||
try {
|
||||
console.log("Connecting to CouchDB...");
|
||||
|
||||
// Test connection
|
||||
await nano.info();
|
||||
console.log("Connected to CouchDB");
|
||||
|
||||
// Clear existing badges
|
||||
@@ -296,56 +288,5 @@ async function seedBadgesCouchDB() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Seed badges into MongoDB (legacy)
|
||||
*/
|
||||
async function seedBadgesMongoDB() {
|
||||
try {
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
// Connect to MongoDB
|
||||
await mongoose.connect(process.env.MONGO_URI, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
});
|
||||
|
||||
console.log("Connected to MongoDB");
|
||||
|
||||
// Clear existing badges (optional - remove if you want to preserve existing badges)
|
||||
await Badge.deleteMany({});
|
||||
console.log("Cleared existing badges");
|
||||
|
||||
// Insert new badges
|
||||
const createdBadges = await Badge.insertMany(badges);
|
||||
console.log(`Successfully seeded ${createdBadges.length} badges`);
|
||||
|
||||
// Display created badges
|
||||
createdBadges.forEach((badge) => {
|
||||
console.log(
|
||||
` ${badge.icon} ${badge.name} (${badge.rarity}) - ${badge.description}`
|
||||
);
|
||||
});
|
||||
|
||||
// Close connection
|
||||
await mongoose.connection.close();
|
||||
console.log("\nDatabase connection closed");
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error("Error seeding badges to MongoDB:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Seed badges into the database
|
||||
*/
|
||||
async function seedBadges() {
|
||||
if (useCouchDB) {
|
||||
await seedBadgesCouchDB();
|
||||
} else {
|
||||
await seedBadgesMongoDB();
|
||||
}
|
||||
}
|
||||
|
||||
// Run the seeder
|
||||
seedBadges();
|
||||
|
||||
Reference in New Issue
Block a user