rsnext/examples/with-mongodb-mongoose/models/Pet.ts
Elijah Ohiwerei 7e76c353b3
feat(examples): Migrate 'with-mongoose' example to TypeScript with total type safety (#53603)
### What?
I kindly request the maintainers to review this Pull Request, which aims to migrate the `with-mongoose` example to TypeScript with total type safety.

### Why?
By doing so, we enhance the overall quality and maintainability of the example, aligning it with modern best practices for type-safe codebases.

### How?
I have thoroughly tested the changes to ensure they do not introduce regressions and maintain compatibility with the existing codebase.



Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
2023-09-01 14:34:13 +00:00

71 lines
1.6 KiB
TypeScript

import mongoose from 'mongoose'
export interface Pets extends mongoose.Document {
name: string
owner_name: string
species: string
age: number
poddy_trained: boolean
diet: string[]
image_url: string
likes: string[]
dislikes: string[]
}
/* PetSchema will correspond to a collection in your MongoDB database. */
const PetSchema = new mongoose.Schema<Pets>({
name: {
/* The name of this pet */
type: String,
required: [true, 'Please provide a name for this pet.'],
maxlength: [60, 'Name cannot be more than 60 characters'],
},
owner_name: {
/* The owner of this pet */
type: String,
required: [true, "Please provide the pet owner's name"],
maxlength: [60, "Owner's Name cannot be more than 60 characters"],
},
species: {
/* The species of your pet */
type: String,
required: [true, 'Please specify the species of your pet.'],
maxlength: [40, 'Species specified cannot be more than 40 characters'],
},
age: {
/* Pet's age, if applicable */
type: Number,
},
poddy_trained: {
/* Boolean poddy_trained value, if applicable */
type: Boolean,
},
diet: {
/* List of dietary needs, if applicable */
type: [String],
},
image_url: {
/* Url to pet image */
required: [true, 'Please provide an image url for this pet.'],
type: String,
},
likes: {
/* List of things your pet likes to do */
type: [String],
},
dislikes: {
/* List of things your pet does not like to do */
type: [String],
},
})
export default mongoose.models.Pet || mongoose.model<Pets>('Pet', PetSchema)