-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5de2e4
commit e30166a
Showing
7 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Connection, ObjectType } from 'typeorm'; | ||
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity'; | ||
|
||
import { User } from '@/db/entities/user.entity'; | ||
import { userFixtures } from '@/db/fixtures/user.fixtures'; | ||
import { encryptPassword } from '@/utils/password.utils'; | ||
|
||
type Entity = ObjectType<Record<string, unknown>>; | ||
|
||
interface Fixture { | ||
entity: Entity; | ||
values: QueryDeepPartialEntity<Entity>[]; | ||
} | ||
|
||
const fixtures: Fixture[] = [{ entity: User, values: userFixtures }]; | ||
|
||
export async function seedDatabase(connection: Connection): Promise<void> { | ||
for (const fixture of fixtures) { | ||
if (fixture.entity === User) { | ||
for (const value of fixture.values) { | ||
const user = value as User; | ||
user.password = await encryptPassword(user.password); | ||
} | ||
} | ||
const repository = connection.getRepository(fixture.entity); | ||
await repository.save(fixture.values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity'; | ||
|
||
import { User } from '@/db/entities/user.entity'; | ||
|
||
export const userFixtures: QueryDeepPartialEntity<User>[] = [ | ||
{ | ||
id: 1, | ||
enabled: true, | ||
email: 'msmith@fixture.example.com' as Email, | ||
firstName: 'Morty', | ||
lastName: 'Smith', | ||
password: 'secret', | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters