Reputation: 574
I'm trying to run TypeORM migrations with ormconfig.json
like this
{
"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
What I have to do? Thank you for your advices!
Upvotes: 37
Views: 49458
Reputation: 3520
I got the same error while following their doc
npx typeorm-ts-node-commonjs migration:run -- -d ./src/data-source.ts
But it worked removing --
npx typeorm-ts-node-commonjs migration:run -d ./src/data-source.ts
So, I updated my package.json a little bit
"scripts": {
"migration:run": "typeorm-ts-node-commonjs migration:run -d ./src/data-source.ts",
"migration:revert": "typeorm-ts-node-commonjs migration:revert -d ./src/data-source.ts"
}
Upvotes: 6
Reputation: 71
Please don't forget --
If you need to pass parameter with dash to npm script, you will need to add them after --. For example, if you need to generate, the command is like this:
npm run typeorm migration:generate -- -n migrationNameHere
Docs here.
Upvotes: 7
Reputation: 564
TypeOrm removed ormconfig.json
support in version 0.3.0. You should use new syntax - create ormconfig.ts
and specify options for you database, for example:
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}'],
});
Then, after running the connection:
await connectionSource.initialize();
You can get entities by:
const myRepo = connectionSource.getRepository(SomeEntity)
Also your scripts in package.json
should look like this:
"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",
After the command, just give the name for migration in the console without -n option
Upvotes: 47
Reputation: 3381
with latest typescript if you are using cli setup as per typeorm setup
then following package.json script will work
"scripts": {
"typeorm": "typeorm-ts-node-commonjs -d ./src/datasources/PostgresDatasource.ts",
}
Run npm run typeorm migration:generate src/migration/initaltables
npm run typeorm migration:run
Upvotes: 14
Reputation: 21
Use the App - Datasource method to initialize your connection and it's quit easier from there
export const AppDataSource = new DataSource({
type: "postgres",
host: "localhost",
port: 5432,
username: "postgres",
password: "ROOT",
database: "userLog",
synchronize: true,
logging: true,
entities: [User, Student],
migrations: ["src/migration/**/*.ts"],
migrationsTableName: "custom_migration_table",
subscribers: ["src/migration/**/*.ts"],
})
Initialize your connection.
AppDataSource.initialize()
.then(async () => {
// do anything here like connecting to your express server or adding document to your db
}
If it's a Javascript project, use this CLI command - typeorm migration:run
But if your project uses typescript, you can use ts-node in conjunction with typeorm to run .ts migration files. use the following commands
"create": "typeorm migration:create ./src/migration/learningMigration"
"generate": "typeorm migration:generate -n PostRefactoring"
"migrate": "npx typeorm-ts-node-commonjs migration:run -d src/data-source",
"revert": "npx typeorm-ts-node-commonjs migration:revert -d src/data-source",
Upvotes: 1
Reputation: 21
Just figure out that you have to define the path to the file where your Datasource is defined.
In my case:
yarn typeorm migration:run -d dist/datasources/datasource.js
Upvotes: 2