/*
/*
 * Using prisma-binding from
 * https://github.com/prisma/prisma-binding
 */
import { Prisma } from 'prisma-binding'
import { typeDefs } from '../database/generated/prisma-client/prisma-schema'

export const db = new Prisma({
  typeDefs,
  endpoint: process.env.PRISMA_ENDPOINT,
  secret: process.env.PRISMA_SECRET,
  debug: false
})

export const populateUser = async (req: any, res: any, next: any) => {
  if (!req.userId) return next()
  const user = await db.query.user(
    { where: { id: req.userId } },
    '{id, email, name, permissions}'
  )
  req.user = user
  next()
}
