Skip to content

RFC: Auto-schema push in development mode (like PayloadCMS) #57

@AliiiBenn

Description

@AliiiBenn

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:

  1. Compares current schema with previous schema
  2. If no changes, skips the push
  3. Uses drizzle-kit's pushSchema API to push changes
  4. Shows warnings if there's risk of data loss
  5. 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:

  1. 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
  2. Option B: Keep manual CLI (current approach)

    • User runs npx collections db:push after config changes
    • Simpler, more explicit
  3. 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions