Anas Badran
Anas Badran
Explore posts from servers
DTDrizzle Team
Created by Anas Badran on 9/19/2024 in #help
Need help building up a query based on nested fields.
I need some help to query the chat that has only the two members only private chat based on the schema below.
3 replies
DTDrizzle Team
Created by Anas Badran on 9/13/2024 in #help
Query based on nested relation
I need help modifying the query code to properly query the chat where it has the the two members.
export const findOrCreateChat = async (fromID: number, toID: number) => {
const existingchat = await db.query.chats.findFirst({
with: { members: true },
where(fields, { and, eq }) {
// the line below needs fix.
return and(eq(chatMembers.userID, fromID), eq(chatMembers.userID, toID));
},

});
if (existingchat) {
return existingchat;
}

const newChat = await db
.insert(chats)
.values({
name: `new chat`,
})
.returning();

await db.insert(chatMembers).values([
{ chatID: newChat[0].id, userID: fromID },
{ chatID: newChat[0].id, userID: toID },
]);

return newChat[0];
};
export const findOrCreateChat = async (fromID: number, toID: number) => {
const existingchat = await db.query.chats.findFirst({
with: { members: true },
where(fields, { and, eq }) {
// the line below needs fix.
return and(eq(chatMembers.userID, fromID), eq(chatMembers.userID, toID));
},

});
if (existingchat) {
return existingchat;
}

const newChat = await db
.insert(chats)
.values({
name: `new chat`,
})
.returning();

await db.insert(chatMembers).values([
{ chatID: newChat[0].id, userID: fromID },
{ chatID: newChat[0].id, userID: toID },
]);

return newChat[0];
};
4 replies
DTDrizzle Team
Created by Anas Badran on 8/25/2024 in #help
Nested queries
Based on the provided schema, how to react the query to fetch first 25 posts a long with their comments and reactions, and each comment have it's own reaction, anyone have any ideas?
export const now = () => sql<Date>`now()`

export const post = pgTable('posts', {
id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at')
.defaultNow()
.$onUpdate(now),
title: varchar('name', { length: 255 }).notNull(),
content: text('content').notNull()
})


export const reaction = pgTable('reactions', {
id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
createdAt: timestamp('created_at').defaultNow().notNull(),
type: ReactionType('type').notNull(),
key: varchar('key', { length: 255 }).notNull()
})

export const comment = pgTable('comments', {
id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
createdAt: timestamp('created_at').defaultNow().notNull(),
content: text('content').notNull(),
postsID: integer('post_id').references(() => post.id, { onDelete: 'cascade' }).notNull()
})
export const now = () => sql<Date>`now()`

export const post = pgTable('posts', {
id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at')
.defaultNow()
.$onUpdate(now),
title: varchar('name', { length: 255 }).notNull(),
content: text('content').notNull()
})


export const reaction = pgTable('reactions', {
id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
createdAt: timestamp('created_at').defaultNow().notNull(),
type: ReactionType('type').notNull(),
key: varchar('key', { length: 255 }).notNull()
})

export const comment = pgTable('comments', {
id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
createdAt: timestamp('created_at').defaultNow().notNull(),
content: text('content').notNull(),
postsID: integer('post_id').references(() => post.id, { onDelete: 'cascade' }).notNull()
})
2 replies
DTDrizzle Team
Created by Anas Badran on 8/24/2024 in #help
Inferring types for nested objects
I need help on how to setup the types for the data returned so I can get correct autocomplete:
4 replies
DTDrizzle Team
Created by Anas Badran on 8/23/2024 in #help
Storing Images
Hi everyone, I just want to know what is the best way to store images according to these info about the system I'm building. - a user needs to have a profile picture. - I need to store a number of images for each user. - a user can create a post, and the post may have an image. - The hero section of the app may have a dynamic image that the admin can change. - I need to store a number of images for each activity 'activity is a table to hold some info about an activity' created.
14 replies
PPrisma
Created by Anas Badran on 8/23/2024 in #help-and-questions
Image storing
Hi everyone, I just want to know what is the best way to store images according to these info about the system I'm building. - a user needs to have a profile picture. - I need to store a number of images for each user. - a user can create a post, and the post may have an image. - The hero section of the app may have a dynamic image that the admin can change. - I need to store a number of images for each activity 'activity is a table to hold some info about an activity' created.
2 replies
DTDrizzle Team
Created by Anas Badran on 8/17/2024 in #help
conditional unique constraint
I want each person to have only one primary phone number, but also have as many as they want non-primary phone numbers
export const personPhone = pgTable(
'person_phone',
{
id: uuid('id').primaryKey().defaultRandom(),
isPrimary: boolean('is_primary').default(false),
personID: uuid('person_id')
.references(() => person.id, { onDelete: 'cascade' })
.notNull(),
phoneID: uuid('phone_id')
.references(() => phone.id, { onDelete: 'cascade' })
.notNull(),
},
(table) => ({
// pk: primaryKey({ columns: [table.personID, table.phoneID] }),
// uniquePrimary: unique().on(table.isPrimary, table.personID),
})
);
export const personPhone = pgTable(
'person_phone',
{
id: uuid('id').primaryKey().defaultRandom(),
isPrimary: boolean('is_primary').default(false),
personID: uuid('person_id')
.references(() => person.id, { onDelete: 'cascade' })
.notNull(),
phoneID: uuid('phone_id')
.references(() => phone.id, { onDelete: 'cascade' })
.notNull(),
},
(table) => ({
// pk: primaryKey({ columns: [table.personID, table.phoneID] }),
// uniquePrimary: unique().on(table.isPrimary, table.personID),
})
);
21 replies
PPrisma
Created by Anas Badran on 8/11/2024 in #help-and-questions
unique constraints
How to tell prisma that I want only one image with isPrimary set to true but also allow any number of images with isPrimary set to false for the same drugItem?
model Image {
id String @id @default(auto()) @map("_id") @db.ObjectId
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
publicID String @unique
width Int
height Int
url String
isPrimary Boolean @default(false)
drugItemID String? @db.ObjectId
drugItem DrugItem? @relation(fields: [drugItemID], references: [id], onDelete: Cascade)
}
model Image {
id String @id @default(auto()) @map("_id") @db.ObjectId
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
publicID String @unique
width Int
height Int
url String
isPrimary Boolean @default(false)
drugItemID String? @db.ObjectId
drugItem DrugItem? @relation(fields: [drugItemID], references: [id], onDelete: Cascade)
}
4 replies
PPrisma
Created by Anas Badran on 6/23/2024 in #help-and-questions
One of two fields (or both) must be provided.
how to defina a constraint that one of two fields (or both) must be provided but they can't be both null
2 replies
PPrisma
Created by Anas Badran on 5/19/2024 in #help-and-questions
Unable to run the seed script successfully
The seed script
import { db } from '@/app/lib/database';
import * as data from '@/app/lib/placeholder-data';

async function main() {
for (const c of data.customers) {
await db.customer.create({ data: c });
}
for (const i of data.invoices) {
await db.invoice.create({ data: i });
}
for (const r of data.revenue) {
await db.revenue.create({ data: r });
}
for (const u of data.users) {
await db.user.create({ data: u });
}
}
main();
console.log('the database has seeded successfully');
import { db } from '@/app/lib/database';
import * as data from '@/app/lib/placeholder-data';

async function main() {
for (const c of data.customers) {
await db.customer.create({ data: c });
}
for (const i of data.invoices) {
await db.invoice.create({ data: i });
}
for (const r of data.revenue) {
await db.revenue.create({ data: r });
}
for (const u of data.users) {
await db.user.create({ data: u });
}
}
main();
console.log('the database has seeded successfully');
the package.json seed script
"seed": "ts-node prisma/seed.ts"
"seed": "ts-node prisma/seed.ts"
7 replies