iolyd
iolyd
DTDrizzle Team
Created by iolyd on 6/6/2024 in #help
Drizzle-zod errors
Yeah, I should've posted how I fixed it for posterity. It was also due to a circular reference on my side where I was importing and using a reference to a table column in a file that also exported constants used to define said table column. Honestly the logs were of 0 help here.
4 replies
DTDrizzle Team
Created by xxxxx on 5/2/2024 in #help
add a prefix to UUID?
For a schema level implementation, you could probably get it working with something along the lines of
const products = pgTable('products', {
id: text('id').primaryKey().default(sql`CONCAT('product_', REPLACE(uuid_generate_v4()::text, '-', '' ))`)
})
const products = pgTable('products', {
id: text('id').primaryKey().default(sql`CONCAT('product_', REPLACE(uuid_generate_v4()::text, '-', '' ))`)
})
8 replies
DTDrizzle Team
Created by iolyd on 1/20/2024 in #help
Less verbose generic types for helper functions
You can look at the source and copy what you want (or you can install the package but I'm still changing things quite a lot so i'd say its a bit unstable)
10 replies
DTDrizzle Team
Created by iolyd on 1/20/2024 in #help
Less verbose generic types for helper functions
Yeah, i made a package with various type / query helpers to help me reuse some wrappers across projects: https://github.com/iolyd/drizzle-orm-helpers
10 replies
DTDrizzle Team
Created by iolyd on 11/2/2023 in #help
Get subquery columns in a way similar to `getTableColumns`
Good question, I personally didn't end up having cases where I had to use it, but I still think it could be useful for others.
12 replies
DTDrizzle Team
Created by James on 1/17/2024 in #help
Extract type from SubQuery
Couldn't agree more, imho the typing system currently is excessively hermetic, inconsistent across query types, and poorly documented. With earlier versions I even faced situations where accessing certain types required using Symbols that drizzle didn't even expose. But I have to say that now things are getting better, and the fact you can achieve almost anything with sql tagged template literals is super appreciated on my part.
24 replies
DTDrizzle Team
Created by James on 1/17/2024 in #help
Extract type from SubQuery
@Gary, el Pingüino Artefacto Those are some types I made, sorry I forgot to include them!
type InferSQLDataType<T extends SQL | SQL.Aliased> =
T extends SQL<infer U> ? U : T extends SQL.Aliased<infer U> ? U : never;

type InferColumnDataType<T extends Column> = T['_']['notNull'] extends true
? T['_']['data']
: T['_']['data'] | null;

type InferRecordDataTypes<T extends Record<string, Column | SQL | SQL.Aliased>> = {
[K in keyof T]: T[K] extends SQL | SQL.Aliased
? InferSQLDataType<T[K]>
: T[K] extends Column
? InferColumnDataType<T[K]>
: never;
};
type InferSQLDataType<T extends SQL | SQL.Aliased> =
T extends SQL<infer U> ? U : T extends SQL.Aliased<infer U> ? U : never;

type InferColumnDataType<T extends Column> = T['_']['notNull'] extends true
? T['_']['data']
: T['_']['data'] | null;

type InferRecordDataTypes<T extends Record<string, Column | SQL | SQL.Aliased>> = {
[K in keyof T]: T[K] extends SQL | SQL.Aliased
? InferSQLDataType<T[K]>
: T[K] extends Column
? InferColumnDataType<T[K]>
: never;
};
24 replies
DTDrizzle Team
Created by James on 1/17/2024 in #help
Extract type from SubQuery
With modified types for the jsonAgg helper I can confirm passing subqueries should work as expected:
export function jsonAgg<T extends Table | Column | Subquery | AnyPgSelect>(
selection: T,
{ notNull = true }: { notNull?: boolean } = {}
): SQL<
T extends Table
? InferSelectModel<T>
: T extends Column
? InferColumnDataType<T>[]
: T extends Subquery
? InferRecordDataTypes<T['_']['selectedFields']>[]
: T extends AnyPgSelect
? Awaited<T>
: never
> {
if (notNull) {
return sql`json_agg(${selection}) filter (where ${selection} is not null)`;
}
return sql`json_agg(${selection})`;
}
export function jsonAgg<T extends Table | Column | Subquery | AnyPgSelect>(
selection: T,
{ notNull = true }: { notNull?: boolean } = {}
): SQL<
T extends Table
? InferSelectModel<T>
: T extends Column
? InferColumnDataType<T>[]
: T extends Subquery
? InferRecordDataTypes<T['_']['selectedFields']>[]
: T extends AnyPgSelect
? Awaited<T>
: never
> {
if (notNull) {
return sql`json_agg(${selection}) filter (where ${selection} is not null)`;
}
return sql`json_agg(${selection})`;
}
Note that I haven't tested queries with aggregated non-aliased subqueries (Any[dialect]Select) through drizzle, so I don't know if it's valid that I also handle these. But db.select().from().as() subqueries work without problem!
24 replies
DTDrizzle Team
Created by iolyd on 11/2/2023 in #help
Get subquery columns in a way similar to `getTableColumns`
Personally my vote goes to striving for making things less specific and providing a single helper. This PR should answer most use case: https://github.com/drizzle-team/drizzle-orm/pull/1789
12 replies
DTDrizzle Team
Created by James on 1/17/2024 in #help
Extract type from SubQuery
In this case, the generic type of jsonAgg would need to be expanded a bit to account for Subquery
24 replies
DTDrizzle Team
Created by James on 1/17/2024 in #help
Extract type from SubQuery
Hard to test without a db I can hit, but I feel something like this should work:
const subArticles = db
.select({
id: articlesTable.id,
name: articlesTable.name,
content: articlesTable.content,
})
.from(articlesTable)
.where(eq(articlesTable.authorId, authorsTable.id))
.as('sub_articles');

const articlesByAuthors = await db
.select({ id: authorsTable.id, name: authorsTable.name, articles: jsonAgg(subArticles) })
.from(authorsTable);
const subArticles = db
.select({
id: articlesTable.id,
name: articlesTable.name,
content: articlesTable.content,
})
.from(articlesTable)
.where(eq(articlesTable.authorId, authorsTable.id))
.as('sub_articles');

const articlesByAuthors = await db
.select({ id: authorsTable.id, name: authorsTable.name, articles: jsonAgg(subArticles) })
.from(authorsTable);
24 replies
DTDrizzle Team
Created by iolyd on 1/20/2024 in #help
Less verbose generic types for helper functions
With simplified types:
export function getSubqueryConfig<S extends Subquery>(subquery: S) {
return (subquery as Record<string, unknown>)[SubqueryConfig as unknown as string] as S['_'];
}
export function getSubqueryConfig<S extends Subquery>(subquery: S) {
return (subquery as Record<string, unknown>)[SubqueryConfig as unknown as string] as S['_'];
}
10 replies
DTDrizzle Team
Created by iolyd on 1/20/2024 in #help
Less verbose generic types for helper functions
Step one to help work with subqueries for me is to provide a config getter:
/**
* Get a subquery's config in a manner similar to `getTableConfig`. Useful to retrieve a subquery's
* alias or fields.
*/
export function getSubqueryConfig<S extends ColumnsSelection, A extends string>(
subquery: WithSubqueryWithSelection<S, A> | SubqueryWithSelection<S, A>
) {
return subquery[SubqueryConfig as unknown as string] as (typeof subquery)['_'];
}
/**
* Get a subquery's config in a manner similar to `getTableConfig`. Useful to retrieve a subquery's
* alias or fields.
*/
export function getSubqueryConfig<S extends ColumnsSelection, A extends string>(
subquery: WithSubqueryWithSelection<S, A> | SubqueryWithSelection<S, A>
) {
return subquery[SubqueryConfig as unknown as string] as (typeof subquery)['_'];
}
10 replies
DTDrizzle Team
Created by iolyd on 1/20/2024 in #help
Less verbose generic types for helper functions
Do you have an example of wrapper function you'd plan on working with? It could maybe help me sketch out more robust type helpers
10 replies
DTDrizzle Team
Created by James on 1/17/2024 in #help
Extract type from SubQuery
@Angelelz do you know if there's a plan for drizzle to provide such helpers of their own?
24 replies
DTDrizzle Team
Created by James on 1/17/2024 in #help
Extract type from SubQuery
Small warning if you go this route: generic types can get a bit messy when you want helpers to handle not only table arguments but also subqueries (Subquery | SubqueryWithSelection | WithSubqueryWithSelection and etc.)
24 replies
DTDrizzle Team
Created by James on 1/17/2024 in #help
Extract type from SubQuery
You could also build sql helpers with generic types for the various sql functions you use to avoid you the trouble of manually typing things every time you want to do things like json_agg, row_to_json, etc. I'm using something like this in a project of mine, maybe this could help?
export function jsonAgg<T extends AnyTable<TableConfig> | AnyColumn>(
selection: T,
{ notNull = true }: { notNull?: boolean } = {}
) {
type R = T extends AnyTable<TableConfig>
? InferSelectModel<T>
: T extends AnyColumn
? InferColumnDataType<T>
: T;
if (notNull) {
return sql<R[] | null>`json_agg(${selection}) filter (where ${selection} is not null)`;
}
return sql<R[] | null>`json_agg(${selection})`;
}
export function jsonAgg<T extends AnyTable<TableConfig> | AnyColumn>(
selection: T,
{ notNull = true }: { notNull?: boolean } = {}
) {
type R = T extends AnyTable<TableConfig>
? InferSelectModel<T>
: T extends AnyColumn
? InferColumnDataType<T>
: T;
if (notNull) {
return sql<R[] | null>`json_agg(${selection}) filter (where ${selection} is not null)`;
}
return sql<R[] | null>`json_agg(${selection})`;
}
24 replies
DTDrizzle Team
Created by iolyd on 11/23/2023 in #help
Malformed migration file
Ah nvm, I think its related to the fact I was using a pkg patch for https://github.com/drizzle-team/drizzle-orm/issues/636 and haven't repatched after updating so the patched-in schemaTo props are unexpected in the metadata
5 replies
DTDrizzle Team
Created by iolyd on 11/2/2023 in #help
Get subquery columns in a way similar to `getTableColumns`
12 replies