Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud development database management


May 22, 2021 Mini Program Cloud Development Advanced



First, the console database advanced operations

Database scripts can be written and executed in the database management page of the cloud development console, which can add, delete, and aggregate the database in the same syntax as the previous API syntax. The operation of the database script can make up for the lack of visualization operations in the cloud development console.

The script already has the following global variables so that we can use db directly in $ script _

  1. const db = wx.cloud.database()
  2. const _ = db.command
  3. const $ = db.command.aggregate

Database scripts also support the following expressions, primarily constants, declarations of variables and objects, and call expressions for database APIs:

The expression How to support it Example
Gets the property Supports getting the legitimate properties of an object, such as db, s, and legitimate properties such as the collection property of db db.collection
The function is called Support db.collection()
new Support new db. Geo.Point(113, 23)
Variable declaration Supports variable declarations, as well as how object deconstructors are declared const Geo = db. G eo const { Point } = db. Geo
The object declaration Support const obj = { age: _.gt(10) }
Constant declaration Support const max = 10
Negative Support const min = -5
Comments Support comment / comment /
Other Not supported

Second, the actual application of database scripts

Cloud development console data visualization management and advanced operations can also achieve a lot of functions similar to the relationship database GUI management tools, after all, GUI management is behind the database script operations, more features you can explore more, the following is only a few simple examples:

1, bulk delete a collection of multiple records

In the process of our development, there are hundreds of pieces in a collection, thousands of pieces of data hope to empty, but do not want to delete the collection and rebuild, then how should we do, can not delete one by one? T he visualization operation of the cloud development console is currently not possible to bulk delete multiple records within a collection, but this feature allows us to easily bulk delete through the script for advanced operations of the console database, and also to create a script template that needs to be clicked directly on the script template for long-term re-use. For example, we want to delete all records that are assembled as china:

  1. db.collection('china')
  2. .where({
  3. _id: _.exists(true)
  4. })
  5. .remove()

Since the remove request only supports deletion by matching where statements, we can include a condition in where it is deleted as long as it exists_id and deleted as _id is basically _id for each record.

2. How to add a field to all the data in the collection

I now have N data in a collection, because of the initial design of the database, now want to add a field to all records, imagine a relationship database and Excel add a column of similar operations, then what should I do? W e can also use the console database for advanced operations of the script. For example, if we want to add a updateTime field to all records in the china collection, we can query the record that needs to add a new field, and then use the update request, and when there is no updateTime field in the record, we'll add:

  1. const serverDate = db.serverDate
  2. db.collection('china')
  3. .where({
  4. _id: _.exists(true)
  5. })
  6. .update({
  7. data: {
  8. updateTime: serverDate(),
  9. }
  10. })

3. How to sort records the way you want them to

I uploaded pictures and articles in bulk on the small terminal, but found that their display order is not sorted according to the order in which I uploaded, but I have a lot of features but rely on sorting this function, what should I do?

Bulk uploads or you upload by time, the sorting of records will not be sorted in the order you think is normal, query to the order of data is generally not the console database display order, this is very normal. I f you have a need for sorting, there are two ways, one is that you can design sorted fields when you develop, for example, if you want the article to be sorted by time, you should set a field to record the release time of the article when the small program publishes the article, and another way is to manually add fields to customize, such as the order of carnation, article top or adjust the order, you may not have the necessary to develop related features, we can use the console to customize, For example, add a field to the record you want to sort to customize the sort order you want, and then use the orderBy when querying the data.

4, how to add more than one data

Using database scripts can be achieved to add multiple data at once, currently using cloud functions can not do to add more than one database to the collection at a time, in syntax, the difference between the two is that the database script's data supports the Array array, while the db.collection('').add({data:{}})

  1. db.collection('china')
  2. .add({
  3. data: [
  4. {
  5. "_id":"202003041020001",
  6. "city":"驻马店",
  7. "province":"河南",
  8. "city_area":15000,
  9. "builtup_area":75.1,
  10. "reg_pop":905.0,
  11. "resident_pop":696.0,
  12. "gdp":1807.69,
  13. },
  14. {
  15. "_id":"202003041020002",
  16. "city":"绍兴",
  17. "province":"浙江",
  18. "city_area":8279,
  19. "builtup_area":199.4,
  20. "reg_pop":443.11,
  21. "resident_pop":496.8,
  22. "gdp":4465.97,
  23. }
  24. ]
  25. })

Third, the import and export of the database

In addition to importing and exporting data from the database using the cloud development console and the cloud development console of Tencent's cloud web pages, we also described earlier how to import and export data using the back-end capabilities of cloud functions, and of course, we can use the following methods:

1、cloudbase-manager-node

I have a lot of pictures and files imported into cloud storage in bulk, but what should I do to get the fileID of these files in bulk? My database has dozens of collections, the database often need to be backed up, each time to export one by one is very cumbersome, there is no good way?

If you have a similar feature, you can use cloudbase-manager-node. Cloudbase-manager-node is very powerful, with interfaces that are richer than @cloudbase/node-sdk, and of course these features require developers to be able to develop them in conjunction with interfaces.

For example, if we want to get the fileID of a cloud storage file in bulk, we can use listDirectoryFiles(cloudPath: string): Promise<IListFileInfo[]> List the names of all the files under the folder, or you can use downloadDirectory(options): Promise<void> for example, if we want to back up the data for all listCollections(options: object): object get the names of all collections, and then use export(collectionName: string, file: object, options: object): object interface exports all records to the specified jason or csv file. We'll give you an overview of how to use this later.

If we want to download a backup of the files or folders in the cloud store and upload files or folders from our local computer to the cloud storage in bulk, we can use the Cloudbase CLI tool, which is very simple and will be covered in a later section of the CloudBase CLI.

2, the database interface in the HTTP API

HTTP API is a very common way to import and export data from cloud development resources using the HTTP API, regardless of platform or syntax, without specifying the details of the code, and we can use the following interfaces for import:

  1. POST https://api.weixin.qq.com/tcb/databasemigrateimport?access_token=ACCESS_TOKEN

You can export using the following interfaces:

  1. POST https://api.weixin.qq.com/tcb/databasemigrateexport?access_token=ACCESS_TOKEN

Fourth, the use of back gear for data backup

Cloud development provides database back-up capabilities that automatically turn on database backups and automatically back up data every morning for up to 7 days. D evelopers can retrieve some of their data and keep it safe by returning the collection (restoring) to a specified point in time with a new back-file task on the cloud console in the event of a database operation error or other situation.

Data access to the database is not affected during back-file. W hen the backsliding is complete, developers can see the original database collection and the post-reverse collection in the collection list. T his allows the previous data to be found and compared to the data in the existing collection. W hen the back file is complete, the developer can select the corresponding collection in the collection list and rename the collection name right-click, depending on the situation. See if the data after the return gear is enabled.