#How to run typeorm migration script from datasource file that import config from ts file 🆘

6 messages · Page 1 of 1 (latest)

sullen siren
#
  1. Can use import datasource config from another ts file in datasource file?
    This my migration script?
  2. Can use ./src/data-source/postgres.ts(typeorm datasource path) that is ts file for run migration?
    "migration:generate": "pnpm typeorm migration:generate -d ./src/data-source/postgres.ts",
    "migration:create": "pnpm  typeorm migration:create",
    "migration:run": "pnpm typeorm migration:run -d ./src/data-source/postgres.ts",
    "migration:revert": "pnpm typeorm migration:revert -d ./src/data-source/postgres.ts"

data-source/postgres.ts

import { DataSource } from 'typeorm'
import { postgresConfig } from '../config/data-source/postgres'

export default new DataSource(postgresConfig)

config file

import { TypeOrmModuleOptions } from '@nestjs/typeorm'
import { DataSourceOptions } from 'typeorm'
import 'dotenv/config'

export const postgresConfig: TypeOrmModuleOptions & DataSourceOptions = {
  type: 'postgres',
  name: 'postgres',
  host: process.env.POSTGRES_HOST,
  port: parseInt(process.env.POSTGRES_PORT, 10) || 5432,
  username: process.env.POSTGRES_USERNAME,
  password: process.env.POSTGRES_PASSWORD,
  database: process.env.POSTGRES_DB,
  entities: [__dirname + '/../../entities/postgres/*.entity.ts'],
  synchronize: false,
  logging: process.env.NODE_ENV !== 'production',
}

Error in comment ↓ ↓ ↓ ↓ ↓ ↓ ↓

#

and this is error when I run migration:run

> pet_passport_backend@0.0.1 migration:run /Users/narawitc/Desktop/repository/pet-passport/pet_passport_backend
> pnpm typeorm migration:run -d ./src/data-source/postgres.ts

Error during migration run:
Error: Unable to open file: "/Users/narawitc/Desktop/repository/pet-passport/pet_passport_backend/src/data-source/postgres.ts". Cannot use import statement outside a module
    at CommandUtils.loadDataSource (/Users/narawitc/Desktop/repository/pet-passport/pet_passport_backend/node_modules/.pnpm/typeorm@0.3.20_mongodb@6.12.0_pg@8.13.1_ts-node@10.9.2_@types+node@22.10.5_typescript@5.7.2_/node_modules/typeorm/commands/CommandUtils.js:22:19)
    at async Object.handler (/Users/narawitc/Desktop/repository/pet-passport/pet_passport_backend/node_modules/.pnpm/typeorm@0.3.20_mongodb@6.12.0_pg@8.13.1_ts-node@10.9.2_@types+node@22.10.5_typescript@5.7.2_/node_modules/typeorm/commands/MigrationRunCommand.js:40:26)
 ELIFECYCLE  Command failed with exit code 1.
#

How to run typeorm migration script by not create dist folder🆘

#

How to run typeorm migration script by not create dist folder 🆘

#

How to run typeorm migration script from datasource file that import config from ts file 🆘

dapper tinsel
#

use mikro-orm