skip to Main Content

I have a database with a lot of data…. that was being handled by Dot Net core framework.
Now i want a small admin panel using nextjs i want to connect that database with my new next js application using Prisma.
But without schema Prisma won’t work.
anyone please explain what is the best way to do it.

I have tried db. pull it creates schema but when i access that schema with Prisma client. it shows error.

const user = await prisma.post.findFirst({ where: { name: "ali" } });

your textenter image description here

2

Answers


  1. If you want to use Prisma without defining a schema, I think you can take advantage of Prisma’s prisma.$queryRaw method, which allows you to send raw SQL queries directly to the database. This way, you can interact with the database without defining Prisma models.

    I add example code.

    import { PrismaClient } from '@prisma/client';
    
    const prisma = new PrismaClient({
      datasources: {
        db: {
          url: 'your_database_connection_url_here',
        },
      },
    });
    
    export async function getServerSideProps() {
      try {
        const data = await prisma.$queryRaw`SELECT * FROM users WHERE name = 'ali'`;
    
        return { props: { data } };
      } catch (error) {
        console.error('Error querying data:', error);
        return { props: { error: 'Error querying data' } };
      } finally {
        await prisma.$disconnect();
      }
    }
    
    Login or Signup to reply.
  2. Try using queryRaw method to execute raw SQL queries against your PostgreSQL database. In your case, hope it’ll work.

    const prisma = require('./prisma/client');
    
    async function fetchData() {
      try {
        const result = await prisma.$queryRaw`SELECT * FROM your_table_name`;
        console.log(result);
      } catch (error) {
        console.error('Error fetching data:', error);
      } finally {
        await prisma.$disconnect();
      }
    }
    
    fetchData();
    

    Or you can also consider creating prisma schema from your existing database using these commands

    • npx prisma introspect
    • npx prisma generate
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search