P
Prisma•2mo ago
Charran

Seeding Prisma db with Excel File

Need some guidance on how to seed a prisma db using an excel file
2 Replies
Prisma AI Help
Prisma AI Help•2mo ago
You decided to hold for human wisdom. We'll chime in soon! Meanwhile, #ask-ai is there if you need a quick second opinion.
Nurul
Nurul•2mo ago
Hello 👋 You would likely need to first convert your Excel file data into a format that can be used in a TypeScript or JavaScript seeding script. Here's a snippet you can use as a reference:
const XLSX = require('xlsx');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function main() {
// Read the Excel file
const workbook = XLSX.readFile('./data.xlsx');
// Assuming data is in the first sheet
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];

// Convert the sheet to JSON
const data = XLSX.utils.sheet_to_json(worksheet);

// Iterate through each row and insert into the DB
for (const row of data) {
// Adjust the mapping to match your Prisma model
await prisma.yourModel.create({
data: {
// Assuming your Excel file has columns like 'name' and 'email'
name: row.name,
email: row.email,
// Map any other columns as necessary
},
});
}
}

main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
const XLSX = require('xlsx');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function main() {
// Read the Excel file
const workbook = XLSX.readFile('./data.xlsx');
// Assuming data is in the first sheet
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];

// Convert the sheet to JSON
const data = XLSX.utils.sheet_to_json(worksheet);

// Iterate through each row and insert into the DB
for (const row of data) {
// Adjust the mapping to match your Prisma model
await prisma.yourModel.create({
data: {
// Assuming your Excel file has columns like 'name' and 'email'
name: row.name,
email: row.email,
// Map any other columns as necessary
},
});
}
}

main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});

Did you find this page helpful?