typeorm-seeding
typeorm-seeding copied to clipboard
Running into a weird typescript compiling error
My factory class
export class UserFactory extends Factory<User> {
protected entity = User;
protected dataSource = AppDataSource;
firstName = faker.person.firstName();
lastName = faker.person.lastName();
protected attrs(): FactorizedAttrs<User> {
return {
firstName: this.firstName,
lastName: this.lastName,
email: faker.internet.email({ firstName: this.firstName, lastName: this.lastName, provider: 'test' }),
username: faker.internet.userName({ firstName: this.firstName, lastName: this.lastName }),
password: faker.internet.password(),
// owner: new LazyInstanceAttribute((instance) => new SingleSubfactory(UserFactory, { pets: [instance] })),
};
}
}
My seed class
export class CreateUsers implements Seeder {
public async run(dataSource: DataSource) {
const CreatedUsers = await new UserFactory().makeMany(10);
const users: User[] = CreatedUsers;
await dataSource.manager.save<User>(users);
}
}
My Entity Class
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ nullable: true })
firstName?: string;
@Column({ nullable: true })
lastName?: string;
@Length(3)
@Column({ unique: true })
username: string;
@IsEmail()
@Column({ nullable: false })
email: string;
@Column({ nullable: false, select: false })
password?: string;
public static create(options: Partial<User>) {
const user = new User();
return Object.assign(user, options);
}
}
My data-source.ts:
const connectionConfig = {
host: getEnvVar('POSTGRES_HOST'),
port: Number(getEnvVar('POSTGRES_PORT')),
username: getEnvVar('POSTGRES_USER'),
password: getEnvVar('POSTGRES_PASSWORD'),
database: getEnvVar('POSTGRES_DB'),
};
export const getDataSource = () => {
return new DataSource({
type: 'postgres',
...connectionConfig,
synchronize: false,
logging:
process.env.NODE_ENV !== 'production' &&
process.env.DEBUG === 'true',
entities: [User, Provider, Pet],
// TODO (task #27) needs to work after build with js files as well
migrations: [join(__dirname, 'migrations/*.ts')],
subscribers: [],
});
};
export const AppDataSource = getDataSource();
export const entityManager = AppDataSource.manager;
The NPM script im running is "seed:run": "ts-node -r tsconfig-paths/register ./../../node_modules/@jorgebodega/typeorm-seeding/dist/cli.js seed -d src/data-source.ts src/seeds/*.ts"
The Error im getting:
nx run backend:seed:run
> nx run backend:"seed:run"
> [email protected] seed:run
> ts-node -r tsconfig-paths/register ./../../node_modules/@jorgebodega/typeorm-seeding/dist/cli.js seed -d src/data-source.ts src/seeds/*.ts
- Loading datasource
Error: Unable to open file: "/Users/jesseberman/Development/Den/packages/backend/src/data-source.ts". ⨯ Unable to compile TypeScript:
src/entity/User.ts(4,2): error TS1238: Unable to resolve signature of class decorator when called as an expression.
The runtime will invoke the decorator with 2 arguments, but the decorator expects 1.
src/entity/User.ts(6,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(7,5): error TS2564: Property 'id' has no initializer and is not definitely assigned in the constructor.
src/entity/User.ts(9,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(12,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(15,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(16,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(17,5): error TS2564: Property 'username' has no initializer and is not definitely assigned in the constructor.
src/entity/User.ts(19,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(20,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(21,5): error TS2564: Property 'email' has no initializer and is not definitely assigned in the constructor.
src/entity/User.ts(23,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
at Function.loadDataSource (/Users/jesseberman/Development/Den/node_modules/src/commands/CommandUtils.ts:22:19)
at async Command.run (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:22:18)
at async Command.parseAsync (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/node_modules/commander/lib/command.js:916:5)
at async bootstrap (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:72:1)
✖ Could not load the data source!
DataSourceImportationError: Could not load the data source!
at Command.run (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:30:11)
at async Command.parseAsync (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/node_modules/commander/lib/command.js:916:5)
at async bootstrap (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:72:1) {
[cause]: Error: Unable to open file: "/Users/jesseberman/Development/Den/packages/backend/src/data-source.ts". ⨯ Unable to compile TypeScript:
src/entity/User.ts(4,2): error TS1238: Unable to resolve signature of class decorator when called as an expression.
The runtime will invoke the decorator with 2 arguments, but the decorator expects 1.
src/entity/User.ts(6,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(7,5): error TS2564: Property 'id' has no initializer and is not definitely assigned in the constructor.
src/entity/User.ts(9,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(12,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(15,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(16,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(17,5): error TS2564: Property 'username' has no initializer and is not definitely assigned in the constructor.
src/entity/User.ts(19,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(20,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
src/entity/User.ts(21,5): error TS2564: Property 'email' has no initializer and is not definitely assigned in the constructor.
src/entity/User.ts(23,6): error TS1240: Unable to resolve signature of property decorator when called as an expression.
Argument of type 'undefined' is not assignable to parameter of type 'Object'.
at Function.loadDataSource (/Users/jesseberman/Development/Den/node_modules/src/commands/CommandUtils.ts:22:19)
at async Command.run (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:22:18)
at async Command.parseAsync (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/node_modules/commander/lib/command.js:916:5)
at async bootstrap (/Users/jesseberman/Development/Den/node_modules/@jorgebodega/typeorm-seeding/src/commands/seed.command.ts:72:1)
}
npm ERR! Lifecycle script `seed:run` failed with error:
npm ERR! Error: command failed
npm ERR! in workspace: [email protected]
npm ERR! at location: /Users/jesseberman/Development/Den/packages/backend
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
> NX Ran target seed:run for project backend (2s)
✖ 1/1 failed
✔ 0/1 succeeded [0 read from cache]
Checking the error that you have... Are you sure this is related to this library? Seems like you have some kind of problems with the entity, and maybe you should check that before. Or maybe is something related to TypeORM itself
@Jberma23 - Working example based on your code :- https://github.com/AaronNGray/typeorm-seeding-example