Define Mongoose models using TypeScript classes.
;;; // UserModel is a regular Mongoose Model with correct types;
A common problem when using Mongoose with TypeScript is that you have to define both the Mongoose model and the TypeScript interface. If the model changes, you also have to keep the TypeScript interface file in sync or the TypeScript interface would not represent the real data structure of the model.
Typegoose aims to solve this problem by defining only a TypeScript interface (class), which needs to be enhanced with special Typegoose decorators (like
Under the hood it uses the Reflect & reflect-metadata API to retrieve the types of the properties, so redundancy can be significantly reduced.
You can just:
- TypeScript 3.9+
- Node 10.15+
- mongoose ^5.9.14
emitDecoratorMetadatamust be enabled in
Note: it is recommended to not use babel see here why
npm i -s @typegoose/typegoose # install typegoose itselfnpm i -s mongoose # install peer-dependencie mongoosenpm i -D @types/mongoose # install all types for mongoose - this is required for typegoose to work in typescript
npm i -Dnpm test
This Project should comply with Semver. It uses the
Major.Minor.Fix standard (or in NPM terms,
Join Our Discord Server
To ask questions or just talk with us, join our Discord Server.
Migrate to 6.0.0
- Please don't add
+1or similar comments to issues. Use the reactions instead.
npm run docgenerates all documentation for all files that can be used as modules (and is used for GitHub Pages)
npm run doc:allgenerates documentation even for internal modules