Skip to content

Commit

Permalink
chore: 优化 cloud-sdk 注释提示;优化 scheduler 调试日志;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Aug 9, 2021
1 parent cf3eefb commit dcdac4e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
78 changes: 74 additions & 4 deletions packages/app-server/src/cloud-sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,79 @@ export type EmitFunctionType = (event: string, param: any) => void
export type GetTokenFunctionType = (payload: any, secret?: string) => string
export type ParseTokenFunctionType = (token: string, secret?: string) => any | null

export interface MongoDriverObject {
client: mongodb.MongoClient
db: mongodb.Db
}

export interface CloudSdkInterface {
/**
* 发送 HTTP 请求,实为 Axios 实例,使用可直接参考 axios 文档
*/
fetch: AxiosStatic

/**
* 获取一个文件存储管理器
* @param namespace 文件的名字空间,如 'public'
*/
storage(namespace: string): FileStorageInterface

/**
* 获取 less api database ORM 实例
*/
database(): Db,

/**
* 调用云函数
*/
invoke: InvokeFunctionType

/**
* 抛出云函数事件,其它云函数可设置触发器监听此类事件
*/
emit: EmitFunctionType

/**
* 云函数全局内存单例对象,可跨多次调用、不同云函数之间共享数据
* 1. 可将一些全局配置初始化到 shared 中,如微信开发信息、短信发送配置
* 2. 可共享一些常用方法,如 checkPermission 等,以提升云函数性能
* 3. 可做热数据的缓存,建议少量使用(此对象是在 node vm 堆中分配,因为 node vm 堆内存限制)
*/
shared: Map<string, any>

/**
* 获取 JWT Token,若缺省 `secret`,则使用当前服务器的密钥做签名
*/
getToken: GetTokenFunctionType

/**
* 解析 JWT Token,若缺省 `secret`,则使用当前服务器的密钥做签名
*/
parseToken: ParseTokenFunctionType
mongodb: mongodb.Db

/**
* 当前应用的 MongoDb Node.js Driver 实例对象。
* 由于 less-api-database ORM 对象只有部分数据操作能力,故暴露此对象给云函数,让云函数拥有完整的数据库操作能力:
* 1. 事务操作
* ```js
* const session = mongo.client.startSession()
* try {
* await session.withTransaction(async () => {
* await mongo.db.collection('xxx').updateOne({}, { session })
* await mongo.db.collection('yyy').deleteMany({}, { session })
* // ...
* })
* } finally {
* await session.endSession()
* }
* ```
* 2. 索引管理
* ```js
* mongo.db.collection('admins').createIndex('username', { unique: true })
* ```
* 3. 聚合操作
*/
mongo: MongoDriverObject
}


Expand All @@ -42,14 +105,18 @@ const cloud: CloudSdkInterface = {
shared: CloudFunction._shared_preference,
getToken: getToken,
parseToken: parseToken,
mongodb: Globals.accessor.db
mongo: {
client: Globals.accessor.conn,
db: Globals.accessor.db
}
}

/**
* 等数据库连接成功后,更新其 mongo 对象,否则为 null
*/
Globals.accessor.ready.then(() => {
cloud.mongodb = Globals.accessor.db
cloud.mongo.client = Globals.accessor.conn
cloud.mongo.db = Globals.accessor.db
})

/**
Expand All @@ -66,7 +133,10 @@ export function create() {
shared: CloudFunction._shared_preference,
getToken: getToken,
parseToken: parseToken,
mongodb: Globals.accessor.db
mongo: {
client: Globals.accessor.conn,
db: Globals.accessor.db
}
}
return cloud
}
Expand Down
7 changes: 4 additions & 3 deletions packages/app-server/src/lib/scheduler/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ export class FrameworkScheduler extends TriggerScheduler {
* @param func_id
* @returns
*/
async getFunctionById(func_id: string): Promise<CloudFunction>{
async getFunctionById(func_id: string): Promise<CloudFunction> {
assert(func_id)
const funcData = await getFunctionById(func_id)
assert.ok(funcData)
assert.ok(funcData, `failed to get function data: ${func_id}`)

const func = new CloudFunction(funcData)
if(!func.compiledCode) {
if (!func.compiledCode) {
logger.warn(`performance warning: function (${func_id} hadn't been compiled, will be compiled automatically)`)
func.compile2js()
}
return func
Expand Down
2 changes: 1 addition & 1 deletion packages/app-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"noUnusedParameters": true,
"outDir": "dist",
"pretty": true,
"removeComments": true,
"removeComments": false,
"stripInternal": true,
"skipDefaultLibCheck": true,
"skipLibCheck": true,
Expand Down

0 comments on commit dcdac4e

Please sign in to comment.