AletheiaFact / AletheiaFact/aletheia

Refactor Badge Queries and Modify Schema for Mongoose `find` Usage

Ouverte
#1,111 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
tech
Langage dominant
TypeScript
Étoiles
55
Forks
20
Merge moyen
2 j 6 h
PR mergées (30 j)
2

Description

## Background Information:

Currently, MongoDB aggregations are used to fetch data from the `BadgeModel` collection, and we want to transition to Mongoose find queries. The goal is to replace these aggregations with Mongoose `find` queries for improved readability and maintainability. Additionally, to enable the use of `find`, a `users` field needs to be added to the `Badge` schema, as the `User` schema already references badges, but the Badge schema lacks this reference.

## What:
Modify the Badge schema to include a `users` field and refactor the badge-related aggregation to use Mongoose `find` queries.

## How:

**Modify `Badge` Schema:**
- Add a `users` field to the Badge schema to establish a reference to the User schema.

```typescript
@Schema()
export class Badge {
// Existing properties...

@Prop({
type: [
{
type: mongoose.Types.ObjectId,
ref: "User",
required: false,
},
],
})
users: User[];
}
```

**Refactor Aggregation to Mongoose `find` Queries:**
- Rewrite the existing aggregation logic using Mongoose's find and populate methods.

```typescript
async listAll() {
return this.BadgeModel.find()
.populate({
path: 'users',
model: 'User',
select: '_id',
})
}
```

## Acceptance Criteria:

- [ ] Code Functionality: Verify that the modified Badge schema successfully establishes a reference to the User schema.
- [ ] Code Readability: Ensure that the refactored code is readable and adheres to best practices for Mongoose queries.
- [ ] Schema Modification: Confirm that the Badge schema includes the users field as described in the modification.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.