Skip to content

Commit

Permalink
feat: 增加 create 函数;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Aug 2, 2021
1 parent a7c3641 commit cdafdb1
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions packages/app-server/src/cloud-sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { invokeInFunction } from "./invoke"

export type InvokeFunctionType = (name: string, param: FunctionContext) => Promise<FunctionResult>
export type EmitFunctionType = (event: string, param: any) => void
export type GetTokenFunctionType = (payload: any) => string
export type ParseTokenFunctionType = (token: string) => any | null
export type GetTokenFunctionType = (payload: any, secret?: string) => string
export type ParseTokenFunctionType = (token: string, secret?: string) => any | null

export interface CloudSdkInterface {
fetch: AxiosStatic
Expand All @@ -30,6 +30,9 @@ export interface CloudSdkInterface {
}


/**
* Cloud SDK 实例
*/
const cloud: CloudSdkInterface = {
database: () => Globals.createDb(),
storage: (namespace: string) => new LocalFileStorage(Config.LOCAL_STORAGE_ROOT_PATH, namespace),
Expand All @@ -42,5 +45,30 @@ const cloud: CloudSdkInterface = {
mongodb: Globals.accessor.db
}

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

/**
* 创建一个 SDK 实例
* @returns
*/
export function create() {
const cloud: CloudSdkInterface = {
database: () => Globals.createDb(),
storage: (namespace: string) => new LocalFileStorage(Config.LOCAL_STORAGE_ROOT_PATH, namespace),
fetch: request,
invoke: invokeInFunction,
emit: (event: string, param: any) => SchedulerInstance.emit(event, param),
shared: CloudFunction._shared_preference,
getToken: getToken,
parseToken: parseToken,
mongodb: Globals.accessor.db
}
return cloud
}

export default cloud

0 comments on commit cdafdb1

Please sign in to comment.