mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
Merge pull request #18 from jcreek/5-set-up-repositories-for-database-access
feat(#5): Add ability to seed pokedex entry data
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,25 @@
|
||||
import PokedexEntryModel, { type PokedexEntry } from '$lib/models/PokedexEntry';
|
||||
|
||||
class PokedexEntryRepository {
|
||||
async findById(id: string): Promise<PokedexEntry | null> {
|
||||
return PokedexEntryModel.findById(id).exec();
|
||||
}
|
||||
|
||||
async findAll(): Promise<PokedexEntry[]> {
|
||||
return PokedexEntryModel.find().exec();
|
||||
}
|
||||
|
||||
async create(data: Partial<PokedexEntry>): Promise<PokedexEntry> {
|
||||
return PokedexEntryModel.create(data);
|
||||
}
|
||||
|
||||
async update(id: string, data: Partial<PokedexEntry>): Promise<PokedexEntry | null> {
|
||||
return PokedexEntryModel.findByIdAndUpdate(id, data, { new: true }).exec();
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await PokedexEntryModel.findByIdAndDelete(id).exec();
|
||||
}
|
||||
}
|
||||
|
||||
export default PokedexEntryRepository;
|
||||
+5
-8
@@ -13,18 +13,16 @@ let mongoConnectionState = 0;
|
||||
|
||||
export const dbConnect = async () => {
|
||||
if (mongoConnectionState === 1) {
|
||||
console.log('connection established');
|
||||
console.log('Connection already established');
|
||||
return;
|
||||
}
|
||||
|
||||
if (mongoose.connections.length > 0) {
|
||||
mongoConnectionState = mongoose.connections[0].readyState;
|
||||
if (mongoConnectionState === 1) {
|
||||
console.log('using existing connection');
|
||||
console.log('Using existing connection');
|
||||
return;
|
||||
}
|
||||
|
||||
await mongoose.disconnect();
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -34,11 +32,10 @@ export const dbConnect = async () => {
|
||||
await mongoose.connection.db.admin().command({ ping: 1 });
|
||||
console.log('Pinged your deployment. You successfully connected to MongoDB!');
|
||||
mongoConnectionState = 1;
|
||||
} finally {
|
||||
// Ensures that the client will close when you finish/error
|
||||
await mongoose.disconnect();
|
||||
} catch (error) {
|
||||
console.error('Error connecting to MongoDB:', error);
|
||||
mongoConnectionState = 0;
|
||||
}
|
||||
mongoConnectionState = 1;
|
||||
};
|
||||
|
||||
export const dbDisconnect = async () => {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
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 dex from '$lib/helpers/dex.json';
|
||||
|
||||
// seed pokedex data
|
||||
export const GET = async () => {
|
||||
// Immediately return if not in development to prevent seeding extra data
|
||||
return;
|
||||
|
||||
await dbConnect()
|
||||
.then(async () => {
|
||||
const repo = new PokedexEntryRepository();
|
||||
|
||||
dex.forEach(async (pokemon) => {
|
||||
const pokemonModel = new PokedexEntryModel({
|
||||
order: pokemon.dexOrder,
|
||||
boxPlacement: {
|
||||
box: pokemon.boxNumber,
|
||||
row: pokemon.rowNumber,
|
||||
column: pokemon.columnNumber
|
||||
},
|
||||
pokedexNumber: pokemon.pokedexNumber,
|
||||
pokemon: pokemon.name,
|
||||
form: pokemon.form,
|
||||
generation: pokemon.generation,
|
||||
originGame: pokemon.originGame,
|
||||
alternativeOrigin: pokemon.altOriginGame,
|
||||
howToObtain: pokemon.notesToObtain
|
||||
});
|
||||
|
||||
await repo.create(pokemonModel);
|
||||
console.log(`Seeded ${pokemon.dexOrder}`);
|
||||
});
|
||||
})
|
||||
.finally(() => dbDisconnect());
|
||||
|
||||
return json({
|
||||
message: 'seeded'
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user