Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing required argument: dataSource

I tried to generate migration files with typeorm version 0.3.6.I don't know why but in version 0.2.x it work with the command

npm run typeorm migration:generate -n <file name>

In newest version,I't so mess with me, I get another to another bugs , finally, I think I almost done but I continue get the error

Missing required argument: dataSource

this is scripts in my 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,...]

My file structure like this

server
 src
   ...
 dist
   data-source.js
   entities/myEntity.js

and the command i use to generate migration

 npm run typeorm migration:generate -n initial -d dist/data-source.js

Am i missing something? How can i fix it?

like image 899
Loc Tran Avatar asked Oct 19 '25 14:10

Loc Tran


1 Answers

In my case, it's was not necessary two files (omrconfig.json and datasource.ts).

You can resolve this case created a file ormconfig.ts how the next:


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;

After you can use the 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"

I'm use the "typeorm": "^0.3.6" and my file structure like this:

server
   typeorm
      migrations
   src
   ormconfig.ts
   package.json
like image 74
Lucas Mikó Avatar answered Oct 21 '25 05:10

Lucas Mikó



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!