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

feat: seed with service #75

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions db/seed.ts
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(),
});
Comment on lines +20 to +30
Copy link
Collaborator

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

}
const userResult = await db.select().from(users);

const userResult = await usersService.findAll();
console.log('User result: ', userResult);

exit(0);
12 changes: 10 additions & 2 deletions src/users/users.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// users.repository.ts
// in charge of database interactions
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import { eq } from 'drizzle-orm';
import { UserToCreate, UserToUpdate } from '@users/users.schema';
import { eq, sql } from 'drizzle-orm';
import { User, UserToCreate, UserToUpdate } from '@users/users.schema';
import { users } from '@users/users.model';

export class UsersRepository {
Expand Down Expand Up @@ -68,4 +68,12 @@ export class UsersRepository {
.returning();
return updatedUser[0];
}

async deleteAll() {
// when deleting all rows, we need to reset the sequence
// otherwise, new rows will start with the next ID
await this.db.execute(sql`ALTER SEQUENCE users_id_seq RESTART WITH 1`);
// we are likely deleting more than one row, return the whole result (and not `result[0]`)
return await this.db.delete(users).returning();
}
}
11 changes: 11 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see two problems with this:

  • It'll generate a valid token for ALL users, which is probably not a good thing to do regardless if it's exposed in an API or not
  • Although the spec doesn't talk about a functionality like this, but I think the returned data format should be changed. Instead of a list of singular user objects ({user: {...}}[]) it could be one plural users object, e.g. {users: [user1, user2]}

}