added seeder script

This commit is contained in:
Santosh Tharu 2024-07-26 09:50:19 +05:45
parent b006747a45
commit baf8e2ac81
3 changed files with 56 additions and 2 deletions

View file

@ -14,7 +14,7 @@
"build": "npm-run-all check-env build-db check-db build-tracker build-geo build-app",
"start": "next start",
"build-docker": "npm-run-all build-db build-tracker build-geo build-app",
"start-docker": "npm-run-all check-db update-tracker start-server",
"start-docker": "npm-run-all check-db update-tracker seed start-server",
"start-env": "node scripts/start-env.js",
"start-server": "node server.js",
"build-app": "next build",
@ -45,7 +45,8 @@
"postbuild": "node scripts/postbuild.js",
"test": "jest",
"cypress-open": "cypress open cypress run",
"cypress-run": "cypress run cypress run"
"cypress-run": "cypress run cypress run",
"seed": "node scripts/seed.js"
},
"lint-staged": {
"**/*.{js,jsx,ts,tsx}": [

21
scripts/seed.js Normal file
View file

@ -0,0 +1,21 @@
/* eslint-disable no-console */
require('dotenv').config();
const { client: prismaClient } = require('@umami/prisma-client');
const { seedAdminUser } = require('./seeds/admin-user');
async function main() {
// seed admin user
await seedAdminUser();
console.log('Seeding complete.');
}
main()
.catch(e => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prismaClient.$disconnect();
});

View file

@ -0,0 +1,32 @@
const { hashPassword } = require('next-basics');
/* eslint-disable no-console */
const { client: prismaClient } = require('@umami/prisma-client');
async function seedAdminUser() {
const adminUserId = process.env.ADMIN_USER_ID;
const adminUsername = process.env.ADMIN_USERNAME;
const adminPassword = process.env.ADMIN_PASSWORD;
if (!adminUserId || !adminUsername || !adminPassword) {
console.error('Admin credentials are not set.');
process.exit(1);
}
await prismaClient.user.upsert({
where: { username: adminUsername },
update: {},
create: {
id: adminUserId,
username: adminUsername,
password: hashPassword(adminPassword),
role: 'admin',
},
});
console.log('Admin user seeded successfully.');
}
module.exports = {
seedAdminUser,
};