forked from hazem/yslootahrobotics
feat: add seed.js for database seeding functionality
This commit is contained in:
parent
22387dbfe1
commit
2397bf7f71
@ -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
51
prisma/seed.js
Normal 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(); });
|
||||||
Loading…
x
Reference in New Issue
Block a user