A simple typegoose integration for Atlas Framework
npm install --save robdeflop/atlas-typegoose-plugin
OR
yarn add robdeflop/atlas-typegoose-plugin
coming soon
In order to use the database connection you need to add the following environmental variables in your .env:
MONGODB_USER=YOUR_USERNAME
MONGODB_SCHEMA=YOUR_SCHEMA
MONGODB_PASSWORD=YOUR_PASSWORD
MONGODB_PORT=27017
MONGODB_HOST=YOUR_HOST_ADDRESS
MONGODB_CONNECTION_STRING=OR_USE_THE_CONNECTION_STRINGIf the connection credentials are set correctly, the server will automatically establish the connection to your MongoDB on server start.
I won't give any in-depth usage guide about typegoose. For more information checkout the official documentations of typegoose and mongoose.
Typegoose works like a wrapper around mongoose. Therefore, you can declare your models as classes.
export class User {
@prop()
firstName: string;
@prop()
lastName: string
}You can save an object of the class above with:
const userObj = getModelForClass(User);
const userDoc = new userObj({
firstName: 'Foo',
lastName: 'Bar'
});
userDoc.save().then((document) => {
//handle the success
}).catch((err) => {
//handle the error
})
userObj.findOne()