Skip to content

Instantly share code, notes, and snippets.

@WangHansen
Last active December 13, 2020 01:09
Show Gist options
  • Save WangHansen/f919a968cbd0758ffe8edcd5233de6dd to your computer and use it in GitHub Desktop.
Save WangHansen/f919a968cbd0758ffe8edcd5233de6dd to your computer and use it in GitHub Desktop.
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