AdamRackis
AdamRackis
Explore posts from servers
TTCTheo's Typesafe Cult
Created by AdamRackis on 10/9/2023 in #questions
Call Next api route handlers in from RSC?
Is it possible to call an api route from an RSC in Next? (assuming you're NOT using SSG) Tim's comments in here seem to imply not, but that seems incredible, if true. https://github.com/vercel/next.js/issues/49578
1 replies
DTDrizzle Team
Created by AdamRackis on 9/16/2023 in #help
Possible to select* off of Joined table?
Consider this SQL query
SELECT sb.*
FROM books b
LEFT JOIN similar_books sb
ON JSON_SEARCH(b.similarBooks, 'one', sb.isbn)
WHERE b.id = ? AND sb.id IS NOT NULL;
SELECT sb.*
FROM books b
LEFT JOIN similar_books sb
ON JSON_SEARCH(b.similarBooks, 'one', sb.isbn)
WHERE b.id = ? AND sb.id IS NOT NULL;
I have it converted to this Drizzle query, which looks to be correct.
const similarBooksQuery = db
.select({
id: similarBooks.id,
title: similarBooks.title,
isbn: similarBooks.isbn,
authors: similarBooks.authors,
mobileImage: similarBooks.mobileImage,
mobileImagePreview: similarBooks.mobileImagePreview,
smallImage: similarBooks.smallImage,
smallImagePreview: similarBooks.smallImagePreview
})
.from(booksTable)
.leftJoin(similarBooks, sql`JSON_SEARCH(${booksTable.similarBooks}, 'one', ${similarBooks.isbn})`)
.where(and(eq(booksTable.id, Number(id)), isNotNull(similarBooks.id)))
const similarBooksQuery = db
.select({
id: similarBooks.id,
title: similarBooks.title,
isbn: similarBooks.isbn,
authors: similarBooks.authors,
mobileImage: similarBooks.mobileImage,
mobileImagePreview: similarBooks.mobileImagePreview,
smallImage: similarBooks.smallImage,
smallImagePreview: similarBooks.smallImagePreview
})
.from(booksTable)
.leftJoin(similarBooks, sql`JSON_SEARCH(${booksTable.similarBooks}, 'one', ${similarBooks.isbn})`)
.where(and(eq(booksTable.id, Number(id)), isNotNull(similarBooks.id)))
My question is, is it possible to just select * from the joined table, without listing out every field? I realize I could probably flip the query around and do a right join, but I don't want to do that; I'd rather keep listing out all the fields if that's my only option. Does Drizzle have a shortcut for this?
5 replies
DTDrizzle Team
Created by AdamRackis on 9/15/2023 in #help
InferModelFromColumns with columns defined with sql``
Let's say I have a select list that looks like this
const defaultBookFields = {
id: books.id,
tags: sql<number[]>`(SELECT JSON_ARRAYAGG(tag) from books_tags WHERE book = \`books\`.id)`.as("tags"),
subjects: sql<number[]>`(SELECT JSON_ARRAYAGG(subject) from books_subjects WHERE book = \`books\`.id)`.as("subjects"),
title: books.title
};
const defaultBookFields = {
id: books.id,
tags: sql<number[]>`(SELECT JSON_ARRAYAGG(tag) from books_tags WHERE book = \`books\`.id)`.as("tags"),
subjects: sql<number[]>`(SELECT JSON_ARRAYAGG(subject) from books_subjects WHERE book = \`books\`.id)`.as("subjects"),
title: books.title
};
This works fine, but if I try to use it with InferModelFromColumns, like so export type FullBook = InferModelFromColumns<typeof defaultBookFields>; I get a TS error - the tags and subjects fields are not Columns, but rather ad hoc SQL results. I do know how to work around this. I could easily Omit<> those two columns, and then manually append a tabs and subjects property of the right type. But is there any way to make this work as is? Absolutely love this library by the way - thank you for the amazing work!
15 replies
TTCTheo's Typesafe Cult
Created by AdamRackis on 9/2/2023 in #questions
Does anyone know how Vercel manages cache eviction?
I have a use case where I really want actual TTL behavior with Vercel cache. I want to specify a period of time, after which NEW DATA are fetched, as opposed to just displaying stale, cached data, while background revalidation happens. The details are below, though they're not terribly relevant to the question. https://twitter.com/AdamRackis/status/1680232416760287233 The workaround I think would work would be to somehow store some sort of timestamp cookie, and incorporate that into my cache key. So my cache key would be something like user-123-transactions-993726153, were the number on the end is the timestamp that would increase over time. I'd set some sort of revalidate value, but over that value, I would just generate a new timestamp, and put that in my cache key when I call the api endpoint. My question is, should I be worried about these cached entries piling up in Vercel's cache (and costing me money)? Or is there some mechanism whereby entries past the revalidate window are removed, automatically? Finding info on this has been incredibly frustrating.
2 replies