使用 ReactiveMongo 库表示要存储在 mongo 中的产品
Representing a product to store in mongo using ReactiveMongo library
我正在尝试为我的 mongodb 集合 "products" 建模产品。
所以看起来像这样:
{
"_id": "abc123",
"sku": "sku123",
"name": "some product name",
"description": "this is a description",
"specifications": {
"name" : "name1",
"metadata": [
{"name1": "value1"},
{"name2": "value2"},
]
}
}
所以我的案例 classes 看起来像:
case class Product(
id: String,
sku: String,
name: String,
description: String,
specifications: Specification
)
case class Specification(
name: String,
metadata: Metadata
)
case class Metadata(
kvp: Map[String, String]
)
所以现在我必须为每种类型的产品、规格和元数据创建处理程序,以便当数据为 read/written 到 mongo 时,它将执行正确的数据映射?
如何映射元数据案例class,有点困惑?
如documentation中所示,大多数时候可以简单地生成Reader
/Writer
。
import reactivemongo.api.bson._
// Add in Metadata companion object to be in default implicit scope
implicit val metadataHandler: BSONDocumentHandler[Metadata] = Macros.handler
// Add in Specification companion object to be in default implicit scope
implicit val specHandler: BSONDocumentHandler[Specification] = Macros.handler
// Add in Product companion object to be in default implicit scope
implicit val productHandler: BSONDocumentHandler[Product] = Macros.handler
那么任何使用 BSON Reader
/Writer
类型类的函数都将接受 Product
/Specification
/Metadata
:
BSON.writeDocument(Product(
id = "X",
sku = "Y",
name = "Z",
description = "...",
specifications = Specification(
name = "Lorem",
metadata = Metadata(Map("foo" -> "bar"))))).foreach { doc: BSONDocument =>
println(BSONDocument pretty doc)
}
/* {
'id': 'X',
'sku': 'Y',
'name': 'Z',
'description': '...',
'specifications': {
'name': 'Lorem',
'metadata': {
'kvp': {
'foo': 'bar'
}
}
}
} */
我正在尝试为我的 mongodb 集合 "products" 建模产品。
所以看起来像这样:
{
"_id": "abc123",
"sku": "sku123",
"name": "some product name",
"description": "this is a description",
"specifications": {
"name" : "name1",
"metadata": [
{"name1": "value1"},
{"name2": "value2"},
]
}
}
所以我的案例 classes 看起来像:
case class Product(
id: String,
sku: String,
name: String,
description: String,
specifications: Specification
)
case class Specification(
name: String,
metadata: Metadata
)
case class Metadata(
kvp: Map[String, String]
)
所以现在我必须为每种类型的产品、规格和元数据创建处理程序,以便当数据为 read/written 到 mongo 时,它将执行正确的数据映射?
如何映射元数据案例class,有点困惑?
如documentation中所示,大多数时候可以简单地生成Reader
/Writer
。
import reactivemongo.api.bson._
// Add in Metadata companion object to be in default implicit scope
implicit val metadataHandler: BSONDocumentHandler[Metadata] = Macros.handler
// Add in Specification companion object to be in default implicit scope
implicit val specHandler: BSONDocumentHandler[Specification] = Macros.handler
// Add in Product companion object to be in default implicit scope
implicit val productHandler: BSONDocumentHandler[Product] = Macros.handler
那么任何使用 BSON Reader
/Writer
类型类的函数都将接受 Product
/Specification
/Metadata
:
BSON.writeDocument(Product(
id = "X",
sku = "Y",
name = "Z",
description = "...",
specifications = Specification(
name = "Lorem",
metadata = Metadata(Map("foo" -> "bar"))))).foreach { doc: BSONDocument =>
println(BSONDocument pretty doc)
}
/* {
'id': 'X',
'sku': 'Y',
'name': 'Z',
'description': '...',
'specifications': {
'name': 'Lorem',
'metadata': {
'kvp': {
'foo': 'bar'
}
}
}
} */