Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/68f4726a-8c80-8009-9231-f5453eebb535
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== gpt-5 == === User: nestjsにrailsでいうfaker::loremみたいなダミーデータ作成するnpmとかないの? === nestjsにrailsでいうfaker::loremみたいなダミーデータ作成するnpmとかないの? === Assistant: あるよ!NestJS(というかNode.js環境全般)でも Rails の Faker に相当するダミーデータ生成ライブラリ がいくつかある👍 === あるよ!NestJS(というかNode.js環境全般)でも Rails の Faker に相当するダミーデータ生成ライブラリ がいくつかある👍 代表的なのをいくつか紹介するね。 ==== ### ==== : <syntaxhighlight lang="bash">npm install @faker-js/faker </syntaxhighlight> 使い方例👇 <syntaxhighlight lang="ts">import { faker } from '@faker-js/faker'; const user = { name: faker.person.fullName(), email: faker.internet.email(), password: faker.internet.password(), introduction: faker.lorem.sentence(), }; </syntaxhighlight> * faker.lorem.words() や faker.lorem.paragraph() などもある * faker.internet.email() なども Rails の Faker::Internet.email にそっくり * 日本語ロケールも対応してる 🇯🇵 ``<code>ts import { fakerJA } from '@faker-js/faker'; fakerJA.person.fullName(); // 日本語名 <syntaxhighlight> ===== : ===== </syntaxhighlight>bash npm install chance <syntaxhighlight> </syntaxhighlight>ts import Chance from 'chance'; const chance = new Chance(); const user = { name: chance.name(), email: chance.email(), bio: chance.sentence(), }; <syntaxhighlight> ===== : ===== </syntaxhighlight>bash npm install casual <syntaxhighlight> </syntaxhighlight>ts import casual from 'casual'; const user = { name: casual.full_name, email: casual.email, description: casual.text, }; <syntaxhighlight> ==== テストやシードデータ生成でよく使う: ==== </syntaxhighlight>ts // src/seeds/user.seed.ts import { faker } from '@faker-js/faker'; import { User } from '../users/user.entity'; export function createUserSeed(): User { const user = new User(); user.name = faker.person.fullName(); user.email = faker.internet.email(); user.password = faker.internet.password(); user.introduction = faker.lorem.sentence(); return user; } <syntaxhighlight> DB Seeder で使う場合は nestjs-seeder と組み合わせるとより便利👇 </syntaxhighlight>bash npm install nestjs-seeder @faker-js/faker </code>`` ==== | | | | ==== | --- | --- | --- | | | | | | | | | | | | | | | | | もし「Seeder としてまとめて NestJS CLI 経由で実行したい」みたいな構成を考えてるなら、 nestjs-seeder の使い方も一緒に紹介しようか? === User: password: faker.internet.password, === password: faker.internet.password(), って書いたらconfirmPasswordはどう書けばいいの? === Assistant: めっちゃ良い質問!👏 === めっちゃ良い質問!👏 faker.internet.password() は毎回ランダム文字列を返すから、 単純に2回呼ぶと password と confirmPassword が一致しない んよね。 Rails の Faker でも同じ問題があるけど、Node.js でも簡単に解決できるよ。 ==== ✅ シンプルな解決方法 ==== 1回だけ password を生成して、変数に保持しておく。 <syntaxhighlight lang="ts">import { faker } from '@faker-js/faker'; const password = faker.internet.password(); const user = { name: faker.person.fullName(), email: faker.internet.email(), password, // 生成した値を使う confirmPassword: password, // 同じ値を再利用! introduction: faker.lorem.sentence(), }; </syntaxhighlight> これで両方一致する👍 ==== 💡もう少し工夫するパターン(関数化) ==== テストやシードで複数ユーザー作るときは、 関数にしておくと扱いやすい。 <syntaxhighlight lang="ts">function createFakeUser() { const password = faker.internet.password(); return { name: faker.person.fullName(), email: faker.internet.email(), password, confirmPassword: password, introduction: faker.lorem.sentence(), }; } const user = createFakeUser(); </syntaxhighlight> ==== 🌱もし Jest テストで使うなら ==== たとえば SignupDto のモックを作るならこう 👇 <syntaxhighlight lang="ts">import { faker } from '@faker-js/faker'; import { SignupDto } from '../auth/dto/signup.dto'; export const mockSignupDto = (): SignupDto => { const password = faker.internet.password(); return { name: faker.person.fullName(), email: faker.internet.email(), password, confirmPassword: password, imageId: faker.string.uuid(), introduction: faker.lorem.sentence(), }; }; </syntaxhighlight> 要は 「faker で1回だけ生成 → 変数に保持 → confirmPassword にも使う」 これが正解やね✨ === User: UUIDは? === UUIDは? === Assistant: ナイス 👏 そこも気になるよね。 === ナイス 👏 そこも気になるよね。 UUID(ユニークID)も faker でしっかり生成できるよ! ==== ✅ faker で UUID を生成する方法 ==== <syntaxhighlight lang="ts">import { faker } from '@faker-js/faker'; const id = faker.string.uuid(); console.log(id); // 例: '5d78f017-ef80-fbbf-3aad-f3f5d6c10043' </syntaxhighlight> Rails の SecureRandom.uuid とほぼ同じ感じだね。 ==== 💡実際のダミーオブジェクト例 ==== <syntaxhighlight lang="ts">import { faker } from '@faker-js/faker'; const password = faker.internet.password(); const user = { id: faker.string.uuid(), name: faker.person.fullName(), email: faker.internet.email(), password, confirmPassword: password, imageId: faker.string.uuid(), // 画像IDとかにも使える introduction: faker.lorem.sentence(), }; </syntaxhighlight> ==== 🧩 もし TypeORM のエンティティ用シードに使うなら ==== UUID カラムに割り当てるなら、そのまま文字列を代入してOK: <syntaxhighlight lang="ts">import { faker } from '@faker-js/faker'; import { User } from '../users/user.entity'; export function createUserSeed(): User { const user = new User(); const password = faker.internet.password(); user.id = faker.string.uuid(); user.name = faker.person.fullName(); user.email = faker.internet.email(); user.password = password; user.confirmPassword = password; user.imageId = faker.string.uuid(); user.introduction = faker.lorem.sentence(); return user; } </syntaxhighlight> ==== 📝 まとめ ==== | 目的 | faker の使い方 | | ------------------- | ---------------------------------------------------------------------- | | ランダムUUID | faker.string.uuid() | | ランダムパスワード | faker.internet.password() | | 一致するconfirmPassword | const password = faker.internet.password(); confirmPassword = password | | 日本語の名前 | fakerJA.person.fullName() | もし「UUIDをv4で統一したい」とか「NestJSでエンティティ側で自動生成したい」場合のやり方も説明できるけど、そっちも気になる?
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)