Passing an ID to Another Schema in a Schema Definition
Is there a way to pass an id to another Schema in the schema definition?
In the above example ProductOption actually has a
export class Product extends Schema.Class<Product>("Product")({
id: Schema.optionalWith(ProductId, {
default: () => ProductId.make(generateId()),
}),
title: Schema.String,
description: Schema.NullOr(Schema.String),
status: Schema.Literal("draft", "published"),
options: Schema.Array(ProductOption),
}) {}export class Product extends Schema.Class<Product>("Product")({
id: Schema.optionalWith(ProductId, {
default: () => ProductId.make(generateId()),
}),
title: Schema.String,
description: Schema.NullOr(Schema.String),
status: Schema.Literal("draft", "published"),
options: Schema.Array(ProductOption),
}) {}In the above example ProductOption actually has a
productIdproductId key. Now I have to pass that in manually when creating my Product.const productEntity = ProductEntity.make({
...validatedProduct,
id: productId,
options: validatedProduct.options.map((option) => {
const optionId = ProductOptionId.make(generateId());
return ProductOptionEntity.make({
...option,
id: optionId,
productId: productId,
values: option.values.map((value) => {
return ProductOptionValueEntity.make({
...value,
id: ProductOptionValueId.make(generateId()),
optionId: ProductOptionId.make(optionId),
});
}),
});
}),
});const productEntity = ProductEntity.make({
...validatedProduct,
id: productId,
options: validatedProduct.options.map((option) => {
const optionId = ProductOptionId.make(generateId());
return ProductOptionEntity.make({
...option,
id: optionId,
productId: productId,
values: option.values.map((value) => {
return ProductOptionValueEntity.make({
...value,
id: ProductOptionValueId.make(generateId()),
optionId: ProductOptionId.make(optionId),
});
}),
});
}),
});