moroshko
moroshko
Explore posts from servers
DTDrizzle Team
Created by moroshko on 6/8/2024 in #help
How to type an optional included relation?
I'd like to implement a function that given an email returns a user. The function also gets an optional includeContacts boolean. When true, all user contacts should be returned as well. Here is what I currently have:
async function getUserByEmail(email: string, options?: { includeContacts: true }) {
const includeContacts = options?.includeContacts ?? false;
const user = await db.query.usersTable.findFirst({
where: eq(usersTable.email, email),
with: includeContacts ? { contacts: true } : undefined,
});

return user ?? null;
}
async function getUserByEmail(email: string, options?: { includeContacts: true }) {
const includeContacts = options?.includeContacts ?? false;
const user = await db.query.usersTable.findFirst({
where: eq(usersTable.email, email),
with: includeContacts ? { contacts: true } : undefined,
});

return user ?? null;
}
The problem is that user contacts are not included in the returned type. Ideally, contacts will be a non-optional field in the returned type when includeContacts is true. Is this possible to achieve? Here is my schema:
export const usersTable = pgTable("users", {
email: text("email").notNull().unique().primaryKey(),
getHeadsUp: boolean("get_heads_up").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const contactsTable = pgTable("contacts", {
id: text("id")
.primaryKey()
.$defaultFn(() => `ct_${crypto.randomUUID()}`),
userEmail: text("user_email").notNull(),
name: text("name").notNull(),
email: text("email").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const usersRelations = relations(usersTable, ({ many }) => ({
contacts: many(contactsTable),
}));

export const contactsRelations = relations(contactsTable, ({ one }) => ({
user: one(usersTable, {
fields: [contactsTable.userEmail],
references: [usersTable.email],
}),
}));
export const usersTable = pgTable("users", {
email: text("email").notNull().unique().primaryKey(),
getHeadsUp: boolean("get_heads_up").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const contactsTable = pgTable("contacts", {
id: text("id")
.primaryKey()
.$defaultFn(() => `ct_${crypto.randomUUID()}`),
userEmail: text("user_email").notNull(),
name: text("name").notNull(),
email: text("email").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const usersRelations = relations(usersTable, ({ many }) => ({
contacts: many(contactsTable),
}));

export const contactsRelations = relations(contactsTable, ({ one }) => ({
user: one(usersTable, {
fields: [contactsTable.userEmail],
references: [usersTable.email],
}),
}));
2 replies
DTDrizzle Team
Created by moroshko on 8/7/2023 in #help
How to separate Dev data from Prod data with one Postgres instance?
I'd love to use Drizzle with Vercel's Postgres Storage (Neon). On the Hobby plan, Vercel offers only one Postgres instance. I realize it's not a Drizzle specific question, but would love to know what's the easiest/best practice these days to separate Dev data from Prod data if all I have is a single Postgres instance? Any references to publicly available code examples would be hugely appreciated 🙏
1 replies