getById from current user

Hi, its my first time using tRCP and I would like to ask how I can get something by id from the current user.
import { z } from "zod";

import {
createTRPCRouter,
publicProcedure,
protectedProcedure,
} from "~/server/api/trpc";


export const depositRouter = createTRPCRouter({
// Get all deposits from the current user
getAll: protectedProcedure
.query(async ({ ctx }) => {
const deposits = await ctx.prisma.deposit.findMany({
where: {
userId: ctx.session.user.id
},
});
return deposits;
}),

// Get a deposit by id from the current user
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const deposit = await ctx.prisma.deposit.findUnique({
where: {
id: input.id,
},
});
return deposit;
}),
});
import { z } from "zod";

import {
createTRPCRouter,
publicProcedure,
protectedProcedure,
} from "~/server/api/trpc";


export const depositRouter = createTRPCRouter({
// Get all deposits from the current user
getAll: protectedProcedure
.query(async ({ ctx }) => {
const deposits = await ctx.prisma.deposit.findMany({
where: {
userId: ctx.session.user.id
},
});
return deposits;
}),

// Get a deposit by id from the current user
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const deposit = await ctx.prisma.deposit.findUnique({
where: {
id: input.id,
},
});
return deposit;
}),
});
23 Replies
Diogo
Diogo2y ago
your where is wrong you also need to add userId: ctx.session.user.id currently he gets the deposit from a specific id, if you want to get from a specific id and userID ( in this case the one logged in ) you need to add ctx.session.user.id but you actually dont even need to use the current user, if the id is unique, you just query it, dont need to specify a user, since it already belongs to that user only
Ayato
Ayato2y ago
OK, but how would I do it with ctx.session.user.id?
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const deposit = await ctx.prisma.deposit.findUnique({
where: {
id: input.id,
userId: ctx.session.user.id
},
});
return deposit;
}),
});
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const deposit = await ctx.prisma.deposit.findUnique({
where: {
id: input.id,
userId: ctx.session.user.id
},
});
return deposit;
}),
});
Because this is giving me an error:
Type '{ id: string; userId: string; }' is not assignable to type 'DepositWhereUniqueInput'.
Object literal may only specify known properties, and 'userId' does not exist in type 'DepositWhereUniqueInput'.ts(2322)
Type '{ id: string; userId: string; }' is not assignable to type 'DepositWhereUniqueInput'.
Object literal may only specify known properties, and 'userId' does not exist in type 'DepositWhereUniqueInput'.ts(2322)
Diogo
Diogo2y ago
do you have userID on your schema? on the deposit model
Ayato
Ayato2y ago
yes it works for the getAll
model Deposit {
id String @id @default(cuid())
userId String
deposit_type depositTypes
name String
description String?
balance Float
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
expenses Expense[]
revenues Revenue[]
fromTransfers Transfer[] @relation("from_Deposit")
toTransfers Transfer[] @relation("to_Deposit")

@@index([userId])
}
model Deposit {
id String @id @default(cuid())
userId String
deposit_type depositTypes
name String
description String?
balance Float
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
expenses Expense[]
revenues Revenue[]
fromTransfers Transfer[] @relation("from_Deposit")
toTransfers Transfer[] @relation("to_Deposit")

@@index([userId])
}
but as I said, getAll works fine
Ayato
Ayato2y ago
Diogo
Diogo2y ago
try this, where: { AND: [ { id: input.id }, { userId: ctx.session.user.id }, ], },
Ayato
Ayato2y ago
now AND is the problem:
Diogo
Diogo2y ago
ohhh wait i get it now its the findunnique it only accepts 1 search paramter so it will never let you use more than 1
Ayato
Ayato2y ago
ohh, but this btw. is also not working:
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const deposit = await ctx.prisma.deposit.findUnique({
where: {
userId: ctx.session.user.id
},
});
return deposit;
}),
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const deposit = await ctx.prisma.deposit.findUnique({
where: {
userId: ctx.session.user.id
},
});
return deposit;
}),
maybe because it is not nnique
Diogo
Diogo2y ago
yeah, bc there is more than 1 deposit and findUnique purpose it to find only 1 since there are multiple deposits with the same userID it will not work
Ayato
Ayato2y ago
do you think this would do?
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const where = {
id: input.id,
userId: ctx.session.user.id,
};
const deposit = await ctx.prisma.deposit.findUnique({
where,
});
return deposit;
}),
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const where = {
id: input.id,
userId: ctx.session.user.id,
};
const deposit = await ctx.prisma.deposit.findUnique({
where,
});
return deposit;
}),
Diogo
Diogo2y ago
but if you use the deposit id it will work everytime
Ayato
Ayato2y ago
it is not giving me an error
Diogo
Diogo2y ago
you can try but i dont think it will
Diogo
Diogo2y ago
i suggest this simply doing this: getById: protectedProcedure .input(z.object({ id: z.string() })) .query(async ({ ctx, input }) => { const deposit = await ctx.prisma.deposit.findUnique({ where: { id: input.id, }, }); return deposit; }), it will always get the one specified
Ayato
Ayato2y ago
Yea, but theoretical if you have the ID you can get the deposit from a another user right?
Diogo
Diogo2y ago
no, bc you are using the deposits ID that ID is unique to that deposit and that deposit is associated to 1 user only
Ayato
Ayato2y ago
alright, thanks for your help @Diogovski
Diogo
Diogo2y ago
you're welcome
Ayato
Ayato2y ago
OK, I checked it and it do is like I thaght: server:
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const deposit = await ctx.prisma.deposit.findUnique({
where: {
id: input.id,
},
});
return deposit;
}),
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const deposit = await ctx.prisma.deposit.findUnique({
where: {
id: input.id,
},
});
return deposit;
}),
client:
const { data } = api.deposit.getById.useQuery({id: "clg2ppm4u0000n5fstlpwqb2f"});
console.log(data);
const { data } = api.deposit.getById.useQuery({id: "clg2ppm4u0000n5fstlpwqb2f"});
console.log(data);
I'm logged in as user 1 but the deposit.id is of a deposit from another user. I think I will just go with this, if you have any better ideas please tell me:
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const deposit = await ctx.prisma.deposit.findFirst({
where: {
id: input.id,
userId: ctx.session.user.id,
},
});
return deposit;
}),
getById: protectedProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }) => {
const deposit = await ctx.prisma.deposit.findFirst({
where: {
id: input.id,
userId: ctx.session.user.id,
},
});
return deposit;
}),
Diogo
Diogo2y ago
but thats bc you let the other user know the deposit id from a different user which you shouldnt do
Ayato
Ayato2y ago
yes ofc, but it feels a but sloppy, just because I know the id of a privat massage of yours I don't think I could request it. ---------------------- CLOSE ---------------------- https://discord.com/channels/966627436387266600/1093273688557768924/1093273688557768924
Want results from more Discord servers?
Add your server