Small program cloud development practice series 02 — cloud database

Time:2020-2-9

Small program cloud development practice series 02 -- cloud database
I used to use relational database for the first time. I want to share my limited experience with you. I hope it will help developers who are just using NoSQL like me.
First of all, let’s talk about the NoSQL integrated in wechat applet cloud development. The official didn’t say what kind of NoSQL database it was, but from the development documents, exposed APIs, and discussions in the official forum, it should be a simplified version of mongodb. It should be pointed out that the development documents of wechat applet about cloud database are very brief. For users like me who do not have much NoSQL experience, they need to refer to the relevant documents of mongodb.

Next, I will focus on one of the most uncomfortable pain points when I use this NoSQL cloud database – document level atomic operation. We often use atomic operations to avoid conflicts when multiple users edit the same field at the same time. Before I use it, I am worried about the difference between schema and not. But I found that I am quite used to it and I like it very much. I will tell you more later. Now let’s see that mongodb only supports document level atomic operations. For me, this restriction encourages me to try to put all relationships in one document. At the beginning, I was a little resistant to this, especially for people coming from relational databases. What bothers me even more is that the cloud database of wechat applet cloud development integration is a simplified version of mongodb, which only provides very limited atomic operation instructions. For some common document level atomic operations, I have to conceive my own solution without providing a direct corresponding command. Here are two such problems I encountered in the actual development and my solutions:

1.
Application scenario: for a video, I need a field called total “likes”. When a user clicks “like”, the field increases by 1. When a user cancels “like”, the field decreases by 1.
Pain point: the applet cloud database only provides atomic operation of increasing instruction, but not decreasing instruction.

const _ = db.command
db.collection('video').doc('video-id').update({
  data: {
    total_likes: _.inc(1)
  }
})

Solution: to implement the decreasing atomic operation, just pass a negative number into the increasing instruction, such as

data: {
  total_likes: _.inc(-1)
}

2.

Application scenario: for an online course, I need a field called subscribers to record how many people have subscribed to the course. When a user clicks “subscription”, the field shall record the ID, name and picture of the user; when a user cancels “subscription”, the user shall be deleted from the subscribers field.
Pain point: we will naturally think of using array data type to maintain the subscribers field. Although the applet cloud database provides some atomic operations for arrays, such as push, pop, shift and unshift, it is unable to unsubscribe the atomic operations for this scenario, because the official has not yet provided the API for manual locking.
Solution: discard array and use the object data type instead to maintain the subscribers field. The final data looks like this:

{
    "subscribers": {
        "userID-1": {
            "Name": "Xiaoming",
            "avatar": "https://avatar-1.com"
        },
        "userID-2": {
            "Name": "Xiaohong",
            "avatar": "https://avatar-2.com"
        },
        "userID-3": {
            "Name": "Xiao Li",
            "avatar": "https://avatar-3.com"
        },
        ...
    }
}

Atomic operation when there is a user subscription:

const subscriber = "subscribers." + user.id;
db.collection('class').where({
  _id: 'classID',
}).limit(1).update({
  data: {
    [subscriber]: {
      avatar: user.avatar,
      name: user.name,
    }
  }
})

Atomic action when a user unsubscribes:

const subscriber = "subscribers." + user.id;
db.collection('class').doc('classID').update({
  data: {
    [subscriber]: _.remove()
  }
})

As mentioned earlier, I like schema free because it is very suitable for rapid iterative development. Moreover, because the cloud database uses a JSON like data structure, for full stack developers, the front end can basically define the data structure. Such a development process is very suitable for small teams, does not need large parallel development, highlights the communication efficiency and the contingency of product demand. By the way, wechat applet cloud development capability is supported from the basic library 2.2.3, but if you want to support all versions of the basic library, you canapp.json / game.jsonAdd field in"cloud": true

Chapter 1 of this series: practical series of cloud development of applets 01 — cloud environment settings

Meetup activity registration group is a small program of activity registration and appointment tool that I recently developed. This series of articles is mainly about my experience in developing this small program. Interested partners can scan the following QR code to enter my applet.
Small program cloud development practice series 02 -- cloud database