prisma-table-names-generator
v1.0.1
Published
Generate a list of table names that can be used in raw Prisma SQL queries
Downloads
16
Readme
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
- 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"
}
- Run
prisma migrate dev
orprisma 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()
})