Commit 3e3897e
Fabricio Vergara
Seed entity with lazy relations
given following model:
```typescript
@entity(User.TABLE_NAME)
export class User {
public static readonly TABLE_NAME = 'user';
@PrimaryGeneratedColumn()
public id: number;
@column({
name: 'first_name',
nullable: false,
})
public firstName: string;
@column({
name: 'last_name',
nullable: false,
})
public lastName: string;
@column({
name: 'email',
nullable: false,
unique: true,
})
public email: string;
@column({
name: 'password',
nullable: false,
})
public password: string;
@manytomany(type => Authority, authority => authority.users)
@jointable({ name: 'user_authority' })
public authorities: Promise<Authority[]>;
}
```
To create a new user entity you need to assign a promise to the entity, but to save you need to pass the authority object directly as mentioned here: typeorm/typeorm#1180 (comment)
```typescript
/**
* User factory
*/
factory.define(User, (faker: typeof Faker, args: any[]) => {
const gender = faker.random.number(1);
const firstName = faker.name.firstName(gender);
const lastName = faker.name.lastName(gender);
const email = faker.internet.email(firstName, lastName);
const authority = args[0] as Authority;
const user = new User();
user.firstName = firstName;
user.lastName = lastName;
user.email = email;
user.password = env.admin.password;
user.authorities = Promise.resolve([authority]);
return user;
});
````1 parent d8f99f0 commit 3e3897e
1 file changed
+7
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
74 | 78 | | |
75 | 79 | | |
76 | 80 | | |
| |||
81 | 85 | | |
82 | 86 | | |
83 | 87 | | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
84 | 91 | | |
0 commit comments