feat: add seed.js for database seeding functionality
Some checks are pending
CI/CD / test-and-build (push) Waiting to run
CI/CD / deploy (push) Blocked by required conditions

This commit is contained in:
Najjar\NajjarV02 2026-04-15 10:49:04 +04:00
parent 22387dbfe1
commit 2397bf7f71
2 changed files with 51 additions and 8 deletions

View File

@ -15,14 +15,6 @@ ENV NEXT_TELEMETRY_DISABLED=1
# Generate Prisma client (reads prisma/schema.prisma + prisma.config.ts) # Generate Prisma client (reads prisma/schema.prisma + prisma.config.ts)
RUN npx prisma generate RUN npx prisma generate
# Compile seed.ts → seed.js (esbuild ships with Next.js)
RUN node_modules/.bin/esbuild prisma/seed.ts \
--bundle \
--platform=node \
--format=cjs \
--packages=external \
--outfile=prisma/seed.js
# Build Next.js — produces .next/standalone (set in next.config.mjs) # Build Next.js — produces .next/standalone (set in next.config.mjs)
RUN npm run build RUN npm run build

51
prisma/seed.js Normal file
View File

@ -0,0 +1,51 @@
'use strict';
const { PrismaClient } = require('../src/generated/prisma/client.js');
const { PrismaLibSql } = require('@prisma/adapter-libsql');
const bcrypt = require('bcryptjs');
const { randomBytes } = require('crypto');
const path = require('path');
const dbUrl = process.env.DATABASE_URL ?? `file:${path.resolve(process.cwd(), 'prisma', 'lootah.db')}`;
const adapter = new PrismaLibSql({ url: dbUrl });
const prisma = new PrismaClient({ adapter });
async function main() {
console.log('Seeding database...');
const existingAdmin = await prisma.adminUser.findUnique({
where: { username: 'admin' },
});
if (!existingAdmin) {
const passwordHash = await bcrypt.hash('admin123', 12);
await prisma.adminUser.create({
data: { username: 'admin', passwordHash },
});
console.log('✓ Created admin user (username: admin, password: admin123)');
console.log(' ⚠️ Change the password after first login!');
} else {
console.log('✓ Admin user already exists, skipping.');
}
const existingSecret = await prisma.appSettings.findUnique({
where: { key: 'jwt_secret' },
});
if (!existingSecret) {
const jwtSecret = randomBytes(64).toString('hex');
await prisma.appSettings.upsert({
where: { key: 'jwt_secret' },
update: { value: jwtSecret },
create: { key: 'jwt_secret', value: jwtSecret },
});
console.log('✓ Generated JWT secret and stored in database.');
} else {
console.log('✓ JWT secret already exists, skipping.');
}
console.log('Seeding complete!');
}
main()
.catch((e) => { console.error(e); process.exit(1); })
.finally(async () => { await prisma.$disconnect(); });