NestJS TypeORM 项目教程

NestJS TypeORM 项目教程

nestjs-typeormNestJS, TypeORM and PostgreSQL — full example development and project setup working with database migrations.项目地址:https://gitcode.com/gh_mirrors/ne/nestjs-typeorm

1. 项目的目录结构及介绍

nestjs-typeorm/
├── src/
│   ├── app.module.ts
│   ├── main.ts
│   ├── config/
│   │   ├── ormconfig.ts
│   ├── entities/
│   │   ├── user.entity.ts
│   ├── repositories/
│   │   ├── user.repository.ts
├── package.json
├── tsconfig.json

目录结构介绍

  • src/:项目源代码目录。
    • app.module.ts:应用程序的根模块。
    • main.ts:应用程序的入口文件。
    • config/:配置文件目录。
      • ormconfig.ts:TypeORM 配置文件。
    • entities/:数据库实体目录。
      • user.entity.ts:用户实体文件。
    • repositories/:数据仓库目录。
      • user.repository.ts:用户数据仓库文件。
  • package.json:项目依赖和脚本配置文件。
  • tsconfig.json:TypeScript 编译配置文件。

2. 项目的启动文件介绍

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

启动文件介绍

  • main.ts 是 NestJS 应用程序的入口文件。
  • 使用 NestFactory.create 方法创建应用程序实例。
  • 通过 app.listen 方法启动 HTTP 服务器,监听端口 3000。

3. 项目的配置文件介绍

ormconfig.ts

import { DataSource } from 'typeorm';

export const dataSource = new DataSource({
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'test',
  password: 'test',
  database: 'test',
  entities: [__dirname + '/../**/*.entity{.ts,.js}'],
  synchronize: true,
});

配置文件介绍

  • ormconfig.ts 是 TypeORM 的数据源配置文件。
  • 配置数据库连接参数,如数据库类型、主机、端口、用户名、密码和数据库名称。
  • entities 指定实体文件的路径。
  • synchronize 设置为 true 时,TypeORM 会在每次应用程序启动时自动同步数据库结构。

以上是 NestJS TypeORM 项目的目录结构、启动文件和配置文件的介绍。希望这些内容能帮助你更好地理解和使用该项目。

nestjs-typeormNestJS, TypeORM and PostgreSQL — full example development and project setup working with database migrations.项目地址:https://gitcode.com/gh_mirrors/ne/nestjs-typeorm

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
NestJS 中使用 TypeORM 连接达梦(DM)数据库,你需要安装必要的依赖并配置TypeORM连接。以下是大致步骤: 1. **安装依赖**: - 首先,确保已经安装了`nestjs`、`typeorm`和对应的`dm2-driver-typeorm`模块。可以使用npm或yarn来安装: ``` npm install @nestjs/typeorm dm2-driver-typeorm ``` 2. **配置项添加**: 在`package.json`文件中添加`typeorm`作为`dependencies`下的一项,并在`nestjs.config.ts`或单独的`ormconfig.js`文件中添加达梦数据库的相关配置。例子如下: ```javascript // ormconfig.js 或 nestjs.config.ts { type: 'DM', host: 'your_host', port: your_port, username: 'your_username', password: 'your_password', database: 'your_database_name', entities: ['src/**/*.entity.ts'], // 定义实体类的路径 synchronize: true, // 是否自动同步表结构,默认为true logging: false, // 关闭TypeORM的日志输出,如果需要可以开启 } ``` 3. **连接到模块**: 在你的 NestJS 应用的服务或模块中,注入`TypeOrmModule`来创建TypeORM的连接池: ```typescript import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { YourEntity } from './entities/your-entity.entity'; // 替换为你的实体名 @Module({ imports: [TypeOrmModule.forRootAsync({ useFactory: () => ({ ...getDmDatabaseConfig(), // 使用上述配置工厂函数 }), })], providers: [YourService], // 如果有服务引用TypeORM }) export class AppModule {} ``` 4. **使用连接**: 现在可以在服务中注入`EntityManager`或`Repository`来操作数据库: ```typescript import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { YourEntity } from './entities/your-entity.entity'; @Injectable() export class YourService { constructor( @InjectRepository(YourEntity) private readonly yourRepository: Repository<YourEntity>, ) {} async findAll() { return await this.yourRepository.find(); } } ``` 完成以上步骤后,你的 NestJS 应用就可以通过TypeORM连接达梦数据库进行操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

滕娴殉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值