|
| 1 | +import { PrismaClient } from '@prisma/client'; |
| 2 | +import uuidAPIKey from 'uuid-apikey'; |
| 3 | +import { genSalt, hash } from 'bcryptjs'; |
| 4 | + |
| 5 | +const prisma = new PrismaClient({ |
| 6 | + log: ['query'], |
| 7 | +}); |
| 8 | + |
| 9 | +async function seed() { |
| 10 | + await prisma.connect(); |
| 11 | + console.log('Seeding default data...'); |
| 12 | + await Promise.all([createDefaultUser(), createDefaultProject()]); |
| 13 | + await prisma.disconnect(); |
| 14 | +} |
| 15 | + |
| 16 | +seed() |
| 17 | + .catch((e) => console.error('e', e)) |
| 18 | + .finally(async () => await prisma.disconnect()); |
| 19 | + |
| 20 | +async function createDefaultUser() { |
| 21 | + const userList = await prisma.user.findMany(); |
| 22 | + console.log(userList); |
| 23 | + if (userList.length === 0) { |
| 24 | + const defaultEmail = '[email protected]'; |
| 25 | + const defaultPassword = '123456'; |
| 26 | + const salt = await genSalt(10); |
| 27 | + await prisma.user |
| 28 | + .create({ |
| 29 | + data: { |
| 30 | + email: defaultEmail, |
| 31 | + firstName: 'fname', |
| 32 | + lastName: 'lname', |
| 33 | + apiKey: uuidAPIKey.create({ noDashes: true }).apiKey, |
| 34 | + password: await hash(defaultPassword, salt), |
| 35 | + }, |
| 36 | + }) |
| 37 | + .then((user) => { |
| 38 | + console.log('###########################'); |
| 39 | + console.log('## CREATING DEFAULT USER ##'); |
| 40 | + console.log('###########################'); |
| 41 | + console.log(''); |
| 42 | + console.log(`The user with the email "${defaultEmail}" and password "${defaultPassword}" was created`); |
| 43 | + console.log(`The Api key is: ${user.apiKey}`); |
| 44 | + }); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async function createDefaultProject() { |
| 49 | + const projectList = await prisma.project.findMany(); |
| 50 | + console.log(projectList); |
| 51 | + if (projectList.length === 0) { |
| 52 | + await prisma.project |
| 53 | + .create({ |
| 54 | + data: { |
| 55 | + name: 'Default project', |
| 56 | + }, |
| 57 | + }) |
| 58 | + .then((project) => { |
| 59 | + console.log('##############################'); |
| 60 | + console.log('## CREATING DEFAULT PROJECT ##'); |
| 61 | + console.log('##############################'); |
| 62 | + console.log(''); |
| 63 | + console.log(`Project name ${project.name}`); |
| 64 | + console.log(`Project key: ${project.id}`); |
| 65 | + }); |
| 66 | + } |
| 67 | +} |
0 commit comments