Implementation of the Builder Pattern for both Javascript/Typescript and compatible with ES5/ES6 classes.
I have been using NestJs for some time now which I love. The framework is relying heavily on OOP and DDD, and I was looking for a clean and convenient way to instantiate my Domain Models.
The Builder Pattern, as implemented by the Java library Lombok, gave me the idea to transpose it to Typescript.
To learn more about this library, see the section Articles.
Install the package using npm
npm i builder-pattern-2
or yarn
yarn add builder-pattern-2
- Automatic generation of the Builder class.
- Auto-completion of the Builder's methods and their typing.
- Compatible with pure Javascript and Typescript.
- Compatible with ES5/ES6 classes.
Generate the Builder of a class using the function createBuilderClass
.
Its setters are based on the parameter names of the constructor of the class. For Example, if the constructor has a parameter id
, the Builder will expose a setter setId
.
import { Builder, createBuilderClass } from 'builder-pattern-2';
/**
* 1. Create your domain model.
*/
class MrMeeseeks {
constructor(private readonly goal: string, private readonly lifespan: number) {}
public getGoal() {
return this.goal;
}
public getLifespan() {
return this.lifespan;
}
}
/**
* 2. Describe its constructor parameters.
*/
interface MrMeeseeksCtor {
goal: string;
lifespan: number;
}
/**
* 3. Generate the Builder class.
*/
const MrMeeseeksBuilder: Builder<MrMeeseeks, MrMeeseeksCtor> = createBuilderClass<MrMeeseeks, MrMeeseeksCtor>(MrMeeseeks);
/**
* 4. Use it!
*/
const builder = new MrMeeseeksBuilder();
const mr = builder.setGoal('Open jam jar').setLifespan(12).build();
console.log(mr.getGoal()); // 'Open jam jar'
console.log(mr.getLifespan()); // 12
const { createBuilderClass } = require('builder-pattern-2');
/**
* 1. Create your domain model.
*/
function MrMeeseeks(goal, lifespan) {
this.goal = goal;
this.lifespan = lifespan;
this.getGoal = function () {
return this.goal;
};
this.getLifespan = function () {
return this.lifespan;
};
}
/**
* 2. Generate the Builder class.
*/
const MrMeeseeksBuilder = createBuilderClass(MrMeeseeks);
/**
* 3. Use it!
*/
const builder = new MrMeeseeksBuilder();
const mr = builder.setGoal('Open jam jar').setLifespan(12).build();
console.log(mr.getGoal()); // 'Open jam jar'
console.log(mr.getLifespan()); // 12
Learn more about the implementation of this library on my tech blog: