Last active
December 13, 2020 01:09
-
-
Save WangHansen/f919a968cbd0758ffe8edcd5233de6dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Document, Schema } from "mongoose" | |
// Subdocument schema | |
const UserAddressSchema = Schema( | |
{ | |
street: String, | |
zipcode: { | |
type: String, | |
required: true, | |
} | |
}, | |
{ _id: false } | |
) | |
// Top-level schema with subdocument embedded | |
const UserSchema = Schema({ | |
... | |
address: UserAddressSchema, | |
... | |
}) | |
/** Types **/ | |
// Interface for subdocument | |
// Notice how this interface doesn't inherent from Document | |
export interface UserAddress { | |
street?: string; | |
zipcode: string; | |
} | |
// Interface for document | |
interface User { | |
... | |
address: UserAddress; | |
... | |
} | |
export interface UserBaseDocument extends User, Document {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment