Skip to content

Multiple schema configs in typegen only generates exports for the last layout #50

@HosakaKeigo

Description

@HosakaKeigo

Description

There seems to be a bug in the typegen tool where running it with multiple schema configurations results in only the last layout being exported in the client/index.ts file. All previous layouts' exports are lost.

The Problem

In the generateTypedClientsSingle function in packages/typegen/src/typegen.ts, the client/index.ts file is deleted at the beginning of each configuration processing:

// Current code (line 126-127)
const clientIndexFilePath = path.join(rootDir, "client", "index.ts");
fs.rmSync(clientIndexFilePath, { force: true }); // ensure clean slate for this file

This means when processing multiple configurations, each one deletes the index.ts file that was just populated by the previous configuration.

Reproduction Steps

  1. Create a configuration with multiple schemas:
[
  {
    "clientSuffix": "Layout",
    "path": "./src/schema",
    "clearOldFiles": false,
    "envNames": {
      "auth": {
        "username": "FM_USERNAME",
        "password": "FM_PASSWORD"
      },
      "server": "MyFMServer",
      "db": "FM_DATABASE_AB"
    },
    "layouts": [
      {
        "schemaName": "MySchema",
        "layoutName": "MyLayout"
      }
    ],
    "validator": "zod/v3"
  },
  {
    "clientSuffix": "Layout",
    "path": "./src/schema",
    "clearOldFiles": false,
    "envNames": {
      "auth": {
        "username": "FM_USERNAME",
        "password": "FM_PASSWORD"
      },
      "server": "MyFMServer",
      "db": "FM_DATABASE_CD"
    },
    "layouts": [
      {
        "schemaName": "MySchema3",
        "layoutName": "MyLayout3"
      }
    ],
    "validator": "zod/v3"
  }
]
  1. Run typegen
  2. Check src/schema/client/index.ts

Current Behavior

The generated index.ts file contains only:

export { client as MySchema3Layout } from "./MySchema3.js";

The export for MySchemaLayout is missing.

Expected Behavior

The generated index.ts file should contain exports for all layouts:

export { client as MySchemaLayout } from "./MySchema.js";
export { client as MySchema3Layout } from "./MySchema3.js";

Root Cause

The issue occurs because:

  1. generateTypedClients loops through each config and calls generateTypedClientsSingle
  2. Each call to generateTypedClientsSingle deletes the client/index.ts file at the start
  3. Previous exports are lost when the file is deleted

Proposed Solution

Move the index.ts deletion to the top-level generateTypedClients function to ensure it's only deleted once at the beginning of the entire process:

  1. In generateTypedClients, delete the index.ts files once for all unique paths before processing any configs
  2. Remove the fs.rmSync(clientIndexFilePath, { force: true }) line from generateTypedClientsSingle

Related Code Lines

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