Replies: 7 comments
-
Yep, we plan to add the "Advanced" section to the docs where we'd explain all the user-facing types and how to use them. Thanks! |
Beta Was this translation helpful? Give feedback.
-
I just came here because this tripped me up. I'm still pretty new to TypeScript and didn't know how to use the Say I have... export const videos = sqliteTable(
'videos',
{
id: integer('id').primaryKey({ autoIncrement: true }),
videoId: text('videoId').notNull(),
title: text('title').notNull(),
createdAt: integer('createdAt', { mode: 'timestamp' }).defaultNow().notNull(),
},
(videos) => ({
nameIdx: uniqueIndex('videoIdx').on(videos.videoId),
})
);
export type Video = InferModel<typeof videos>; ...and the following wrapper: export const addVideo = (data: Video) => db.insert(videos).values(data).run(); Then I couldn't figure out how to call the wrapper without addVideo({
videoId: uuidv4(),
title: lorem.generateWords(randomNumber(20, 6)),
} as Video); |
Beta Was this translation helpful? Give feedback.
-
@jschuur try to use export type Video = InferModel<typeof videos, 'insert'>; It will generated needed types for |
Beta Was this translation helpful? Give feedback.
-
how to you infermodel with relations ? |
Beta Was this translation helpful? Give feedback.
-
Since there is no advanced documentation yet, does anybody have a hint regarding the types of the parameters of the mentioned functions in the meantime?
This would be great! |
Beta Was this translation helpful? Give feedback.
-
On the enhancement backlog - see #695. |
Beta Was this translation helpful? Give feedback.
-
Here is what I used for const model = mysqlTable(...)
type ModelColumns = keyof InferSelectModel<typeof modal>
function get(orderBy: ModelColumns, orderDir: "asc" | "desc") {
db.query.model.findMany({
orderBy: (model, {asc, desc}) => orderDir === "asc" ? asc(model[orderBy]) : desc(model[orderBy])
})
} |
Beta Was this translation helpful? Give feedback.
-
I spent the past 6 weeks trying out over a dozen different ORMs in the typescript world. They all have their pros and cons, but Drizzle seems to be one of the best when it comes to type safety. However, those strong types are wasted because there's basically no documentation on how to use them in a more powerful way.
You should declare what types you need for passing around different parts of Drizzle:
import type { MySql2Database } from 'drizzle-orm/mysql2'
, but I'm only guessingYou should also be able to declare what types you need for the major part of query building, so that queries can be built programatically. For example
import type { SQL } from 'drizzle-orm/sql'
SQL
is part of it, but there's certainly morenumber
number
Beta Was this translation helpful? Give feedback.
All reactions