apass-opensdk-hugong
Version:
飞书Apass低代码平台-飞书开放平台-相关的接口整合和常用的方法整合
286 lines (270 loc) • 9.03 kB
JavaScript
class Object_ {
#hg = null
constructor(hg) {
this.#hg = hg
}
/**
* 获取对象的所有字段
* @param objectApiName 对象 API 名称
* @returns 字段列表
*/
async getFields(objectApiName){
return await application.metadata.object(objectApiName).getFields()
}
/**
* 获取对象的指定字段
* @param objectApiName 对象 API 名称
* @param fieldApiName 字段 API 名称
* @returns 字段对象
*/
async getField(objectApiName,fieldApiName){
return await application.metadata.object(objectApiName).field(fieldApiName)
}
/**
* 执行 OQL 查询,使用系统权限
* @param {*} oql OQL 查询语句
* @param {*} nameArgs 命名参数对象
* @returns 查询结果
*/
async oqlUseSystemAuth(oql,nameArgs = null){
return await this.oql(oql,nameArgs,true)
}
/**
* 执行 OQL 查询
* @param oql OQL 查询语句
* @param nameArgs 命名参数对象
* @returns 查询结果
*/
async oql(oql,nameArgs = null,useSystemAuth = false){
const object = application.data.oql(oql,nameArgs);
if (useSystemAuth) {
object.useSystemAuth()
}
return await object.execute()
}
/**
* 分页执行 OQL 查询
* @param {*} oql OQL 查询语句
* @param {*} nameArgs 命名参数对象
* @param {*} callback 每批记录回调;提供时方法返回 void
* @param {*} useSystemAuth 是否使用系统权限;默认 false
* @returns 查询结果列表
*/
async oqlPaging(oql,nameArgs = null,callback = null,useSystemAuth = false){
let page = 0
let records = []
let list = []
do {
records = await this.oql(`${oql} LIMIT 200 offset ${(page++) * 200}`,nameArgs,useSystemAuth);
if (callback) {
await callback(records)
}else{
list.push(...records)
}
} while (records.length > 0);
return list
}
/**
* 清空表中满足条件的数据
* @param objectApiName 表名
* @param where 条件对象;省略时全部清空
* @param useSystemAuth 是否使用系统权限;默认 false
*/
async clearData(objectApiName, where = null, useSystemAuth = false) {
const object = application.data.object(objectApiName).select('_id').where(where ? where : { _id: application.operator.gte(0) })
if (useSystemAuth) {
object.useSystemAuth()
}
await object.findStream(async records => {
this.#hg.log4('clear data len', records.length)
const object = application.data.object(objectApiName)
if (useSystemAuth) {
object.useSystemAuth()
}
await object.batchDelete(records.map(it => ({ _id: it._id })))
})
}
/**
* 查询列表,使用系统权限
* @param {*} objectApiName 表名
* @param {*} fields 需要返回的字段,默认 ['_id','_name']
* @param {*} where 过滤条件
* @param {*} callback 每批记录回调;提供时方法返回 void
* @param {*} orderby 排序字段,默认 '_id'
*/
async findListUseSystemAuth(objectApiName, fields, where, callback, orderby){
return await this.findList(objectApiName, fields, where, callback, orderby, true)
}
/**
* 查询列表
* @param objectApiName 表名
* @param field 需要返回的字段,默认 ['_id','_name']
* @param where 过滤条件
* @param callback 每批记录回调;提供时方法返回 void
* @param orderby 排序字段,默认 '_id'
* @param useSystemAuth 是否使用系统权限;默认 false
* @returns 无 callback 时返回全部记录
*/
async findList(objectApiName, fields, where, callback, orderby, useSystemAuth = false) {
const list = []
const object = application.data.object(objectApiName).select(fields || ['_id', '_name']).where(where || { _id: application.operator.gte(0) }).orderByDesc(orderby || '_id')
if (useSystemAuth) {
object.useSystemAuth()
}
await object.findStream(async records => {
if (callback) {
this.#hg.log4('find all len', records.length)
await callback(records)
} else {
this.#hg.log4('find all size', records.length)
list.push(...records)
}
})
if (!callback) {
this.#hg.log4('find all len', list.length)
}
return list
}
/**
* 查询单条记录,使用系统权限
* @param {*} objectApiName 表名
* @param {*} fields 需要返回的字段,默认 ['_id','_name']
* @param {*} where 过滤条件
*/
async findOneUseSystemAuth(objectApiName, fields, where){
return await this.findOne(objectApiName, fields, where, true)
}
/**
* 查询单条记录
* @param objectApiName 表名
* @param field 需要返回的字段,默认 ['_id','_name']
* @param where 过滤条件
* @param useSystemAuth 是否使用系统权限;默认 false
* @returns 记录对象或 null
*/
async findOne(objectApiName, fields, where, useSystemAuth = false) {
const object = application.data.object(objectApiName).select(fields || ['_id', '_name']).where(where || { _id: application.operator.gte(0) })
if (useSystemAuth) {
object.useSystemAuth()
}
return await object.findOne()
}
/**
* 查询单条记录,使用系统权限
* @param {*} objectApiName 表名
* @param {*} fields 需要返回的字段,默认 ['_id','_name']
* @param {*} where 过滤条件
*/
async findOneByIdUseSystemAuth(objectApiName, id, fields){
return await this.findOneById(objectApiName, id, fields, true)
}
/**
* 根据 ID 查询单条记录
* @param objectApiName 表名
* @param id 记录 ID
* @param fields 需要返回的字段,默认 ['_id','_name']
* @param useSystemAuth 是否使用系统权限;默认 false
* @returns 记录对象或 null
*/
async findOneById(objectApiName, id, fields, useSystemAuth = false) {
const object = application.data.object(objectApiName).select(fields || ['_id', '_name']).where({ _id: id })
if (useSystemAuth) {
object.useSystemAuth()
}
return await object.findOne()
}
/**
* 批量创建记录
* @param objectApiName 表名
* @param list 记录列表
* @param useSystemAuth 是否使用系统权限;默认 false
* @returns 创建的记录 ID 列表或 null
*/
async batchCreate(objectApiName, list, useSystemAuth = false) {
if (list.length) {
const object = application.data.object(objectApiName)
if (useSystemAuth) {
object.useSystemAuth()
}
return await object.batchCreate(list)
}
return null
}
/**
* 创建单条记录
* @param objectApiName 表名
* @param data 记录数据
* @param useSystemAuth 是否使用系统权限;默认 false
* @returns 创建的记录 ID 或 null
*/
async create(objectApiName, data, useSystemAuth = false) {
const object = application.data.object(objectApiName)
if (useSystemAuth) {
object.useSystemAuth()
}
return await object.create(data)
}
/**
* 批量更新记录
* @param objectApiName 表名
* @param list 记录列表
* @param useSystemAuth 是否使用系统权限;默认 false
* @returns 更新的记录 ID 列表或 null
*/
async batchUpdate(objectApiName, list, useSystemAuth = false) {
if (list.length) {
const object = application.data.object(objectApiName)
if (useSystemAuth) {
object.useSystemAuth()
}
return await object.batchUpdate(list)
}
return null
}
/**
* 更新单条记录
* @param objectApiName 表名
* @param data 记录数据
* @param useSystemAuth 是否使用系统权限;默认 false
* @returns 更新的记录 ID 或 null
*/
async update(objectApiName, data, useSystemAuth = false) {
const object = application.data.object(objectApiName)
if (useSystemAuth) {
object.useSystemAuth()
}
return await object.update(data)
}
/**
* 删除单条记录
* @param objectApiName 表名
* @param _id 记录 ID
* @param useSystemAuth 是否使用系统权限;默认 false
* @returns 删除的记录 ID 或 null
*/
async deleteOne(objectApiName, _id, useSystemAuth = false) {
const object = application.data.object(objectApiName)
if (useSystemAuth) {
object.useSystemAuth()
}
return await object.delete(_id)
}
/**
* 批量删除记录
* @param objectApiName 表名
* @param _ids 记录 ID 列表
* @param useSystemAuth 是否使用系统权限;默认 false
* @returns 删除的记录 ID 列表或 null
*/
async deleteBatchByIds(objectApiName, _ids, useSystemAuth = false) {
if (_ids.length) {
const object = application.data.object(objectApiName)
if (useSystemAuth) {
object.useSystemAuth()
}
return await object.batchDelete(_ids)
}
return null
}
}
module.exports = Object_