如何设置 typeorm .env 文件?

How to set up typeorm .env file?

我在 nestjs 启动项目中创建了一个 ormconfig.env 文件,并将 this 文档中的变量放在那里,并在此处添加了这一行

@Module({
  imports: [
    TypeOrmModule.forRoot(),
    TaskModule,
  ],
})
export class AppModule {
}`

并且控制台显示此错误:

Error: EACCES: permission denied, scandir '/Library/Application Support/Apple/AssetCache/Data' at Object.fs.readdirSync (fs.js:904:18)

我应该如何在 nestjs 中正确设置 typeorm .env 文件?

节点似乎正在尝试扫描您的完整 文件系统以查找实体文件,当然没有这样做的权限。

确保您的项目文件夹中有 TYPEORM_ENTITIES 变量的路径。

例如在项目的src文件夹下递归查找所有以.entity.ts结尾的文件:

TYPEORM_ENTITIES = src/**/**.entity.ts

我遇到了与问题中相同的问题。由于其他答案没有解决我的问题,我不得不环顾四周。我将把我的解决方案留给那些和我一样在 Webpack + TypeORM 上也有类似问题的人。

这是我需要做的才能让它发挥作用。

import { createConnection, getConnectionManager } from "typeorm";

// For hot reload to work need to require files
import { Job } from "../jobs/job.entity";
import { JobAction } from "../jobs/jobaction.entity";

export const databaseProviders = [
  {
    provide: "DATABASE_CONNECTION",
    keepConnectionAlive: true,
    useFactory: async () => {
      try {
        const conn = await createConnection({
          ...connectionOption,
          // add entitities manually
          entities: [Job, JobAction],
        });
        return conn;
      } catch (err) {
        // If AlreadyHasActiveConnectionError occurs, return already existent connection
        if (err.name === "AlreadyHasActiveConnectionError") {
          const existentConn = getConnectionManager().get("default");
          return existentConn;
        }
        throw err;
      }
    },
  },
];