Nestgram
  • ⭐About Nestgram
  • 📄Guide
  • 📰News
  • đŸ”ŧHandling updates
    • Handling commands
    • Handling text messages
    • Handling other updates
    • Entities: url, email and other
    • Other arguments in handler
    • Handling media files and download it
  • đŸĒļNestgram Features
    • Services
    • Middlewares and Params
    • Sending messages correctly
    • Answer class
    • Scopes
    • States
    • Views
  • đŸ’ŦMessages
    • Sending a photo, video and other media
    • Sending a media group
    • Send location (live) or venue
    • Send contact
    • Send poll
    • Send dice
    • Edit/delete messages
    • Copy or Forward a message
  • âŒ¨ī¸Keyboards
    • Keyboard types, building keyboard
    • Handle underTheMessage keyboard button click by Alert or Toast
    • Keyboard layouts
  • âš™ī¸Config
    • CLI
    • Webhooks and run config
    • Api class
    • Modules
      • Mongo module
    • Controller Helper class
  • 🤖API Reference
    • Set chat action
    • Save user profile photo
    • Ban, unban user or chat
    • Restrict or Promote user
    • Set chat permissions
    • Set chat admin custom title
    • Chat invite links
    • Join requests
    • Update chat info, photo, title, description and more
    • Pin or unpin messages
    • Get chat info, leave chat
    • Check user subscription
    • Chat sticker set
    • My commands
    • My default admin rights
    • Menu button
Powered by GitBook
On this page
  • Install @nestgram/mongo and mongoose
  • Connect mongoose
  • Create schema
  • Import schema
  • Get the schema in the service
  • Then
  1. Config
  2. Modules

Mongo module

You can use mongoose in your projects

Install @nestgram/mongo and mongoose

npm i @nestgram/mongo mongoose
npm i @types/mongoose --save-dev
// or
yarn add @nestgram/mongo mongoose
yarn add @types/mongoose -D

Connect mongoose

app.module.ts
import { Module } from 'nestgram';
import { UseMongoConnection } from '@nestgram/mongo';

import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  services: [AppService],
  modules: [
    UseMongoConnection('uri'), // pass uri here
  ],
})
export class AppModule {}

Create schema

user.schema.ts
import { Schema, Prop, NestSchema } from '@nestgram/mongo';
import { Document } from 'mongoose';

@Schema()
export class User {
  @Prop() email: string; // email field (string type)
  @Prop() name: string;
  @Prop() age: number; // age field (number type)
  @Prop({ type: [Object], default: [] }) articles: any[]; // params for field
}

export type UserDocument = User & Document; // export user document
NestSchema.reg(User); // reg schema

Import schema

app.module.ts
import { Module } from 'nestgram';
import { UseMongoConnection, UseMongo } from '@nestgram/mongo';

import { User } from './user.schema.ts';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  services: [AppService],
  modules: [
    UseMongoConnection('uri'), // pass uri here
    UseMongo(User), // pass schemas here that will be passed to your service
  ],
})
export class AppModule {}

Get the schema in the service

app.service.ts
import { Service } from 'nestgram';
import { Model } from 'mongoose';
import { User } from './user.schema.ts';

@Service()
export class AppService {
  constructor(private readonly User: Model<User>) {}
}

Then

You can then use the model in service methods and call it in controllers

PreviousModulesNextController Helper class

Last updated 2 years ago

âš™ī¸