mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
feat(*): Complete MongoDB to Supabase migration cleanup
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
import mongoose, { Schema, Document } from 'mongoose';
|
||||
|
||||
export interface PokedexMetadata extends Document {
|
||||
lastModified: Date;
|
||||
}
|
||||
|
||||
const pokedexMetadataSchema = new Schema<PokedexMetadata>({
|
||||
lastModified: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
}
|
||||
});
|
||||
|
||||
export default mongoose.model<PokedexMetadata>('PokedexMetadata', pokedexMetadataSchema);
|
||||
@@ -1,13 +1,13 @@
|
||||
import mongoose, { Schema, Document } from 'mongoose';
|
||||
|
||||
export interface RegionGameMapping extends Document {
|
||||
// Supabase-based RegionGameMapping interface
|
||||
export interface RegionGameMapping {
|
||||
id?: number;
|
||||
region: string;
|
||||
games: string[];
|
||||
game: string; // Note: Different from MongoDB version which had 'games' array
|
||||
}
|
||||
|
||||
const regionGameMappingSchema = new Schema<RegionGameMapping>({
|
||||
region: String,
|
||||
games: Array<string>
|
||||
});
|
||||
|
||||
export default mongoose.model<RegionGameMapping>('RegionGameMapping', regionGameMappingSchema);
|
||||
// Database record type for Supabase
|
||||
export interface RegionGameMappingDB {
|
||||
id: number;
|
||||
region: string;
|
||||
game: string;
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import mongoose, { Schema, Document } from 'mongoose';
|
||||
|
||||
export interface User extends Document {
|
||||
username: string;
|
||||
// Add other user-related fields as needed
|
||||
}
|
||||
|
||||
const userSchema = new Schema<User>({
|
||||
username: String
|
||||
// Add other user-related fields as needed
|
||||
});
|
||||
|
||||
export default mongoose.model<User>('User', userSchema);
|
||||
@@ -1,19 +0,0 @@
|
||||
import mongoose, { Schema, Document } from 'mongoose';
|
||||
|
||||
export interface UserCatchEntry extends Document {
|
||||
userId: mongoose.Types.ObjectId; // Reference to the user
|
||||
pokedexEntryId: mongoose.Types.ObjectId; // Reference to the pokedex entry
|
||||
haveToEvolve: boolean;
|
||||
caught: boolean;
|
||||
inHome: boolean;
|
||||
}
|
||||
|
||||
const userCatchEntrySchema = new Schema<UserCatchEntry>({
|
||||
userId: { type: mongoose.Types.ObjectId, ref: 'User' },
|
||||
pokedexEntryId: { type: mongoose.Types.ObjectId, ref: 'PokedexEntry' },
|
||||
haveToEvolve: Boolean,
|
||||
caught: Boolean,
|
||||
inHome: Boolean
|
||||
});
|
||||
|
||||
export default mongoose.model<UserCatchEntry>('UserCatchEntry', userCatchEntrySchema);
|
||||
@@ -1,48 +0,0 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { getEnv } from '$lib/utils/env';
|
||||
const env = getEnv();
|
||||
|
||||
/*
|
||||
0 - disconnected
|
||||
1 - connected
|
||||
2 - connecting
|
||||
3 - disconnecting
|
||||
4 - uninitialized
|
||||
*/
|
||||
let mongoConnectionState = 0;
|
||||
|
||||
export const dbConnect = async () => {
|
||||
if (mongoConnectionState === 1) {
|
||||
console.log('Connection already established');
|
||||
return;
|
||||
}
|
||||
|
||||
if (mongoose.connections.length > 0) {
|
||||
mongoConnectionState = mongoose.connections[0].readyState;
|
||||
if (mongoConnectionState === 1) {
|
||||
console.log('Using existing connection');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await mongoose.connect(env.MONGO_URL, {
|
||||
serverApi: { version: '1', strict: true, deprecationErrors: true }
|
||||
});
|
||||
await mongoose.connection.db.admin().command({ ping: 1 });
|
||||
console.log('Pinged your deployment. You successfully connected to MongoDB!');
|
||||
mongoConnectionState = 1;
|
||||
} catch (error) {
|
||||
console.error('Error connecting to MongoDB:', error);
|
||||
mongoConnectionState = 0;
|
||||
}
|
||||
};
|
||||
|
||||
export const dbDisconnect = async () => {
|
||||
if (process.env.NODE_ENV === 'development') return;
|
||||
if (mongoConnectionState === 0) return;
|
||||
|
||||
await mongoose.disconnect();
|
||||
mongoConnectionState = 0;
|
||||
console.log('disconnected from mongodb');
|
||||
};
|
||||
@@ -1,7 +1,3 @@
|
||||
export function getEnv() {
|
||||
return process.env.hasOwnProperty('MONGO_URL')
|
||||
? process.env
|
||||
: {
|
||||
MONGO_URL: import.meta.env.VITE_MONGO_URL
|
||||
};
|
||||
return process.env;
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { dbConnect, dbDisconnect } from '$lib/utils/db';
|
||||
import PokedexMetadataModel from '$lib/models/PokedexMetadata';
|
||||
|
||||
export const GET = async () => {
|
||||
await dbConnect();
|
||||
|
||||
const metadata = await PokedexMetadataModel.findOne();
|
||||
await dbDisconnect();
|
||||
|
||||
if (metadata) {
|
||||
return json({ lastModified: metadata.lastModified });
|
||||
} else {
|
||||
return json({ message: 'Metadata not found' }, { status: 404 });
|
||||
}
|
||||
};
|
||||
@@ -1,49 +0,0 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { dbConnect, dbDisconnect } from '$lib/utils/db';
|
||||
import PokedexEntryModel from '$lib/models/PokedexEntry';
|
||||
import PokedexEntryRepository from '$lib/repositories/PokedexEntryRepository';
|
||||
import RegionGameMapping from '$lib/models/RegionGameMapping';
|
||||
import RegionGameMappingRepository from '$lib/repositories/RegionGameMappingRepository';
|
||||
import PokedexMetadataModel from '$lib/models/PokedexMetadata';
|
||||
import dex from '$lib/helpers/pokedex.json';
|
||||
import RegionGameMappingJson from '$lib/helpers/region-game-mapping.json';
|
||||
|
||||
// Function to update the lastModified field in the metadata collection
|
||||
const updateLastModified = async () => {
|
||||
const metadata = await PokedexMetadataModel.findOne();
|
||||
if (metadata) {
|
||||
metadata.lastModified = new Date();
|
||||
await metadata.save();
|
||||
} else {
|
||||
await PokedexMetadataModel.create({ lastModified: new Date() });
|
||||
}
|
||||
};
|
||||
|
||||
// Seed Pokedex data
|
||||
export const GET = async () => {
|
||||
// Immediately return if not in development to prevent seeding extra data
|
||||
return;
|
||||
|
||||
await dbConnect()
|
||||
.then(async () => {
|
||||
// Create pokedex entries
|
||||
const repo = new PokedexEntryRepository();
|
||||
|
||||
dex.forEach(async (pokemon: PokedexEntryModel) => {
|
||||
await repo.create(pokemon);
|
||||
console.log(`Seeded ${pokemon.pokedexNumber} ${pokemon.pokemon} ${pokemon.form}`);
|
||||
});
|
||||
|
||||
// Create region-game mappings
|
||||
const regionGameMappingRepository = new RegionGameMappingRepository();
|
||||
await regionGameMappingRepository.create(RegionGameMappingJson as RegionGameMapping[]);
|
||||
|
||||
// Update the lastModified field after seeding
|
||||
await updateLastModified();
|
||||
})
|
||||
.finally(() => dbDisconnect());
|
||||
|
||||
return json({
|
||||
message: 'seeded'
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user