在1.0新建函数如下
import` cloud from '@lafjs/cloud'
export async function main(ctx: FunctionContext) {
const oldAppID = '' // 0.8 app id
const oldToken = '' // 0.8 token,复制0.8平台接口header里面的authorization值
const newAppID = '' //1.0 app id
const newToken = '' // 1.0 token,复制1.0平台接口header里面的authorization值
try{
// 查询旧版集合列表数据
const collection = await cloud.fetch.get(
`https://console.lafyun.com/sys-api/apps/${oldAppID}/dbm/collections`,
{
headers: {
'Authorization': oldToken
}
}
);
// console.log(collection.data)
var collection_list = []
collection.data.forEach(async item => {
collection_list.push(item.name)
// 新版添加集合
const params = { name: item.name }
const createRes = await cloud.fetch.post(`https://laf.run/v1/apps/${newAppID}/collections`, params, {
headers: {
'authorization': newToken,
}
})
if (createRes.data.error) {
console.log(`collections ${item.name} migrate failed: ${createRes.data.error}`)
} else {
console.log(`collections ${item.name} migrate success`)
}
})
// console.log(collection_list)
// 查询旧版集合数据
for(let name of collection_list){
var entryParams = {
action: 'database.queryDocument',
collectionName: name,
limit: 500, // 获取集合里的数据,修改limit获取全部数据
query: {}
}
const collection_data = await cloud.fetch.post(
`https://console.lafyun.com/sys-api/apps/${oldAppID}/dbm/entry`, entryParams,
{
headers: {
'Authorization': oldToken
}
}
);
// console.log(collection_data.data.data.list)
// 新版集合插入数据
if(collection_data.data.data.list.length > 0){
for(let add_data of collection_data.data.data.list){
console.log(add_data)
var addParams = {
"collectionName": name,
"data": add_data,
"multi": false,
"action": "database.addDocument"
}
console.log(addParams)
const createRes = await cloud.fetch.post(`https://laf.run/v1/apps/${newAppID}/databases/proxy`, addParams,
{
headers: {
'authorization': newToken,
}
})
if (createRes.data.error) {
console.log(`collections data ${name} migrate failed: ${createRes.data.error}`)
} else {
console.log(`collections data ${name} migrate success`)
}
}
}
}
}catch(error){
console.log(error)
}
}
实现方法参照了“倒数第三”大佬的云函数迁移