Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataSourceImportationError: Could not load the data source! #227

Closed
gardelin opened this issue Dec 27, 2022 · 3 comments
Closed

DataSourceImportationError: Could not load the data source! #227

gardelin opened this issue Dec 27, 2022 · 3 comments

Comments

@gardelin
Copy link

I need help with running seeder.

// config/database.ts

import { DataSourceOptions } from 'typeorm';
import env from './env';
import { User } from '../../user/User';

export const config: DataSourceOptions = {
    type: 'postgres',
    host: env.POSTGRES_HOST,
    port: env.POSTGRES_LOCAL_PORT,
    username: env.POSTGRES_USER,
    password: env.POSTGRES_PASSWORD,
    database: env.POSTGRES_DB,
    entities: [Book, Author, User],
    synchronize: true
};

// datasource.ts

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?

@gardelin
Copy link
Author

Ahhh I was running command on host instead of docker

@Jberma23
Copy link

Jberma23 commented Jul 8, 2023

What was the fix for this? do you mean running it within the docker container itself? rather than an npm script

@breathingcyborg
Copy link

What was the fix for this? do you mean running it within the docker container itself? rather than an npm script

@Jberma23

Faced the same issue this morning.

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.

 "seed:run": "ts-node ./node_modules/@jorgebodega/typeorm-seeding/dist/cli.js seed -d src/database/datasource.ts"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants