本节重点记录一下数据库的设计。mongodb
考虑到数据之间不会有太多的关联,决定使用 MongoDB。这样在后端与数据库通讯时就有两种主要方式:使用 mongodb 或者 Mongoose。参考了这篇文章,比较了一下二者的异同。咱们的数据不会很复杂,而且但愿更便捷地管理数据,这种状况下 Mongoose 更加适合。数据库
对于这个项目来讲,主要的表有 4 个:json
详细字段: tasks后端
{
"taskId": 1,
"userId": 0,
"name": "Fruit",
"desc": "Eat fruit every day",
"type": "task",
"isOneTime": false,
"score": 2,
"maxTimes": 3,
"timesUsedToday": 2,
"createdAt": 1573404126959,
"lastUpdatedAt": 1573404126959
}
复制代码
在建立 Mongoose 的 Schema 时代码以下:数据库设计
// define task schema
const taskSchema = mongoose.Schema({
userId: { type: String, required: true },
taskId: { type: Number },
name: { type: String, required: true },
desc: { type: String, required: true },
type: { type: String, required: true },
isOneTime: { type: Boolean, required: true },
score: { type: Number, required: true },
maxTimes: { type: Number, required: true },
timesUsedToday: { type: Number, required: true },
createdAt: { type: Date, required: true },
lastUpdatedAt: { type: Date, required: true }
})
复制代码
设置taskId
自增,须要使用 mongoose-auto-increment
插件mongoose
// ... 文件顶端引入库的代码
// define task schema
taskSchema.plugin(autoIncrement.plugin, {
model: 'TaskModel',
field: 'taskId',
startAt: 0
});
复制代码
这样设置好后,每次写入新的 task 其中的 taskId 都会自增。post
logs、 scores 和 users 差不太多。ui
其实这样的设计有一些冗余。由于 task 和 user 一一对应,score 和 user 一一对应,能够只保留 tasks 集合中的 score。spa
在数据库设计好以后,就能够着手开发后端功能了。后端开发过程会在下一篇文章中记录。插件
系列文章:
React + MobX + Electron + Node.js + MongoDB 全栈项目开发实践(零)前言
React + MobX + Electron + Node.js + MongoDB 全栈项目开发实践(一)
React + MobX + Electron + Node.js + MongoDB 全栈项目开发实践(二)容许 decorator
React + MobX + Electron + Node.js + MongoDB 全栈项目开发实践(三)使用 MobX 实现流畅数据流
React + MobX + Electron + Node.js + MongoDB 全栈项目开发实践(四)—— API 设计