refactor: switch to vercel pg

This commit is contained in:
iCrawl
2023-11-13 21:07:01 +01:00
parent 9a8110047e
commit c4767bacde
10 changed files with 125 additions and 89 deletions

View File

@@ -52,11 +52,11 @@
"@discordjs/ui": "workspace:^",
"@microsoft/tsdoc": "^0.14.2",
"@microsoft/tsdoc-config": "0.16.2",
"@planetscale/database": "^1.11.0",
"@react-icons/all-files": "^4.1.0",
"@vercel/analytics": "^1.1.1",
"@vercel/edge-config": "^0.4.1",
"@vercel/og": "^0.5.20",
"@vercel/postgres": "^0.5.1",
"ariakit": "2.0.0-next.44",
"bright": "^0.8.4",
"class-variance-authority": "^0.7.0",

View File

@@ -1,26 +1,15 @@
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { connect } from '@planetscale/database';
const sql = connect({
url: process.env.DATABASE_URL!,
async fetch(url, init) {
delete init?.cache;
return fetch(url, { ...init, next: { revalidate: 3_600 } });
},
});
import { sql } from '@vercel/postgres';
export const fetchVersions = async (packageName: string): Promise<string[]> => {
if (process.env.NEXT_PUBLIC_LOCAL_DEV || process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview') {
if (process.env.NEXT_PUBLIC_LOCAL_DEV === 'true' || process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview') {
return ['main'];
}
try {
const { rows } = await sql.execute('select version from documentation where name = ? order by version desc', [
packageName,
]);
const { rows } = await sql`select version from documentation where name = ${packageName} order by version desc`;
// @ts-expect-error: https://github.com/planetscale/database-js/issues/71
return rows.map((row) => row.version);
} catch {
return [];
@@ -28,7 +17,7 @@ export const fetchVersions = async (packageName: string): Promise<string[]> => {
};
export const fetchModelJSON = async (packageName: string, version: string) => {
if (process.env.NEXT_PUBLIC_LOCAL_DEV) {
if (process.env.NEXT_PUBLIC_LOCAL_DEV === 'true') {
try {
const res = await readFile(
join(process.cwd(), '..', '..', 'packages', packageName, 'docs', 'docs.api.json'),
@@ -43,12 +32,8 @@ export const fetchModelJSON = async (packageName: string, version: string) => {
if (process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview') {
try {
const { rows } = await sql.execute('select data from documentation where name = ? and version = ?', [
packageName,
'main',
]);
const { rows } = await sql`select data from documentation where name = ${packageName} and version = ${'main'}`;
// @ts-expect-error: https://github.com/planetscale/database-js/issues/71
return rows[0]?.data ?? null;
} catch {
return null;
@@ -56,12 +41,8 @@ export const fetchModelJSON = async (packageName: string, version: string) => {
}
try {
const { rows } = await sql.execute('select data from documentation where name = ? and version = ?', [
packageName,
version,
]);
const { rows } = await sql`select data from documentation where name = ${packageName} and version = ${version}`;
// @ts-expect-error: https://github.com/planetscale/database-js/issues/71
return rows[0]?.data ?? null;
} catch {
return null;

View File

@@ -7,7 +7,7 @@ import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { useMemo } from 'react';
const isDev = process.env.NEXT_PUBLIC_LOCAL_DEV ?? process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview';
const isDev = process.env.NEXT_PUBLIC_LOCAL_DEV === 'true' ?? process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview';
export default function VersionSelect({ versions }: { readonly versions: string[] }) {
const pathname = usePathname();

View File

@@ -1,26 +1,15 @@
import { connect } from '@planetscale/database';
import { get } from '@vercel/edge-config';
import { sql } from '@vercel/postgres';
import { NextResponse, type NextRequest } from 'next/server';
import { PACKAGES } from './util/constants';
const sql = connect({
url: process.env.DATABASE_URL!,
async fetch(url, init) {
delete init?.cache;
return fetch(url, { ...init, next: { revalidate: 3_600 } });
},
});
async function fetchLatestVersion(packageName: string): Promise<string> {
if (process.env.NEXT_PUBLIC_LOCAL_DEV || process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview') {
if (process.env.NEXT_PUBLIC_LOCAL_DEV === 'true' || process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview') {
return 'main';
}
const { rows } = await sql.execute('select version from documentation where name = ? order by version desc', [
packageName,
]);
const { rows } = await sql`select version from documentation where name = ${packageName} order by version desc`;
// @ts-expect-error: https://github.com/planetscale/database-js/issues/71
return rows.map((row) => row.version).at(1) ?? 'main';
}