Hi everyone, welcome to the ninth chapter in our Tencent Cloud Developer Associate course, WeChat Mini Program CloudBase. At the end of this chapter, you'll be able to use WeChat Mini Program CloudBase and understand different Cloud development modes. In this chapter, we'll cover two sections, overview of WeChat Mini Program CloudBase and overview of Tencent CloudBase. Let's get started with Section 1, overview of WeChat Mini Program CloudBase. In this video, we'll cover an overview of WeChat Mini Program CloudBase, its use of Cloud databases, it's use of Cloud storage, and its use of Cloud functions. What is WeChat Mini Program CloudBase? WeChat Mini Program CloudBase provides multiple basic capabilities that allow the developer to seamlessly integrate and create the back-end of an application. In this sense, CloudBase is a service designed for and used by application developers. CloudBase provides the basic capabilities of Cloud databases, Cloud storage, Cloud functions, and Cloud calls. The first fundamental capability of CloudBase is the Cloud database. Since Cloud WeChat Mini Program development provides a JSON database that can be operated in the front end of the WeChat Mini Program and also read or write in the Cloud function, there is no need for developers to build their own database. The second capability of CloudBase is Cloud storage. Cloud WeChat Mini Program development combined with CDN in the front end, can be used to directly upload or download Cloud files without having to build your own storage. Third, a Cloud function is a piece of code running in the Cloud with no need to manage the server. It supports multiple front-end development tools. Fourth, a Cloud call is a native WeChat service integration. With a Cloud call, any WeChat Mini Program would be able to use the open interface without performing authentication for server side calls, access to open data and other capabilities. Now let's look at the steps of how to use WeChat Mini Program CloudBase. First of all, you need to create a CloudBase template that will correspond to the type of the application you are building. The next step is to activate CloudBase. After activating CloudBase, you can try out an existing WeChat Mini Program demo. Lastly, you can view this demo in the console. Here is how to create a CloudBase template. First, in the first new project panel, you can select an empty directory where you have to complete the AppID. This AppID is a unique identifier that identifies the API call. Then check Cloud development Quickstart project, and click "Create" to get a sample WeChat Mini Program that shows the basic Cloud development capabilities. Then you can choose to use no Cloud service or use WeChat Mini Program CloudBase as the back-end. Now, note that Cloud development capabilities are supported from base library version 2.2.3. Starting from base library 2.4.1, Cloud development is available in WeChat Mini Program plug-ins. When using Cloud development in plugins, the Cloud resources of the plugin party are used instead of the Cloud resources of the host, which is the same as the WeChat Mini Programs in terms of usage. Now, if you've already created a CloudBase template, you need to select your language. After creating your first Cloud development WeChat Mini Program, you need to open CloudBase before you can use its Cloud development capabilities. On the left side of the Developer Tools toolbar, click the Cloud Base button to open the Cloud development console. Follow the prompts to open the Cloud development window, and then create a Cloud environment. Now by default, you can create two environments that are isolated from each other, and each environment contains separate database instances, storage spaces, Cloud function configurations, and other resources. Each environment is identified by a unique environment ID, and the initially created environment automatically becomes the default environment. Once you have created a basic project, you will be offered the option to access an overview of the back-end console. The Cloud development console is where Cloud development resources are managed. The console provides four capabilities. The first capability is operation analysis. You can view Cloud development monitoring, quota usage, and user access. Second, you can manage the database in the console. Third, you can view and manage the storage space. Fourth, you can also view Cloud function lists, configurations, and logs in the back-end console. Now as you can see, the chart informs you of how much of the default quota you have already used. Now let's look at the architecture of WeChat Mini Program CloudBase. Keep in mind that the services of CloudBase are provided by Tencent Cloud. CloudBase provides a nice and easy to use interface. Using the Cloud back-end capability integrated with WeChat Mini Program Cloud development, developers do not need to care about servers and domain names. The serverless architecture is integrated into the developer tools. In this way, the function as a service and back-end as a service make the development process easier and faster for developers. Regarding billing, while WeChat Mini Program CloudBase is currently free of charge, there is a quota for free usage. Once you reach the limit, you will have to pay for additional usage. Let's move on to look at the use of the Cloud database. CloudBase provides a NoSQL database where each record in the database is a JSON formatted object. A database can have multiple collections that are equivalent to tables and relational data. Each collection can be viewed as a JSON array, where each object in the array is a record or document, and the record is in the format of a JSON object. This table here shows how the concepts and the relational database correspond to the concepts and a document-based at database. The Cloud database provides certain data types, such as string, number, object, array, boolean, null, and so on. You can basically store any type of information like in any other Cloud databases. But it is important to understand which common data types are in our development process. Among the common data types, GeoPoint is a relatively new concept here. GeoPointed type is used to represent a geographic point uniquely marked with a point by latitude and longitude, which is a special type of data storage. Note that if you need to look up a field of type GeoPoint, you must create a GeoPoint index. The specific geolocation API can be found in the Geo API documentation. In the front end of the panel, you can choose to create a to-dos collection for database management. The first step is to open the console, select the Database tab, and create a collection through the ad collection portal. Now suppose we want to create a to-do list WeChat Mini Program, we create a collection named to-dos. Once the collection is created, you will see the to-dos collection management to interface in which you can add records, find records, manage indexes, and manage permissions. Since the Cloud console provides an interactive interface to visually add data, you can simply click "Add record" to add to-do items, or use the API to push records into the collection. Now let's look at permissions control for Cloud Database Management. This table is an example of permission control between clients and the admin. As you can tell, the permissions of the database are always divided into the client side and the management side. The client side is also called the WeChat Mini Program side, while the management side is the Cloud console. Since the client side operates the database with strict security rules restrictions, the client is always restricted by permissions control, and therefore cannot write the data created by others. Whereas the management side has all the permissions to read and write the database. In this way, permission control segregates the responsibilities for both clients and the admin. Next, let's look at the process of using Cloud database APIs. As shown in the slide, the process is very straightforward. Once you have created the database, you have to get a database reference and then construct the query. The last step is to send a request with this API on the database platform. For example, the code shows how you can add a record once you have named a collection. Each record has an ID field to uniquely identify a record and an OpenID field to identify the records creator. Note that records created in the administration side will not have an OpenID field, as they are records created by the administrator. Developers can customize IDs, but cannot customize and modify OpenIDs. Since the OpenIDs are created by default by the system at document creation, based on the WeChat Mini Program user and can be used by developers to identify and locate the document. To query data in a collection, you need to first get a database reference by instantiating the constant db object. Once the constant database is instantiated, you can use the "collection" method to get a reference to a collection. Next, use the "where" method to pass in an object, and the database will return the JSON documents whose fields are equal to the specified value in the collection. This is a very standard way of working with an object code and pushing it to the database. For example, here in the collection of books, when you query any published information that contains United States, it will return the corresponding result. Notice that this type of data query is different from normal SQL query. But the basic idea is the same. Essentially, you'll take a collection set and then try to reduce the results by pushing some clauses, which narrows down the result and leads to a smaller subset of query. Here, is another example of updating data through db collection dot update. You can parse in the data to be updated locally through data. Then you can set the Boolean done field from true to false or vice versa. Another function we have is to delete data, which allows you to remove the collection entirely. Now, let's look at how to import and export data in a Cloud-based console. For importing data, first open the Cloud development console, switch to the database tab, and then select the collection you want to import data from, and click the "Import" button. In the next step, you should select the CSV or JSON file to be imported and the conflict handling mode and click the "Import" button to start importing. For exporting data from one environment to another, you'll have to select the format, save the location and the fields to be exported, and click the "Export" button to start the export process. Let's move on to the use of Cloud Storage, which is the second item that Cloud-based provides as a service. Cloud Storage provides a highly available, stable, and secure Cloud storage service that supports any amount and form of unstructured data storage, such as videos and images and visual management in the console. In particular, Cloud Storage includes the features of storage management, permission settings, upload management, file search, and components support. First, storage management supports folders for easy file categorization. It also supports file uploading, deleting, moving, downloading, and so on. Second, for permission settings, you can flexibly set which users can read and write the files in the folder to ensure the data security of the business. Third, you can view file upload history, progress, and status in upload management. Forth, Cloud-based supports file search by prefix name and subdirectory files. Lastly, it also supports parsing in Cloud file IDs and images, audio, and other components. Here's, the example code for uploading and downloading a file. Once you have called the wx.cloud.upload file method, you just need to mention the path to upload a file to the Cloud. After a successful upload, you will get a unique file identifier or the file ID, and subsequent operations will be based on the file ID instead of the URL. You can use the file ID to download files. But users will only be able to download files to which they have access. There is another set of examples for deleting a file and getting a temporary link. Again, you can use the same file ID to delete the file via the wx.cloud.delete file method. Notice that deleting a file can lead to success or fail results. Next, you can also exchange a temporary file network link based on the file ID, which will be valid for two hours. Now, let's look at the use of the Cloud heart function. A Cloud function is a function that runs on the Cloud server side. In terms of physical design, a Cloud Function can consist of multiple files occupying a certain amount of CPU memory and other computing resources. Since the execution space of each Cloud function is completely independent, although Cloud functions can potentially run on the same server, it is not visible by the user. A Cloud function is written like a locally defined at Javascript method with the node.js code running in the Cloud. When the Cloud function is called on the WeChat Mini program side, the defined code is executed in the node.js runtime environment. Here is the process of creating a Cloud Function. The first step is to create a function directory and configure this directory. Next, create a function after indicating the workspace where it runs. Finally, write the logic code for the function and deploy it. Now, as we discussed before in the Cloud Function you can create a file in the storage, get the object ID, delete the file or send it to a specific user. Here I'll show you how to create and configure a function directory. First, you need to create this type of directory in the folder where you'll find your Cloud Functions. Once the Cloud directory is initialized, you should find the project.config.json file in the project root directory. Add a new Cloud Function root field and specify the local directory that already exists as the local root directory of the Cloud Function. After completing the designation, the icon of the root directory of the Cloud Function will become a Cloud directory icon. The first-level directory under the root directory of the Cloud Function will have the same name as the Cloud Function and if the corresponding online environment exists for that Cloud function, we'll use a special Cloud icon to market. Next, we'll move on to discuss how to create a function, write the logic code, and deploy it. You can right-click on the "Cloud Function root directory" and create a new Node.js Cloud Function in the context menu. Then the developer tool will create the Cloud Function directory and the entry index.js file locally and create the corresponding Cloud Function in the online environment. After successful creation, the tool will prompt for the immediate local installation of the dependencies and will then automatically install wx-server sdk. As you can tell from this Cloud Function template there are two incoming parameters for the Cloud Function. One is the event object and the other is the context object. Event it refers to the event that triggers the Cloud Function and event is the incoming parameter when the Cloud Function is called from the WeChat mini program side. The context object contains the call information and running status of this call which can be used to understand how the service is running. On top of the two parameters, the template also requires wx-server-sdk by default which is a library provided by WeChat to help us manipulate the database and store and call other Cloud Functions in the Cloud Function. Now we can simply fill in the logic code to the template. For example, the code on the right hand side means to add the incoming event A and event B and return them to the caller as some fields. Now before calling this Cloud Function in the WeChat mini program, we also need to deploy the Cloud Function to the Cloud. To do so, right-click on the "Cloud Function directory" and in the context menu, package the Cloud Function as a whole and upload and deploy it to the online environment. In order to invoke this cloud function from a WeChat mini program, you just need to add a function name and pass the parameters that you have defined. Now, it's important that these parameters match otherwise the function will not be able to run successfully. Then the function will return what you ask it to return. In this example, it is the sum of one and two. When you run the function, you may also see common errors that need debugging but the console we'll point you directly to the issue. This slide shows common errors during the invocation of functions from WeChat mini programs. For example, you have to initialize the Cloud Function before calling it or else you will get the error saying that Cloud API isn't enabled. If you encounter such errors during the development process please add the corresponding Cloud Function initialization code in the configuration file. Now, there are two solutions to this error. The first solution is to perform the initialization to the Cloud Function before using the Cloud Function as shown in the top left image. The second one is to perform the corresponding initialization operation in the onLaunch function in the registered file app.js as shown in the top right image. As a review of what you've learned in this section, let's go over the following reflection question. How do we develop a function in a WeChat mini program? You can pause the video for a few minutes and think about how you would answer this question.