AdamRackis
AdamRackis
Explore posts from servers
DTDrizzle Team
Created by AdamRackis on 9/16/2023 in #help
Possible to select* off of Joined table?
You're too fast - yep - I discovered that also. Was about to add that info here. Thanks so much!
5 replies
DTDrizzle Team
Created by AdamRackis on 9/15/2023 in #help
InferModelFromColumns with columns defined with sql``
😄
15 replies
DTDrizzle Team
Created by AdamRackis on 9/15/2023 in #help
InferModelFromColumns with columns defined with sql``
Got it! I was close with my InferSelection<T>. Here's how you do it
type ExtractTypeFromMySqlColumn<T extends MySqlColumn> = T extends MySqlColumn<infer U>
? U extends { notNull: true }
? U["data"]
: U["data"] | null
: never;

type ExtractSqlType<T> = T extends MySqlColumn<infer U, object> ? ExtractTypeFromMySqlColumn<T> : T extends SQL.Aliased<infer V> ? V : never;
type InferSelection<T> = {
[K in keyof T]: ExtractSqlType<T[K]>;
};
type ExtractTypeFromMySqlColumn<T extends MySqlColumn> = T extends MySqlColumn<infer U>
? U extends { notNull: true }
? U["data"]
: U["data"] | null
: never;

type ExtractSqlType<T> = T extends MySqlColumn<infer U, object> ? ExtractTypeFromMySqlColumn<T> : T extends SQL.Aliased<infer V> ? V : never;
type InferSelection<T> = {
[K in keyof T]: ExtractSqlType<T[K]>;
};
Which yields this, copied from the type's intellisense
type Test = {
id: number;
tags: number[];
subjects: number[];
title: string;
pages: number | null;
userId: string;
authors: string[] | null;
isbn: string | null;
publisher: string | null;
publicationDate: string | null;
isRead: number;
dateAdded: Date;
mobileImage: string | null;
mobileImagePreview: unknown;
smallImage: string | null;
smallImagePreview: unknown;
mediumImage: string | null;
mediumImagePreview: unknown;
}
type Test = {
id: number;
tags: number[];
subjects: number[];
title: string;
pages: number | null;
userId: string;
authors: string[] | null;
isbn: string | null;
publisher: string | null;
publicationDate: string | null;
isRead: number;
dateAdded: Date;
mobileImage: string | null;
mobileImagePreview: unknown;
smallImage: string | null;
smallImagePreview: unknown;
mediumImage: string | null;
mediumImagePreview: unknown;
}
15 replies
DTDrizzle Team
Created by AdamRackis on 9/15/2023 in #help
InferModelFromColumns with columns defined with sql``
This seems to work. It's not pretty, but it seems to work. Unfortunately the resulting type is a union, the first member of which has a bunch of nevers. But it looks like the subsequent unions (which intellisense does not reveal) make the end result correct
type This = ArrayOf<Awaited<ReturnType<MySqlSelectBuilder<typeof defaultBookFields, any>["from"]>>>;
type This = ArrayOf<Awaited<ReturnType<MySqlSelectBuilder<typeof defaultBookFields, any>["from"]>>>;
15 replies
DTDrizzle Team
Created by AdamRackis on 9/15/2023 in #help
InferModelFromColumns with columns defined with sql``
That's a the type helper I'm trying to write to do what's described in the question. Ie, that's my attempted solution of this
15 replies
DTDrizzle Team
Created by AdamRackis on 9/15/2023 in #help
InferModelFromColumns with columns defined with sql``
Ohhhhh I think that would work. Very clever! Incidentally, any idea why this doesn't work. This feels like it should
type InferSelection<T> = T extends MySqlColumn<infer U, object> ? U["data"] : T extends SQL.Aliased<infer V> ? V : never;
type InferSelection<T> = T extends MySqlColumn<infer U, object> ? U["data"] : T extends SQL.Aliased<infer V> ? V : never;
But I get bizarre ts errors like
Type 'MySqlColumn<{ name: "id"; tableName: "books"; dataType: "number"; columnType: "MySqlInt"; data: number; driverParam: string | number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, object>' does not satisfy the constraint 'Table<TableConfig<Column<any, object, object>>>'. Type 'MySqlColumn<{ name: "id"; tableName: "books"; dataType: "number"; columnType: "MySqlInt"; data: number; driverParam: string | number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, object>' is missing the following properties from type 'Table<TableConfig<Column<any, object, object>>>': $inferSelect, $inferInsert, [IsDrizzleTable]
15 replies
DTDrizzle Team
Created by AdamRackis on 9/15/2023 in #help
InferModelFromColumns with columns defined with sql``
I don't think (afaik) you can evaluate expressions like that, in type space. I got as far as this
type X = ReturnType<typeof db.select<typeof defaultBookFields>>;
type Y = ReturnType<X["from"]>;
type FulBook = ArrayOf<Awaited<ReturnType<Y["execute"]>>>;
type X = ReturnType<typeof db.select<typeof defaultBookFields>>;
type Y = ReturnType<X["from"]>;
type FulBook = ArrayOf<Awaited<ReturnType<Y["execute"]>>>;
but the resulting type has a bunch of never types on it. In fact, everything is never except for my tags and subjects, presumably because I never passed a generic type to from but I'm not sure how to do that, and
type Z = ReturnType<Y<typeof books>
type Z = ReturnType<Y<typeof books>
doesn't work (even if I remove the ReturnType from Y)
15 replies