Skip to content

Commit

Permalink
feat: auto init database
Browse files Browse the repository at this point in the history
  • Loading branch information
besscroft committed May 2, 2024
1 parent 65cb827 commit dfc238c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 27 deletions.
2 changes: 0 additions & 2 deletions components/admin/dashboard/TagTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export default function TagTable(props: Readonly<HandleProps>) {

const { data } = useSWRHydrated(props)

console.log(data)

return (
<>
<Card isBlurred shadow="sm" className="h-48">
Expand Down
3 changes: 0 additions & 3 deletions components/admin/upload/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,6 @@ export default function FileUpload() {
multiple: false,
maxCount: 1,
customRequest: (file) => onRequestUpload(file),
onChange: (event) => {
console.log(event)
},
beforeUpload: async (file) => await onBeforeUpload(file),
onRemove: (file) => {
setStorageSelect(false)
Expand Down
21 changes: 0 additions & 21 deletions doc/sql/data.sql

This file was deleted.

40 changes: 40 additions & 0 deletions instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { PrismaClient } from '@prisma/client'

export async function register() {
try {
if (process.env.NEXT_RUNTIME === 'edge') {
return
}
const prisma = new PrismaClient()
if (prisma) {
await prisma.$transaction(async (tx) => {
await tx.configs.createMany({
data: [
{ config_key: 'accesskey_id', config_value: '', detail: '阿里 OSS / AWS S3 AccessKey_ID' },
{ config_key: 'accesskey_secret', config_value: '', detail: '阿里 OSS / AWS S3 AccessKey_Secret' },
{ config_key: 'region', config_value: '', detail: '阿里 OSS / AWS S3 Region 地域,如:oss-cn-hongkong' },
{ config_key: 'endpoint', config_value: '', detail: '阿里 OSS / AWS S3 Endpoint 地域节点,如:oss-cn-hongkong.aliyuncs.com' },
{ config_key: 'bucket', config_value: '', detail: '阿里 OSS / AWS S3 Bucket 存储桶名称,如:picimpact' },
{ config_key: 'storage_folder', config_value: '', detail: '存储文件夹(S3),严格格式,如:picimpact 或 picimpact/images ,填 / 或者不填表示根路径' },
{ config_key: 'alist_token', config_value: '', detail: 'alist 令牌' },
{ config_key: 'alist_url', config_value: '', detail: 'AList 地址,如:https://alist.besscroft.com' },
{ config_key: 'secret_key', config_value: 'pic-impact', detail: 'SECRET_KEY' },
{ config_key: 'r2_accesskey_id', config_value: '', detail: 'Cloudflare AccessKey_ID' },
{ config_key: 'r2_accesskey_secret', config_value: '', detail: 'Cloudflare AccessKey_Secret' },
{ config_key: 'r2_endpoint', config_value: '', detail: 'Cloudflare Endpoint 地域节点,如:https://<ACCOUNT_ID>.r2.cloudflarestorage.com' },
{ config_key: 'r2_bucket', config_value: '', detail: 'Cloudflare Bucket 存储桶名称,如:picimpact' },
{ config_key: 'r2_storage_folder', config_value: '', detail: '存储文件夹(Cloudflare R2),严格格式,如:picimpact 或 picimpact/images ,填 / 或者不填表示根路径' },
{ config_key: 'r2_public_domain', config_value: '', detail: 'Cloudflare R2 自定义域(公开访问)' },
],
skipDuplicates: true,
})
})
console.log('数据库初始化完毕!')
await prisma.$disconnect()
} else {
console.error('数据库初始化失败,请检查您的连接信息!')
}
} catch (e) {
console.error('初始化数据失败,您可能需要准备干净的数据表,请联系管理员!', e)
}
}
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const nextConfig = {
experimental: {
serverComponentsExternalPackages: ['pg'],
instrumentationHook: true,
},
eslint: {
ignoreDuringBuilds: true,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"private": true,
"author": "Bess Croft <[email protected]>",
"scripts": {
"dev": "next dev",
"dev": "pnpm run prisma:generate && next dev",
"build": "next build",
"build:vercel": "pnpm run prisma:deploy && next build",
"start": "next start",
"lint": "next lint",
"prisma:format": "npx prisma format",
Expand Down
52 changes: 52 additions & 0 deletions server/lib/operate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,56 @@ export async function updateTagShow(id: number, show: number) {
}
})
return resultRow
}

export async function initConfig() {
await db.$transaction(async (tx) => {
await tx.$executeRaw`
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('accesskey_id', '', '阿里 OSS / AWS S3 AccessKey_ID', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('accesskey_secret', '', '阿里 OSS / AWS S3 AccessKey_Secret', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('region', '', '阿里 OSS / AWS S3 Region 地域,如:oss-cn-hongkong', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('endpoint', '', '阿里 OSS / AWS S3 Endpoint 地域节点,如:oss-cn-hongkong.aliyuncs.com', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('bucket', '', '阿里 OSS / AWS S3 Bucket 存储桶名称,如:picimpact', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('storage_folder', '', '存储文件夹(S3),严格格式,如:picimpact 或 picimpact/images ,填 / 或者不填表示根路径', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('alist_token', '', 'alist 令牌 ', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('alist_url', '', 'AList 地址,如:https://alist.example.com', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('secret_key', '', 'SECRET_KEY', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('r2_accesskey_id', '', 'Cloudflare AccessKey_ID', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('r2_accesskey_secret', '', 'Cloudflare AccessKey_Secret', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('r2_endpoint', '', 'Cloudflare Endpoint 地域节点,如:https://<ACCOUNT_ID>.r2.cloudflarestorage.com', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('r2_bucket', '', 'Cloudflare Bucket 存储桶名称,如:picimpact', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('r2_storage_folder', '', '存储文件夹(Cloudflare R2),严格格式,如:picimpact 或 picimpact/images ,填 / 或者不填表示根路径', NOW())
ON CONFLICT (config_key) DO NOTHING;
INSERT INTO "public"."Configs" (config_key, config_value, detail, create_time)
VALUES ('r2_public_domain', '', 'Cloudflare R2 自定义域(公开访问)', NOW())
ON CONFLICT (config_key) DO NOTHING;
`
})
}

0 comments on commit dfc238c

Please sign in to comment.