• Stars
    star
    279
  • Rank 147,967 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Typegoose with NestJS

nestjs-typegoose

NPM

npm version Build Status Coverage Status npm npm bundle size David

Description

Injects typegoose models for nest components and controllers. Typegoose equivalant for @nestjs/mongoose.

Using Typegoose removes the need for having a Model interface.

Installation

npm install --save nestjs-typegoose

or

yarn add nestjs-typegoose

Documentation

Here is the full documentation describing all basic and advanced features.

Basic usage

You can checkout the example project for more details.

app.module.ts

import { Module } from "@nestjs/common";
import { TypegooseModule } from "nestjs-typegoose";
import { CatsModule } from "./cat.module.ts";

@Module({
  imports: [
    TypegooseModule.forRoot("mongodb://localhost:27017/nest", {
      useNewUrlParser: true,
    }),
    CatsModule,
  ],
})
export class ApplicationModule {}

Create class that describes your schema

cat.model.ts

import { prop } from "@typegoose/typegoose";
import { IsString } from "class-validator";

export class Cat {
  @IsString()
  @prop({ required: true })
  name: string;
}

Inject Cat for CatsModule

cat.module.ts

import { Module } from "@nestjs/common";
import { TypegooseModule } from "nestjs-typegoose";
import { Cat } from "./cat.model";
import { CatsController } from "./cats.controller";
import { CatsService } from "./cats.service";

@Module({
  imports: [TypegooseModule.forFeature([Cat])],
  controllers: [CatsController],
  providers: [CatsService],
})
export class CatsModule {}

Get the cat model in a service

cats.service.ts

import { Injectable } from "@nestjs/common";
import { InjectModel } from "nestjs-typegoose";
import { Cat } from "./cat.model";
import { ReturnModelType } from "@typegoose/typegoose";

@Injectable()
export class CatsService {
  constructor(
    @InjectModel(Cat) private readonly catModel: ReturnModelType<typeof Cat>
  ) {}

  async create(createCatDto: { name: string }): Promise<Cat> {
    const createdCat = new this.catModel(createCatDto);
    return await createdCat.save();
  }

  async findAll(): Promise<Cat[] | null> {
    return await this.catModel.find().exec();
  }
}

Finally, use the service in a controller!

cats.controller.ts

import { Controller, Get, Post, Body } from "@nestjs/common";
import { CatsService } from "./cats.service";
import { Cat } from "./cats.model.ts";

@Controller("cats")
export class CatsController {
  constructor(private readonly catsService: CatsService) {}

  @Get()
  async getCats(): Promise<Cat[] | null> {
    return await this.catsService.findAll();
  }

  @Post()
  async create(@Body() cat: Cat): Promise<Cat> {
    return await this.catsService.create(cat);
  }
}

Requirements

  1. @typegoose/typegoose +6.1.5
  2. @nestjs/common +6.10.1
  3. @nestjs/core +6.10.1
  4. mongoose (with typings @types/mongoose) +5.7.12

License

nestjs-typegoose is MIT licensed.

More Repositories

1

awesomewm-dotfiles

Dotfiles for awesomewm.
Lua
8
star
2

nextjs-mdx-blog-example

A simple repo using mdx as a blog WITH OPTIMIZED IMAGES πŸŒ†.
TypeScript
3
star
3

home-cluster

My home Kubernetes (k3s) cluster managed by GitOps (Flux)
Shell
2
star
4

nextjs-portfolio

The OLD code for my portfolio website.
TypeScript
2
star
5

csvtasks

πŸ“šπŸ“Š Sync a csv file with google tasks (I use this for homework).
TypeScript
2
star
6

kdot

A rust dot file manager.
Rust
2
star
7

pforwords

The code for my sister's blog (moved from Wordpress to Gatsby).
TypeScript
2
star
8

cu-api

An API for CU Boulder students.
TypeScript
2
star
9

raspi-streamer

A makeshift raspberry pi web server streamer.
TypeScript
1
star
10

javascript-interpreter-rust

Long term project to create an interpreter for javascript in rust.
Rust
1
star
11

template-typescript-mono-repo

βš™οΈ A template for building typescript mono repos.
JavaScript
1
star
12

cu-cli

A cli for CU Boulder students to sync classes to google calendar, check their courses and see their gpa.
TypeScript
1
star
13

dotfiles-bloated

My arch linux dot files.
Shell
1
star
14

journal-site

TypeScript
1
star
15

mdx-yaml-full

A gatsby plugin to convert !mdx yaml tags to mdx.
TypeScript
1
star
16

resume-generator

πŸ“ Some code to generate resume pdfs on the web or node.
TypeScript
1
star
17

nestjs-test

TypeScript
1
star
18

docx-react

βš› πŸ—ƒ A JSX wrapper around `docx` for creating docx files.
TypeScript
1
star
19

rust-embedded-planter

Rust
1
star
20

cpp-learning

I need to learn C++.
C++
1
star
21

todolist-backend

TypeScript
1
star
22

cu

TypeScript
1
star
23

dev-landing-react

A landing page in reactjs.
TypeScript
1
star
24

mini-gpt

https://www.youtube.com/watch?v=kCc8FmEb1nY&t=2706s
Jupyter Notebook
1
star
25

photo-mover

A rust cli tool to sort files based on EXIF metadata.
Rust
1
star
26

test-semvar

A testing repo for semvar with existing projects.
JavaScript
1
star
27

raspi-streamer-consumer

JavaScript
1
star
28

nand2tetris

This is a project to learn about computer systems from https://www.nand2tetris.org/
Assembly
1
star
29

pascal-interpreter

pascal interpreter written in c++.
C++
1
star
30

portfolio-old

CSS
1
star
31

neural-network

πŸ”’ 🧐 🧠 This is my first neural network for recognizing MNIST digits.
Python
1
star
32

movie-chooser

TypeScript
1
star
33

awesome-docs-to-typescript

A project to convert awesomewm's docs (in lua) to TypeScript typings for use in TypeScript2Lua.
TypeScript
1
star
34

llvm-python-project

A project to convert a subset of python to llvm IR.
Python
1
star
35

dotfiles

Another repo for my dotfiles...
Shell
1
star
36

rust-embedded-playground

A repo to learn embedded rust on the stm32f3 discovery board
Rust
1
star
37

lora-radio

Lora radio communication with Raspberry Pi and adafruit Clue board.
Rust
1
star
38

movie-chooser-backend

JavaScript
1
star
39

jx-terraform-gke

HCL
1
star