forked from ethanniser/NextFaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.ts
229 lines (209 loc) · 5.71 KB
/
queries.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { cookies } from "next/headers";
import { verifyToken } from "./session";
import {
categories,
products,
subcategories,
subcollections,
users,
} from "@/db/schema";
import { db } from "@/db";
import { eq, and, count } from "drizzle-orm";
import { unstable_cache } from "./unstable-cache";
import { sql } from "drizzle-orm";
export async function getUser() {
const sessionCookie = (await cookies()).get("session");
if (!sessionCookie || !sessionCookie.value) {
return null;
}
const sessionData = await verifyToken(sessionCookie.value);
if (
!sessionData ||
!sessionData.user ||
typeof sessionData.user.id !== "number"
) {
return null;
}
if (new Date(sessionData.expires) < new Date()) {
return null;
}
const user = await db
.select()
.from(users)
.where(and(eq(users.id, sessionData.user.id)))
.limit(1);
if (user.length === 0) {
return null;
}
return user[0];
}
export const getProductsForSubcategory = unstable_cache(
(subcategorySlug: string) =>
db.query.products.findMany({
where: (products, { eq, and }) =>
and(eq(products.subcategory_slug, subcategorySlug)),
orderBy: (products, { asc }) => asc(products.slug),
}),
["subcategory-products"],
{
revalidate: 60 * 60 * 2, // two hours,
},
);
export const getCollections = unstable_cache(
() =>
db.query.collections.findMany({
with: {
categories: true,
},
orderBy: (collections, { asc }) => asc(collections.name),
}),
["collections"],
{
revalidate: 60 * 60 * 2, // two hours,
},
);
export const getProductDetails = unstable_cache(
(productSlug: string) =>
db.query.products.findFirst({
where: (products, { eq }) => eq(products.slug, productSlug),
}),
["product"],
{
revalidate: 60 * 60 * 2, // two hours,
},
);
export const getSubcategory = unstable_cache(
(subcategorySlug: string) =>
db.query.subcategories.findFirst({
where: (subcategories, { eq }) => eq(subcategories.slug, subcategorySlug),
}),
["subcategory"],
{
revalidate: 60 * 60 * 2, // two hours,
},
);
export const getCategory = unstable_cache(
(categorySlug: string) =>
db.query.categories.findFirst({
where: (categories, { eq }) => eq(categories.slug, categorySlug),
with: {
subcollections: {
with: {
subcategories: true,
},
},
},
}),
["category"],
{
revalidate: 60 * 60 * 2, // two hours,
},
);
export const getCollectionDetails = unstable_cache(
async (collectionSlug: string) =>
db.query.collections.findMany({
with: {
categories: true,
},
where: (collections, { eq }) => eq(collections.slug, collectionSlug),
orderBy: (collections, { asc }) => asc(collections.slug),
}),
["collection"],
{
revalidate: 60 * 60 * 2, // two hours,
},
);
export const getProductCount = unstable_cache(
() => db.select({ count: count() }).from(products),
["total-product-count"],
{
revalidate: 60 * 60 * 2, // two hours,
},
);
// could be optimized by storing category slug on the products table
export const getCategoryProductCount = unstable_cache(
(categorySlug: string) =>
db
.select({ count: count() })
.from(categories)
.leftJoin(
subcollections,
eq(categories.slug, subcollections.category_slug),
)
.leftJoin(
subcategories,
eq(subcollections.id, subcategories.subcollection_id),
)
.leftJoin(products, eq(subcategories.slug, products.subcategory_slug))
.where(eq(categories.slug, categorySlug)),
["category-product-count"],
{
revalidate: 60 * 60 * 2, // two hours,
},
);
export const getSubcategoryProductCount = unstable_cache(
(subcategorySlug: string) =>
db
.select({ count: count() })
.from(products)
.where(eq(products.subcategory_slug, subcategorySlug)),
["subcategory-product-count"],
{
revalidate: 60 * 60 * 2, // two hours,
},
);
export const getSearchResults = unstable_cache(
async (searchTerm: string) => {
let results;
// do we really need to do this hybrid search pattern?
if (searchTerm.length <= 2) {
// If the search term is short (e.g., "W"), use ILIKE for prefix matching
results = await db
.select()
.from(products)
.where(sql`${products.name} ILIKE ${searchTerm + "%"}`) // Prefix match
.limit(5)
.innerJoin(
subcategories,
sql`${products.subcategory_slug} = ${subcategories.slug}`,
)
.innerJoin(
subcollections,
sql`${subcategories.subcollection_id} = ${subcollections.id}`,
)
.innerJoin(
categories,
sql`${subcollections.category_slug} = ${categories.slug}`,
);
} else {
// For longer search terms, use full-text search with tsquery
const formattedSearchTerm = searchTerm
.split(" ")
.filter((term) => term.trim() !== "") // Filter out empty terms
.map((term) => `${term}:*`)
.join(" & ");
results = await db
.select()
.from(products)
.where(
sql`to_tsvector('english', ${products.name}) @@ to_tsquery('english', ${formattedSearchTerm})`,
)
.limit(5)
.innerJoin(
subcategories,
sql`${products.subcategory_slug} = ${subcategories.slug}`,
)
.innerJoin(
subcollections,
sql`${subcategories.subcollection_id} = ${subcollections.id}`,
)
.innerJoin(
categories,
sql`${subcollections.category_slug} = ${categories.slug}`,
);
}
return results;
},
["search-results"],
{ revalidate: 60 * 60 * 2 }, // two hours
);