Is it a good practice to use a single Builder pattern for both Creating and Updating an entity?

Published: (May 26, 2026 at 10:29 PM EDT)
2 min read
Source: Dev.to

Source: Dev.to

Problem

I am implementing the Builder pattern in a Node.js (TypeScript) backend to instantiate a Permission object before passing it to my repository layer. I want to reuse the same Builder for both creating and updating a permission, but the presence of the id and the validation rules change completely depending on the action:

  • When Creating

    • The id attribute is never sent (the database generates it).
    • Both name and module are strictly mandatory.
  • When Updating

    • The id is strictly mandatory.
    • name and module become optional, with the condition that at least one of them must be provided (they cannot both be missing).
    • If a field is not provided, it should remain null or undefined so the repository knows it shouldn’t overwrite that specific column.
import { CustomError } from "../../../shared";

export class PermissionBuilder {
    private name: string = '';
    private module: string = '';

    constructor() {}

    setName(name: string): PermissionBuilder {
        if (!name || !name.trim()) throw CustomError.badRequest('Name cannot be empty');
        this.name = name;
        return this;
    }

    setModule(module: string): PermissionBuilder {
        if (!module || !module.trim()) throw CustomError.badRequest('Module cannot be empty');
        this.module = module;
        return this;
    }

    build(): { name: string; module: string } {
        return {
            name: this.name,
            module: this.module,
        };
    }
}

Questions

  1. Is it considered a good practice to handle this conditional logic (where id is missing for creation but required for partial updates) inside a single Builder, or should I split this into two separate builders (e.g., CreatePermissionBuilder and UpdatePermissionBuilder)?

  2. If keeping a single Builder is the right approach, how should I restructure the internal state and the build() method to handle a payload that dynamically changes its required fields based on whether an id is present or not?

0 views
Back to Blog

Related posts

Read more »