import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { User } from './user.entity'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'sqlite', database: ':memory:', entities: [User], synchronize: true, }), ], }) export class AppModule {}
The TypeOrmModule.forRoot configures a SQLite in-memory database with synchronize: true. This means the database schema is auto-created on app start. The ':memory:' path tells SQLite to use RAM, not disk.
import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { User } from './user.entity'; import { Post } from './post.entity'; @Module({ imports: [ TypeOrmModule.forFeature(_____) ], }) export class AppModule {}
The forFeature method expects an array of entity classes. The correct syntax is [User, Post]. Curly braces or semicolons are invalid here.
import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { User } from './user.entity'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'postgres', host: 'localhost', port: 5432, username: 'user', password: 'pass', database: 'testdb', entities: [User], synchronize: true, }), ], }) export class AppModule {}
The configuration is valid syntax-wise. However, if the PostgreSQL server is down or credentials are incorrect, the app will throw a connection error at runtime.
connection.isConnected after app initialization?import { Injectable } from '@nestjs/common'; import { DataSource } from 'typeorm'; @Injectable() export class DatabaseService { constructor(private dataSource: DataSource) {} async checkConnection() { return this.dataSource.isInitialized && this.dataSource.isConnected; } } // AppModule setup import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { DatabaseService } from './database.service'; @Module({ imports: [ TypeOrmModule.forRoot({ type: 'sqlite', database: ':memory:', entities: [], synchronize: true, }), ], providers: [DatabaseService], }) export class AppModule {}
When TypeORM finishes initializing the DataSource, isInitialized and isConnected are both true. Since the app uses an in-memory SQLite with synchronize, connection is established immediately.
forRoot() sets up the main database connection and should be called once. forFeature() registers entities for injection in feature modules. Synchronize can be true in development but should be false in production.