Нарбек
Нарбек
DTDrizzle Team
Created by Нарбек on 6/27/2024 in #help
How to combine 'union' and 'orderBy'
const a = db.select({ transaction: onlineSales.transactionId }).from(onlineSales);
const b = db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales);
const result = await unionAll(a, b).orderBy( across resulting rows ??? );
const a = db.select({ transaction: onlineSales.transactionId }).from(onlineSales);
const b = db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales);
const result = await unionAll(a, b).orderBy( across resulting rows ??? );
9 replies
DTDrizzle Team
Created by Нарбек on 6/3/2024 in #help
How to get count with main query
Hi. Can i combine these two queries?
const data = await db
.select({ id: S.appointment.id })
.from(S.appointment)
.orderBy(desc(S.appointment.createdAt))
.limit(perPage)
.offset(perPage * (page - 1));

const resultCount = await db.select({ value: count() }).from(S.appointment);
const data = await db
.select({ id: S.appointment.id })
.from(S.appointment)
.orderBy(desc(S.appointment.createdAt))
.limit(perPage)
.offset(perPage * (page - 1));

const resultCount = await db.select({ value: count() }).from(S.appointment);
1 replies
DTDrizzle Team
Created by Нарбек on 2/23/2024 in #help
Long names for relations are not supported?
here is all relations:
export const medMembersExaminationsRelations = relations(medMembersExaminations, ({many, one}) => ({
longNameLongNameLongName: one(medCommissionMembers, {
fields: [medMembersExaminations.medCommissionMemberId],
references: [medCommissionMembers.id]
})
}))

export const medCommissionMembersRelations = relations(medCommissionMembers, ({ one, many }) => ({
users: one(users, { fields: [medCommissionMembers.userId], references: [users.id] }),
medMemberExaminations: many(medMembersExaminations),
}));

export const usersRelations = relations(users, ({ many }) => ({
userOrganizations: many(userOrganizations),
medCommissionMembers: many(medCommissionMembers),
}));

export const userOrganizationsRelations = relations(userOrganizations, ({ one }) => ({
organization: one(organizations, {
fields: [userOrganizations.organizationId],
references: [organizations.id],
}),
user: one(users, {
fields: [userOrganizations.userId],
references: [users.id],
}),
}));
export const medMembersExaminationsRelations = relations(medMembersExaminations, ({many, one}) => ({
longNameLongNameLongName: one(medCommissionMembers, {
fields: [medMembersExaminations.medCommissionMemberId],
references: [medCommissionMembers.id]
})
}))

export const medCommissionMembersRelations = relations(medCommissionMembers, ({ one, many }) => ({
users: one(users, { fields: [medCommissionMembers.userId], references: [users.id] }),
medMemberExaminations: many(medMembersExaminations),
}));

export const usersRelations = relations(users, ({ many }) => ({
userOrganizations: many(userOrganizations),
medCommissionMembers: many(medCommissionMembers),
}));

export const userOrganizationsRelations = relations(userOrganizations, ({ one }) => ({
organization: one(organizations, {
fields: [userOrganizations.organizationId],
references: [organizations.id],
}),
user: one(users, {
fields: [userOrganizations.userId],
references: [users.id],
}),
}));
Here is my request
await db.query.medMembersExaminations.findMany({
with: {
longNameLongNameLongName: {
with: {
users: {
with: {
userOrganizations: {
with: { organization: true },
},
},
},
},
},
},
});
await db.query.medMembersExaminations.findMany({
with: {
longNameLongNameLongName: {
with: {
users: {
with: {
userOrganizations: {
with: { organization: true },
},
},
},
},
},
},
});
I got this error: column medMembersExaminations_longNameLongNameLongName_users_userOrgan.organization_id does not exist But when i use short names, like this:
export const medMembersExaminationsRelations = relations(medMembersExaminations, ({many, one}) => ({
short: one(medCommissionMembers, {
fields: [medMembersExaminations.medCommissionMemberId],
references: [medCommissionMembers.id]
})
}))
export const medMembersExaminationsRelations = relations(medMembersExaminations, ({many, one}) => ({
short: one(medCommissionMembers, {
fields: [medMembersExaminations.medCommissionMemberId],
references: [medCommissionMembers.id]
})
}))
It works. Can someone explain me pls?
2 replies
DTDrizzle Team
Created by Нарбек on 1/12/2024 in #help
How to put all leftJoins in a var
to get data and count i have to duplicate leftJoins. I can put all conditions in a var for where clause, but i don't know how to be with leftJoins? example:
if (opts.input.orderId) {
conditions.push(eq(orders.id, opts.input.orderId));
}

const data = await db
.select()
.from(orders)
.where(and(...conditions))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.orderBy(desc(orders.createdAt))
.limit(perPage)
.offset(perPage * (page - 1));

const count = await db
.select({ count: sql`count('*')`.mapWith(Number) })
.from(orders)
.where(and(...conditions))
.where(and(...conditions))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
if (opts.input.orderId) {
conditions.push(eq(orders.id, opts.input.orderId));
}

const data = await db
.select()
.from(orders)
.where(and(...conditions))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.orderBy(desc(orders.createdAt))
.limit(perPage)
.offset(perPage * (page - 1));

const count = await db
.select({ count: sql`count('*')`.mapWith(Number) })
.from(orders)
.where(and(...conditions))
.where(and(...conditions))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
2 replies
DTDrizzle Team
Created by Нарбек on 1/11/2024 in #help
all property
hi. im getting this error. Property 'all' does not exist on type 'Omit<PgSelectBase<"
15 replies