Created
November 30, 2022 16:13
-
-
Save cherryramatisdev/3453f44b96d0945810a45f05fee66c51 to your computer and use it in GitHub Desktop.
How to setup mongoDB using typeorm on nestjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Module, NestModule } from '@nestjs/common'; | |
import { TypeOrmModule } from '@nestjs/typeorm'; | |
@Module({ | |
imports: [ | |
TypeOrmModule.forRoot({ | |
...{ | |
type: 'mongodb', | |
url: '<your-mongo-db-atlas-url-here>', | |
synchronize: false, | |
logging: false, | |
entities: [__dirname + '../../teste/entities/*.entity{.ts,.js}'], // Insert the path for your entities | |
}, | |
name: 'mongo', // name the connection to handle multiple databases | |
}), | |
], | |
providers: [], | |
}) | |
export class DefaultModule implements NestModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller, Get } from '@nestjs/common'; | |
import { InjectRepository } from '@nestjs/typeorm'; | |
import { MongoRepository } from 'typeorm'; | |
import { TestEntity } from './entities/test.entity'; | |
@Controller('test') | |
export class TestController { | |
constructor( | |
@InjectRepository(TestEntity, 'mongo') | |
private readonly repository: MongoRepository<TestEntity>, | |
) {} | |
// This method is performing an atlas search (https://www.mongodb.com/atlas/search) as an example | |
@Get() | |
public testMethod() { | |
return this.repository.aggregate([ | |
{ | |
$search: { | |
index: 'default', | |
text: { | |
query: 'per inf', | |
path: { | |
wildcard: '*', | |
}, | |
}, | |
}, | |
}, | |
]).toArray() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Column, Entity, ObjectID, ObjectIdColumn } from 'typeorm'; | |
@Entity('test') | |
export class TestEntity { | |
@ObjectIdColumn() | |
id: ObjectID; | |
@Column() | |
searchWord: string; | |
@Column() | |
algorithmId: number; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Module } from '@nestjs/common'; | |
import { TypeOrmModule } from '@nestjs/typeorm'; | |
import { TestEntity } from './entities/test.entity'; | |
import { TestController } from './test.controller'; | |
@Module({ | |
imports: [TypeOrmModule.forFeature([TestEntity], 'mongo')], | |
controllers: [TestController], | |
}) | |
export class TestModule {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment