Last active
May 19, 2018 16:28
-
-
Save rubycut/b3842a0f5627b1a9588625c287c58932 to your computer and use it in GitHub Desktop.
This file contains hidden or 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, Model, model, Types } from "mongoose" | |
// We are definining typescript basic typescript interface first, properties only | |
export interface IHouse { | |
id?: any | |
name: string | |
rooms: Types.DocumentArray<IRoomDocument> | |
owners: Types.DocumentArray<IOwnerDocument> | |
} | |
// We are definiting document which will have all mongoose document methods mixed in, plus our customer Document methods | |
export interface IHouseDocument extends IHouse, Document { | |
currentValue(): Promise<string[]> | |
} | |
// here we are defining interface for model, and our example find function | |
export interface IHouseModel extends Model<IHouseDocument> { | |
findInCity( | |
city: string, | |
street: string, | |
zipCode: string | |
): IHouseDocument | |
} | |
// we do our standard mongoose schema definition here | |
export const houseSchema: Schema = new Schema({ | |
name: String, | |
}) | |
// defining function which we described in IHouseDocument | |
houseSchema.methods.currentValue = async function(): Promise<string[]> { | |
// do something ... | |
} | |
// defining function which we described in IHouseModel | |
houseSchema.statics.findInCity = async ( | |
city: string, | |
street: string, | |
zipCode: string | |
) => { | |
// do something ... | |
} | |
export const House = model<IHouseDocument, IHouseModel>("House", houseSchema) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment