
-- student_select2 对应 SQLSELECT *FROM `{{ model.tableName() }}`WHERE `start_date` > {{ d_start }} AND `end_date` < {{ d_end }}
const res = await app.models.student.runSQLTemplate({templateName: 'student_select2',envType: "pre",params: {"d_start": "2025-01-01","d_end": "2026-01-01"}})console.log(res)
prod,pre。SELECT * FROM student,不能使用 SELECT * FROM user,INSERT 等其他指令同理。{{param}} 的方式引入变量参数。变量参数用于在实际调用和运行 SQL 模板时传递变量值。{{ }} 符号进行包裹。函数类别 | 函数表达式 | 说明 |
模型 | model.tableName() | 获得 SQL 模板所属模型对应的表名,自动识别体验和生产 |
模型 | model.tableName('my_model') | 获得指定模型对应的表名 |
模型 | model.dataId() | 自动生成系统风格的数据 ID |
用户 | user.userId() | 在运行时,获取当前登录人的用户标识 |
用户 | user.openId() | 在运行时,获取当前登录人的 openId,如果是小程序登录用户,则对应微信 openId |
权限 | auth.rowPermission() | 在运行时,获取当前登录人对应角色的模型行权限,模型为 SQL 模板绑定模型 |
权限 | auth.rowPermission('my_model') | 在运行时,获取当前登录人对应角色的模型行权限,模型由用户指定 |
系统 | system.currentEpoch() | 获取当前 10 位秒级时间戳 |
系统 | system.currentEpochMillis() | 获取当前 13 位毫秒级时间戳 |
model.tableName()。model.tableName('my_model_name')。-- 直接使用表名SELECT *FROM `student-preview`;-- 使用 SQL 模板绑定的当前模型数据SELECT *FROM {{ model.tableName() }};-- 使用模型student和class联表数据,当前绑定student模型SELECT student.*, class.*FROM {{ model.tableName() }} as studentINNER JOIN {{ model.tableName('class_info') }} as classON class.student_id = student._id;
model.dataId()。-- 手动写入数据标识INSERT INTO `my_model_table` (`_id`, `name`, `age`)VALUES ( 'xyz', '张三', 18);-- SELECT * FROM `my_model_table`-- ('xyz', '张三', 18)-- 函数写入数据标识INSERT INTO `my_model_table` (`_id`, `name`, `age`)VALUES ( {{ model.dataId() }}, '张三', 18);-- SELECT * FROM `my_model_table`-- ('B3PVJN4U2C', '张三', 18)
auth.rowPermission()。auth.rowPermission('my_model_name')。auth.rowPermission() 等价于 auth.rowPermission('xyz')。
-- 单表读权限SELECT *FROM 'student'WHERE `price` > 100 AND {{ auth.rowPermission() }}-- auth.rowPermission() 表示默认模型'student'的行权限-- 联表读权限SELECT *FROM 'model1_table' AS table1LEFT JOIN 'model2_table' AS table2ON table2.relate_id = table1._id AND {{ auth.rowPermission('model2') }}WHERE table1.`price` > 100 AND {{ auth.rowPermission() }}-- auth.rowPermission() 表示默认模型'model1'的行权限-- auth.rowPermission('model2') 表示指定模型'model2'的行权限
-- 仅删除当前登录人有写权限的数据DELETE FROM 'student'WHERE `age` > 10 AND {{ auth.rowPermission() }}-- 仅更新当前登录人有写权限的数据UPDATE 'student'SET `name` = '张三'WHERE {{ auth.rowPermission() }}
user.userId()。user.openId()。user.userId(), _openid 数据来自 user.openId()-- 约定:插入数据时写入owner和_openIdINSERT INTO 'my_model_table' (`_id`,`name`,`age`, `owner`, `_openid`)VALUES ('xyz', '张三', 18, {{ user.userId() }}, {{ user.openId() }})
system.currentEpoch()。system.currentEpochMillis()。UNIX_TIMESTAMP(NOW()) ,表示 13 位毫秒级时间戳戳 UNIX_TIMESTAMPNOW(3)) * 1000。UPDATE 'student'SET `name` = '张三', `updatedAt` = {{ system.currentEpochMillis() }}WHERE `_id` = 'xyz';
model.tableName() 表示;如果使用当前环境下其他的表(联表),使用函数 model.tableName('your_model_name')SELECT * FROM {{ model.tableName() }} as t1LEFT JOIN {{ model.tableName('other_model_name') }} as t2ON t2.relate_id = t1._id;
model.dataId()。user.userId()、user.openId()赋值。user.userId())createdAt、updatedAt(使用函数 system.currentEpochMillis())。INSERT INTO {{ model.tableName() }}(`_id`, `owner`, `_openid`, `createBy`, `updateBy`, `createdAt`, `updatedAt`)VALUES({{ model.dataId() }}, {{ user.userId() }}, {{ user.openId() }}, {{ user.userId() }}, {{ user.userId() }}, {{ system.currentEpochMillis() }}, {{ system.currentEpochMillis() }})
updatedAt,使用函数 system.currentEpochMillis()。UPDATE 'student'SET `name` = '张三', `updatedAt` = {{ system.currentEpochMillis() }}WHERE `_id` = 'xyz';
auth.rowPermission() 或者 auth.rowPermission('model_name')。-- 写权限UPDATE 'student'SET `name` = '张三'WHERE `_id` = {{ id }} AND {{ auth.rowPermission() }}-- 读权限SELECT * FROM {{ model.tableName() }}WHERE `_id` = {{ id }} AND {{ auth.rowPermission() }}
字段 | 说明 | 类型 | 系统字段 |
name | 教师姓名 | 文本 | 否 |
age | 年龄 | 数字 | 否 |
is_head_teacher | 是否班主任 | 布尔 | 否 |
gender | 性别 | 文本 | 否 |
class_id | 所属班级 | 文本 | 否 |
owner | 所有人 | 关联关系 | 是 |
_openid | 记录创建者 | 文本 | 是 |
createBy | 创建人 | 关联关系 | 是 |
updateBy | 修改人 | 关联关系 | 是 |
createdAt | 创建时间 | 日期时间 | 是 |
updatedAt | 更新时间 | 日期时间 | 是 |
INSERT INTO {{ model.tableName() }}(`_id`, `name`, `age`, `is_head_teacher`, `gender`, `owner`, `_openid`, `createBy`, `updateBy`, `createdAt`, `updatedAt`)VALUES({{ model.dataId() }}, {{ name }}, {{ age }}, {{ is_head_teacher }}, {{ gender }}, {{ user.userId() }}, {{ user.openId() }}, {{ user.userId() }}, {{ user.userId() }}, {{ system.currentEpochMillis() }}, {{ system.currentEpochMillis() }})
teacher_info,SQL 模板标识 insert_teacher_info_01,提供自定义变量入参。const res = await app.models.teacher_info.runSQLTemplate({templateName: 'insert_teacher_info_01',envType: "pre",params: {"name": "张老师","age": 30,"is_head_teacher": true,"gender": "男"}})
REPLACE INTO {{ model.tableName() }}(`_id`, `name`, `age`, `is_head_teacher`, `gender`, `owner`, `_openid`, `createBy`, `updateBy`, `createdAt`, `updatedAt`)VALUES({{ id }}, {{ name }}, {{ age }}, {{ is_head_teacher }}, {{ gender }}, {{ user.userId() }}, {{ user.openId() }}, {{ user.userId() }}, {{ user.userId() }}, {{ system.currentEpochMillis() }}, {{ system.currentEpochMillis() }})
teacher_info,SQL 模板标识 replace_teacher_info_01,提供自定义变量入参。const res = await app.models.teacher_info.runSQLTemplate({templateName: 'replace_teacher_info_01',envType: "pre",params: {"id": "123456", // 如果 id 数据存在则更新,不存在则新建"name": "张老师","age": 28,"is_head_teacher": false,"gender": "男"}})
INSERT INTO {{ model.tableName() }}(`_id`, `name`, `age`, `is_head_teacher`, `gender`, `owner`, `_openid`, `createBy`, `updateBy`, `createdAt`, `updatedAt`)VALUES({{ model.dataId() }}, {{ myParam }}, {{ user.userId() }}, {{ user.openId() }}, {{ user.userId() }}, {{ user.userId() }}, {{ system.currentEpochMillis() }}, {{ system.currentEpochMillis() }})
const res = await app.models.teacher_info.runSQLTemplate({templateName: 'insert_teacher_info_02',envType: "pre",params: {"myParam": ["王老师", 31, true, "女"]}})
{{ myParam }} 等价于 {{ name }}, {{ age }}, {{ is_head_teacher }}, {{ gender }}UPDATE {{ model.tableName() }}SET `name` = {{ name }}, `age` = {{ age }}, `updatedAt` = {{ system.currentEpochMillis() }}WHERE `_id` = {{ id }} AND {{ auth.rowPermission() }}
const res = await app.models.teacher_info.runSQLTemplate({templateName: 'update_teacher_info_01',envType: "pre",params: {"name": "李老师","age": 30,"id": "123456"}})
DELETE FROM {{ model.tableName() }}WHERE `_id` = {{ id }} AND {{ auth.rowPermission() }}
const res = await app.models.teacher_info.runSQLTemplate({templateName: 'delete_teacher_info_01',envType: "pre",params: {"id": "123456"}})
SELECT *FROM {{ model.tableName() }}WHERE `_id` = {{ id }} AND {{ auth.rowPermission() }}
const res = await app.models.teacher_info.runSQLTemplate({templateName: 'get_teacher_info_01',envType: "pre",params: {"id": "123456"}})
SELECT *FROM {{ model.tableName() }}WHERE `age` > {{ age }} AND {{ auth.rowPermission() }}
const res = await app.models.teacher_info.runSQLTemplate({templateName: 'get_teacher_info_02',envType: "pre",params: {"age": 20}})
SELECT teacher.* , class.*FROM {{ model.tableName() }} AS teacherLEFT JOIN {{ model.tableName('class_info') }} AS classON teacher.class_id = class._idWHERE teacher.`name` LIKE {{ var_name }} AND {{ auth.rowPermission() }}ORDER BY teacher.`age` DESCLIMIT 20 OFFSET 0
const res = await app.models.teacher_info.runSQLTemplate({templateName: 'get_teacher_info_03',envType: "pre",params: {"var_name": "张%"}})
-- 原始语法:不支持select * from `your_table` WHERE your_column IS NULL-- 替代语法:支持select * from `your_table` WHERE your_column <=> NULL-- 原始语法:不支持select * from `your_table` WHERE your_column IS NOT NULL-- 替代语法:支持select * from `your_table` WHERE !(your_column <=> NULL)
文档反馈