Skip to content

Commit

Permalink
feat: add seeding data (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
lamngockhuong authored Nov 19, 2024
1 parent 1e6b9b6 commit 552b0bc
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/database-typeorm/src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './article.entity';
export * from './comment.entity';
export * from './tag.entity';
export * from './user-follows.entity';
export * from './user.entity';
14 changes: 14 additions & 0 deletions packages/database-typeorm/src/factories/article.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { setSeederFactory } from 'typeorm-extension';
import { ArticleEntity } from '../entities';

export default setSeederFactory(ArticleEntity, (fake) => {
const article = new ArticleEntity();

article.title = fake.lorem.sentence();
article.slug = fake.lorem.slug();
article.description = fake.lorem.sentence();
article.body = fake.lorem.paragraphs(10);
article.authorId = 1;

return article;
});
12 changes: 12 additions & 0 deletions packages/database-typeorm/src/factories/comment.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { setSeederFactory } from 'typeorm-extension';
import { CommentEntity } from '../entities';

export default setSeederFactory(CommentEntity, (fake) => {
const comment = new CommentEntity();

comment.body = fake.lorem.paragraphs(1);
comment.articleId = 1;
comment.authorId = 1;

return comment;
});
14 changes: 14 additions & 0 deletions packages/database-typeorm/src/factories/tag.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { setSeederFactory } from 'typeorm-extension';
import { TagEntity } from '../entities';

export default setSeederFactory(TagEntity, async (fake) => {
const tag = new TagEntity();

let uniqueName: string;
do {
uniqueName = fake.lorem.words({ min: 1, max: 4 });
} while (await TagEntity.findOneBy({ name: uniqueName }));
tag.name = uniqueName;

return tag;
});
16 changes: 16 additions & 0 deletions packages/database-typeorm/src/factories/user.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { setSeederFactory } from 'typeorm-extension';
import { UserEntity } from '../entities';

export default setSeederFactory(UserEntity, (fake) => {
const user = new UserEntity();

const firstName = fake.person.firstName();
const lastName = fake.person.lastName();
user.username = `${firstName.toLowerCase()}${lastName.toLowerCase()}`;
user.email = fake.internet.email({ firstName, lastName });
user.password = '12345678';
user.bio = fake.lorem.sentence();
user.image = fake.image.avatar();

return user;
});
26 changes: 26 additions & 0 deletions packages/database-typeorm/src/seeds/1732019848273-user-seeder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { DataSource } from 'typeorm';
import { Seeder, SeederFactoryManager } from 'typeorm-extension';
import { UserEntity } from '../entities';

export class UserSeeder1732019848273 implements Seeder {
track = false;

public async run(
dataSource: DataSource,
factoryManager: SeederFactoryManager,
): Promise<any> {
const repository = dataSource.getRepository(UserEntity);
const userFactory = factoryManager.get(UserEntity);

const adminUser = await repository.findOneBy({ username: 'admin' });
if (!adminUser) {
const user = await userFactory.make({
username: 'admin',
email: '[email protected]',
});
await repository.insert(user);
}

await userFactory.saveMany(10);
}
}
15 changes: 15 additions & 0 deletions packages/database-typeorm/src/seeds/1732028144623-tag-seeder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DataSource } from 'typeorm';
import { Seeder, SeederFactoryManager } from 'typeorm-extension';
import { TagEntity } from '../entities';

export class TagSeeder1732028144623 implements Seeder {
track = false;

public async run(
dataSource: DataSource,
factoryManager: SeederFactoryManager,
): Promise<any> {
const tagFactory = factoryManager.get(TagEntity);
await tagFactory.saveMany(10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getRandomInt } from '@repo/nest-common';
import { DataSource } from 'typeorm';
import { Seeder, SeederFactoryManager } from 'typeorm-extension';
import { ArticleEntity, TagEntity, UserEntity } from '../entities';

export class ArticleSeeder1732028230352 implements Seeder {
track = false;

public async run(
dataSource: DataSource,
factoryManager: SeederFactoryManager,
): Promise<any> {
// Get random users
const userRepository = dataSource.getRepository(UserEntity);
const numberOfUsers = await userRepository.count();
const randomOffset = getRandomInt(0, numberOfUsers - 1);

const users = await userRepository
.createQueryBuilder('user')
.skip(randomOffset)
.take(10)
.getMany();

// Get random tags
const tagRepository = dataSource.getRepository(TagEntity);
const numberOfTags = await tagRepository.count();
const randomTagOffset = getRandomInt(0, numberOfTags - 1);

const tags = await tagRepository
.createQueryBuilder('tag')
.skip(randomTagOffset)
.take(10)
.getMany();

const articleFactory = factoryManager.get(ArticleEntity);
for (const user of users) {
const randomTagNumber = getRandomInt(0, tags.length - 1);
await articleFactory.saveMany(5, {
authorId: user.id,
tags: tags.slice(0, randomTagNumber).slice(0, 5),
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getRandomInt } from '@repo/nest-common';
import { DataSource } from 'typeorm';
import { Seeder, SeederFactoryManager } from 'typeorm-extension';
import { ArticleEntity, CommentEntity, UserEntity } from '../entities';

export class CommentSeeder1732031567099 implements Seeder {
track = false;

public async run(
dataSource: DataSource,
factoryManager: SeederFactoryManager,
): Promise<any> {
// Get random users
const userRepository = dataSource.getRepository(UserEntity);
const numberOfUsers = await userRepository.count();
const randomOffset = getRandomInt(0, numberOfUsers - 1);

const users = await userRepository
.createQueryBuilder('user')
.skip(randomOffset)
.take(10)
.getMany();

// Get random articles
const articleRepository = dataSource.getRepository(ArticleEntity);
const numberOfArticles = await articleRepository.count();
const randomArticleOffset = getRandomInt(0, numberOfArticles - 1);

const articles = await articleRepository
.createQueryBuilder('article')
.skip(randomArticleOffset)
.take(10)
.getMany();

const commentFactory = factoryManager.get(CommentEntity);
for (const user of users) {
const randomArticleNumber = getRandomInt(0, articles.length - 1);
await commentFactory.saveMany(5, {
authorId: user.id,
articleId: articles[randomArticleNumber].id,
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { getRandomInt } from '@repo/nest-common';
import { DataSource } from 'typeorm';
import { Seeder, SeederFactoryManager } from 'typeorm-extension';
import { UserEntity, UserFollowsEntity } from '../entities';

export class UserFollowsSeeder1732032435851 implements Seeder {
track = false;

public async run(
dataSource: DataSource,
_factoryManager: SeederFactoryManager,
): Promise<any> {
// Get random users
const userRepository = dataSource.getRepository(UserEntity);
const numberOfUsers = await userRepository.count();
const randomOffset = getRandomInt(0, numberOfUsers - 1);

const users = await userRepository
.createQueryBuilder('user')
.skip(randomOffset)
.take(10)
.getMany();

const userFollowsRepository = dataSource.getRepository(UserFollowsEntity);
for (const user of users) {
const randomUserNumber = getRandomInt(0, users.length - 1);
const randomFoloweeId = users[randomUserNumber].id;
const followeeId =
randomFoloweeId === user.id
? users[randomUserNumber + 1].id
: randomFoloweeId;
const isExist = await userFollowsRepository.existsBy({
followerId: user.id,
followeeId,
});

if (!isExist) {
await userFollowsRepository.save({
followerId: user.id,
followeeId,
});
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { getRandomInt } from '@repo/nest-common';
import { DataSource } from 'typeorm';
import { Seeder, SeederFactoryManager } from 'typeorm-extension';
import { ArticleEntity, UserEntity } from '../entities';

export class UserFavoritesSeeder1732032454792 implements Seeder {
track = false;

public async run(
dataSource: DataSource,
factoryManager: SeederFactoryManager,

Check warning on line 11 in packages/database-typeorm/src/seeds/1732032454792-user-favorites-seeder.ts

View workflow job for this annotation

GitHub Actions / Build and test (20.16.0)

'factoryManager' is defined but never used. Allowed unused args must match /^_/u
): Promise<any> {
// Get random users
const userRepository = dataSource.getRepository(UserEntity);
const numberOfUsers = await userRepository.count();
const randomOffset = getRandomInt(0, numberOfUsers - 1);

const users = await userRepository
.createQueryBuilder('user')
.leftJoinAndSelect('user.favorites', 'favorites')
.skip(randomOffset)
.take(10)
.getMany();

// Get random articles
const articleRepository = dataSource.getRepository(ArticleEntity);
const numberOfArticles = await articleRepository.count();
const randomArticleOffset = getRandomInt(0, numberOfArticles - 1);

const articles = await articleRepository
.createQueryBuilder('article')
.skip(randomArticleOffset)
.take(10)
.getMany();

for (const user of users) {
const randomArticleNumber = getRandomInt(0, articles.length - 1);
const isExist = user.favorites.some(
(favorite) => favorite.id === articles[randomArticleNumber].id,
);

if (!isExist) {
user.favorites.push(articles[randomArticleNumber]);
await userRepository.save(user);
}
}
}
}
1 change: 1 addition & 0 deletions packages/nest-common/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './number.util';
export * from './password.util';
export * from './validate-config.util';
3 changes: 3 additions & 0 deletions packages/nest-common/src/utils/number.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getRandomInt(start: number = 0, end: number): number {
return Math.floor(Math.random() * (end - start + 1)) + start;
}

0 comments on commit 552b0bc

Please sign in to comment.