Is it a good practice to use a single Builder pattern for both Creating and Updating an entity?
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
idattribute is never sent (the database generates it). - Both
nameandmoduleare strictly mandatory.
- The
-
When Updating
- The
idis strictly mandatory. nameandmodulebecome 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
nullorundefinedso the repository knows it shouldn’t overwrite that specific column.
- The
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
-
Is it considered a good practice to handle this conditional logic (where
idis missing for creation but required for partial updates) inside a single Builder, or should I split this into two separate builders (e.g.,CreatePermissionBuilderandUpdatePermissionBuilder)? -
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 anidis present or not?