diff --git a/packages/app-server/src/cloud-sdk/index.ts b/packages/app-server/src/cloud-sdk/index.ts index fbe995c3be..24fbefd586 100644 --- a/packages/app-server/src/cloud-sdk/index.ts +++ b/packages/app-server/src/cloud-sdk/index.ts @@ -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 + + /** + * 获取 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 } @@ -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 }) /** @@ -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 } diff --git a/packages/app-server/src/lib/scheduler/scheduler.ts b/packages/app-server/src/lib/scheduler/scheduler.ts index 60251b4892..6a28092d24 100644 --- a/packages/app-server/src/lib/scheduler/scheduler.ts +++ b/packages/app-server/src/lib/scheduler/scheduler.ts @@ -19,13 +19,14 @@ export class FrameworkScheduler extends TriggerScheduler { * @param func_id * @returns */ - async getFunctionById(func_id: string): Promise{ + async getFunctionById(func_id: string): Promise { 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 diff --git a/packages/app-server/tsconfig.json b/packages/app-server/tsconfig.json index 194d5d5ac1..29b4e42683 100644 --- a/packages/app-server/tsconfig.json +++ b/packages/app-server/tsconfig.json @@ -19,7 +19,7 @@ "noUnusedParameters": true, "outDir": "dist", "pretty": true, - "removeComments": true, + "removeComments": false, "stripInternal": true, "skipDefaultLibCheck": true, "skipLibCheck": true,