json column type mismatch when using db.query

Hey all i have a schema with a json column, when I import the table records the type is inferred as 'unknown'. Any ideas on how to get this working, or do I have to infer the types manually? Thank you!
No description
3 Replies
Jim
JimOP13mo ago
No description
Jim
JimOP13mo ago
ohhh I think i need to use the helper: .$type<{ foo: string }>() https://orm.drizzle.team/docs/column-types/mysql#json
MySQL column types - Drizzle ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Jim
JimOP13mo ago
Still having issues:
const getRecords = async ({ limit = 10 }: { limit?: number } = {}) => {
const records: SelectRecordType[] = await db.query.record.findMany({
limit
});

return records;
};
const getRecords = async ({ limit = 10 }: { limit?: number } = {}) => {
const records: SelectRecordType[] = await db.query.record.findMany({
limit
});

return records;
};
Gives me a long typescript error complaining about the json column being mismatched this is how I define my types and schema:
export const record = mysqlTable("record", {
id: serial("id").primaryKey(),
idSlug: varchar("id_slug", { length: 32 }).notNull().unique(),
createdAt: timestamp("created_at").defaultNow().notNull(),
lastModified: timestamp("last_modified").defaultNow().onUpdateNow(),
refNo: varchar("ref_no", { length: 32 }),
breakdown: json("breakdown"),
});

export const InsertRecord = createInsertSchema(record);
export const SelectRecord = createSelectSchema(record);
export type InsertRecordType = z.infer<typeof InsertRecord>;
export type SelectRecordType = z.infer<typeof SelectRecord>;
export const record = mysqlTable("record", {
id: serial("id").primaryKey(),
idSlug: varchar("id_slug", { length: 32 }).notNull().unique(),
createdAt: timestamp("created_at").defaultNow().notNull(),
lastModified: timestamp("last_modified").defaultNow().onUpdateNow(),
refNo: varchar("ref_no", { length: 32 }),
breakdown: json("breakdown"),
});

export const InsertRecord = createInsertSchema(record);
export const SelectRecord = createSelectSchema(record);
export type InsertRecordType = z.infer<typeof InsertRecord>;
export type SelectRecordType = z.infer<typeof SelectRecord>;
I'll cast the type for now using as
Want results from more Discord servers?
Add your server