Caching a user session with multiple relations (drizzle-orm postgresql)

Hello, I was wondering what would be the best way to handle this problem. I have a user schema and relations made in drizzle like this:
export const user = createTable("user", {
createdAt: timestamp("created_at", { withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
email: text("email").notNull().unique(),
emailVerified: boolean("email_verified").notNull(),
id: text("id").primaryKey(),
image: text("image"),
musicVolume: real("music_volume").default(0.25).notNull(),
name: text("name").notNull(),
role: varchar("role", { length: 255 }).notNull().default("user"),
updatedAt: timestamp("updated_at", { withTimezone: true }).$onUpdate(
() => new Date(),
),
});

export const userRelations = relations(user, ({ one, many }) => ({
accounts: many(account),
guildMembership: one(musicGuildMembers),
tutorialProgress: many(musicTutorialUserProgress),
}));
export const user = createTable("user", {
createdAt: timestamp("created_at", { withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
email: text("email").notNull().unique(),
emailVerified: boolean("email_verified").notNull(),
id: text("id").primaryKey(),
image: text("image"),
musicVolume: real("music_volume").default(0.25).notNull(),
name: text("name").notNull(),
role: varchar("role", { length: 255 }).notNull().default("user"),
updatedAt: timestamp("updated_at", { withTimezone: true }).$onUpdate(
() => new Date(),
),
});

export const userRelations = relations(user, ({ one, many }) => ({
accounts: many(account),
guildMembership: one(musicGuildMembers),
tutorialProgress: many(musicTutorialUserProgress),
}));
To get these values, I used a customSession plugin like this:
customSession(async ({ user, session }) => {
if (!user) {
return { session };
}
const customUser = await db.query.user.findFirst({
where: eq(users.id, user.id),
with: {
guildMembership: {
with: {
guild: true,
},
},
tutorialProgress: true,
},
});
return {
session,
user: {
...customUser,
},
};
}),
],
customSession(async ({ user, session }) => {
if (!user) {
return { session };
}
const customUser = await db.query.user.findFirst({
where: eq(users.id, user.id),
with: {
guildMembership: {
with: {
guild: true,
},
},
tutorialProgress: true,
},
});
return {
session,
user: {
...customUser,
},
};
}),
],
The problem is that, as I could find in other posts, customSession isnt cached by default and in this case I am really only joining tables together based on relations declared above. So I was wondering if there is any recommended approach for that
1 Reply
Ping
Ping2mo ago
in the future we hope to have customSession caching to work, but in the mean time you won't be able to do much for now

Did you find this page helpful?