Glorrin
Glorrin
Explore posts from servers
DTDrizzle Team
Created by Glorrin on 11/23/2023 in #help
need help to optimise a query
[schema in first comment] I want to get an object like this:
const event = {
id: event.id,
name: event.name,
eventCharacters: [{
id: eventCharacter.id,
character: { id: character.id, name: character.name },
countSoftReserves: count(eventCharacterSoftReserved.*)
}]
}
const event = {
id: event.id,
name: event.name,
eventCharacters: [{
id: eventCharacter.id,
character: { id: character.id, name: character.name },
countSoftReserves: count(eventCharacterSoftReserved.*)
}]
}
I have an eventId, and a userId event should only be the one associated with eventId (that's easy) eventCharacters should be filtered with eventCharacters.character.userId = userId This I have no clue on how to do it with findFirst This is not a proper way to make a query but I hope it reprensents what I need I could go the select way but then I will have an array with event.name repeated many times and event characters repeated for each softReserves. I am not sure if I should do 2 or 3 queries and recombined or if only 1 would be fine Is there some kind of good practice guide around ? I tried this:
const raid = await db.query.event.findFirst({
where: eq(event.id, eventId),
with: {
eventCharacters: {
with: {
eventCharacterSoftReserves: { columns: { id: true } }, // I only need the count of that but this is good enough for now
character: { columns: { id: true, name: true } },
},
where: eq(character.userId, userId), // doesn't work, can't figure out how to filter on character
columns: { id: true },
},
},
columns: { id: true, name: true },
});
const raid = await db.query.event.findFirst({
where: eq(event.id, eventId),
with: {
eventCharacters: {
with: {
eventCharacterSoftReserves: { columns: { id: true } }, // I only need the count of that but this is good enough for now
character: { columns: { id: true, name: true } },
},
where: eq(character.userId, userId), // doesn't work, can't figure out how to filter on character
columns: { id: true },
},
},
columns: { id: true, name: true },
});
2 replies
TTCTheo's Typesafe Cult
Created by Glorrin on 11/16/2023 in #questions
server side ref
Is there a way to create a ref of a "use server" component to pass it down to a a client component :
"use server"
// [...]
async function MyServerComponent(){
// useRef doesn't work here
return
<>
<MyClientComponent refs={[ref1, ref2]} />
<Component1 ref={ref1} />
<Component2 ref={ref2} />
</>
}
"use server"
// [...]
async function MyServerComponent(){
// useRef doesn't work here
return
<>
<MyClientComponent refs={[ref1, ref2]} />
<Component1 ref={ref1} />
<Component2 ref={ref2} />
</>
}
6 replies
DTDrizzle Team
Created by Glorrin on 11/10/2023 in #help
Another SSL error on import
I have an existing database and I wanted to try drizzle on it. Database is from a lesser known provider and is impossible to find reliable help to access it, but unfortunately I cannot change it. I want to use drizzle-kit introspect:pg here is my drizzle.config.ts
import { type Config } from "drizzle-kit"

import { env } from "@/env.mjs"

export default {
schema: "./src/server/db/schema.ts",
driver: "pg",
dbCredentials: {
connectionString: `${env.DATABASE_URL}?sslmode=prefer&ssl={"rejectUnauthorized":true, "ca": "${env.DATABASE_CA}"}`,
},
} satisfies Config
import { type Config } from "drizzle-kit"

import { env } from "@/env.mjs"

export default {
schema: "./src/server/db/schema.ts",
driver: "pg",
dbCredentials: {
connectionString: `${env.DATABASE_URL}?sslmode=prefer&ssl={"rejectUnauthorized":true, "ca": "${env.DATABASE_CA}"}`,
},
} satisfies Config
I also tried connectionString: ${env.DATABASE_URL}?ssl{"rejectUnauthorized":true}, and prety much every permutation between those 2 I could find but I always have the same error :
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
^

Error: self-signed certificate in certificate chain
at TLSSocket.onConnectSecure (node:_tls_wrap:1674:34)
at TLSSocket.emit (node:events:515:28)
at TLSSocket._finishInit (node:_tls_wrap:1085:8)
at ssl.onhandshakedone (node:_tls_wrap:871:12) {
code: 'SELF_SIGNED_CERT_IN_CHAIN'
}

Node.js v21.1.0
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
^

Error: self-signed certificate in certificate chain
at TLSSocket.onConnectSecure (node:_tls_wrap:1674:34)
at TLSSocket.emit (node:events:515:28)
at TLSSocket._finishInit (node:_tls_wrap:1085:8)
at ssl.onhandshakedone (node:_tls_wrap:871:12) {
code: 'SELF_SIGNED_CERT_IN_CHAIN'
}

Node.js v21.1.0
I tried the answers from the other SSL posts in this discord but unfortunately nothing I tried worked.
1 replies
TTCTheo's Typesafe Cult
Created by Glorrin on 4/27/2023 in #questions
why do we add prisma in ctx instead of using import ?
As stated in the doc
We include the Prisma Client in Context by default and recommend using this instead of importing it separately in each file.
We include the Prisma Client in Context by default and recommend using this instead of importing it separately in each file.
but there is no explanation Why is it better ? What is the difference ?
8 replies
TTCTheo's Typesafe Cult
Created by Glorrin on 4/6/2023 in #questions
What is the correct order to put files in S3 and update DB ?
I am doing something very simple. I need to update some content which has images. What is the correct order of doing the update: 1) optimistic - send update mutation to api (I am using rest but trpc will replace it soon*) api update the content on db and send back put urls for client - client then use those urls to put stuff it S3 advantage: less api calls disadvantage: not sure if things actualy exist in S3 2) controlled - request put urls - client then use those urls to put stuff it S3 - send update mutations with correct access avantage: less likely that the file doesn't exist in S3 disadvantages: more calls and client could put stuff in S3 and never update db
9 replies
TTCTheo's Typesafe Cult
Created by Glorrin on 2/15/2023 in #questions
Log management for the server
It is the first time I need to implement logging and I am not sure who to ask. There is a 2 kind of informations I want to track : - how long each request take server side - for each user what are the request they make and on which frequency I am not sure if both of those information can be tracked with the same tool. Also should I just implement it myself by adding a line in my database every time I get a request ? I am in the process of migrating my apps so for now I can't use axiom next on the server, at least for a few more weeks I will need to use express still, and axiom send me in a rabbit hole of adding winston, and may morgan ? This seems complicated, and for a problem as common as simple server logs I don't think it should be that complicated, what am I missing ?
1 replies
TTCTheo's Typesafe Cult
Created by Glorrin on 11/5/2022 in #questions
For low computational work should I do it on server or client ?
It probably won't make a difference at low scale. So I probably shouldn't worry too much about this but still. Here is an example to make my question clearer. I want to implement a translated input on my client. I implement a list of dynamic inputs on the client, each row is an object containing the language and the value (2 inputs per row). So client side data is an array of object like this:
type Skill = {
...
description: {value: string; language: string}[]
}
type Skill = {
...
description: {value: string; language: string}[]
}
On DB side I want this array to be a JSON object with the language as key and value as... value.
type Skill = {
...
description: {[language:string]: string}
}
type Skill = {
...
description: {[language:string]: string}
}
Should I format the object on the client and then send it formatted to the server. Or should I send the array unformatted to the server and then transform it server side before writing it to DB ? I guess using client ressources as much as possible makes more sense financialy. Apart from obvious security/critical reasons. When would you guys stop using client ressources to use server instead ?
4 replies
TTCTheo's Typesafe Cult
Created by Glorrin on 9/22/2022 in #questions
GCP SQL via prisma
I got a more or less empty t3-app for test I have tested on 2 databases. postgres on Railway, works perfectly fine postgres on GCP SQL on the second one prisma works fine, I can even do prisma studio and I have no problem accessing data but when I launch the app and I got this error :
tRPC failed on user.users: TRPCError:
Invalid `prisma.user.findMany()` invocation:


Authentication failed against database server at DB_URL, the provided database credentials for DB_USER are not valid.
tRPC failed on user.users: TRPCError:
Invalid `prisma.user.findMany()` invocation:


Authentication failed against database server at DB_URL, the provided database credentials for DB_USER are not valid.
I am probably missing a gcp setting somewhere but why would it work localy via prisma studio but not in nextjs ?
1 replies