什么Laf升级了!🎉🎉🎉🎉 2023年6月30日正式下线,我的老数据怎么办!!!
新建云函数backup-data
复制如下代码
import cloud from '@/cloud-sdk'
const batchSize = 100;
const downloadJSONFile = (ctx: FunctionContext, data = [], filename = 'demo.json') => {
ctx.response.set('Content-Disposition', `attachment; filename="${filename}"`)
ctx.response.set('Content-Type', 'application/octet-stream')
ctx.response.send(Buffer.from(JSON.stringify(data, null, '\t')))
}
const fetchCollectionData = async (collectionName) => {
if (!collectionName) {
return [];
}
const db = cloud.database();
const { total, ok } = await db.collection(collectionName).count();
if (!ok || !total || total <= 0) {
return [];
}
const dataList = [];
const batch = Math.floor(total / batchSize);
const left = total % batchSize;
var skipIndex = 0;
for (var i = 0; i < batch; i++) {
const { data } = await db.collection(collectionName)
.skip(skipIndex)
.limit(batchSize)
.get();
dataList.splice(0,0,...data);
skipIndex += batchSize;
}
const { data } = await db.collection(collectionName)
.skip(skipIndex)
.limit(left)
.get();
dataList.splice(0, 0, ...data);
return dataList;
}
exports.main = async function (ctx: FunctionContext) {
const { auth, body, query } = ctx
let collectionName = query['collectionName'];
if (!collectionName) {
return "params error";
}
const data = await fetchCollectionData(collectionName);
if (!data || data.length <= 0) {
return "no data";
}
downloadJSONFile(ctx, data, `${collectionName}.json`);
}
浏览器访问云函数
https://xxxxx.lafyun.com/backup-data?collectionName=demo
xxxxx改成自己的appId;backup-data修改为自己的云函数标识名;demo修改为自己的云集合名称
查看自己的云集合
适合小数据量的备份,大数据量的记得找官方同学进行数据的迁移!!!