1.0.1 • Published 9 months ago

prisma-table-names-generator v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Prisma Table Names Generator

The code for this prisma generator is based on the prisma-kysely generator. Without it, this generator would not exist.

Want to use Prisma's raw SQL queries but don't want to write the table names by hand, leading to potential errors? This generator is for you!

Installation

npm install prisma-table-names-generator

Usage

  1. Add the generator to your schema.prisma file:
generator tableNames {
  provider = "prisma-table-names-generator"

  // Optional: specify the output directory
  output = "../db/generated"
  // Optional: specify the output file name
  fileName = "table.ts"
}
  1. Run prisma migrate dev or prisma generate to generate the table names file and use it in your queries:
import { PrismaClient } from '@prisma/client'
import { Table } from '../db/generated/table'

const prisma = new PrismaClient()

async function main() {
  const users = await prisma.$queryRaw(`SELECT * FROM ${Table.User}`)
  console.log(users)
}

main()
  .catch(e => {
    throw e
  })
  .finally(async () => {
    await prisma.$disconnect()
  })