我试着用ormconfig.json个这样的

{
  "name": "default",
  "type": "postgres",
  "host": "ip-is-here",
  "port": 5432,
  "username": "name",
  "password": "12345",
  "database": "db1",
  "synchronize": false,
  "logging": false,
  "entities": ["dist/storage/**/*.js"],
  "migrations": ["dist/storage/migrations/**/*.js"],
  "cli": {
    "entitiesDir": "src/storage",
    "migrationsDir": "src/storage/migrations"
  }
}

via yarn typeorm migration:run
But get an error:

Missing required argument: dataSource

我该怎么办?

推荐答案

TypeForm放弃了使用ormconfig.更晚版本的json.你应该使用新的轴.创建ormconfig.ts并为您的数据库指定选项,在我的例子中,其外观如下:

    export const connectionSource = new DataSource({
        migrationsTableName: 'migrations',
        type: 'postgres',
        host: 'localhost',
        port: 5432,
        username: 'user',
        password: 'pass',
        database: 'somehealthchecker',
        logging: false,
        synchronize: false,
        name: 'default',
        entities: ['src/**/**.entity{.ts,.js}'],
        migrations: ['src/migrations/**/*{.ts,.js}'],
        subscribers: ['src/subscriber/**/*{.ts,.js}'],
    });

运行连接后

    await connectionSource.initialize();

然后你可以通过使用

    const myRepo = connectionSource.getRepository(SomeEntity)

还有你的脚本.json应该类似于此示例

"migration:generate": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:generate -d src/modules/config/ormconfig.ts",
"migration:up": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:run -d src/modules/config/ormconfig.ts",
"migration:down": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:revert -d src/modules/config/ormconfig.ts",

在命令之后,只需在控制台中给出迁移的名称,而不使用-n选项

Node.js相关问答推荐

Azure虚拟机上的JS Express:可疑请求?

如何解决TypeError:requ.isAuthenticated不是函数错误?

Mongoose更新在^8.0.3版中,许多似乎不能按预期工作

如何呈现ejs.renderFile中包含的子模板?

如何使用NodeJS在mongodb中更新文档

Rest-Api动态图像路径和Express除非

使用pm2启动服务器

有没有办法判断 UUID 是否是使用 node.js 中的特定命名空间生成的?

为什么这个 module.export 函数如果我直接传递它就不起作用,但如果我将它包装在一个匿名函数中就可以工作

yarn 安装失败,因为 node-gyp 正在寻找过时的 node 版本标头

结合后端(Express)和前端(Angular)路由

如何删除mongodb中嵌套数组中所有出现的数组元素

Zod 模式中的self 数组

使用 .pipe(res) 向客户端发送音频不允许您搜索?

使用 Node.js 在内存中缓冲整个文件

将 myproject/.npmrc 与注册表一起使用

密码的 Node.js 散列

Node.js 17.0.1 Gatsby 错误-数字信封 routine ::不支持 ... ERR_OSSL_EVP_UNSUPPORTED

nodejs:Ajax 与 Socket.IO,优缺点

桌面应用程序仅支持 oauth_callback 值 'oob'/oauth/request_token