Files
HKSingleParty/03_source/cms_backend/prisma/seeds/userItem.ts
louiscklaw 834f58bde1 update,
2025-05-30 01:14:10 +08:00

127 lines
3.4 KiB
TypeScript

import { PrismaClient } from '@prisma/client';
import { generateHash } from 'src/utils/hash';
import { Config, names, uniqueNamesGenerator } from 'unique-names-generator';
import { faker } from '@faker-js/faker';
import { faker as enFaker } from '@faker-js/faker/locale/en_US';
import { faker as zhFaker } from '@faker-js/faker/locale/zh_CN';
import { faker as jaFaker } from '@faker-js/faker/locale/ja';
import { faker as koFaker } from '@faker-js/faker/locale/ko';
import { faker as twFaker } from '@faker-js/faker/locale/zh_TW';
const SEED_EMAIL_DOMAIN = 'seed.com';
const prisma = new PrismaClient();
async function userItem() {
const config: Config = { dictionaries: [names] };
const firstName = uniqueNamesGenerator(config);
const lastName = uniqueNamesGenerator(config);
const username = `${firstName.toLowerCase()}-${lastName.toLowerCase()}`;
const alice = await prisma.userItem.upsert({
where: { id: 'admin_uuid' },
update: {},
create: {
name: `admin test`,
city: '',
role: '',
email: `admin@123.com`,
state: '',
status: '',
address: '',
country: '',
zipCode: '',
company: '',
avatarUrl: '',
phoneNumber: '',
isVerified: true,
//
username: 'admin@123.com',
password: await generateHash('Aa1234567'),
},
});
for (let i = 1; i < 20; i++) {
const CJK_LOCALES = {
en: enFaker,
zh: zhFaker,
ja: jaFaker,
ko: koFaker,
tw: twFaker,
};
function getRandomCJKFaker() {
const locales = Object.keys(CJK_LOCALES);
const randomKey = locales[Math.floor(Math.random() * locales.length)] as keyof typeof CJK_LOCALES;
return CJK_LOCALES[randomKey];
}
const randomFaker = getRandomCJKFaker();
await prisma.userItem.upsert({
where: { id: i.toString() },
update: {},
create: {
name: randomFaker.person.fullName(),
city: randomFaker.location.city(),
role: ROLE[Math.floor(Math.random() * ROLE.length)],
email: randomFaker.internet.email(),
state: randomFaker.location.state(),
status: STATUS[Math.floor(Math.random() * STATUS.length)],
address: randomFaker.location.streetAddress(),
country: randomFaker.location.country(),
zipCode: randomFaker.location.zipCode(),
company: randomFaker.company.name(),
avatarUrl: randomFaker.image.avatar(),
phoneNumber: randomFaker.phone.number(),
isVerified: true,
//
username: randomFaker.internet.username(),
password: await generateHash('Abc1234!'),
},
});
}
console.log('seed user done');
}
const userItemSeed = userItem()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});
export { userItemSeed };
const ROLE = [
`CEO`,
`CTO`,
`Project Coordinator`,
`Team Leader`,
`Software Developer`,
`Marketing Strategist`,
`Data Analyst`,
`Product Owner`,
`Graphic Designer`,
`Operations Manager`,
`Customer Support Specialist`,
`Sales Manager`,
`HR Recruiter`,
`Business Consultant`,
`Financial Planner`,
`Network Engineer`,
`Content Creator`,
`Quality Assurance Tester`,
`Public Relations Officer`,
`IT Administrator`,
`Compliance Officer`,
`Event Planner`,
`Legal Counsel`,
`Training Coordinator`,
];
const STATUS = ['active', 'pending', 'banned'];