TypeScript Mongoose Metadata
Using awesome-metadata (a.k.a atm) to emit additional metadata for all classes, this project converts these metadata into a Mongoose Schema.
Goals
The goal of this project is to offer an automatic way to map classes to mongo database by using mongoose and metadata from classes.
The actual TypeScript metadata emitted is not powerful enough to do it, so this project depends on atm.
Features
- Class body to mongoose schema.
- Every member is mapped into the mongoose schema.
- Non optional members are marked as required.
- @virtual decorator.
- If a member is @virtual it won't be mapped into the Schema.
- Decorators for mongoose hooks (pre/post save, pre/post load...).
- pre/post hooks for save, init, validate, and remove
- pre/post create hook, triggered before/after doing
new Model({...})
.
- Interface body to mongoose schema.
- Not supported because atm does not support interfaces yet.
Example of usage
Let say we want to implement a basic user with login and password fields.
Also, the user will have a logger created after the user is loaded or created.
;;; /** * User roles */ /** * User definition as a class instead of as a Schema. */ // Important to include the metadata generated from the awesome-metadata emitter after // the definition of the class, and before generating the model.; ; ;
Metadata emitted
This project emits two metadatas:
mongoose-metadata:virtuals
: The list of@virtual
members of a class.mongoose-metadata:model
: The model generated by usingclassToModel
function.