You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's say I have the typescript models as follows:
interface LibraryBook {
isDigital: false;
name: string
}
interface AmazonBook {
name: string;
isDigital: true
}
interface Author {
name: string;
book: Book
}
type Book = AmazonBook | LibraryBook;
How can define Author schema and subsequently Book schema I add a discriminator to the book property of Author model leveraging isDigital property?
Based on my understanding w.r.t documentation @ https://mongoosejs.com/docs/discriminators.html the only way I can think of is by defining a schema matching the following Author definition
interface Author {
name: string;
book: {
isDigital: boolean;
bookData: Book
}
}
// Mongoose Schema
const bookSchema = new Schema({}, { discriminatorKey: "isDigital" });
// Casting as mongoose.Schema.Types.DocumentArray as the discriminator is not availabel int typescipt definitions
(bookSchema.path('bookData') as mongoose.Schema.Types.DocumentArray).discriminator("YES", <schema of amazon book>)
(bookSchema.path('bookData') as mongoose.Schema.Types.DocumentArray).discriminator("NO", <schema of library book>)
const authorSchema = new Schema<Author & Document>({
name: String,
book: bookSchema,
});
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Let's say I have the typescript models as follows:
How can define
Author
schema and subsequentlyBook
schema I add a discriminator to thebook
property ofAuthor
model leveragingisDigital
property?Based on my understanding w.r.t documentation @ https://mongoosejs.com/docs/discriminators.html the only way I can think of is by defining a schema matching the following
Author
definitionBeta Was this translation helpful? Give feedback.
All reactions