-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: seed with service #75
Open
yamcodes
wants to merge
2
commits into
main
Choose a base branch
from
feature/seed-with-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,36 @@ | ||
import { exit } from 'process'; | ||
import { db } from '@/database.providers'; | ||
import { users } from '@users/users.model'; | ||
import { faker } from '@faker-js/faker'; | ||
import { UsersRepository } from '@/users/users.repository'; | ||
import { UsersService } from '@/users/users.service'; | ||
import { AuthService } from '@/auth/auth.service'; | ||
|
||
const usersRepsitory = new UsersRepository(db); | ||
const authService = new AuthService(); | ||
const usersService = new UsersService(usersRepsitory, authService); | ||
|
||
console.log('Truncating the user database'); | ||
await db.delete(users); | ||
console.log('The database is empty: ', await db.select().from(users)); | ||
await usersService.deleteAll(); | ||
const truncateResult = await usersService.findAll(); | ||
console.log('Truncate result: ', truncateResult); | ||
|
||
for (let i = 0; i < 10; i++) { | ||
const data = { | ||
id: faker.datatype.number(), | ||
// Create and update 10 users with random data | ||
for (let i = 1; i <= 10; i++) { | ||
// Create a new user | ||
await usersService.createUser({ | ||
email: faker.internet.email(), | ||
username: faker.internet.userName(), | ||
password: await Bun.password.hash(faker.internet.password()), | ||
bio: faker.lorem.text(), | ||
image: faker.image.imageUrl(), | ||
}; | ||
console.log('Upserting user:', data); | ||
password: faker.internet.password(), | ||
}); | ||
|
||
await db.insert(users).values(data); | ||
console.log('User upserted'); | ||
// Update the user's bio and image | ||
await usersService.updateUser(i, { | ||
bio: faker.lorem.paragraph(), | ||
image: faker.internet.avatar(), | ||
}); | ||
} | ||
const userResult = await db.select().from(users); | ||
|
||
const userResult = await usersService.findAll(); | ||
console.log('User result: ', userResult); | ||
|
||
exit(0); |
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 |
---|---|---|
|
@@ -59,6 +59,10 @@ export class UsersService { | |
return await this.generateUserResponse(user); | ||
} | ||
|
||
async deleteAll() { | ||
return await this.repository.deleteAll(); | ||
} | ||
|
||
async generateUserResponse(user: UserInDb) { | ||
return { | ||
user: { | ||
|
@@ -70,4 +74,11 @@ export class UsersService { | |
}, | ||
}; | ||
} | ||
|
||
async findAll() { | ||
const users = await this.repository.findAll(); | ||
return await Promise.all( | ||
users.map((user) => this.generateUserResponse(user)), | ||
); | ||
} | ||
Comment on lines
+78
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see two problems with this:
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be able to create a user with all attributes. It's just that the frontend won't send the two optional keys in the registration request