All Projects → poppinss → manager

poppinss / manager

Licence: MIT License
The builder (Manager) pattern implementation used by AdonisJs

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to manager

go-builder-generator-idea-plugin
IntelliJ IDEA / GoLand golang plugin for generating Builder pattern code of golang struct.
Stars: ✭ 21 (+50%)
Mutual labels:  builder-pattern
design-patterns-php
All Design Patterns Samples in PHP
Stars: ✭ 43 (+207.14%)
Mutual labels:  builder-pattern
adonis-sail
⛵Generate a ready-to-use local docker environment for your Adonis application
Stars: ✭ 36 (+157.14%)
Mutual labels:  adonisjs
adonis-lucid-soft-deletes
Addon for soft deletes AdonisJS Lucid ORM
Stars: ✭ 55 (+292.86%)
Mutual labels:  adonisjs
electro
A free and open-source Automatic Account Creator (AAC) written in Javascript Stack;
Stars: ✭ 20 (+42.86%)
Mutual labels:  adonisjs
adonisjs-create-react-app
Adonisjs + Create React App Boilerplate
Stars: ✭ 22 (+57.14%)
Mutual labels:  adonisjs
saas-react-starter-kit-boilerplate
SaaStr is a React SaaS boilerplate to kickstart your new SaaS adventure as fast as possible. Built on top of Adonis JS for the BackEnd and React Starter Kit for the Front-End
Stars: ✭ 100 (+614.29%)
Mutual labels:  adonisjs
adonisjs-electron
Boilerplate for adonisjs + Electron
Stars: ✭ 17 (+21.43%)
Mutual labels:  adonisjs
youtube-adonisjs-iniciando
Project created in the AdonisJS video
Stars: ✭ 16 (+14.29%)
Mutual labels:  adonisjs
server
👨🏾‍🍳 Server for Ferdi that you can re-use to run your own
Stars: ✭ 26 (+85.71%)
Mutual labels:  adonisjs
adonis-fcm
Firebase Cloud Messaging for AdonisJS
Stars: ✭ 19 (+35.71%)
Mutual labels:  adonisjs
adonis-support-ticket
A support ticket application in AdonisJs
Stars: ✭ 28 (+100%)
Mutual labels:  adonisjs
Flapi
Flapi is an API generator for Java, which generates 'smart' interfaces for improved fluency in your code.
Stars: ✭ 56 (+300%)
Mutual labels:  builder-pattern
gerar-boletos
Biblioteca em Node.js para geração de boletos utilizando PDFKit.
Stars: ✭ 81 (+478.57%)
Mutual labels:  adonisjs
assembler
Set of commands to build and serve AdonisJS projects, along with `make:` commands
Stars: ✭ 25 (+78.57%)
Mutual labels:  adonisjs
flexiblesearchbuilder
Flexible search query builder is SAP Hybris Commerce extension (released as a library) that provides developer-friendly way to build flexible search queries. The aim of this extension is to write().flexibleSearchQueries().easily() in compile-time safe manner without a need to remember the syntax.
Stars: ✭ 15 (+7.14%)
Mutual labels:  builder-pattern
XENA
XENA is the managed remote administration platform for botnet creation & development powered by blockchain and machine learning. Aiming to provide an ecosystem which serves the bot herders. Favoring secrecy and resiliency over performance. It's micro-service oriented allowing for specialization and lower footprint. Join the community of the ulti…
Stars: ✭ 127 (+807.14%)
Mutual labels:  adonisjs
adonis-modules
📦 Discover all AdonisJS packages developed by the community
Stars: ✭ 23 (+64.29%)
Mutual labels:  adonisjs
adonis-cache
Cache provider for AdonisJS framework
Stars: ✭ 66 (+371.43%)
Mutual labels:  adonisjs
adonisjs-laravel-mix
An AdonisJs fullstack application blueprint with Laravel Mix
Stars: ✭ 17 (+21.43%)
Mutual labels:  adonisjs

Manager Pattern

Implementation of the Manager pattern used by AdonisJS

gh-workflow-image typescript-image npm-image license-image synk-image

Manager pattern is a way to ease the construction of objects of similar nature. To understand it better, we will follow an imaginary example through out this document.

Table of contents

Scanerio

Let's imagine we are creating a mailing library and it supports multiple drivers like: SMTP, Mailgun, PostMark and so on. Also, we want the users of our library to use each driver for multiple times using different configuration. For example:

Using the Mailgun driver with different accounts. Maybe one account for sending promotional emails and another account for sending transactional emails.

Basic implementation

The simplest way to expose the drivers, is to export them directly and let the consumer construct instances of them. For example:

import { Mailgun } from 'my-mailer-library'

const promotional = new Mailgun(configForPromtional)
const transactional = new Mailgun(configForTransactional)

promotional.send()
transactional.send()

The above approach works perfect, but has few drawbacks

  • If the construction of the drivers needs more than one constructor arguments, then it will become cumbersome for the consumer to satisfy all those dependencies.
  • They will have to manually manage the lifecycle of the constructed objects. Ie promotional and transactional in this case.

Using a Manager Class

What we really need is a Manager to manage and construct these objects in the most ergonomic way.

Step1: Define a sample config object.

First step is to move the mappings to a configuration file. The config mimics the same behavior we were trying to achieve earlier (in the Basic example), but defines it declaratively this time.

const mailersConfig = {
  default: 'transactional',

  list: {
    transactional: {
      driver: 'mailgun',
    },

    promotional: {
      driver: 'mailgun',
    },
  },
}

Step 2: Create manager class and accept mappings config

import { Manager } from '@poppinss/manager'

class MailManager implements Manager {
  protected singleton = true

  constructor(private config) {
    super({})
  }
}

Step 3: Using the config to constructor drivers

The Base Manager class will do all the heavy lifting for you. However, you will have to define certain methods to resolve the values from the config file.

import { Manager } from '@poppinss/manager'

class MailManager implements Manager {
  protected singleton = true

  protected getDefaultMappingName() {
    return this.config.default
  }

  protected getMappingConfig(mappingName: string) {
    return this.config.list[mappingName]
  }

  protected getMappingDriver(mappingName: string) {
    return this.config.list[mappingName].driver
  }

  constructor(private config) {
    super({})
  }
}

Step 4: Move drivers construction into the manager class

The final step is to write the code for constructing drivers. The Base Manager uses a convention for this. Anytime the consumer will ask for the mailgun driver, it will invoke createMailgun method. So the convention here is to prefix create followed by the camelCase driver name.

import { Manager } from '@poppinss/manager'

class MailManager implements Manager {
  // ... The existing code

  public createMailgun(mappingName, config) {
    return new Mailgun(config)
  }

  public createSmtp(mappingName, config) {
    return new Smtp(config)
  }
}

Usage

Once done, the consumer of the Mailer class just needs to define the mappings config and they are good to go.

const mailersConfig = {
  default: 'transactional',

  list: {
    transactional: {
      driver: 'mailgun',
    },

    promotional: {
      driver: 'mailgun',
    },
  },
}

const mailer = new MailManager(mailersConfig)

mailer.use('transactional').send()
mailer.use('promotional').send()
  • The lifecycle of the mailers is now encapsulated within the manager class. The consumer can call mailer.use() as many times as they want, without worrying about creating too many un-used objects.
  • They just need to define the mailers config once and get rid of any custom code required to construct individual drivers.

Extending from outside-in

The Base Manager class comes with first class support for adding custom drivers from outside-in using the extend method.

const mailer = new MailManager(mailersConfig)

mailer.extend('postmark', (manager, mappingName, config) => {
  return new PostMark(config)
})

The extend method receives a total of three arguments:

  • The manager object is the reference to the mailer object.
  • The name of the mapping inside the config file.
  • The actual configuration object.
  • The callback should return an instance of the Driver.

Driver Interface

The Manager class can also leverage static Typescript types to have better intellisense support and also ensure that the drivers added using the extend method adhere to a given interface.

Defining Drivers Interface

Following is a dummy interface, we expect all drivers to adhere too

interface DriverContract {
  send(): Promise<void>
}

Passing interface to Manager

import { Manager } from '@poppinss/manager'
import { DriverContract } from './Contracts'

class MailManager implements Manager<DriverContract> {}

Mappings Type

The mappings config currently has any type and hence, the mailer.use method cannot infer correct return types.

In order to improve intellisense for the use method. You will have to define a type for the mappings too.

type MailerMappings = {
  transactional: Mailgun
  promotional: Mailgun
}

type MailerConfig = {
  default: keyof MailerMappings
  list: {
    [K in keyof MailerMappings]: any
  }
}

const mailerConfig: MailerConfig = {
  default: 'transactional',

  list: {
    transactional: {
      driver: 'mailgun',
      // ...
    },

    promotional: {
      driver: 'mailgun',
      // ...
    },
  },
}

Finally, pass the MailerMappings to the Base Manager class

import { DriverContract, MailerMappings } from './Contracts'

class MailManager implements Manager<DriverContract, DriverContract, MailerMappings> {}

Once mailer mappings have been defined, the use method will have proper return types.

Mapping suggestions

Return type

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].