Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kavignon/a7d912a8062e5f38cba181e5e0e30a0b to your computer and use it in GitHub Desktop.
Save Kavignon/a7d912a8062e5f38cba181e5e0e30a0b to your computer and use it in GitHub Desktop.
Picking up TS, Node, event-driven and Kafka as a backend engineer

Purpose

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.

Tech stack to explore

  • Javascript
  • Typescript
  • Node.js
  • Kafka/KafkaJS
  • ElasticSearch
  • MySQL
  • Docker

Resources for learning

Backend development through Javascript/Typescript

Containerization

Javascript / Typescript

Language features

Discriminated unions & enums to capture specific values

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';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment