feat: 添加 Vercel Postgres 数据库支持
- 安装 @vercel/postgres 和 pg 依赖 - 创建 PostgresAdapter 适配器,兼容 D1 接口 - 创建 PostgresStorage 类,实现完整的 IStorage 接口 - 添加 Postgres 数据库初始化脚本和 schema - 更 next.config.js 排除 Postgres 模块的客户端打包 - 添加 VERCEL_DEPLOYMENT.md 部署指南 - 支持 Vercel serverless 环境部署 注意事项:观影室功能在 Vercel 上不可用(需要 WebSocket 支持)
This commit is contained in:
+26
-3
@@ -7,7 +7,7 @@ import { RedisStorage } from './redis.db';
|
||||
import { DanmakuFilterConfig,Favorite, IStorage, PlayRecord, SkipConfig } from './types';
|
||||
import { UpstashRedisStorage } from './upstash.db';
|
||||
|
||||
// storage type 常量: 'localstorage' | 'redis' | 'upstash' | 'kvrocks' | 'd1',默认 'localstorage'
|
||||
// storage type 常量: 'localstorage' | 'redis' | 'upstash' | 'kvrocks' | 'd1' | 'postgres',默认 'localstorage'
|
||||
const STORAGE_TYPE =
|
||||
(process.env.NEXT_PUBLIC_STORAGE_TYPE as
|
||||
| 'localstorage'
|
||||
@@ -15,6 +15,7 @@ const STORAGE_TYPE =
|
||||
| 'upstash'
|
||||
| 'kvrocks'
|
||||
| 'd1'
|
||||
| 'postgres'
|
||||
| undefined) || 'localstorage';
|
||||
|
||||
// 创建存储实例
|
||||
@@ -31,16 +32,38 @@ function createStorage(): IStorage {
|
||||
if (typeof window !== 'undefined') {
|
||||
throw new Error('D1Storage can only be used on the server side');
|
||||
}
|
||||
const adapter = getD1Adapter();
|
||||
const d1Adapter = getD1Adapter();
|
||||
// 动态导入 D1Storage 以避免客户端打包
|
||||
const { D1Storage } = require('./d1.db');
|
||||
return new D1Storage(adapter);
|
||||
return new D1Storage(d1Adapter);
|
||||
case 'postgres':
|
||||
// PostgresStorage 只能在服务端使用,客户端会报错
|
||||
if (typeof window !== 'undefined') {
|
||||
throw new Error('PostgresStorage can only be used on the server side');
|
||||
}
|
||||
const postgresAdapter = getPostgresAdapter();
|
||||
// 动态导入 PostgresStorage 以避免客户端打包
|
||||
const { PostgresStorage } = require('./postgres.db');
|
||||
return new PostgresStorage(postgresAdapter);
|
||||
case 'localstorage':
|
||||
default:
|
||||
return null as unknown as IStorage;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Postgres 适配器
|
||||
* 使用 Vercel Postgres (@vercel/postgres)
|
||||
*/
|
||||
function getPostgresAdapter(): any {
|
||||
// 动态导入适配器以避免客户端打包
|
||||
const { PostgresAdapter } = require('./postgres-adapter');
|
||||
|
||||
console.log('Using Vercel Postgres database');
|
||||
|
||||
return new PostgresAdapter();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 D1 适配器
|
||||
* 开发环境:使用 better-sqlite3
|
||||
|
||||
Reference in New Issue
Block a user