Doron Torangy
Doron Torangy
DTDrizzle Team
Created by iolyd on 1/20/2024 in #help
Less verbose generic types for helper functions
I don't we are going for the same thing, I'm looking more for a domain specific wrapper functions, where the only generic thing is providing a projection while maintaining type safety, something like so:
async getUsersWithProjection<T extends SelectedFields>(filters: UsersFilters, fields: T) {
const qb = this.db.select(fields).from(users).$dynamic()
return this.withUsersFilter(qb, filters).execute()
}

withPagination<T extends PgSelect>(qb: T, page: number, pageSize: number = 10) {
return qb.limit(pageSize).offset(page * pageSize)
}

private withUsersFilter<T extends PgSelect>(qb: T, filters: UsersFilters) {
const conditions: SQL<unknown>[] = []
if (filters.banned !== undefined) conditions.push(eq(users.banned, filters.banned))
if (filters.deleted !== undefined) conditions.push(eq(users.deleted, filters.deleted))
if (filters.emailVerified !== undefined) conditions.push(eq(users.emailVerified, filters.emailVerified))
if (filters.roles) conditions.push(inArray(users.role, filters.roles))
if (filters.accountSignInMethods) conditions.push(arrayOverlaps(users.accountSignInMethods, filters.accountSignInMethods))
if (filters.textSearch)
conditions.push(
or(
ilike(users.firstName, `%${filters.textSearch}%`),
ilike(users.lastName, `%${filters.textSearch}%`),
ilike(users.email, `%${filters.textSearch}%`)
)!
)
if (filters.orderBy) {
switch (filters.orderBy) {
case UsersOrderByFilter.CreatedAtAsc: {
qb = qb.orderBy(asc(users.createdAt))
break
}
case UsersOrderByFilter.CreatedAtDesc: {
qb = qb.orderBy(desc(users.createdAt))
break
}
}
}

if (conditions.length) qb = qb.where(and(...conditions))
if (filters.page && filters.limit) qb = withPagination(qb, filters.page, filters.limit)
return qb
}
async getUsersWithProjection<T extends SelectedFields>(filters: UsersFilters, fields: T) {
const qb = this.db.select(fields).from(users).$dynamic()
return this.withUsersFilter(qb, filters).execute()
}

withPagination<T extends PgSelect>(qb: T, page: number, pageSize: number = 10) {
return qb.limit(pageSize).offset(page * pageSize)
}

private withUsersFilter<T extends PgSelect>(qb: T, filters: UsersFilters) {
const conditions: SQL<unknown>[] = []
if (filters.banned !== undefined) conditions.push(eq(users.banned, filters.banned))
if (filters.deleted !== undefined) conditions.push(eq(users.deleted, filters.deleted))
if (filters.emailVerified !== undefined) conditions.push(eq(users.emailVerified, filters.emailVerified))
if (filters.roles) conditions.push(inArray(users.role, filters.roles))
if (filters.accountSignInMethods) conditions.push(arrayOverlaps(users.accountSignInMethods, filters.accountSignInMethods))
if (filters.textSearch)
conditions.push(
or(
ilike(users.firstName, `%${filters.textSearch}%`),
ilike(users.lastName, `%${filters.textSearch}%`),
ilike(users.email, `%${filters.textSearch}%`)
)!
)
if (filters.orderBy) {
switch (filters.orderBy) {
case UsersOrderByFilter.CreatedAtAsc: {
qb = qb.orderBy(asc(users.createdAt))
break
}
case UsersOrderByFilter.CreatedAtDesc: {
qb = qb.orderBy(desc(users.createdAt))
break
}
}
}

if (conditions.length) qb = qb.where(and(...conditions))
if (filters.page && filters.limit) qb = withPagination(qb, filters.page, filters.limit)
return qb
}
10 replies
DTDrizzle Team
Created by iolyd on 1/20/2024 in #help
Less verbose generic types for helper functions
Don't have an answer for you but this is a huge deal breaker for me when using drizzle to not have the ability to wrap queries with my own functions So If you find a solution I'll appreciate it if you can share it here 🙂
10 replies
DTDrizzle Team
Created by Hebilicious on 12/21/2023 in #help
Type Helper to select fields
There is a solution here https://discord.com/channels/1043890932593987624/1129395697200939038 but there are still cases where is not perfect
5 replies
DTDrizzle Team
Created by Doron Torangy on 7/14/2023 in #help
Get type for select query?
This works, but I noticed that we lose $dynamic query for some reason.
async getUsersWithProjection<T extends SelectedFields>(filters: UsersFilters, fields: T) {
const qb = this.db.select(fields).from(users).$dynamic()
return this.withUsersFilter(qb, filters).execute()
^ -- The types of '_.dynamic' are incompatible between these types.
Type 'boolean' is not assignable to type 'true'.
}

private withUsersFilter<T extends PgSelect>(qb: T, filters: UsersFilters) {
return qb.where(...)
}
async getUsersWithProjection<T extends SelectedFields>(filters: UsersFilters, fields: T) {
const qb = this.db.select(fields).from(users).$dynamic()
return this.withUsersFilter(qb, filters).execute()
^ -- The types of '_.dynamic' are incompatible between these types.
Type 'boolean' is not assignable to type 'true'.
}

private withUsersFilter<T extends PgSelect>(qb: T, filters: UsersFilters) {
return qb.where(...)
}
6 replies
DTDrizzle Team
Created by Hebilicious on 12/21/2023 in #help
Type Helper to select fields
any news about the solution for this?
5 replies
DTDrizzle Team
Created by Doron Torangy on 7/14/2023 in #help
Get type for select query?
@Dan Kochetov any news about this one?
6 replies
DTDrizzle Team
Created by Doron Torangy on 7/14/2023 in #help
Get type for select query?
Is this question maybe related to this? https://github.com/drizzle-team/drizzle-orm/issues/504 If so where can I find that API layer?
6 replies