Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Embedding a Subset of One Archetype's Properties in another Archetype
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: 'fake@test.tk'
});
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
You can’t perform that action at this time.