云函数开发工具 alicf
设置阿里云调用参数
./cloudfunctions/config.json
{
"accessKeyId": "Your Aliyun AccessKeyId",
"accessKeySecret": "Your Aliyun AccessKeySecret",
"spaceId": "Your Aliyun Serverless SpaceId"
}
创建云函数
会在Serverless空间创建函数
Command:
npm run cf create test-demo
部署云函数
将云函数部署部署到Serverless空间
Command:
npm run cf deploy test-demo
运行云函数
运行Serverless空间云函数
Command:
npm run cf invoke test-demo [参数, 默认为{}]
添加触发器
给Serverless云函数添加触发器
./cloudfunctions/config.json
"triggers": {
"test-demo": { // 函数名
"cron": "0 */20 7-16 * 4-6 *",
"payload": ""
}
}
+ test-demo: 函数名
+ cron: 触发表达式
+ payload: 定时触发传入参数
Command:
npm run cf trigger test-demo
变量说明
alicf
: 对象存储db
: 云数据库操作对象file
: 云文件操作对象cloudfunction
: 云函数操作对象, 调用其他云函数cloudfunction.invoke(funcName)
httpclient
: HTTP请求工具, https://help.aliyun.com/document_detail/435817.html
alicf.get
const {db, file, cloudfunction, httpclient} = alicf.get()
db 补充文档
查询集合数量 count
Command:
db.collection('user').count({_id:{$exists:true}})
Response:
{"success":true,"affectedDocs":0,"result":0}
更新一条记录 updateOne
Command:
db.collection('user').updateOne({_id}, {
$set: {
...data,
time: Date.now()
}
})
Response:
{"success":true,"affectedDocs":0,"result":{"ok":1,"n":0,"nModified":0,"upserted":null}}
插入记录 insertOne
Command:
db.collection('user').insertOne({
...data,
time: Date.now()
})
Response:
{"success":true,"affectedDocs":1,"result":{"insertedId":"648d9cc80c801c4baaa5266a","ok":1,"n":1}}
查询一条记录 findOne
Response:
{"success":true,"affectedDocs":1,"result":{"_id":1234567890,"name":"test"}}