Skip to content

Instantly share code, notes, and snippets.

@rubycut
Last active May 19, 2018 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubycut/b3842a0f5627b1a9588625c287c58932 to your computer and use it in GitHub Desktop.
Save rubycut/b3842a0f5627b1a9588625c287c58932 to your computer and use it in GitHub Desktop.
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