假设我有 3 个 node.js 项目(1 个应用程序后端、1 个应用程序管理后端、1 个分析 api)。在每个项目中,我都有一个模型架构调用贷款。
{
attributes: {
userId: { type: String, required: true, index: true, ref: 'users', comment: '用户id' },
amount: { type: Number, required: true, min: 0},
totalAmount: { type: Number, required: true, min: 0},
penaltyInterest: { type: Number, min: 0, required: true, default:
0 }
}
methods: {
getFee () {//some calculation ops
}
save() {//some db ops
}
sendTo3rdComponent() {//some network ops
}
}
这个模型有:一些方法,它的模式设计,api实现。我如何在其他两个项目中重用它?
在多个项目中重用设计和 API 非常重要。
通常我们通过将组件公开为 npm 包来重用该组件。然而,这个组件有它自己的数据库操作和网络操作。将其作为 npm 包是否可能且正确?
另一种选择就像eggjs https://eggjs.org/zh-cn/tutorials/progressive.html
那么除了复制粘贴之外还有什么优雅的解决方案呢?