Ste*_*olz 16 migration typeorm
当尝试自动生成迁移时,我收到以下错误。
文件必须包含 TypeScript / JavaScript 代码并导出 DataSource 实例
这是我正在运行的命令:
typeorm migration:generate projects/core/migrations/user -d db_config.ts -o
Run Code Online (Sandbox Code Playgroud)
我的 db_config.ts 文件如下所示:
import { DataSource } from "typeorm";
const AppDataSource = new DataSource({
type: "postgres",
host: process.env.PGHOST,
port: 5432,
username: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE,
entities: ["./projects/**/entities/*.ts"],
migrations: ["./projects/**/migrations/**.js"],
synchronize: true,
logging: false,
});
export default AppDataSource
Run Code Online (Sandbox Code Playgroud)
我当前的文件结构如下所示:
我的 index.ts 文件如下所示:
import express from "express";
import { AppDataSource } from "./data-source";
import budget_app from "./projects/budget_app/routes";
export const app = express();
const port = 3000;
AppDataSource.initialize()
.then(() => {
console.log("Data Source has been initialized!");
})
.catch((err) => {
console.error("Error during Data Source initialization", err);
});
// export default AppDataSource;
app.get("/", (req, res) => {
res.send("Hello World!!!!");
});
app.use("/budget_app", budget_app);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
Run Code Online (Sandbox Code Playgroud)
我还在 Docker 容器中与我的 postgres 数据库一起运行它。我已经确认连接有效,因为如果我执行synchronize=true,它将很好地创建表。我只是无法创建迁移。
所以我很困惑,不知道从这里到哪里去解决这个问题。提前感谢您的帮助!
小智 13
我在migrationsin方面遇到了麻烦typeorm,最终找到了一个可以持续工作的解决方案。
对我来说,构建然后使用js datasource是行不通的,所以我为那些在钢铁方面遇到困难的人提供了我的解决方案typeorm-migrations。
这是我的逐步解决方案:
datasource在类似datasource.config.ts.
import * as mysqlDriver from 'mysql2';
import {DataSourceOptions} from 'typeorm';
import dotenv from 'dotenv';
dotenv.config();
export function getConfig() {
return {
driver: mysqlDriver,
type: 'mysql',
host: process.env.MYSQL_HOST,
port: parseInt(process.env.MYSQL_PORT, 10),
username: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DB,
synchronize: false,
migrations: [__dirname + '/../../typeorm-migrations/*.{ts,js}'],
entities: [__dirname + '/../**/entity/*.{ts,js}'],
} as DataSourceOptions;
}
Run Code Online (Sandbox Code Playgroud)
创建一个名称类似于migration.config.ts.
const datasource = new DataSource(getConfig()); // config is one that is defined in datasource.config.ts file
datasource.initialize();
export default datasource;
Run Code Online (Sandbox Code Playgroud)
现在您可以在文件中定义迁移命令package.json。
"migration:up": "ts-node -r tsconfig-paths/register --project ./tsconfig.json ./node_modules/.bin/typeorm migration:run -d config/migration.config.ts",
"migration:down": "ts-node -r tsconfig-paths/register --project ./tsconfig.json ./node_modules/.bin/typeorm migration:revert -d config/migration.config.ts"
Run Code Online (Sandbox Code Playgroud)
通过运行,yarn run migration:up您将能够在typeorm-migrations文件夹中运行您定义的迁移。
我在使用文件时遇到了同样的问题.env(如果您没有 .env 文件,这个答案可能与您无关)。
看起来,CLI 不会从 dotenv 中选择环境变量,因此您必须自己加载它们。例如,使用dotenv库,将其放在数据源文件的顶部:
import * as dotenv from 'dotenv';
dotenv.config();
// export const AppDataSource = new DataSource()...
Run Code Online (Sandbox Code Playgroud)
或者,在运行脚本时提供真实的环境变量:
PGHOST=... PGUSER=... PGDATABASE=... PGPASSWORD=... typeorm migration:generate ...
小智 5
我遇到了同样的问题(typeorm 0.3.4)。我只是使用npx typeorm migration:show -d ./src/data-source.ts并得到与上面相同的错误(File must contain a TypeScript / JavaScript code and export a DataSource instance),同时生成迁移文件本身以某种方式工作,但没有运行/显示迁移本身。
我的数据源看起来像这样
export const AppDataSource = new DataSource({
type: 'postgres',
url: process.env.DATABASE_URL,
logging: true,
entities: ['dist/entities/*.js'],
migrations: ['dist/migrations/*.js'],
});
Run Code Online (Sandbox Code Playgroud)
因为我的tsc输出位于/dist.所以根据上面的注释,我开始使用从 TypeScript 生成的数据源文件,并且错误消息发生了变化:
npx typeorm migration:run -d ./dist/appDataSource.js
CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established.
Run Code Online (Sandbox Code Playgroud)
因此,我查看了数据库日志,意识到它想要使用标准 unix 用户连接到 postgres,它不尊重数据源代码中的连接字符串。我还必须向命令提供所有环境变量,并且它有效:
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/tsgraphqlserver npx typeorm migration:run -d ./dist/appDataSource.js
小智 3
我实际上遇到了同样的问题。
我能够通过使用*.js而不是解决它*.ts
请尝试这样的事情:
tsc && typeorm migration:generate -d db_config.ts projects/core/migrations/user
我的tsconfig.json样子是这样的
{
"compilerOptions": {
"target": "esnext",
"module": "CommonJS",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "./build",
"removeComments": false,
"resolveJsonModule": true,
"esModuleInterop": true,
}
}
Run Code Online (Sandbox Code Playgroud)
我建议你在 typeorm github repo 上打开一个问题,我认为这可能是一个错误。
| 归档时间: |
|
| 查看次数: |
28402 次 |
| 最近记录: |