jz-api-core 提供的基础功能模块
- DBService - 直接对mongo数据库进行查询、统计等操作
- Server - 服务器相关,用于初始化Hapi服务器
- Logger - Log服务
- Mongo - Mongo数据库的schema定义,一般不需要直接使用
- CacheService - 一般DBService内部调用
- Common - 通用的一些对象的申明
- Lib - 一些常用方法库
- AuthService - 权限校验服务,一般不用直接调用
- EntityResolver - 生成的Graphql调用的方法,一般不直接调用
- Hooks - 一般不直接使用
Common
- 定义了一些和config文件对应的对象类型,一般不需要直接使用。有帮助的一些对象如下
- Entity - 所有数据库对象的基础,默认包含6个缺省的字段
- _id
- createdAt - 创建时间的timestamp(毫秒)
- createdBy - 创建用户的id
- updatedAt - 最近一次更改时间的timestamp(毫秒)
- updatedBy - 最近一次更改人的id
- isRemove - true表示该记录被标记为删除,整个系统只有软删除,缺省值为false
- MutationResult - graphql mutation的结果
- success - 是否操作成功
- error - 如有错误则包含错误信息
- MutationSuccess - 上面MutationResult的一个实例,返回{success: true}
- Me - 当前登录用户的信息,默认只包含两个field,这个信息由框架在用户登录的时候自动填写
- id - 用户的id
- role - 当前用户的角色
- SysAdmin - 上面Me的一个实例,用于指代系统管理员,用户比如cron job中创建临时Context时设置Me信息
- AnonymousUser - Me的另一个实例,实际值为 {_id:'0', role: 'user'} 代表未登录用户(因为id为0)
- Context - 最重要的对象之一,一个request在服务器的生命周期会贯穿着一个Context object,里面的一些属性非常用有
- me - Me的实例,包含当前用户的信息
- startTime - 这个context被初始化的时间
- query/mutation/variables 等一些和graphql相关的信息,一般不用管
- error - Error若出错,可以将错误设置到Context里,不过一般的处理方式是直接throw Error
- isAdmin(me: Me) - 函数,返回true如果当前用户是超级管理员
- initContext(query: string, variables: string, mutation: string, operationName: string, me: Me) - 函数,用于初始化一个Context,主要用户类似cron job这种不是从request初始化的context
- Query - 代表一个查询接口,包含四个fields
- args - Object,所有Query一定要包含args,里面对应mongo的查询条件,比如 {_id: 'abcfd'}
- limit - 分页时用,未提供的时候默认页面大小为100
- sort - 排序
- skip - 忽略多少条记录,用于mongo数据库查询分页时,返回指定页的数据时使用,比如页面大小limit=10,需要第三页时skip为20
- isLogin(me: Me) - 当前用户是否已经登录
DBService
/**
* return entity given id
* @param entityType
* @param id
* @param fieldsToKeep
* @param me
* @param useCache
* @param context
*/
export function getEntityById(entityType: string, id: string, fieldsToHide: string[], fieldsToKeep: string[], context: Context, useCache: boolean = true, runHook: boolean = false): object
/**
* Return single entity from given query. query here represents a regular mongo search query object
* @param query
* @param fields
* @param me
* @param context
*/
export function getSingleEntity(entityType: string, query: any, fieldsToHide: string[], fieldsToKeep: string[], context: Context, runHook: boolean = false): object | null
/**
* return list of entities
* @param query
* @param fields
* @param me
* @param context
*/
export function getEntityList(entityType: string, query: Query, fieldsToHide: string[], fieldsToKeep: string[], context: Context, runHook: boolean = false)
/**
* get list of entities given id array
* @param query
* @param fields
* @param me
* @param context
*/
export function getEntitiesByIds(entityType: string, ids: string[], fieldsToHide: string[], fieldsToKeep: string[], context: Context, useCache: boolean = true, runHook: boolean = false)
/**
* similar to getEntityList but returned result will contain total count from the query, good for pagination
*/
export function getEntityListWithCount(entityType: string, query: Query, fieldsToHide: string[], fieldsToKeep: string[], context: Context, runHook: boolean = false)
export function createEntity(entityType: string, obj: any, context: Context, runHook: boolean = false)
/**
* bulk create documents in given collection in batch. Note that beforeSave and afterSave batch won't be invoked in this case to avoid n+1 issues. Caller should
* handle beforeSave and afterSave logic by writing optimized code if needed.
* @param entityType
* @param objs
* @param context
*/
export function batchCreate(entityType: string, objs: object[], context: Context)
/**
* mainly used by graphql directly, not for normal usage
*/
export function updateEntity(entityType: string, query: any, context: Context, runHook: boolean = false)
export function update(entityType: string, findQuery: object, uQuery: object, context: Context, isMulti: boolean = false)
/**
* refer to mongo aggregate for details
*/
export function aggregate(entityType: string, arr: any[], context: Context)
export function count(entityType: string, args: object, context: Context)
Logger
封装了log4js,引用方法
import { Logger } from 'jz-api-core'
const logger = Logger.getLogger('com.my.company.package.class');
logger.trace('message');
logger.debug('message');
logger.info('message');
logger.warn('message');
logger.error('message');
logger.fatal('message');
Lib
一些常用方法
/**
* 返回错误信息
* @param res response object
* @param err error object
*/
export function errorRes(res, err) {}
/**
* 返回正常http处理结果
* @param res response object
* @param data data object to be returned
*/
export function okRes(res, data) {}
/**
* will throw error if current user is not logged in
* @param rootValue the rootValue
*/
export function requireLogin(rootValue) => {}
/**
* Return a basic object structure with a generated id, createdAt, updatedAt timestamp and isRemove set to false
*/
export function createBasicOj () {}
/**
* Randomly genreate a password from abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-+= these characters with the given length
* @param length length of string to be generated
*/
export function generatePassword (length: number) : string {}
/**
* Randomly generate a string from ABCDEFGHJKMNOPQRSTUVWXYZ0123456789 these characters, useful for genreating invitation code etc..
* @param length length of string to be generated
*/
export function generateCode (length: number) : string {}
/**
* Return today's 12:00 am timestamp in ms
*/
export function getTodayStartTimestamp() : number {}