stampit API
  • Introduction
  • Essentials
    • What is a Stamp
    • Installation
    • Specification
      • Merging algorithm
      • Object creation internals
    • FAQ
  • API
    • Quick start
    • Basics
    • Methods
    • Properties
    • Deep properties
    • Initializers
    • Static properties
    • Static deep properties
    • Configuration
    • Deep configuration
    • Composers
    • Property descriptors
    • Static property descriptors
    • Name
  • Ecosystem
    • Ecosystem Overview
    • @stamp/collision
    • @stamp/required
    • @stamp/privatize
    • @stamp/named (DEPRECATED)
    • @stamp/instanceof
Powered by GitBook
On this page
  • Descriptor merging algorithm
  • Other ways to add deep configuration
  1. API

Deep configuration

PreviousConfigurationNextComposers

Last updated 6 years ago

Deep Configuration is a storage of an arbitrary data in the .deepConfiguration metadata of stamp's descriptor. But unlike regular (shallow) configuration these are deeply merged together by stamp .

let Kue = stampit.deepConf({
  Kue: {
    name: 'kue',
    priority: 'normal',
    attempts: 3,
    delay: 500,
    ttl: 5,
    events: false
  }
})

const HighPriorityKue = stampit.deepConf({
  Kue: {
    priority: 'high'
  }
})

const MyKue = stampit(Kue, HighPriorityKue) // composing them together

MyKue.compose.deepConfiguration.Kue.attempts === 3
MyKue.compose.deepConfiguration.Kue.priority === 'high'
Kue = Kue.statics({
  configureKue(options) {
    return this.deepConf({Kue: options}) // create new stamp by composing parent stamp with some configuration
  },

  connectionString(connectionString) { return this.configureKue({connectionString} },
  priority(priority)                 { return this.configureKue({priority} },
  attempts(attempts)                 { return this.configureKue({attempts}) },
  delay(delay)                       { return this.configureKue({delay}) },
  ttl(ttl)                           { return this.configureKue({ttl}) },
  events(events)                     { return this.configureKue({events}) }
})

const MyRegularJobKue = Kue.attempts(5).delay(1000).ttl(10).events(true).priority('low')
Kue = Kue.init(function (payload, { stamp }) {
  const conf = stamp.compose.deepConfiguration.Kue
  this.conf = conf
  this.redisClient = require('redis').createClient(conf.connectionString)
})
.methods({
  put(data) {
    return this.redisClient.put(this.queueName, { ...this.conf, data })
  }
})

const myKue = Kue()
myKue.put({ title: 'Vasyl Boroviak', email: 'vasyl@example.com', template: 'welcome-email' })

Descriptor merging algorithm

See below - the Kue value is always a new object.

Kue.compose.deepConfiguration.Kue !== MyKue.compose.deepConfiguration.Kue
// NEVER EQUAL! NO MATTER WHAT!

Other ways to add deep configuration

Exactly the same stamp can be created in few ways. Here they all are.

const myKueConf = { Kue: { priority: 'normal' } }

const MyKue = stampit({
  deepConf: {
    Kue: myKueConf
  }
})

const MyKue = stampit({
  deepConfiguration: {
    Kue: myKueConf
  }
})

const MyKue = stampit.deepConf({
  Kue: myKueConf
})

const MyKue = stampit.deepConfiguration({
  Kue: myKueConf
})

const MyKue = stampit().deepConf({
  Kue: myKueConf
})

const MyKue = stampit().deepConfiguration({
  Kue: myKueConf
})

You can set deep configuration in :

You can access the configuration in stamp (aka constructors).

The deepConfiguration is deeply merged using stamp .

static methods
initializers
deep merging algorithm
deep merging algorithm