Type safety on JSONB fields from PostgreSQL #386
DenisBessa
started this conversation in
Show and tell
Replies: 3 comments
-
I wonder if it possible to take this idea to the next level. Something like this: schema.ts: // zod just as an example - it can be any suitable schema
const metaSchema = z.object({
username: z.string(),
});
export const document = sqliteTable("documents", {
meta: json("meta", metaSchema), And then query could look like this: const data = await db.select({ username: document.meta.username })
// or with special operator to avoid conflicts with other methods/fields
// kind of reminds `json_extract` syntax '$.username'
const data = await db.select({ username: document.meta.$.username })
// still can be used as any other column
const data = await db.select({ meta: document.meta }) PS |
Beta Was this translation helpful? Give feedback.
0 replies
-
I have made a few improvements on the function that generates a type-safe JSONB field on Drizzle. Here they are:
Here's how to use it:
(you need to define the type of the column in the schema definition with $type function). |
Beta Was this translation helpful? Give feedback.
0 replies
-
The same for MySQL json fields would be fantastic to have 😍 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I really like Drizzle, and I think I managed to solve the problem to get nested values from JSONB fields in a type-safe way.
Here is my code:
and I can use it like this:
Beta Was this translation helpful? Give feedback.
All reactions