Json as a string

I currently have this schema:
export const decks = mysqlTable("decks", {
uuid: char("uuid", { length: 36 }).primaryKey(),
name: varchar("name", { length: 25 }).notNull(),
cards: json("cards").$type<Card[]>().default([]).notNull(),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});

export const ZodCard = z.object({
question: z.string("question").max(255).min(6),
answer: z.string("answer").max(255).min(6),
})
export type Card = z.infer<typeof ZodCard>;
export const decks = mysqlTable("decks", {
uuid: char("uuid", { length: 36 }).primaryKey(),
name: varchar("name", { length: 25 }).notNull(),
cards: json("cards").$type<Card[]>().default([]).notNull(),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});

export const ZodCard = z.object({
question: z.string("question").max(255).min(6),
answer: z.string("answer").max(255).min(6),
})
export type Card = z.infer<typeof ZodCard>;
When I get the set of cards from a deck as such:
db.query.decks.findFirst({
where: ((deck, { eq }) => eq(deck.uuid, input.uuid)),
}).then((deck) => deck?.cards || []))
db.query.decks.findFirst({
where: ((deck, { eq }) => eq(deck.uuid, input.uuid)),
}).then((deck) => deck?.cards || []))
It returns this string: "[]". It's $type<Card[]>() supposed to serialize and deserialize it for me?
1 Reply
StillLutto
StillLuttoOP4mo ago
It's even weirder as when I type deck.cards in my code editor (zed), it does show the Card object.

Did you find this page helpful?