stampit API
Search…
Static deep properties
The static deep properties are properties attached to stamp itself. But unlike regular (shallow) static properties these are deeply merged together by stamp deep merging algorithm.
1
const SchemaTypes = stampit.deepStatics({
2
Types: {
3
String,
4
Number,
5
Array
6
}
7
})
8
9
const ObjectIdSchemaType = stampit.deepStatics({
10
Types: {
11
ID: String,
12
ObjectId: require('./object-id')
13
}
14
})
15
16
const Schema = stampit(SchemaTypes, ObjectIdSchemaType, { // stamp deep merging kicks in this line
17
init(data, { stamp }) {
18
// stamp.Types.String, stamp.Types.ID, ...
19
}
20
})
21
22
// Schema.Types.String
23
// Schema.Types.Number
24
// Schema.Types.Array
25
// as well as
26
// Schema.Types.ID
27
// Schema.Types.ObjectId
Copied!

Descriptor merging algorithm

The staticDeepProperties are deeply merged using stamp deep merging algorithm. See below - the Types value is always a new object.
1
SchemaTypes.Types !== Schema.Types // NEVER EQUAL! NO MATTER WHAT!
Copied!
Sometimes a regular (shallow) static property has the same name as a deep static property. The shallow static property always wins.
1
const NullTypes = stampit.statics({
2
Types: null
3
})
4
5
const NoTypesSchema = Schema.compose(NullTypes)
6
7
NoTypesSchema.Types === null // the regular property overwrites deep property
Copied!

Other ways to add static deep properties

Exactly the same stamp can be created in few ways. Here they all are.
1
const myTypes = { String, ID: String, Number, Array }
2
3
const AuthServiceToken = stampit({
4
deepStatics: {
5
Types: myTypes
6
}
7
})
8
9
const HasLog = stampit({
10
staticDeepProperties: {
11
Types: myTypes
12
}
13
})
14
15
const HasLog = stampit.deepStatics({
16
Types: myTypes
17
})
18
19
const HasLog = stampit.staticDeepProperties({
20
Types: myTypes
21
})
22
23
const HasLog = stampit().deepStatics({
24
Types: myTypes
25
})
26
27
const HasLog = stampit().staticDeepProperties({
28
Types: myTypes
29
})
Copied!
Last modified 3yr ago