-
Notifications
You must be signed in to change notification settings - Fork 1
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
1e6b9b6
commit 552b0bc
Showing
13 changed files
with
283 additions
and
0 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
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
14
packages/database-typeorm/src/factories/article.factory.ts
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 { 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
12
packages/database-typeorm/src/factories/comment.factory.ts
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,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; | ||
}); |
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 { 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; | ||
}); |
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,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
26
packages/database-typeorm/src/seeds/1732019848273-user-seeder.ts
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,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
15
packages/database-typeorm/src/seeds/1732028144623-tag-seeder.ts
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,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); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/database-typeorm/src/seeds/1732028230352-article-seeder.ts
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,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), | ||
}); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/database-typeorm/src/seeds/1732031567099-comment-seeder.ts
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,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, | ||
}); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/database-typeorm/src/seeds/1732032435851-user-follows-seeder.ts
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,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, | ||
}); | ||
} | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/database-typeorm/src/seeds/1732032454792-user-favorites-seeder.ts
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,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, | ||
): 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); | ||
} | ||
} | ||
} | ||
} |
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,2 +1,3 @@ | ||
export * from './number.util'; | ||
export * from './password.util'; | ||
export * from './validate-config.util'; |
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,3 @@ | ||
export function getRandomInt(start: number = 0, end: number): number { | ||
return Math.floor(Math.random() * (end - start + 1)) + start; | ||
} |