我们有一个内容模型,我们在中央级别设置内容,但如果用户愿意,他们可以将内容编辑到附属级别。如果他们不想对从中心设置的内容进行任何更改,则不必进行更改。
让我们假设一个简单的api端点,它只接受companyId。这是它的回应方式:
GET /offers (central values)
[{
offerId: 1,
title: "Central offer title",
paragraph: "Lorem ipsum central",
price: 100
}, {...more offers}]公司123
/offers?companyId=123
[{
offerId: 1,
title: "Company offer title",
paragraph: "Lorem ipsum central", // Inherited from central
price: 125
}, {...more offers}]456公司,它是123的子公司
/offers?companyId=456
[{
offerId: 1,
title: "Company offer title", // Inherited from Company 1
paragraph: "Lorem ipsum subsidiary",
price: 125, // Inherited from Company 1
custom_field: "A completely custom field for subsidiary" // Field only available for subsidiary
}, {...more offers}]在以前的实现中,我们已经完成了以下内容:
{
offerId: 1,
values: [
{
companyId: null,
title: "Central offer title",
paragraph: "Lorem ipsum central",
price: 100
},
{
companyId: 123,
title: "Company offer title",
price: 125
},
{
companyId: 456,
paragraph: "Lorem ipsum subsidiary",
custom_field: "A completely custom field for subsidiary"
}
]
}然后在应用程序中,我们对此进行了编译,以便针对子公司的值是特定的,但仍继承了来自中央公司或母公司的数据。
现在我们将要编写新的应用程序,应该再次允许这种类型的内容继承,我们对这种方法有疑问,并想知道是否有其他方法来模拟这种情况。
还有什么其他方法可以模拟这种类型的行为?
发布于 2021-06-11 20:05:27
你在用mongoose吗?如果是,请使用Discriminators
const event1 = new Event({ time: Date.now() });
const event2 = new ClickedLinkEvent({ time: Date.now(), url: 'google.com' });
const event3 = new SignedUpEvent({ time: Date.now(), user: 'testuser' });在这个精读中有更多关于这个主题的内容:https://dev.to/helenasometimes/getting-started-with-mongoose-discriminators-in-expressjs--22m9
如果不是,那么我认为您应该遵循与mongoose相同的方法,将每个公司作为单独的文档。因为在您的旧设置中,有几件事:
如果您只按公司搜索(而不是您所说的提供ID ),您可以将它们反向搜索。但这仍然很奇怪。我将它们分成两个单独的东西,并在它们之间使用引用。通过这种方式,您可以:
这种方法会让你做相反的事情(向公司提供服务)。
https://stackoverflow.com/questions/67936533
复制相似问题