ObjectQL
ObjectQL 是面向对象的跨数据库查询语法。基于ObjectQL语法编写的函数兼容不同类型的数据库。
开发插件时,可以直接调用内核 @steedos/objectql
包操作数据库。
添加数据
支持添加单条数据和批量添加数据,以direct前缀表示直接执行相关操作不触发触发器。
insert
添加单条记录。该函数会触发配置的触发器函数: beforeInsert 、afterInsert 。添加 “任务” 等特殊记录后会发送通知给指定人员。
- 使用: insert(doc, userSession?)
- 参数:
- doc:
Dictionary<any>
您希望插入的数据。 - userSession:
SteedosUserSession
当前用户登录信息,非必填;如果未传入userSession,则不进行权限校验。
- doc:
- 返回值: 插入成功后的数据。
const { spaceId, userId } = this
const objectql = require('@steedos/objectql');
const objectApiName = "contacts";
var now = new Date();
// const _id = await objectql.getObject('contacts')._makeNewID();
const doc = {
// _id: _id,
name: '小妹',
space: spaceId,
owner: userId,
created_by: userId,
modified_by: userId,
created: now,
modified: now
};
const result = await objectql.getObject(objectApiName).insert(doc);
console.log('result==>',result)
/*
result==> {
_id: '84SFjtKFfonskA6CS',
name: '小妹',
space: 'M6x6ddKvpj9ddcYYm',
owner: '606d7baa3393516019dbfb39',
created_by: '606d7baa3393516019dbfb39',
modified_by: '606d7baa3393516019dbfb39',
created: 2021-10-27T08:14:47.158Z,
modified: 2021-10-27T08:14:47.158Z
}
*/
如果doc中包含自定义 _id , 则以此为准,否则会自动生成一个 _id 。
内置生成ID的工具函数: objectql.getObject(objectApiName)._makeNewID()
在触发器函数中可以通过this获取 spaceId、userId的值。
基础属性 | 属性含义 | 是否必填 |
---|---|---|
name | 记录名称 | yes |
space | 当前工作区id | yes |
owner | 记录所属人员 | yes |
created_by | 创建人员 | yes |
modified_by | 修改人员 | yes |
created | 创建时间 | no (系统会自动维护) |
modified | 修改时间 | no (系统会自动维护) |
directInsert
添加单条记录。不会触发执行相关触发器函数。
- 使用: directInsert(doc, userSession?)
- 参数:
- doc:
Dictionary<any>
您希望插入的数据。 - userSession:
SteedosUserSession
当前用户登录信息,非必填;如果未传入userSession,则不进行权限校验。
- doc:
- 返回值: 插入成功后的数 据。
const { spaceId, userId } = this
const objectql = require('@steedos/objectql');
const objectApiName = "contacts";
var now = new Date();
// const _id = await objectql.getObject('contacts')._makeNewID();
const doc = {
// _id: _id,
name: '大哥',
space: spaceId,
owner: userId,
created_by: userId,
modified_by: userId,
created: now,
modified: now
};
await objectql.getObject(objectApiName).directInsert(doc);