sujjeee
DTDrizzle Team
•Created by sujjeee on 3/28/2024 in #help
Querying User Data Response Format
Currently, I'm querying user data using the following code:
However, I want the response to be in the format like this:
Please suggest if anyone knows how I can achieve this.
const res = await db
.select({
users,
emails
})
.from(users)
.leftJoin(workspaces, eq(workspaces.id, users.workspace_id))
.leftJoin(templates, eq(templates.workspace_id, users.workspace_id))
.leftJoin(
emails,
or(
eq(emails.user_email, users.email),
eq(emails.template_id, templates.id),
),
)
.where(
and(eq(users.id, user_id), eq(users.workspace_id, workspace_id)),
);
const res = await db
.select({
users,
emails
})
.from(users)
.leftJoin(workspaces, eq(workspaces.id, users.workspace_id))
.leftJoin(templates, eq(templates.workspace_id, users.workspace_id))
.leftJoin(
emails,
or(
eq(emails.user_email, users.email),
eq(emails.template_id, templates.id),
),
)
.where(
and(eq(users.id, user_id), eq(users.workspace_id, workspace_id)),
);
However, I want the response to be in the format like this:
{
user: {...},
emails: [{}, {}, ...]
}
{
user: {...},
emails: [{}, {}, ...]
}
1 replies
DTDrizzle Team
•Created by sujjeee on 1/27/2024 in #help
Internal error: Error: There is not enough information to infer relation "users.contacts"
Hey can someone please help me with this error
this is my schmea
but when i do like this
im getting this error
⨯ Internal error: Error: There is not enough information to infer relation "users.contacts"
please help me !!
export const userRole = pgEnum("userRole", ["admin", "user"]);
export const users = pgTable("users", {
id: uuid("id").defaultRandom().primaryKey(),
email: text("email").unique().notNull(),
avatar_url: text("avatar_url"),
first_name: text("first_name").notNull(),
last_name: text("last_name").notNull(),
job_title: text("job_title"),
department: text("department"),
slug: text("slug").notNull(),
about_title: text("about_title"),
about: varchar("about", { length: 100 }),
created_at: timestamp("created_at").defaultNow(),
workspace_id: uuid("workspace_id").references(() => workspaces.id, {
onDelete: "cascade",
onUpdate: "cascade"
})
});
export const usersRelations = relations(users, ({ one, many }) => ({
permission: one(permissions, {
fields: [users.email],
references: [permissions.user_email]
}),
contacts: many(contacts,{
relationName: 'users_contacts',
})
}));
export const contactType = pgEnum(....);
export const contacts = pgTable("contacts", {
id: uuid("id").defaultRandom().primaryKey(),
type: contactType("type").default("phone").notNull(),
value: text("value").notNull(),
title: text("title").notNull(),
created_at: timestamp("created_at").defaultNow(),
user_email: text("user_email")
.references(() => users.email, {
onDelete: "cascade",
onUpdate: "cascade"
})
.unique()
.notNull()
});
export const contactsRelations = relations(contacts, ({ many }) => ({
users: many(users)
}));
export const userRole = pgEnum("userRole", ["admin", "user"]);
export const users = pgTable("users", {
id: uuid("id").defaultRandom().primaryKey(),
email: text("email").unique().notNull(),
avatar_url: text("avatar_url"),
first_name: text("first_name").notNull(),
last_name: text("last_name").notNull(),
job_title: text("job_title"),
department: text("department"),
slug: text("slug").notNull(),
about_title: text("about_title"),
about: varchar("about", { length: 100 }),
created_at: timestamp("created_at").defaultNow(),
workspace_id: uuid("workspace_id").references(() => workspaces.id, {
onDelete: "cascade",
onUpdate: "cascade"
})
});
export const usersRelations = relations(users, ({ one, many }) => ({
permission: one(permissions, {
fields: [users.email],
references: [permissions.user_email]
}),
contacts: many(contacts,{
relationName: 'users_contacts',
})
}));
export const contactType = pgEnum(....);
export const contacts = pgTable("contacts", {
id: uuid("id").defaultRandom().primaryKey(),
type: contactType("type").default("phone").notNull(),
value: text("value").notNull(),
title: text("title").notNull(),
created_at: timestamp("created_at").defaultNow(),
user_email: text("user_email")
.references(() => users.email, {
onDelete: "cascade",
onUpdate: "cascade"
})
.unique()
.notNull()
});
export const contactsRelations = relations(contacts, ({ many }) => ({
users: many(users)
}));
const workspaceUsers = await db.query.users.findMany({
where: eq(users.workspace_id, loggedWorkspace.data.id),
with: {
permission:true,
contacts: true
}
});
const workspaceUsers = await db.query.users.findMany({
where: eq(users.workspace_id, loggedWorkspace.data.id),
with: {
permission:true,
contacts: true
}
});
5 replies