You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import { DataSource } from 'typeorm';
import { config as databaseConfig } from './common/config/database';
const AppDataSource = new DataSource(databaseConfig);
export default AppDataSource;
// UserSeeder.ts
import { Seeder } from '@jorgebodega/typeorm-seeding';
import { DataSource } from 'typeorm';
import { User } from './User';
import { faker } from '@faker-js/faker';
export default class UserSeeder extends Seeder {
public async run(dataSource: DataSource) {
const users: User[] = [];
Array.from({ length: 10 }).forEach(() => {
users.push(this.createRandomUser());
});
await dataSource.createEntityManager().save<User>(users);
}
private createRandomUser(): User {
const user = new User();
user.firstName = faker.name.firstName();
user.lastName = faker.name.lastName();
user.email = faker.internet.email();
user.password = faker.internet.password();
user.createdAt = faker.date.past();
return user;
}
}
Running npx typeorm-seeding seed -d src/datasource.ts /app/src/user/UserSeeder.ts I got following error:
DataSourceImportationError: Could not load the data source!
at Command.run (/Users/bla/development/node.js/node_modules/@jorgebodega/typeorm-seeding/dist/commands/seed.command.js:25:15)
at async Command.parseAsync (/Users/bla/development/node.js/node_modules/commander/lib/command.js:916:5)
at async bootstrap (/Users/bla/development/node.js/node_modules/@jorgebodega/typeorm-seeding/dist/commands/seed.command.js:61:5) {
[cause]: Error: Unable to open file: "/Users/bla/development/node.js/src/datasource.ts". Cannot use import statement outside a module
at CommandUtils.loadDataSource (/Users/bla/development/node.js/node_modules/typeorm/commands/CommandUtils.js:22:19)
at async Command.run (/Users/bla/development/node.js/node_modules/@jorgebodega/typeorm-seeding/dist/commands/seed.command.js:20:22)
at async Command.parseAsync (/Users/bla/development/node.js/node_modules/commander/lib/command.js:916:5)
at async bootstrap (/Users/bla/development/node.js/node_modules/@jorgebodega/typeorm-seeding/dist/commands/seed.command.js:61:5)
}
Any idea what I'm doing wrong?
The text was updated successfully, but these errors were encountered:
Reading the error message it looks like since datasource has import statements, so common js require in CommandUtils of typeorm is failing.
To workaround I've changed the seed:run command to directly use ts-node. And things are working now. Not sure why everyone else is not having this issue.
I need help with running seeder.
// config/database.ts
// datasource.ts
// UserSeeder.ts
Running
npx typeorm-seeding seed -d src/datasource.ts /app/src/user/UserSeeder.ts
I got following error:Any idea what I'm doing wrong?
The text was updated successfully, but these errors were encountered: