create-t3-turbo icon indicating copy to clipboard operation
create-t3-turbo copied to clipboard

bug: Client component - Drizzle schema - env error

Open john093e opened this issue 2 years ago • 5 comments

Provide environment information

turbo dev

Describe the bug

inside a client component:

import { schema } from "@acme/db"

Seems to load the whole index.ts and so return env error as env can't be use in client.

Link to reproduction

To reproduce

import {schema} from "@acme/db"

schema.post.status.enumValues.map((status) => ({ // do somthing with it

Additional information

No response

john093e avatar Apr 12 '24 11:04 john093e

Why do you need your schema in client components?

dBianchii avatar Apr 12 '24 11:04 dBianchii

Maybe I don't use it well :/

In order to create an option list for example in a table, for filtering, ordering, or to build form to create or update.

For example a "Post" table with a column "status" set to an enumerate value like "archived | editing | published", i would use like this

import {schema} from "@acme/db"

<select> 
 {schema.post.status.enumValues.map((status) => (
 <option>{status}</option>
 ))}
 </select>

john093e avatar Apr 12 '24 13:04 john093e

You can define the status-enum in @acme/validators then re-export it from there. You can also use drizzle-zod there for getting the enum values https://orm.drizzle.team/docs/zod.

andrewdoro avatar Apr 12 '24 13:04 andrewdoro

@andrewdoro , I still think you can't use schemas that came from drizzle-zod in client components. I tried it recently myself.

See: https://github.com/t3-oss/create-t3-turbo/discussions/966

dBianchii avatar Apr 12 '24 14:04 dBianchii

@dBianchii this was introduced by this https://github.com/andrewdoro/create-t3-turbo/commit/5db63fbaf5fbfebd1209e89bf345aa1d018c540e ~~@juliusmarminge saw your PR, was thinking about a simpler solution https://github.com/t3-oss/create-t3-turbo/pull/990~~ update: nvm you actually opened the same PR

andrewdoro avatar Apr 12 '24 15:04 andrewdoro