Skip to content

Commit

Permalink
make unstable_cache typesafe again
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanniser committed Oct 21, 2024
1 parent b321ed6 commit 3c3a7fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
25 changes: 12 additions & 13 deletions src/lib/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export async function getUser() {
}

export const getProductsForSubcategory = unstable_cache(
(subcategory) =>
(subcategorySlug: string) =>
db.query.products.findMany({
where: (products, { eq, and }) =>
and(eq(products.subcategory_slug, subcategory)),
and(eq(products.subcategory_slug, subcategorySlug)),
orderBy: (products, { asc }) => asc(products.slug),
}),
["subcategory-products"],
Expand All @@ -71,9 +71,9 @@ export const getCollections = unstable_cache(
);

export const getProductDetails = unstable_cache(
(product) =>
(productSlug: string) =>
db.query.products.findFirst({
where: (products, { eq }) => eq(products.slug, product),
where: (products, { eq }) => eq(products.slug, productSlug),
}),
["product"],
{
Expand All @@ -82,9 +82,9 @@ export const getProductDetails = unstable_cache(
);

export const getSubcategory = unstable_cache(
(subcategory) =>
(subcategorySlug: string) =>
db.query.subcategories.findFirst({
where: (subcategories, { eq }) => eq(subcategories.slug, subcategory),
where: (subcategories, { eq }) => eq(subcategories.slug, subcategorySlug),
}),
["subcategory"],
{
Expand All @@ -93,9 +93,9 @@ export const getSubcategory = unstable_cache(
);

export const getCategory = unstable_cache(
(category) =>
(categorySlug: string) =>
db.query.categories.findFirst({
where: (categories, { eq }) => eq(categories.slug, category),
where: (categories, { eq }) => eq(categories.slug, categorySlug),
with: {
subcollections: {
with: {
Expand All @@ -111,13 +111,12 @@ export const getCategory = unstable_cache(
);

export const getCollectionDetails = unstable_cache(
async (cn) =>
async (collectionName: string) =>
db.query.collections.findMany({
with: {
categories: true,
},
where: (collections, { eq }) =>
eq(collections.name, decodeURIComponent(cn)),
where: (collections, { eq }) => eq(collections.name, collectionName),
orderBy: (collections, { asc }) => asc(collections.name),
}),
["collection"],
Expand All @@ -136,7 +135,7 @@ export const getProductCount = unstable_cache(

// could be optimized by storing category slug on the products table
export const getCategoryProductCount = unstable_cache(
(categorySlug) =>
(categorySlug: string) =>
db
.select({ count: count() })
.from(categories)
Expand All @@ -157,7 +156,7 @@ export const getCategoryProductCount = unstable_cache(
);

export const getSubcategoryProductCount = unstable_cache(
(subcategorySlug) =>
(subcategorySlug: string) =>
db
.select({ count: count() })
.from(products)
Expand Down
8 changes: 2 additions & 6 deletions src/lib/unstable-cache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { unstable_cache as next_unstable_cache } from "next/cache";
import { cache } from "react";

// sorry theo but we're using an any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Callback<T> = (...args: any[]) => Promise<T>;

// next_unstable_cache doesn't handle deduplication, so we wrap it in React's cache
export const unstable_cache = <T>(
callback: Callback<T>,
export const unstable_cache = <Inputs extends unknown[], Output>(
callback: (...args: Inputs) => Promise<Output>,
key: string[],
options: { revalidate: number },
) => cache(next_unstable_cache(callback, key, options));

0 comments on commit 3c3a7fe

Please sign in to comment.