Added cacht tags
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import type { NextConfig } from "next";
|
||||
|
||||
const nextConfig: NextConfig = {};
|
||||
const nextConfig: NextConfig = {
|
||||
experimental: {
|
||||
useCache: true
|
||||
}
|
||||
};
|
||||
|
||||
export default nextConfig;
|
||||
@@ -1,9 +1,15 @@
|
||||
import { eq, desc, count } from "drizzle-orm";
|
||||
import { db } from "../drizzle/db";
|
||||
import { urls, visits } from "../drizzle/schema";
|
||||
import { cacheTag } from "next/dist/server/use-cache/cache-tag";
|
||||
import { revalidateTag } from "next/cache";
|
||||
|
||||
export function getAllUrls() {
|
||||
return db.query.urls.findMany({
|
||||
export async function getAllUrls() {
|
||||
"use cache"
|
||||
|
||||
cacheTag("urls")
|
||||
|
||||
return await db.query.urls.findMany({
|
||||
orderBy: desc(urls.createdAt),
|
||||
with: {
|
||||
visits: true
|
||||
@@ -11,28 +17,52 @@ export function getAllUrls() {
|
||||
})
|
||||
}
|
||||
|
||||
export function getUrlBySlug(slug: string) {
|
||||
return db.query.urls.findFirst({
|
||||
export async function getUrlBySlug(slug: string) {
|
||||
"use cache"
|
||||
|
||||
cacheTag("urls")
|
||||
|
||||
return await db.query.urls.findFirst({
|
||||
where: eq(urls.slug, slug)
|
||||
})
|
||||
}
|
||||
|
||||
export function getVisitsBySlugById(id: string) {
|
||||
return db.select({ count: count() }).from(visits).where(eq(visits.urlId, id))
|
||||
export async function insertUrl(data: typeof urls.$inferInsert) {
|
||||
"use cache"
|
||||
|
||||
revalidateTag("url")
|
||||
|
||||
return await db.insert(urls).values(data)
|
||||
}
|
||||
|
||||
export function insertUrl(data: typeof urls.$inferInsert) {
|
||||
return db.insert(urls).values(data)
|
||||
export async function updateUrl(id: string, data: Omit<Partial<typeof urls.$inferInsert>, "id">) {
|
||||
"use cache"
|
||||
|
||||
revalidateTag("url")
|
||||
|
||||
return await db.update(urls).set(data).where(eq(urls.id, id))
|
||||
}
|
||||
|
||||
export function updateUrl(id: string, data: Omit<Partial<typeof urls.$inferInsert>, "id">) {
|
||||
return db.update(urls).set(data).where(eq(urls.id, id))
|
||||
export async function deleteUrl(id: string) {
|
||||
"use cache"
|
||||
|
||||
revalidateTag("url")
|
||||
|
||||
return await db.delete(urls).where(eq(urls.id, id))
|
||||
}
|
||||
|
||||
export function deleteUrl(id: string) {
|
||||
return db.delete(urls).where(eq(urls.id, id))
|
||||
export async function getVisitsBySlugById(id: string) {
|
||||
"use cache"
|
||||
|
||||
cacheTag("visits")
|
||||
|
||||
return await db.select({ count: count() }).from(visits).where(eq(visits.urlId, id))
|
||||
}
|
||||
|
||||
export function trackVisit(data: typeof visits.$inferInsert) {
|
||||
return db.insert(visits).values(data)
|
||||
export async function trackVisit(data: typeof visits.$inferInsert) {
|
||||
"use cache"
|
||||
|
||||
revalidateTag("visits")
|
||||
|
||||
return await db.insert(visits).values(data)
|
||||
}
|
||||
Reference in New Issue
Block a user