nk
nk
DTDrizzle Team
Created by nk on 6/27/2024 in #help
SelectionMap with nested objects
Hi, I'm trying to re-create the db.query({ with: {} }) but it's not working with nested objects because I don't know the type. For example:
// Note the "any", which should be the type I'm missing.
type SelectionMap<T extends ProductSelect> = {
[K in keyof T]: T[K] extends true ? { [key: string]: any } : {};
};

export namespace ProductSearchService {
function getSelect<T extends ProductSelect>(select: T): SelectionMap<T> {
return {
...(select.test
? {
testA: {
id: testTable.id,
name: testTable.name,
},
testB: {
id: testTable2.id,
name: testTable2.name,
},
}
: {}),
// Note the "any", which should be the type I'm missing.
type SelectionMap<T extends ProductSelect> = {
[K in keyof T]: T[K] extends true ? { [key: string]: any } : {};
};

export namespace ProductSearchService {
function getSelect<T extends ProductSelect>(select: T): SelectionMap<T> {
return {
...(select.test
? {
testA: {
id: testTable.id,
name: testTable.name,
},
testB: {
id: testTable2.id,
name: testTable2.name,
},
}
: {}),
the product.testA will work, but product.testA.id will not, which is what I'm trying to fix. I imagine there is a helper for this?
1 replies
DTDrizzle Team
Created by nk on 5/19/2024 in #help
column "roles" is of type integer[] but default expression is of type integer
roles: integer("roles")
.array()
.$type<UserRole[]>()
.default([UserRole.Member]),
roles: integer("roles")
.array()
.$type<UserRole[]>()
.default([UserRole.Member]),
I don't understand what the problem is. It's clearly an array, right?
2 replies
DTDrizzle Team
Created by nk on 1/29/2024 in #help
Dynamic select with types?
Does anyone know how to do this correctly? Example:
async t<T>(select: Something) {
return db.select(select).from(table);
}

const data: { id: number; }[] = t<{ id: number; }[]>({ id: table.id });
async t<T>(select: Something) {
return db.select(select).from(table);
}

const data: { id: number; }[] = t<{ id: number; }[]>({ id: table.id });
16 replies
DTDrizzle Team
Created by nk on 1/27/2024 in #help
Question
export const db = drizzle(pg, {
schema: {
...userSchema,
/// ... like 15+ other schemas.
},
});
export const db = drizzle(pg, {
schema: {
...userSchema,
/// ... like 15+ other schemas.
},
});
Is this how you're supposed to do it? Or should I export the schema and use separate instances. It almost seems like using an instance for each one of my instances was faster by a ms or 2 than doing it like this, which is why I'm curious.
5 replies
DTDrizzle Team
Created by nk on 1/20/2024 in #help
Using "which" to check an alias isn't working.
// missing FROM-clause entry for table "notSubcategory"
search?.categoryId ? eq(notSubcategoryTable.categoryId, search.categoryId) : undefined,
// works fine
search?.subcategoryId
? eq(productTable.categoryId, search.subcategoryId)
: undefined,
// missing FROM-clause entry for table "notSubcategory"
search?.categoryId ? eq(notSubcategoryTable.categoryId, search.categoryId) : undefined,
// works fine
search?.subcategoryId
? eq(productTable.categoryId, search.subcategoryId)
: undefined,
...(select?.category
? {
subcategory: categoryTable,
category: notSubcategoryTable,
}
: {}),
...(select?.category
? {
subcategory: categoryTable,
category: notSubcategoryTable,
}
: {}),
if (select?.category) {
query.leftJoin(
categoryTable,
eq(productTable.categoryId, categoryTable.id)
);
query.leftJoin(
notSubcategoryTable,
eq(notSubcategoryTable.id, categoryTable.categoryId)
);

groupBy.push(notSubcategoryTable.id);
groupBy.push(categoryTable.id);
}
if (select?.category) {
query.leftJoin(
categoryTable,
eq(productTable.categoryId, categoryTable.id)
);
query.leftJoin(
notSubcategoryTable,
eq(notSubcategoryTable.id, categoryTable.categoryId)
);

groupBy.push(notSubcategoryTable.id);
groupBy.push(categoryTable.id);
}
Am I missing something?
9 replies
DTDrizzle Team
Created by nk on 1/19/2024 in #help
Recursive category/subcategories, can't infer relation
No description
6 replies
DTDrizzle Team
Created by nk on 1/15/2024 in #help
$dynamic with multiple groupBy statements
Is there a way to do this? It seems that using more than one is overwriting the previous one. Example:
function withAGroupBy<T extends PgSelect>(query: T) {
return query
.leftJoin(...)
.groupBy(something1.id);
}
function withASecondGroupBy<T extends PgSelect>(query: T) {
return query
.leftJoin(...)
.groupBy(something2.id);
}

query = something.$dynamic;
withAGroupBy(query);
withASecondGroupBy(query);
function withAGroupBy<T extends PgSelect>(query: T) {
return query
.leftJoin(...)
.groupBy(something1.id);
}
function withASecondGroupBy<T extends PgSelect>(query: T) {
return query
.leftJoin(...)
.groupBy(something2.id);
}

query = something.$dynamic;
withAGroupBy(query);
withASecondGroupBy(query);
6 replies