stampit API
Search…
Deep properties
The deep properties add properties to the objects created from your stamps. These are default properties of your objects. But unlike regular (shallow) properties these are deeply merged together by stamp deep merging algorithm.
1
const AuthServiceToken = stampit.deepProps({
2
Secrets: {
3
authServiceToken: process.env.AUTH_SERVICE_TOKEN
4
}
5
})
6
7
const S3KeyIdAndSecret = stampit.deepProps({
8
Secrets: {
9
S3: {
10
keyId: process.env.AWS_ACCESS_KEY_ID,
11
secret: process.env.AWS_SECRET_ACCESS_KEY
12
}
13
}
14
})
15
16
const DefaultSecrets = stampit(AuthServiceToken, S3KeyIdAndSecret) // composing them together
17
18
DefaultSecrets().Secrets.authServiceToken === process.env.AUTH_SERVICE_TOKEN
19
DefaultSecrets().Secrets.S3.keyId === process.env.AWS_ACCESS_KEY_ID
20
DefaultSecrets().Secrets.S3.secret === process.env.AWS_SECRET_ACCESS_KEY
Copied!

Descriptor merging algorithm

The deepProperties are deeply merged using stamp deep merging algorithm. See below - the Secrets value is always a new object.
1
S3KeyIdAndSecret().Secrets !== DefaultSecrets().Secrets // NEVER EQUAL! NO MATTER WHAT!
Copied!
Sometimes a regular (shallow) property has the same name as a deep property. While creating an object instance the shallow property wins. See stamp internals.
1
const NullSecrets = stampit.props({
2
Secrets: null
3
})
4
5
const AuthServiceToken = stampit.deepProps({
6
Secrets: {
7
authServiceToken: process.env.AUTH_SERVICE_TOKEN
8
}
9
})
10
11
const Result = NullSecrets.compose(AuthServiceToken)
12
13
Result().Secrets === null // the regular property overwrites deep property while creating an object
Copied!

Other ways to add deep properties

Exactly the same stamp can be created in few ways. Here they all are.
1
const mySecrets = { authServiceToken: process.env.AUTH_SERVICE_TOKEN }
2
3
const AuthServiceToken = stampit({
4
deepProps: {
5
Secrets: mySecrets
6
}
7
})
8
9
const HasLog = stampit({
10
deepProperties: {
11
Secrets: mySecrets
12
}
13
})
14
15
const HasLog = stampit.deepProps({
16
Secrets: mySecrets
17
})
18
19
const HasLog = stampit.deepProperties({
20
Secrets: mySecrets
21
})
22
23
const HasLog = stampit().deepProps({
24
Secrets: mySecrets
25
})
26
27
const HasLog = stampit().deepProperties({
28
Secrets: mySecrets
29
})
Copied!
Last modified 3yr ago