Typeorm + nodejs。执行程序或命令类型迁移:运行记录或错误:
从“ typeorm”导入{MigrationInterface,QueryRunner,表};
SyntaxError:无法在模块外部使用import语句。 -------------------------------------------------- -------------------------------------------------- --------------------------------------
package.json: `{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ts-node-dev --transpile-only --ignore-watch node_modules src/server.ts",
"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.17.8",
"express": "^4.17.1",
"sqlite3": "^5.0.0",
"tsc": "^1.20150623.0",
"typeorm": "^0.2.28"
},
"devDependencies": {
"ts-node-dev": "^1.0.0",
"typescript": "^4.0.3"
}
}
`
ormconfig.json: `{
"type": "sqlite",
"database": "./src/database/database.sqlite",
"migrations": ["./src/database/migrations/*.ts"],
"cli": {
"migrationsDir": "./src/database/migrations"
}
}
`
Arquivo: `import { MigrationInterface, QueryRunner, Table } from "typeorm";
export class createOrphanages1604201571493 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "orphanages",
columns: [
{
name: "id",
type: "integer",
unsigned: true,
isPrimary: true,
isGenerated: true,
generationStrategy: "increment",
},
{
name: "name",
type: "varchar",
},
{
name: "latitude",
type: "decimal",
scale: 10,
precision: 2,
},
{
name: "about",
type: "text",
},
{
name: "intructions",
type: "text",
},
{
name: "open_on_weekends",
type: "boolean",
default: false,
},
],
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable("orphanages");
}
}
`