'How to get data from MySQL using prisma
I have a database stored in MySQL, and I am reading this documentation in how to get this data using prisma:
However, what I don't understand is how to get a specific table.
For example my data base is called "mydb" and I have 2 tables inside named "example1" and "example2". So if I want to get the data from "example1" table how do I do that? Because the way that I understand from the documentation is connecting with my database but how do I connect with my table? What should I add in the URL?
DATABASE_URL="mysql://johndoe:XXX@mysql–instance1.123456789012.us-east-1.rds.amazonaws.com:3306/mydb"
Solution 1:[1]
You cannot connect to a specific table through a connection string. You can connect to a specific schema in the database though.
If you want to get data from specific tables you would need to generate PrismaClient and access the data through queries.
To generate Prisma Client, you would need to invoke
npx prisma generate
Here's an example query to get all data from example1 table:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
const allData = await prisma.example1.findMany({
where: {},
});
console.log('allData', allData);
}
main()
.catch((e) => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});
This guide should be helpful to you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nurul Sundarani |
