NubeBuster
NubeBuster
Explore posts from servers
TTCTheo's Typesafe Cult
Created by NubeBuster on 5/30/2024 in #questions
Queryraw condition joining in Prisma
This question was also posted in the Prisma Discord but it's been a few days without replies. https://discord.com/channels/937751382725886062/1244937417493053440 I need to use a procedure and then filter on this from a sqlserver database. I am having some issues constructing a query that is dynamic and safe. Prisma.join is only for joining values, but how do I join conditions? What makes it complicated is that the conditions are dynamic filters. Some some may be present or not. Example
const whereClause = []

if (zipCodeLike && zipCodeLike.length > 0) {
const clauses = zipCodeLike.map((_, index) =>
index === 0 ? ` recentZIPCode LIKE ` : ` OR recentZIPCode LIKE `,
);
whereClause.push(Prisma.sql([...clauses, ""], ...zipCodeLike));
}


if (nameLike && nameLike.length > 0) {
const clauses = nameLike.map((_, index) =>
index === 0 ? ` name LIKE ` : ` OR name LIKE `,
);
whereClause.push(Prisma.sql([...clauses, ""], ...nameLike));
}

//How do I construct the where clause into a queryRaw
// in a way that they have brackets and AND between them? I tried explaining ChatGPT that the Prisma.join function is only for values but it cannot wrap it's processor around it. Perhaps what I am trying to do is impossible (and hence maybe bad)
const whereClause = []

if (zipCodeLike && zipCodeLike.length > 0) {
const clauses = zipCodeLike.map((_, index) =>
index === 0 ? ` recentZIPCode LIKE ` : ` OR recentZIPCode LIKE `,
);
whereClause.push(Prisma.sql([...clauses, ""], ...zipCodeLike));
}


if (nameLike && nameLike.length > 0) {
const clauses = nameLike.map((_, index) =>
index === 0 ? ` name LIKE ` : ` OR name LIKE `,
);
whereClause.push(Prisma.sql([...clauses, ""], ...nameLike));
}

//How do I construct the where clause into a queryRaw
// in a way that they have brackets and AND between them? I tried explaining ChatGPT that the Prisma.join function is only for values but it cannot wrap it's processor around it. Perhaps what I am trying to do is impossible (and hence maybe bad)
7 replies
PPrisma
Created by NubeBuster on 5/28/2024 in #help-and-questions
Queryraw condition joining
I need to use a procedure and then filter on this from a sqlserver database. I am having some issues constructing a query that is dynamic and safe. Prisma.join is only for joining values, but how do I join conditions? What makes it complicated is that the conditions are dynamic filters. Some some may be present or not. Example
const whereClause = []

if (zipCodeLike && zipCodeLike.length > 0) {
const clauses = zipCodeLike.map((_, index) =>
index === 0 ? ` recentZIPCode LIKE ` : ` OR recentZIPCode LIKE `,
);
whereClause.push(Prisma.sql([...clauses, ""], ...zipCodeLike));
}


if (nameLike && nameLike.length > 0) {
const clauses = nameLike.map((_, index) =>
index === 0 ? ` name LIKE ` : ` OR name LIKE `,
);
whereClause.push(Prisma.sql([...clauses, ""], ...nameLike));
}

//How do I construct the where clause into a queryRaw
// in a way that they have brackets and AND between them? I tried explaining ChatGPT that the Prisma.join function is only for values but it cannot wrap it's processor around it. Perhaps what I am trying to do is impossible (and hence maybe bad)
const whereClause = []

if (zipCodeLike && zipCodeLike.length > 0) {
const clauses = zipCodeLike.map((_, index) =>
index === 0 ? ` recentZIPCode LIKE ` : ` OR recentZIPCode LIKE `,
);
whereClause.push(Prisma.sql([...clauses, ""], ...zipCodeLike));
}


if (nameLike && nameLike.length > 0) {
const clauses = nameLike.map((_, index) =>
index === 0 ? ` name LIKE ` : ` OR name LIKE `,
);
whereClause.push(Prisma.sql([...clauses, ""], ...nameLike));
}

//How do I construct the where clause into a queryRaw
// in a way that they have brackets and AND between them? I tried explaining ChatGPT that the Prisma.join function is only for values but it cannot wrap it's processor around it. Perhaps what I am trying to do is impossible (and hence maybe bad)
10 replies
TTCTheo's Typesafe Cult
Created by NubeBuster on 3/1/2024 in #questions
Using uploadthing for serving static files
https://youtu.be/mi9IDtgS6hc?si=KKqLTxWOf9L6hf8Y&t=441 In this video Theo said that for serving static mp3 files uploadthing is a good option to prevent unnecessary bandwidth usage for the highly available cdn of vercel. I've just uploaded my static mp3 files, which are loaded by every visitor, to uploadthing. Now I run into the problem that I have no way to list the files in order to use them. I am considering making a seperate node 'project' folder which I will use to upload the background video to uploadthing automatically when I run a script to process the files using ffmpeg. The current video files count 42 files in m4s format for streaming video. Now what would be a good way to automatically upload them and then be able to serve them like so:
<video><source src="/background/simple.mpd" type="application/dash+xml" /></video>
<video><source src="/background/simple.mpd" type="application/dash+xml" /></video>
The .mpd is the main file that AFAIK defines which files are part of the stream. I consider writing the links for these files into a file in the /public/ folder of my next project so that it's accessible to the users. How would I then adjust my code to properly serve the files in a way that nextjs understands it?
2 replies
TTCTheo's Typesafe Cult
Created by NubeBuster on 7/30/2023 in #questions
How does vercel run my code? Is my cache code working as intended?
You can use something for a long time yet not know what you're working with. I just came to a realization that something I am doing may not be correct:
type Object = {
//some data
};

const CACHE_DURATION = 60 * 60 * 1000;
let cache: { timestamp: number; data: Object[] } | null = null;

export async function getObjects() {
const currentTime = Date.now();

if (cache && currentTime - cache.timestamp < CACHE_DURATION)
return cache.data;

const objects = await new Promise<Object[]>((resolve, reject) => {
https.get("https://example.com/", (res: IncomingMessage) => {
let data = "";

res.on("data", (chunk: string) => {
data += chunk;
});

res.on("end", () => {
const root = parse(data);
const servers = root.querySelectorAll("tr.server-list__row");

const objects: Object[] = [];

servers.forEach((server) => {
const data = server.querySelector("foo");
objects.push({
//data
});
});

objects.sort();

resolve(objects);
});

res.on("error", (error: Error) => {
reject(
new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: error.message,
})
);
});
});
});

cache = { timestamp: currentTime, data: worlds };
return objects;
}

export const someRouter = createTRPCRouter({
list: protectedProcedure.query(async () => {
return await getObjects();
}),
});
type Object = {
//some data
};

const CACHE_DURATION = 60 * 60 * 1000;
let cache: { timestamp: number; data: Object[] } | null = null;

export async function getObjects() {
const currentTime = Date.now();

if (cache && currentTime - cache.timestamp < CACHE_DURATION)
return cache.data;

const objects = await new Promise<Object[]>((resolve, reject) => {
https.get("https://example.com/", (res: IncomingMessage) => {
let data = "";

res.on("data", (chunk: string) => {
data += chunk;
});

res.on("end", () => {
const root = parse(data);
const servers = root.querySelectorAll("tr.server-list__row");

const objects: Object[] = [];

servers.forEach((server) => {
const data = server.querySelector("foo");
objects.push({
//data
});
});

objects.sort();

resolve(objects);
});

res.on("error", (error: Error) => {
reject(
new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: error.message,
})
);
});
});
});

cache = { timestamp: currentTime, data: worlds };
return objects;
}

export const someRouter = createTRPCRouter({
list: protectedProcedure.query(async () => {
return await getObjects();
}),
});
This is one of my routers. It fetches data from an external source. The goal is to cache this data so I don't have to fetch it each time my clients send a request. But I am starting to understand that serverless stuff doesn't work that way... I am using T3 and hosting on vercel. Does this code make any sense? Or should I use something like redis for this?
3 replies
TTCTheo's Typesafe Cult
Created by NubeBuster on 6/24/2023 in #questions
Continuous process while server is running
With NextJs my definition of a server is being challenged. I see a server as a program that can serve requests and do background tasks. With nextjs it seems to only do the former. I have a web application where users can manage instances of a program on several machines. The machines run a kotlin program that manages the machine processes of the program instances locally. I want this kotlin program to communicate with the T3 server backend to listen for requests such as making a screenshot. I was thinking of doing this with a websocket. When the user opens the /instances web page, they will see the running instances of the program on the machines. I want the user to be able to click on Screenshot button and then the backend notifies the kotlin programs that a screenshot needs to be made. The kotlin program makes the screenshot and then uploads it to the backend. Then the image can be served to the user. If I put this in root.ts
const wss: WebSocketServer = new WebSocketServer({ port: 8080 });

wss.on("connection", (ws) => {
console.log("test");
ws.on("message", (message: string) => {
console.log(`Received message => ${message}`);
ws.send(`Received your message: ${message}`);
});

ws.send("Hello! Message from server!!");
});

wss.on("error", console.error);
const wss: WebSocketServer = new WebSocketServer({ port: 8080 });

wss.on("connection", (ws) => {
console.log("test");
ws.on("message", (message: string) => {
console.log(`Received message => ${message}`);
ws.send(`Received your message: ${message}`);
});

ws.send("Hello! Message from server!!");
});

wss.on("error", console.error);
this code runs each time a web page is visited or something. Instead I want this to be called when the T3 server starts and then keep running. Is that possible? Is that correct usage of NextJs? Should I take a whole different approach?
6 replies