mongodb - React Native 游戏、MongoDB 或 Firestore
问题描述
我正在为安卓开发一款多人反应原生游戏,它只是一个文本游戏。目前,我一直只在客户端工作,但我正在慢慢达到需要选择一种管理数据的方式的地步。因为我一直在尝试学习新东西——也是第一次使用 RN——所以我遇到了 Firestore,我很好奇它是否是我项目中的一个可行选择。
例如,在这个游戏中,您可以购买/开设公司。如果我要在 nodejs + MongoDB 中处理此操作,我会将 JWT 令牌发送到server/api/newcompany
.
假设我有 2 个 MongoDB 模型,Users
其中包含所有用户信息和Company
游戏中所有公司的列表。首先,我会检查用户是否有足够的钱来开设公司,然后基于此返回一些消息,我的代码将是这样的:
// routes.js
...
router.post('/api/newcompany',AuthCheck, Company.new)
// controllers/company.js
...
new: async function (req, res, next) {
if (!req.body.companyType || !validCompanyType) {
return res.status(200).send('invalid/missing message and other failed checks')
}
//assuming token data would be passed from auth middleware and would have company prices somewhere
const user = await Users.find({id:token.userId})
if (user.money < companyPrice) {
res.status(200).send('no money')
} else {
const newCompany = new Company()
newCompany.name = req.body.name
...
new.Companny.save(function(){
res.status(200).send('all good')
})
}
}
查看 Firestore,我将拥有与上述相同的 2 个文档。Users
带有由其 ID 和文档标识的子文档(用户)列表Companies
。
我可以进行与上述相同的用户检查吗?我将如何处理?我会使用 Firestore 规则来检查用户是否有足够的钱,还是会使用我的服务器和管理 SDK 作为中介来处理更复杂的操作并使用直接 Firestore 连接来检索数据?
谢谢你的时间。
解决方案
推荐阅读
- scala - 如何为要在 from_json 中使用的 json 定义模式来解析值
- react-native - 如何将 Metro bundler 配置为仅包含某些特定文件?
- swift - 核心数据没有 icloud 同步...再次
- c++ - 在 C++ 中是否可以使用 if 墙代替其他东西?
- node.js - 如何在导入之前定义环境变量?
- javascript - 如何平滑 GPS 点的高程增益
- python - 如何在 python 类中动态定义方法?
- css - 如何防止在 dir="RTL" 上反转 CSS Flex / Grid 布局
- typescript - switchMap 操作符不执行返回的 observable
- python - 带有 test/ 和 src/ 的 Python unittest 没有找到 src 模块