Evanston
Evanston
DTDrizzle Team
Created by Evanston on 1/4/2025 in #help
Filtering Entire Objects by Related Records in Drizzle ORM Query API
Ok thanks! This was very helpful.
10 replies
DTDrizzle Team
Created by Evanston on 1/4/2025 in #help
Filtering Entire Objects by Related Records in Drizzle ORM Query API
bump
10 replies
DTDrizzle Team
Created by Evanston on 1/4/2025 in #help
Filtering Entire Objects by Related Records in Drizzle ORM Query API
Here is my schema
import { primaryKey, pgTable, serial, text, integer, pgEnum, timestamp } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';

export const sessionTable = pgTable("session", {
id: text("id").primaryKey(),
userId: integer("user_id")
.notNull()
.references(() => userTable.id),
expiresAt: timestamp("expires_at", {
withTimezone: true,
mode: "date"
}).notNull()
});

export const userTable = pgTable('user', {
id: serial('id').primaryKey(),
fname: text('fname').notNull(),
lname: text('lname').notNull(),
email: text('email').notNull().unique(),
password: text('password').notNull(),
});
export const userRelations = relations(userTable, ({ many }) => ({
userToSource: many(userSourceTable),
signups: many(userSignupStatusTable)
}));

export const sourceTable = pgTable("source", {
id: serial('id').primaryKey(),
url: text('url').unique(),
});
export const sourceRelations = relations(sourceTable, ({ many }) => ({
userToSource: many(userSourceTable),
signups: many(signupTable)
}));

export const userSourceTable = pgTable(
'users_to_source',
{
userId: integer('user_id')
.notNull()
.references(() => userTable.id),
sourceId: integer('source_id')
.notNull()
.references(() => sourceTable.id, {onDelete: 'cascade'}),
},
(t) => ({
pk: primaryKey({ columns: [t.userId, t.sourceId] }),
}),
);
export const usersToSourceRelation = relations(userSourceTable, ({ one }) => ({
source: one(sourceTable, {
fields: [userSourceTable.sourceId],
references: [sourceTable.id],
}),
user: one(userTable, {
fields: [userSourceTable.userId],
references: [userTable.id],
}),
}));
import { primaryKey, pgTable, serial, text, integer, pgEnum, timestamp } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';

export const sessionTable = pgTable("session", {
id: text("id").primaryKey(),
userId: integer("user_id")
.notNull()
.references(() => userTable.id),
expiresAt: timestamp("expires_at", {
withTimezone: true,
mode: "date"
}).notNull()
});

export const userTable = pgTable('user', {
id: serial('id').primaryKey(),
fname: text('fname').notNull(),
lname: text('lname').notNull(),
email: text('email').notNull().unique(),
password: text('password').notNull(),
});
export const userRelations = relations(userTable, ({ many }) => ({
userToSource: many(userSourceTable),
signups: many(userSignupStatusTable)
}));

export const sourceTable = pgTable("source", {
id: serial('id').primaryKey(),
url: text('url').unique(),
});
export const sourceRelations = relations(sourceTable, ({ many }) => ({
userToSource: many(userSourceTable),
signups: many(signupTable)
}));

export const userSourceTable = pgTable(
'users_to_source',
{
userId: integer('user_id')
.notNull()
.references(() => userTable.id),
sourceId: integer('source_id')
.notNull()
.references(() => sourceTable.id, {onDelete: 'cascade'}),
},
(t) => ({
pk: primaryKey({ columns: [t.userId, t.sourceId] }),
}),
);
export const usersToSourceRelation = relations(userSourceTable, ({ one }) => ({
source: one(sourceTable, {
fields: [userSourceTable.sourceId],
references: [sourceTable.id],
}),
user: one(userTable, {
fields: [userSourceTable.userId],
references: [userTable.id],
}),
}));
10 replies
DTDrizzle Team
Created by Evanston on 1/4/2025 in #help
Filtering Entire Objects by Related Records in Drizzle ORM Query API
Can you please elaborate on what you mean by this?
10 replies
DTDrizzle Team
Created by Evanston on 1/4/2025 in #help
Filtering Entire Objects by Related Records in Drizzle ORM Query API
Is the current best implementation to just filter out the sources variable after the fact?
10 replies