The purpose of the following gist is to allow me (and interested readers) to level up our backend knowledge to design, implement and maintain Pub/Sub services in Node.js using Kafka as the message bus.
I'll be documenting my journey and learning here.
- Javascript
- Typescript
- Node.js
- Kafka/KafkaJS
- ElasticSearch
- MySQL
- Docker
- Javascript for the impatient programmers
- Deep Javascript: Theory and Techniques
- Effective Typescript
- API Design Patterns
- Building Event-Driven Microservices
Primitives have their place in software. Elegantly capturing domain values with primitives can be hazardous due to lack of type enforcement in the primitive. Let's say that we're building a text adventure game. Let's also say that we want to represent all the available classes in the roster. A naive approach might be to use a collection of strings and call it a day. The issue is that nothing blocks the code from making a change in one of those strings or even how would you enforce at the type-signature of a function whether you're handling one of the supported values for the roster.
Approach 1: Define an enum of a string
export enum MagicUser {
Mage = 'Mage',
Sorcerer = 'Sorcerer',
Wizard = 'Wizard',
}
Doing so will ensure that if you try to use a value from that type, Typescript can catch errors at design-time.
let magicalUser: MagicUser = 'Knight'; // Error
Approach 2: Using a string variants to restrict the allowed strings within the domain
export type MagicUser = 'Mage' | 'Sorcerer' | 'Wizard';