3 --- complete set of resources to obtain data from the server to your own database

Gets a function of the current database cloud single song list

Batch process acquires data from the action message to the database and for timing the cut, to reinsertion

 

API prepared in advance to obtain data transmission request

const URL = 'http://musicapi.xiecheng.live/personalized'

Corresponding to the request into a database collection among extra attention here to await and async

// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
//云数据库的初始化
const db = cloud.database()
const rp = require('request-promise')
const URL = 'http://musicapi.xiecheng.live/personalized'
// 云函数入口函数
exports.main = async(event, context) => {
  const playlist = await rp(URL).then((res) => {
    return JSON.parse(res).result
  })
  // console.log(playlist)
  for(let i =0, len = playlist.length; i<len ; i++){
    await  db.collection("playlist").add({
      data:{
        ...playlist[i],
        createTime:db.serverDate()
      }
    }).then((res) => {
      console.log("插入成功")
    }).catch((err) => {
      console.error("插入失败")
    })
  }
}

 

 

Question 1: If the current song database already exists on how to avoid de-emphasis?

1 first read the information stored in the database in the playlist list

There are two data acquisition server playlist 

3 compares the new data stored by circulating newData []

4 and then inserted into the database

const list = db.collection("playlist").get()

 

// 云函数入口函数
exports.main = async(event, context) => {

  //取到数据库 playlist中所有数据(放在list中data中),注意:该操作是异步操作 要加await
  const list = await playlistCollection.get()

  //等待 rp获取资源  并  将结果由字符串转化为对象
  const playlist = await rp(URL).then((res) => {
    return JSON.parse(res).result
  })
  //此时 list中为获取的 数据库 的资源,
  //playlist为 从服务器获取的资源
  //插入数据库之前 要进行去重复
  const newData = []//用来放去重复之后要插入的数据
  for(let i=0,len1 = playlist.length; i<len1;i++){
    let flag =true

    for(j=0,len2 = list.data.length; j<len2; j++){
      if(playlist[i].id === list.data[j].id){
        flag =false
        break
      }
      //若获取服务器中的第【i】条数据与 获取数据库并存放在list中的数据都不重复
      //则将数据写入newData[] 用来后期插入数据库
      if(flag){
        newData.push(playlist)
      } 
    }
  }

  // 循环newData[]中的数据 并 加入数据库
  for(let i =0, len = newData.length; i<len ; i++){
    await playlistCollection.add({
      data:{
        ...newData[i],
        createTime:db.serverDate()
      }
    }).then((res) => {
      console.log("插入成功")

 

 

Question 2: Take data to the cloud function is limited up to get access to the data 100, data from the applet clients to obtain up to get 20, how to break the limit?

 Single song information will continue to increase, so 100 is not satisfied demand

1 Total number of data pieces acquired here is a return object (asynchronous operation + await)

const countResult = await playlistCollection.count()

2 obtained by the current object .total number of digital type, then the total number of obtained

 const total = countResult.total

3 = several calculation requires taking the number of the total number of pieces / time to take note of is rounded up to

  To define a constant number calculated batchTimes

const MAX_LIMIT = 100

const batchTimes = Math.ceil(total / MAX_LIMIT)

4 Note here due to the multiple additions to wait so all get processed and use to Promise

 

const tasks = []
for (let i = 0; i < batchTimes; i++) {
    let promise = playlistCollection.skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
    tasks.push(promise)
  }
  let list = {
    data: []
  }
  if (tasks.length > 0) {//如果需要分多次取
    list = (await Promise.all(tasks)).reduce((acc, cur) => {//当所有完成
      return {
        data: acc.data.concat(cur.data)//拼接上当前数据
      }
    })
  }

 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
//云数据库的初始化,生成一个DB对象
const db = cloud.database()

const rp = require('request-promise')

const URL = 'http://musicapi.xiecheng.live/personalized'

//因为要多次获取数据库,所以再获取数据用playlistCollection就可以
const playlistCollection = db.collection("playlist")

const MAX_LIMIT = 100

/////////////////////////////////
///////////云函数入口函数//////////
//////////////////////////////////
exports.main = async (event, context) => {
  // const list = await playlistCollection.get()
  const countResult = await playlistCollection.count()
  const total = countResult.total
  const batchTimes = Math.ceil(total / MAX_LIMIT)
  const tasks = []
  for (let i = 0; i < batchTimes; i++) {
    let promise = playlistCollection.skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
    tasks.push(promise)
  }
  let list = {
    data: []
  }
  if (tasks.length > 0) {
    list = (await Promise.all(tasks)).reduce((acc, cur) => {
      return {
        data: acc.data.concat(cur.data)
      }
    })
  }

  const playlist = await rp(URL).then((res) => {
    return JSON.parse(res).result
  })

  const newData = []
  for (let i = 0, len1 = playlist.length; i < len1; i++) {
    let flag = true
    for (let j = 0, len2 = list.data.length; j < len2; j++) {
      if (playlist[i].id === list.data[j].id) {
        flag = false
        break
      }
    }
    if (flag) {
      newData.push(playlist[i])
    }
  }

  for (let i = 0, len = newData.length; i < len; i++) {
    await playlistCollection.add({
      data: {
        ...newData[i],
        createTime: db.serverDate(),
      }
    }).then((res) => {
      console.log('插入成功')
    }).catch((err) => {
      console.error('插入失败')
    })
  }

  return newData.length
}

Question three: add data manually every time you! ?

Solution: Timing function is used to trigger the trigger function of the cloud

Reference: https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/functions/triggers.html

1 Create one-json function (note written comments within json function is not available)

Set 2 per day 391,521 finer Point Timing

3 Upload trigger

 

Published 47 original articles · won praise 5 · Views 2336

Guess you like

Origin blog.csdn.net/qq_41440031/article/details/104703995