Tobias
Tobias
Explore posts from servers
DTDrizzle Team
Created by Tobias on 5/23/2024 in #help
Two tables that references each other
No description
3 replies
DTDrizzle Team
Created by Tobias on 4/12/2024 in #help
nullsNotDistinct on composite unique
I have the following table
export const customersTable = pgTable("customers", {
id: serial('id').primaryKey(),
first_name: text('first_name'),
last_name: text('last_name'),
email: text('email'),
tenant_id: integer('tenant_id').references(() => tenantsTable.id, { onDelete: 'cascade' }).notNull()
}, (t) => ({
unq: unique().on(t.email, t.tenant_id).nullsNotDistinct()
}));
export const customersTable = pgTable("customers", {
id: serial('id').primaryKey(),
first_name: text('first_name'),
last_name: text('last_name'),
email: text('email'),
tenant_id: integer('tenant_id').references(() => tenantsTable.id, { onDelete: 'cascade' }).notNull()
}, (t) => ({
unq: unique().on(t.email, t.tenant_id).nullsNotDistinct()
}));
My wish is to be able to add how many customers as possible to a specific tenant_id with null email and no constraint. But only one customer per email and per tenant should be allowed. The above solution doesn't work and I want to be able to specifically target "nullsNotDisticint" on the email column only and not the whole composite unique key. How can I solve this?
1 replies
DTDrizzle Team
Created by Tobias on 3/6/2024 in #help
drizzle-kit push:pg and sql commands
I have a schema that I can easily push to my PostgreSQL database. But whenever I have something I want to do outside what I can define within the schema, I need to do a manual sql command in my database. For example I want to run
alter table "table_name" enable row level security;
alter table "table_name" enable row level security;
for my tables, and I want to add that so it is applied when I run drizzle-kit push:pg. Is this possible?
2 replies
DTDrizzle Team
Created by Tobias on 2/16/2024 in #help
Filter on many to many relationship
I want to find all sites where a user is a member in. I have 3 tables that I have setup relations between
export const users = pgTable("users", {
id: text("id").primaryKey(),
name: text("name"),
email: text("email").unique(),
avatarUrl: text('avatar_url')
});

export const sites = pgTable("sites", {
id: serial("id").primaryKey(),
createdAt: timestamp('created_at', {
withTimezone: true
}).defaultNow(),
uuid: uuid('uuid').defaultRandom(),
name: text("name").notNull(),
slug: text("slug").unique().notNull()
});

export const usersRelations = relations(users, ({ many }) => ({
sites: many(siteMembers),
}));

export const sitesRelations = relations(sites, ({ many }) => ({
members: many(siteMembers),
}));

export const siteMembers = pgTable('site_members', {
siteId: integer('site_id').references(() => sites.id, { onDelete: 'cascade' }),
userId: text("user_id").notNull().references(() => users.id, { onDelete: 'cascade' })
}, (table) => {
return {
pk: primaryKey(table.siteId, table.userId),
}

});

export const userSiteMembersRelations = relations(siteMembers, ({ one }) => ({
sites: one(sites, {
fields: [siteMembers.siteId],
references: [sites.id],
}),
user: one(users, {
fields: [siteMembers.userId],
references: [users.id],
}),
}));
export const users = pgTable("users", {
id: text("id").primaryKey(),
name: text("name"),
email: text("email").unique(),
avatarUrl: text('avatar_url')
});

export const sites = pgTable("sites", {
id: serial("id").primaryKey(),
createdAt: timestamp('created_at', {
withTimezone: true
}).defaultNow(),
uuid: uuid('uuid').defaultRandom(),
name: text("name").notNull(),
slug: text("slug").unique().notNull()
});

export const usersRelations = relations(users, ({ many }) => ({
sites: many(siteMembers),
}));

export const sitesRelations = relations(sites, ({ many }) => ({
members: many(siteMembers),
}));

export const siteMembers = pgTable('site_members', {
siteId: integer('site_id').references(() => sites.id, { onDelete: 'cascade' }),
userId: text("user_id").notNull().references(() => users.id, { onDelete: 'cascade' })
}, (table) => {
return {
pk: primaryKey(table.siteId, table.userId),
}

});

export const userSiteMembersRelations = relations(siteMembers, ({ one }) => ({
sites: one(sites, {
fields: [siteMembers.siteId],
references: [sites.id],
}),
user: one(users, {
fields: [siteMembers.userId],
references: [users.id],
}),
}));
Now I am trying to figure out how to query all sites where user with a specific ID is a member in. But when I do the following query, I just get all sites but with an empty array for the sites where the user is not a member in. My goal is to only get the sites where the user is a member in. How can I query this?
await db.query.sites.findMany({
with: {
members: {
where: (members, { eq }) => eq(members.userId, user.id)
},
}
})
await db.query.sites.findMany({
with: {
members: {
where: (members, { eq }) => eq(members.userId, user.id)
},
}
})
4 replies
CDCloudflare Developers
Created by Tobias on 9/7/2023 in #general-help
Cloudflare for SaaS custom domains webhooks for status updates
No description
4 replies
CDCloudflare Developers
Created by Tobias on 7/17/2023 in #pages-help
Adding custom domain with to cloudflare pages with åäö in it (IDN)
8 replies