Created
January 20, 2018 02:29
-
-
Save vkarpov15/9b10c975a535d0bbfb9d83285958839e to your computer and use it in GitHub Desktop.
Modeling Stores and Products with Archetype
This file contains hidden or 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'); | |
// Generic product type, because prices are specific to each store | |
const ProductType = new Archetype({ | |
name: { | |
$type: 'string', | |
$required: true | |
} | |
}).compile('ProductType'); | |
// Extend the base `ProductType` to have a price | |
const StoreProductType = ProductType. | |
path('price', { | |
$type: 'number', | |
$required: true | |
}). | |
compile('StoreProductType'); | |
// Each store now has an array of products that have prices | |
const StoreType = new Archetype({ | |
products: { | |
$type: [StoreProductType], | |
$required: true, | |
$default: [] | |
} | |
}).compile('StoreType'); | |
// StoreType { | |
// products: | |
// [ StoreProductType { name: 'Professional AngularJS', price: 34.99 } ] } | |
console.log(new StoreType({ | |
products: [ | |
{ name: 'Professional AngularJS', price: 34.99 } | |
] | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment