Get type from select object ?

Is it possible to get the type from my select object ? I know its inferred in the response, which gives me the correct type. But i want to be able also set the type myself so it can be used in other files. const SourceSelectFields = { siteCode: mapboxSources.siteCode, id: mapboxSources.id, type: mapboxSources.type, url: mapboxSources.url, isFunction: mapboxSources.isFunction, tiles: sqljson_group_array(${mapboxSourceTiles.tile}).mapWith( (json: string) => JSON.parse(json) as string[] ), }; type SourceWithField = typeof SourceSelectFields //For instance something like so ?
2 Replies
eXecuteye
eXecuteye2mo ago
I go about it this way:
export const userSelectSchema = createSelectSchema(userTable).pick({ id: true, firstName: true, lastName: true });

export const userSelect = {
id: userTable.id,
firstName: userTable.firstName,
lastName: userTable.lastName,
};

export type User = z.infer<typeof userSelectSchema>;
export const userSelectSchema = createSelectSchema(userTable).pick({ id: true, firstName: true, lastName: true });

export const userSelect = {
id: userTable.id,
firstName: userTable.firstName,
lastName: userTable.lastName,
};

export type User = z.infer<typeof userSelectSchema>;
you can further improve on this by providing the select with the correct type. The following is a type that i put together to get Select/Columns/With typing for by providing a table
// Query type helpers (need to be defined here to avoid circular reference)
type TablesWithRelations = ExtractTablesWithRelations<typeof schema>;

// Select type helpers (need to be defined here to avoid circular reference)
/** Helper types to apply typing to predefined select object (detailedTableSelect: {id: table.id} satisfies DrizzleSelectType<"table">) */
export type DrizzleSelectType<TableName extends keyof TablesWithRelations> = Partial<{
[K in keyof ExtractTablesWithRelations<typeof schema>[TableName]["columns"]]: ExtractTablesWithRelations<typeof schema>[TableName]["columns"][K];
}>;

/** Helper types to apply typing to predefined queryColumns object (detailedTableQueryColumns: {id: true} satisfies QueryColumnType<"table">) */
export type DrizzleQueryColumnType<TableName extends keyof TablesWithRelations> = DBQueryConfig<
"many",
true,
ExtractTablesWithRelations<typeof schema>,
ExtractTablesWithRelations<typeof schema>[TableName]
>["columns"];

/** Helper types to apply typing to predefined queryWith object (detailedTableQueryWith: {id: true} satisfies QueryWithType<"table">) */
export type DrizzleQueryWithType<TableName extends keyof TablesWithRelations> = DBQueryConfig<
"many",
true,
ExtractTablesWithRelations<typeof schema>,
ExtractTablesWithRelations<typeof schema>[TableName]
>["with"];
// Query type helpers (need to be defined here to avoid circular reference)
type TablesWithRelations = ExtractTablesWithRelations<typeof schema>;

// Select type helpers (need to be defined here to avoid circular reference)
/** Helper types to apply typing to predefined select object (detailedTableSelect: {id: table.id} satisfies DrizzleSelectType<"table">) */
export type DrizzleSelectType<TableName extends keyof TablesWithRelations> = Partial<{
[K in keyof ExtractTablesWithRelations<typeof schema>[TableName]["columns"]]: ExtractTablesWithRelations<typeof schema>[TableName]["columns"][K];
}>;

/** Helper types to apply typing to predefined queryColumns object (detailedTableQueryColumns: {id: true} satisfies QueryColumnType<"table">) */
export type DrizzleQueryColumnType<TableName extends keyof TablesWithRelations> = DBQueryConfig<
"many",
true,
ExtractTablesWithRelations<typeof schema>,
ExtractTablesWithRelations<typeof schema>[TableName]
>["columns"];

/** Helper types to apply typing to predefined queryWith object (detailedTableQueryWith: {id: true} satisfies QueryWithType<"table">) */
export type DrizzleQueryWithType<TableName extends keyof TablesWithRelations> = DBQueryConfig<
"many",
true,
ExtractTablesWithRelations<typeof schema>,
ExtractTablesWithRelations<typeof schema>[TableName]
>["with"];
This has som caveats though... (eg: Columns/With types only work for findMany) Hope this helps
BubbleTrouble
BubbleTrouble2mo ago
Great thanks !
Want results from more Discord servers?
Add your server