-
Notifications
You must be signed in to change notification settings - Fork 1
RFC: Auto-schema push in development mode (like PayloadCMS) #57
Description
Summary
This issue documents how PayloadCMS handles automatic schema creation in development mode and proposes implementing a similar feature for @deessejs/collections.
How PayloadCMS Handles Schema
1. Automatic Schema Push (Development Mode)
In PayloadCMS, when you start the server in development mode, the database tables are automatically created/updated. This happens in packages/db-postgres/src/connect.ts:
// Only push schema if not in production
if (
process.env.NODE_ENV !== 'production' &&
process.env.PAYLOAD_MIGRATING !== 'true' &&
this.push !== false
) {
await pushDevSchema(this as unknown as DrizzleAdapter)
}The pushDevSchema function:
- Compares current schema with previous schema
- If no changes, skips the push
- Uses drizzle-kit's
pushSchemaAPI to push changes - Shows warnings if there's risk of data loss
- Prompts for confirmation before applying
2. Generated Types File (payload-types.ts)
PayloadCMS generates a payload-types.ts file containing TypeScript interfaces for all collections and globals.
Why is it needed?
- TypeScript cannot infer types from runtime config
- The file is generated via:
payload generate:types - It provides full type inference and IntelliSense
Example output:
export interface Config {
collections: {
posts: Post;
users: User;
};
globals: {
global: Global;
};
}
export interface Post {
id: number;
title?: string | null;
content?: ...;
}Current State of @deessejs/collections
Current Behavior
- Schema must be pushed manually:
npx collections db:push - Types are inferred directly from TypeScript config
- No automatic schema push on server start
Is a generated types file needed?
No, because:
- We use TypeScript with direct type inference from config
- Types are already available through the API
- The user defines collections with proper typing
Proposal: Auto-Schema Push in Development
We could implement automatic schema push similar to PayloadCMS by:
-
Option A: Auto-push on server start (Next.js dev server)
- Modify the Next.js plugin (
withCollections) to push schema on startup - Check if tables exist before pushing
- Modify the Next.js plugin (
-
Option B: Keep manual CLI (current approach)
- User runs
npx collections db:pushafter config changes - Simpler, more explicit
- User runs
-
Option C: Add a flag to enable auto-push
withCollections({ autoSchemaPush: true })- Only pushes in development mode
Discussion
What's the preferred approach? Should we implement automatic schema push or keep the manual CLI approach?