-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.ts
160 lines (146 loc) · 3.6 KB
/
db.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
import { remember } from "@epic-web/remember";
import { PrismaClient, type User, type Post } from "@prisma/client";
import { withAccelerate } from "@prisma/extension-accelerate";
import { withOptimize } from "@prisma/extension-optimize";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { notFound, redirect } from "next/navigation";
const isProd = process.env.NODE_ENV === "production";
const generateClient = () => {
return isProd
? new PrismaClient().$extends(withAccelerate())
: new PrismaClient()
.$extends(withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY! }))
.$extends(withAccelerate());
};
export const prisma = remember("prisma", generateClient);
export const authAdapter = PrismaAdapter(prisma);
export function createSingleUser() {
const name = "Daniel Van Cuylenburg";
const email = "[email protected]";
const id = "cm796vlvf00001i0w7889xx9y";
const image =
"https://lh3.googleusercontent.com/a/ACg8ocItaSg693lHCK4wTckqdqNKYnBTHHQtfpYGLdPvtJCQwXr0-XpN=s96-c";
const type = "oidc";
const provider = "google";
const providerAccountId = process.env.GOOGLE_PROVIDER_ID;
const providerAccountToken = process.env.GOOGLE_ACCESS_TOKEN;
const tokenType = "bearer";
const scope =
"openid https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile";
const idToken = process.env.ID_TOKEN;
return {
id,
name,
email,
image,
type,
provider,
providerAccountId,
providerAccountToken,
tokenType,
scope,
idToken,
};
}
const content = JSON.stringify({
type: "doc",
content: [
{
type: "paragraph",
content: [{ type: "text", text: "Hello World! 🌎️" }],
},
{ type: "paragraph" },
{
type: "paragraph",
content: [{ type: "text", text: "I hope that this works out nicely" }],
},
],
});
export function createSeedPosts() {
return [
{
title: "First Post",
content,
},
{
title: "Second Post",
content,
},
{
title: "Third Post",
content,
},
];
}
export async function getUsersPosts(userId: string) {
const user = await prisma.user.findUnique({
cacheStrategy: {
swr: 120,
ttl: 120,
},
where: { id: userId },
select: {
id: true,
email: true,
name: true,
Post: {
select: {
id: true,
title: true,
slug: true,
content: true,
createdAt: true,
updatedAt: true,
},
},
},
});
// TODO: Add test coverage to ensure redirect happens
if (!user) redirect("/login");
return { user, posts: user.Post };
}
export async function getPost(slug: string): Promise<{
post: Omit<Post, "ownerId">;
user: Pick<User, "id" | "name" | "email" | "image">;
}> {
try {
const data = await prisma.post.findUnique({
cacheStrategy: {
swr: 240,
ttl: 240,
},
where: { slug },
select: {
id: true,
title: true,
slug: true,
content: true,
createdAt: true,
updatedAt: true,
owner: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
});
if (!data) notFound();
return {
post: {
id: data.id,
title: data.title,
slug: data.slug,
content: data.content,
createdAt: data.createdAt,
updatedAt: data.updatedAt,
},
user: data.owner,
};
} catch (error) {
console.error(error);
throw new Error("Failed to get post");
}
}