我尝试用typeorm 0.3.6生成迁移文件,我不知道为什么,但是在0.2.x版本中,它与命令一起工作
npm run typeorm migration:generate -n <file name>
在最新版本中,我没有那么乱我自己,我得到了另一个到另一个错误,最后,我想我几乎完成了,但我继续错误
Missing required argument: dataSource
这是我package.json中的脚本
"scripts": {
"server": "nodemon dist/index.js",
"watch": "tsc -w",
"test": "echo \"Error: no test specified\" && exit 1",
"typeorm": "ts-node ./node_modules/typeorm/cli.js"
},
ormconfig.json
{
"type":"postgres",
"host":"localhost",
"port":5432,
"username":"postgres",
"password":"",
"database":"test-deploy",
"entities":["dist/entities/*.js"],
"migrations":["dist/migrations/*.js"]
}
dataSource.ts
export const dataSource = new DataSource({
type:"postgres",
username: process.env.PG_USERNAME_DEV,
password: process.env.PG_PASSWORD_DEV,
database: "memories",
synchronize: false,
logging: false,
entities: [Admin,...],
subscribers: [],
migrations: [],
})
//For ApolloServer
export const resolvers : NonEmptyArray<Function> =[AdminResolver,...]
我的文件结构像这样
server
src
...
dist
data-source.js
entities/myEntity.js
以及用于生成迁移的命令
npm run typeorm migration:generate -n initial -d dist/data-source.js
我是不是遗漏了什么?我怎么才能修好它?
发布于 2022-06-13 02:15:32
在我的例子中,这不是必需的两个文件(omrconfig.json和datasource.ts)。
您可以解决此情况,然后创建一个文件ormconfig.ts
:
const AppDataSource = new DataSource({
type: 'mysql',
host: 'localhost',
port: 645664,
username: 'test',
password: 'test',
database: 'oracli',
synchronize: false,
logging: true,
"entities": [
"src/typeorm/**/*.ts"
],
"migrations": [
"typeorm/migrations/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
});
export default AppDataSource;
在您可以使用scrtipts之后:
"migrate:create": "typeorm migration:create \"./typeorm/migrations/",
"migrate:up": "ts-node --transpile-only ./node_modules/typeorm/cli.js migration:run -d ormconfig.ts",
"migrate:down": "ts-node --transpile-only ./node-modules/typeorm/cli.js migration:revert"
我使用的"typeorm": "^0.3.6"
和我的文件结构如下:
server
typeorm
migrations
src
ormconfig.ts
package.json
https://stackoverflow.com/questions/72002706
复制相似问题