How to set up Typegoose
If you want to programm an API under the following conditions, you can use the provided minimal example below:
Conditions: NodeJSTypescriptMongoDB (locally)
Example:
If you are working with typescript, a project structure, similar to this is recommended:
├── dist| ├── (your compiled JS)├── src| ├── models| | ├── user.model.ts| ├── user-repository.ts| ├── app.ts| ├── index.ts├── test| ├── (your tests)
Furthermore, it's required to install the following packages
npm install --save typescript mongoose express @types/express @types/mongoose typegoose
index.ts:
This file, I just have for bootstraping purposes
import app from './app';const port = 3000;app.listen(port, (err) => { if (err) { return console.log(err); } return console.log('Server up and running on ' + port);});
app.ts:
Here, the actual logic is going on
import { UserRepository } from './user-repository';import * as express from 'express';export class App { public app; constructor() { this.app = express(); this.config(); this.mountRoutes(); } private mountRoutes() { // Definition of the possible API routes this.app.route('/users') .get((req, res) => { var repo = new UserRepository(); // we catch the result with the typical "then" repo.getUsers().then((x) => { // .json(x) instead of .send(x) should also be okay res.status(200).send(x); }); }); // here with parameter // | // v this.app.route('/users/:id') .get((req, res) => { var repo = new UserRepository(); repo.getUser(req.params.id).then((x) => { res.status(200).send(x); }); }); }}export default new App().app;
user-repository.ts
import * as mongoose from 'mongoose';import { User, UserModel } from './models/user.model';export class UserRepository { constructor() { // protocol host port database // | | | | // v v v v mongoose.connect('mongodb://localhost:27017/mongotest'); // this only works, if your mongodb has no auth configured // if you need authentication, use the following: // mongoose.connect(MONGO_URL, { // auth: { // user: MONGO_DB_USER, // password: MONGO_DB_PASSWORD // }, // }) } async getUser(id: number): Promise<User> { // json query object as usual // | // v return UserModel.findOne({"userId": id}); } async getUsers(): Promise<User[]> { return UserModel.find({}); }}
user.model.ts
import * as mongoose from 'mongoose';import { prop, Typegoose } from 'typegoose';export class User extends Typegoose{ // properties // | // v @prop() userId: number; @prop() firstname?: string; @prop() lastname: string; @prop() email: string;}export const UserModel = new User().getModelForClass(User, { existingMongoose: mongoose, // had many problems without that definition of the collection name // so better define it // | // v schemaOptions: {collection: 'users'}})
As obvious, I had a mongodb running locally, having a database called mongotest and inside that a collection called users.