Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

perf(migration): 使用 tasks 来完成 migration 和 seed 任务

Timothy Yin4c7a1b9e4d64
diff --git a/Dockerfile b/Dockerfile
index fd5a74a0..f607599f 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -10,14 +10,12 @@ COPY packages/webgl-image/package.json ./packages/webgl-image/
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
FROM base AS build
-ARG MAPBOX_TOKEN
WORKDIR /usr/src/app
COPY --from=deps /usr/src/app/node_modules ./node_modules
COPY --from=deps /usr/src/app/packages/webgl-image/node_modules ./packages/webgl-image/node_modules
COPY . .
-ENV MAPBOX_TOKEN=$MAPBOX_TOKEN
-RUN pnpm run build:deps
-RUN pnpm run build
+RUN NODE_OPTIONS="--max-old-space-size=4096" pnpm run build:deps
+RUN NODE_OPTIONS="--max-old-space-size=4096" pnpm run build
FROM node:22-alpine AS runtime
RUN apk add --no-cache perl exiftool
@@ -46,4 +44,4 @@ ENV NODE_ENV=production
ENV NITRO_PORT=3000
ENV NITRO_HOST=0.0.0.0
-CMD ["sh", "-c", "node scripts/migrate.mjs && node .output/server/index.mjs"]
+CMD ["sh", "-c", "node .output/server/index.mjs"]
diff --git a/docker-compose.yml b/docker-compose.yml
index 08269e3e..c5e3869b 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,28 +1,14 @@
services:
chronoframe:
- build:
- context: .
- dockerfile: Dockerfile
+ image: ghcr.io/hoshinosuzumi/chronoframe:latest
container_name: chronoframe
+ restart: unless-stopped
ports:
- - "3000:3000"
+ - '3000:3000'
+ volumes:
+ - ./data:/app/data
env_file:
- .env
- environment:
- - NODE_ENV=production
- - NITRO_PORT=3000
- - NITRO_HOST=0.0.0.0
- # - STORAGE_PROVIDER=s3
- # - PROVIDER_S3_ENDPOINT=your-s3-endpoint
- # - PROVIDER_S3_BUCKET=your-bucket-name
- # - PROVIDER_S3_REGION=your-region
- # - PROVIDER_S3_ACCESS_KEY_ID=your-access-key
- # - PROVIDER_S3_SECRET_ACCESS_KEY=your-secret-key
- # - PROVIDER_S3_PREFIX=your-prefix
- # - PROVIDER_S3_CDN_URL=your-cdn-url
- restart: unless-stopped
- volumes:
- - ./data:/app/.data
# database:
# image: postgres:15-alpine
diff --git a/nuxt.config.ts b/nuxt.config.ts
index c51968cf..c1562adc 100644
--- a/nuxt.config.ts
+++ b/nuxt.config.ts
@@ -49,6 +49,9 @@ export default defineNuxtConfig({
deployConfig: true,
nodeCompat: true,
},
+ experimental: {
+ tasks: true,
+ },
},
vite: {
diff --git a/scripts/migrate.mjs b/scripts/migrate.mjs
index 89324712..cc18e0c9 100644
--- a/scripts/migrate.mjs
+++ b/scripts/migrate.mjs
@@ -65,7 +65,7 @@ try {
if (existingUsers.length === 0) {
console.log('No users found, creating admin user...')
const hashedPassword = await hashPassword(
- process.env.CFRAME_ADMIN_PASSWORD || 'admin',
+ process.env.CFRAME_ADMIN_PASSWORD || 'CF1234@!',
)
await db.insert(usersTable).values({
diff --git a/server/plugins/seed.ts b/server/plugins/seed.ts
new file mode 100644
index 00000000..af63e059
--- /dev/null
+++ b/server/plugins/seed.ts
@@ -0,0 +1,4 @@
+export default defineNitroPlugin(async (_nitroApp) => {
+ await runTask('db:migrate')
+ await runTask('db:seed')
+})
diff --git a/server/tasks/db/migrate.ts b/server/tasks/db/migrate.ts
new file mode 100644
index 00000000..521b6388
--- /dev/null
+++ b/server/tasks/db/migrate.ts
@@ -0,0 +1,29 @@
+import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
+import { fileURLToPath } from 'url'
+import { dirname, join } from 'path'
+
+export default defineTask({
+ meta: {
+ name: 'db:migrate',
+ description: 'Migrate the database',
+ },
+ async run() {
+ const log = logger.dynamic('db')
+ const db = useDB()
+
+ log.info('Migrating database...')
+
+ const __filename = fileURLToPath(import.meta.url)
+ const __dirname = dirname(__filename)
+
+ migrate(db, {
+ migrationsFolder: join(__dirname, '../../server/database/migrations'),
+ })
+
+ log.success('Database migrated successfully.')
+
+ return {
+ result: 'success',
+ }
+ },
+})
diff --git a/server/tasks/db/seed.ts b/server/tasks/db/seed.ts
new file mode 100644
index 00000000..3f6693a0
--- /dev/null
+++ b/server/tasks/db/seed.ts
@@ -0,0 +1,33 @@
+export default defineTask({
+ meta: {
+ name: 'db:seed',
+ description: 'Seed the database with default admin user',
+ },
+ async run() {
+ const log = logger.dynamic('db')
+ const db = useDB()
+
+ log.info('Seeding database with default admin user...')
+ const users = await db.select().from(tables.users)
+
+ if (users.length > 0) {
+ log.info('User already exists, skipping seeding.')
+ } else {
+ log.info('No users found, creating default admin user...')
+ await db.insert(tables.users).values({
+ username: process.env.CFRAME_ADMIN_NAME || 'chronoframe',
+ email: process.env.CFRAME_ADMIN_EMAIL || '[email protected]',
+ password: await hashPassword(
+ process.env.CFRAME_ADMIN_PASSWORD || 'CF1234@!',
+ ),
+ createdAt: new Date(),
+ isAdmin: 1,
+ })
+ log.success('Default admin user created.')
+ }
+
+ return {
+ result: 'success',
+ }
+ },
+})
diff --git a/server/utils/logger.ts b/server/utils/logger.ts
index 36c68921..62c6c468 100644
--- a/server/utils/logger.ts
+++ b/server/utils/logger.ts
@@ -14,7 +14,7 @@ export const logger = {
fs: mConsola.withTag('cframe/fs'),
image: mConsola.withTag('cframe/image'),
location: mConsola.withTag('cframe/location'),
- dynamic: (id: string) => mConsola.withTag(id.toUpperCase()),
+ dynamic: (id: string) => mConsola.withTag(`cframe/${id}`),
}
export type Logger = Omit<typeof logger, 'dynamic'>