This section focuses on the database design.

Considering that there is not much correlation between the data, we decided to use MongoDB. There are two main ways for the backend to communicate with the database: using mongodb or Mongoose. With reference to this article, the similarities and differences between the two are compared. Mongoose is a better fit for a situation where our data is not too complex and we want to manage it more easily.

For this project, there are four main tables:

  • Tasks Records the task information
  • Scores User’s total score
  • Logs Logs of the user
  • Users User information

Detail field: 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
}
Copy the code

When creating Mongoose’s Schema, the code is as follows:

// 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}})Copy the code

To set the taskId increment, use the Mongoose-Auto-increment plug-in

/ /... Input library code at the top of the file
// define task schema

taskSchema.plugin(autoIncrement.plugin, {
  model: 'TaskModel'.field: 'taskId'.startAt: 0
});
Copy the code

When this is done, the taskId value increases each time a new task is written.

Logs, scores, and Users are not too different.

In fact, there is some redundancy in this design. Because task is one-to-one with user and score is one-to-one with user, you can keep only score in the Tasks collection.

With the database designed, you can start developing the back-end functionality. The back-end development process will be documented in the next article.

Series of articles:

React + MobX + Electron + node. js + MongoDB full stack project development practice (zero) Introduction

React + MobX + Electron + node.js + MongoDB

Allow decorators

React + MobX + Electron + node. js + MongoDB

React + MobX + Electron + node. js + MongoDB full stack project development practice (4) — API design