균어
균어
Explore posts from servers
DTDrizzle Team
Created by 균어 on 3/15/2024 in #help
how to get generic table type
I tried to use AnyPgTable type to make dynamic function
async findById(id: number, table: AnyPgTable) {
const res = await this.db.select().from(table).where(eq(table.id, id));

return res.at(0);
}
async findById(id: number, table: AnyPgTable) {
const res = await this.db.select().from(table).where(eq(table.id, id));

return res.at(0);
}
typescript: Property 'id' does not exist on type 'AnyPgTable'. [2339]
typescript: Property 'id' does not exist on type 'AnyPgTable'. [2339]
but i need to have id type all tables have serial id column
id: serial('id').primaryKey(),
id: serial('id').primaryKey(),
2 replies
DTDrizzle Team
Created by 균어 on 3/5/2024 in #help
How to get findMany with count?
I have following query
const result = await this.db.query.serviceOrders.findMany({
where: and(...wheres),
with: {
vehicle: {
with: {
make: true,
model: true,
},
},
customer: true,
branch: {
columns: {
name: true,
},
},
},
orderBy: [desc(serviceOrders.startedDate)],
});
const result = await this.db.query.serviceOrders.findMany({
where: and(...wheres),
with: {
vehicle: {
with: {
make: true,
model: true,
},
},
customer: true,
branch: {
columns: {
name: true,
},
},
},
orderBy: [desc(serviceOrders.startedDate)],
});
and have following relation
export const serviceOrdersRelation = relations(
serviceOrders,
({ one, many }) => ({
vehicle: one(vehicles, {
fields: [serviceOrders.vehicleId],
references: [vehicles.id],
}),
customer: one(customers, {
fields: [serviceOrders.customerId],
references: [customers.id],
}),
branch: one(branches, {
fields: [serviceOrders.branchId],
references: [branches.id],
}),
serviceOrderItems: many(serviceOrderItems),
payments: many(serviceOrderPayments),
}),
);
export const serviceOrdersRelation = relations(
serviceOrders,
({ one, many }) => ({
vehicle: one(vehicles, {
fields: [serviceOrders.vehicleId],
references: [vehicles.id],
}),
customer: one(customers, {
fields: [serviceOrders.customerId],
references: [customers.id],
}),
branch: one(branches, {
fields: [serviceOrders.branchId],
references: [branches.id],
}),
serviceOrderItems: many(serviceOrderItems),
payments: many(serviceOrderPayments),
}),
);
how to get serviceOrders with serviceOrderItems count?
9 replies
DTDrizzle Team
Created by 균어 on 2/9/2024 in #help
PgTable type how to get at least have id field?
i want to make common crud service in nest js how to set PgTable type to have id field?
import { Inject, Injectable } from '@nestjs/common';
import { PgInsertValue, PgTable } from 'drizzle-orm/pg-core';
import { DB, DBType } from './drizzle.provider';
import { eq } from 'drizzle-orm';

@Injectable()
export class DrizzleService<T extends PgTable> {
constructor(
@Inject(DB) private readonly db: DBType,
private readonly table: T,
) { }

async create(
input: PgInsertValue<T>,
): Promise<typeof this.table.$inferSelect> {
const [res] = await this.db.insert(this.table).values(input).returning();

return res;
}

async update(id: number, input: Partial<PgInsertValue<T>>) {
const [res] = await this.db
.update(this.table)
.set(input)
.where(eq(this.table.id, id))
.returning();
}
}
import { Inject, Injectable } from '@nestjs/common';
import { PgInsertValue, PgTable } from 'drizzle-orm/pg-core';
import { DB, DBType } from './drizzle.provider';
import { eq } from 'drizzle-orm';

@Injectable()
export class DrizzleService<T extends PgTable> {
constructor(
@Inject(DB) private readonly db: DBType,
private readonly table: T,
) { }

async create(
input: PgInsertValue<T>,
): Promise<typeof this.table.$inferSelect> {
const [res] = await this.db.insert(this.table).values(input).returning();

return res;
}

async update(id: number, input: Partial<PgInsertValue<T>>) {
const [res] = await this.db
.update(this.table)
.set(input)
.where(eq(this.table.id, id))
.returning();
}
}
2 replies
DTDrizzle Team
Created by 균어 on 11/24/2023 in #help
Postgresql Join select columns with case
const res = await db.select({
...getTableColumns(table1),
items: table2
}).from(table1).leftJoin(table2, eq(table1.id, table2.mainId))
const res = await db.select({
...getTableColumns(table1),
items: table2
}).from(table1).leftJoin(table2, eq(table1.id, table2.mainId))
in this case i want to get table2 tables with
db.select({
...getTableColumns(table2),
partNumber: sql<string>`case when ${products.id} is not null then ${products.partNumber} else ${inventoryItems.partNumber} end`,
name: sql<string>`case when ${products.id} is not null then ${products.nameEn} else ${inventoryItems.name} end`,
}).from(table2)
db.select({
...getTableColumns(table2),
partNumber: sql<string>`case when ${products.id} is not null then ${products.partNumber} else ${inventoryItems.partNumber} end`,
name: sql<string>`case when ${products.id} is not null then ${products.nameEn} else ${inventoryItems.name} end`,
}).from(table2)
like this how to do this?
8 replies
TTCTheo's Typesafe Cult
Created by 균어 on 6/18/2023 in #questions
How to get singular type on trpc query
const serviceOrders = api.serviceOrders.getAll.useQuery({}); const [selectedServiceOrder, setSelectedServiceOrder] = useState<(typeof serviceOrders.data)[0]>(); it says typescript: Property '0' does not exist on type '(ServiceOrder & { vehicle: Vehicle & { model: { name: string; }; }; })[] | undefined'. [2339]
9 replies