stampit API
Search…
Deep configuration
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 deep merging algorithm.
1
let Kue = stampit.deepConf({
2
Kue: {
3
name: 'kue',
4
priority: 'normal',
5
attempts: 3,
6
delay: 500,
7
ttl: 5,
8
events: false
9
}
10
})
11
12
const HighPriorityKue = stampit.deepConf({
13
Kue: {
14
priority: 'high'
15
}
16
})
17
18
const MyKue = stampit(Kue, HighPriorityKue) // composing them together
19
20
MyKue.compose.deepConfiguration.Kue.attempts === 3
21
MyKue.compose.deepConfiguration.Kue.priority === 'high'
Copied!
You can set deep configuration in static methods:
1
Kue = Kue.statics({
2
configureKue(options) {
3
return this.deepConf({Kue: options}) // create new stamp by composing parent stamp with some configuration
4
},
5
6
connectionString(connectionString) { return this.configureKue({connectionString} },
7
priority(priority) { return this.configureKue({priority} },
8
attempts(attempts) { return this.configureKue({attempts}) },
9
delay(delay) { return this.configureKue({delay}) },
10
ttl(ttl) { return this.configureKue({ttl}) },
11
events(events) { return this.configureKue({events}) }
12
})
13
14
const MyRegularJobKue = Kue.attempts(5).delay(1000).ttl(10).events(true).priority('low')
Copied!
You can access the configuration in stamp initializers (aka constructors).
1
Kue = Kue.init(function (payload, { stamp }) {
2
const conf = stamp.compose.deepConfiguration.Kue
3
this.conf = conf
4
this.redisClient = require('redis').createClient(conf.connectionString)
5
})
6
.methods({
7
put(data) {
8
return this.redisClient.put(this.queueName, { ...this.conf, data })
9
}
10
})
11
12
const myKue = Kue()
13
myKue.put({ title: 'Vasyl Boroviak', email: '[email protected]', template: 'welcome-email' })
Copied!

Descriptor merging algorithm

The deepConfiguration is deeply merged using stamp deep merging algorithm.
See below - the Kue value is always a new object.
1
Kue.compose.deepConfiguration.Kue !== MyKue.compose.deepConfiguration.Kue
2
// NEVER EQUAL! NO MATTER WHAT!
Copied!

Other ways to add deep configuration

Exactly the same stamp can be created in few ways. Here they all are.
1
const myKueConf = { Kue: { priority: 'normal' } }
2
3
const MyKue = stampit({
4
deepConf: {
5
Kue: myKueConf
6
}
7
})
8
9
const MyKue = stampit({
10
deepConfiguration: {
11
Kue: myKueConf
12
}
13
})
14
15
const MyKue = stampit.deepConf({
16
Kue: myKueConf
17
})
18
19
const MyKue = stampit.deepConfiguration({
20
Kue: myKueConf
21
})
22
23
const MyKue = stampit().deepConf({
24
Kue: myKueConf
25
})
26
27
const MyKue = stampit().deepConfiguration({
28
Kue: myKueConf
29
})
Copied!
Last modified 3yr ago