Created
October 20, 2017 23:59
-
-
Save vkarpov15/0dc21e98acfb96e72d0bb9b602adb3ad to your computer and use it in GitHub Desktop.
Embedding a Subset of One Archetype's Properties in another Archetype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Archetype = require('archetype'); | |
// Simplified customer type for the sake of example. This customer | |
// type has 2 top-level properties: a string `email` and an object | |
// `name` that has nested string properties `first` and `last`. | |
const CustomerType = new Archetype({ | |
name: { | |
first: { $type: 'string' }, | |
last: { $type: 'string' } | |
}, | |
email: { | |
$type: 'string' | |
} | |
}).compile('CustomerType'); | |
// The `.pick()` function takes an array of property names. Once you | |
// compile the new archetype below, you'll get a type with only one | |
// property, `name`. | |
const RequestCustomerType = CustomerType.pick(['name']).compile('RequestCustomerType'); | |
// `RequestCustomerType` is an archetype, which means you can use it | |
// as a property value. Remember that composability is a core design | |
// goal of Archetype: you can re-use all or part of an archetype in | |
// another archetype. | |
const RequestType = new Archetype({ | |
customer: { $type: RequestCustomerType }, | |
createdAt: { $type: Date } | |
}).compile('RequestType'); | |
const customer = new CustomerType({ | |
name: { first: 'Val', last: 'Karpov' }, | |
email: '[email protected]' | |
}); | |
const request = new RequestType({ | |
customer, | |
createdAt: new Date() | |
}); | |
// "RequestCustomerType { name: { first: 'Val', last: 'Karpov' } }" | |
console.log(request.customer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment