feat(*): Add pagination to my dex

This commit is contained in:
Josh Creek
2024-07-12 21:33:02 +01:00
parent 8d86d93bc0
commit 2cf3a03f8f
4 changed files with 124 additions and 127 deletions
@@ -0,0 +1,45 @@
import PokedexEntryModel, { type PokedexEntry } from '$lib/models/PokedexEntry';
import CatchRecordModel, { type CatchRecord } from '$lib/models/CatchRecord';
import { CombinedData } from '$lib/models/CombinedData';
class CombinedDataRepository {
async findCombinedData(page: number = 1, limit: number = 20): Promise<CombinedData[]> {
const skip = (page - 1) * limit;
const combinedData: CombinedData[] = await PokedexEntryModel.aggregate([
{
$lookup: {
from: 'catchrecords',
localField: '_id',
foreignField: 'pokedexEntryId',
as: 'catchRecord'
}
},
{
$unwind: {
path: '$catchRecord',
preserveNullAndEmptyArrays: true
}
},
{
$sort: {
'boxPlacement.box': 1,
'boxPlacement.row': 1,
'boxPlacement.column': 1
}
},
{ $skip: skip },
{ $limit: limit }
]).exec();
return combinedData.map((data) => ({
pokedexEntry: data as PokedexEntry,
catchRecord: data.catchRecord as CatchRecord
}));
}
async countCombinedData(): Promise<number> {
return PokedexEntryModel.countDocuments().exec();
}
}
export default CombinedDataRepository;