|
1 | | -import {prismaClient} from '@/server/prisma-client'; |
| 1 | +import {prismaClient} from './prisma-client'; |
2 | 2 | import {Changelog} from '@prisma/client'; |
3 | 3 | import {unstable_cache} from 'next/cache'; |
4 | 4 |
|
5 | | -export const getChangelogs = unstable_cache( |
6 | | - async () => { |
7 | | - const changelogs = await prismaClient.changelog.findMany({ |
8 | | - include: { |
9 | | - categories: true, |
10 | | - }, |
11 | | - where: { |
12 | | - published: true, |
13 | | - }, |
14 | | - orderBy: { |
15 | | - publishedAt: 'desc', |
16 | | - }, |
17 | | - }); |
18 | | - const hashHex = (buffer: ArrayBuffer) => { |
19 | | - const hexCodes = Array.from(new Uint8Array(buffer)).map(value => |
20 | | - value.toString(16).padStart(2, '0') |
21 | | - ); |
22 | | - return hexCodes.join(''); |
23 | | - }; |
24 | | - const hashChangelogs = (changelogs: Changelog[]) => |
25 | | - crypto.subtle |
26 | | - .digest( |
27 | | - 'SHA-256', |
28 | | - new TextEncoder().encode( |
29 | | - changelogs |
30 | | - .map(changelogs => changelogs.id + ':' + changelogs.updatedAt) |
31 | | - .join(',') |
32 | | - ) |
33 | | - ) |
34 | | - .then(hashHex); |
35 | | - return { |
36 | | - changelogs, |
37 | | - hash: await hashChangelogs(changelogs), |
38 | | - }; |
39 | | - }, |
40 | | - ['changelogs'], |
41 | | - {tags: ['changelogs']} |
42 | | -); |
| 5 | +const hashHex = (buffer: ArrayBuffer) => { |
| 6 | + const hexCodes = Array.from(new Uint8Array(buffer)).map(value => |
| 7 | + value.toString(16).padStart(2, '0') |
| 8 | + ); |
| 9 | + return hexCodes.join(''); |
| 10 | +}; |
| 11 | + |
| 12 | +const hashChangelogs = async (changelogs: Changelog[]) => { |
| 13 | + return crypto.subtle |
| 14 | + .digest('SHA-256', new TextEncoder().encode(JSON.stringify(changelogs))) |
| 15 | + .then(hashHex); |
| 16 | +}; |
| 17 | +export const getChangelogsUncached = async () => { |
| 18 | + const changelogs = await prismaClient.changelog.findMany({ |
| 19 | + include: { |
| 20 | + categories: true, |
| 21 | + }, |
| 22 | + where: { |
| 23 | + published: true, |
| 24 | + }, |
| 25 | + orderBy: { |
| 26 | + publishedAt: 'desc', |
| 27 | + }, |
| 28 | + }); |
| 29 | + return { |
| 30 | + changelogs, |
| 31 | + hash: await hashChangelogs(changelogs), |
| 32 | + }; |
| 33 | +}; |
| 34 | + |
| 35 | +export const getChangelogs = unstable_cache(getChangelogsUncached, ['changelogs'], { |
| 36 | + tags: ['changelogs'], |
| 37 | +}); |
0 commit comments