#Direct Database Access in Payload Website

4 messages · Page 1 of 1 (latest)

delicate saffron
#

I have a case where I'd like a feature I've added to a Payload driving Nextjs website to be able to directly store tabular data in the same database that is managed by Payload. Payload uses Drizzle by the looks of it, but this feature, as is, uses Drizzle directly, and my Payload app, built with the website template, doesn't expose Drizzle directly anywhere.

Because this is tabular data it seems a bit too far removed from the db to create and manage a collection for it, so what are my other options for achieving this?

crisp relicBOT
delicate saffron
#

Direct Database Access in Payload Website

dense sundial
# delicate saffron I have a case where I'd like a feature I've added to a Payload driving Nextjs we...

Hello 👋

Is the payload.db.drizzle exposed value what you need?
See: https://payloadcms.com/docs/database/postgres#access-to-drizzle

You will need to generate the type schema:
npx payload generate:db-schema

Then, you can use the exposed value as follows:

import { posts } from './payload-generated-schema'
// To avoid installing Drizzle, you can import everything that drizzle has from our re-export path.
import { eq, sql, and } from '@payloadcms/db-postgres/drizzle'

// Drizzle's Querying API: https://orm.drizzle.team/docs/rqb
const posts = await payload.db.drizzle.query.posts.findMany()
// Drizzle's Select API https://orm.drizzle.team/docs/select
const result = await payload.db.drizzle
  .select()
  .from(posts)
  .where(
    and(eq(posts.id, 50), sql`lower(${posts.title}) = 'example post title'`),
  )

If this message answered your question, please mark this post as solved! Thank you.