-- SQL corresponding to student_select2SELECT *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 and pre.SELECT * FROM student, but you cannot use SELECT * FROM user. The same rule applies to other commands such as INSERT.{{param}} format. Variable parameters are used to pass variable values when the SQL template is actually invoked and executed.{{ }} notation.Function Category | Function expressions | Description |
Model | model.tableName() | Obtains the table name corresponding to the model to which the SQL template belongs, automatically identifying the experience and production environments. |
Model | model.tableName('my_model') | Obtains the table name corresponding to the specified model. |
Model | model.dataId() | Automatically generates a system-style data ID. |
User | user.userId() | Obtains the user identifier of the currently logged-in user at runtime. |
User | user.openId() | Obtains the openId of the currently logged-in user at runtime. If the user is logged in via a mini program, the openId corresponds to the WeChat openId. |
Permission | auth.rowPermission() | Obtains the model row permissions for the role corresponding to the currently logged-in user at runtime, where the model is the one bound to the SQL template. |
Permission | auth.rowPermission('my_model') | Obtains the model row permissions for the role corresponding to the currently logged-in user at runtime, where the model is specified by the user. |
System | system.currentEpoch() | Obtains the current 10-digit second-level timestamp. |
System | system.currentEpochMillis() | Obtains the current 13-digit millisecond-level timestamp. |
model.tableName().model.tableName('my_model_name').-- Use table names directlySELECT *FROM `student-preview`;-- Use the current model data bound to the SQL templateSELECT *FROM {{ model.tableName() }};-- Use the joined table data of the student and class models, with the student model currently bound.SELECT student.*, class.*FROM {{ model.tableName() }} as studentINNER JOIN {{ model.tableName('class_info') }} as classON class.student_id = student._id;
model.dataId().-- Manually write the data identifierINSERT INTO `my_model_table` (`_id`, `name`, `age`)VALUES ('xyz', 'Zhang San', 18);-- SELECT * FROM `my_model_table`-- ('xyz', 'Zhang San', 18)-- Function writes the data identifierINSERT INTO `my_model_table` (`_id`, `name`, `age`)VALUES ({{ model.dataId() }}, 'Zhang San', 18);-- SELECT * FROM `my_model_table`-- ('B3PVJN4U2C', 'Zhang San', 18)
auth.rowPermission().auth.rowPermission('my_model_name').auth.rowPermission() is equivalent to auth.rowPermission('xyz').-- Single-table read permissionsSELECT *FROM 'student'WHERE `price` > 100 AND {{ auth.rowPermission() }}-- auth.rowPermission() represents the row permissions of the default model 'student'.-- Multi-table join read permissionsSELECT *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() represents the row permissions of the default model 'model1'.-- auth.rowPermission('model2') represents the row permissions of the specified model 'model2'.
-- Only delete data for which the currently logged-in user has write permissions.DELETE FROM 'student'WHERE `age` > 10 AND {{ auth.rowPermission() }}-- Only update data for which the currently logged-in user has write permissions.UPDATE 'student'SET `name` = 'Zhang San'WHERE {{ auth.rowPermission() }}
user.userId().user.openId().user.userId(), while the _openid data is derived from user.openId().-- Convention: Write owner and _openId when inserting data.INSERT INTO 'my_model_table' (`_id`,`name`,`age`, `owner`, `_openid`)VALUES ('xyz', 'Zhang San', 18, {{ user.userId() }}, {{ user.openId() }})
system.currentEpoch().system.currentEpochMillis().UNIX_TIMESTAMP(NOW()), and the 13-digit millisecond-level timestamp is represented by UNIX_TIMESTAMP(NOW(3)) * 1000.UPDATE 'student'SET `name` = 'Zhang San', `updatedAt` = {{ system.currentEpochMillis() }}WHERE `_id` = 'xyz';
model.tableName(). If you are using other tables in the current environment (for joins), use the function 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() for assignment.user.userId() and user.openId().user.userId()) and createdAt and updatedAt (using the function system.currentEpochMillis()) based on business needs.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 using the function system.currentEpochMillis().UPDATE 'student'SET `name` = 'Zhang San', `updatedAt` = {{ system.currentEpochMillis() }}WHERE `_id` = 'xyz';
auth.rowPermission() or auth.rowPermission('model_name').-- Write permissionsUPDATE 'student'SET `name` = 'Zhang San'WHERE `_id` = {{ id }} AND {{ auth.rowPermission() }}-- Read permissionsSELECT * FROM {{ model.tableName() }}WHERE `_id` = {{ id }} AND {{ auth.rowPermission() }}
Field | Description | Type | System Fields |
name | Teacher name | Text | No |
age | Age | Digits | No |
is_head_teacher | Whether head teacher | Boolean | No |
gender | Gender | Text | No |
class_id | Class | Text | No |
owner | Owner | Association relationship | Yes |
_openid | Record creator | Text | Yes |
createBy | Creator | Association relationship | Yes |
updateBy | Modifier | Association relationship | Yes |
createdAt | Creation time | Date and time | Yes |
updatedAt | Update time | Date and time | Yes |
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 and the SQL template identifier insert_teacher_info_01 to provide custom variable input parameters.const res = await app.models.teacher_info.runSQLTemplate({templateName: 'insert_teacher_info_01',envType: "pre",params: {"name": "Teacher Zhang","age": 30,"is_head_teacher": true,"gender": "male"}})
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 and the SQL template identifier replace_teacher_info_01 to provide custom variable input parameters.const res = await app.models.teacher_info.runSQLTemplate({templateName: 'replace_teacher_info_01',envType: "pre",params: {"id": "123456", // Update if the id data exists; otherwise, create a new record."name": "Teacher Zhang","age": 28,"is_head_teacher": false,"gender": "male"}})
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": ["Teacher Wang", 31, true, "female"]}})
{{ myParam }} in the SQL template is equivalent to {{ 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": "Teacher Li","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": "Zhang%"}})
-- Original syntax: Not supported.select * from `your_table` WHERE your_column IS NULL-- Alternative syntax: Supported.select * from `your_table` WHERE your_column <=> NULL-- Original syntax: Not supported.select * from `your_table` WHERE your_column IS NOT NULL-- Alternative syntax: Supported.select * from `your_table` WHERE !(your_column <=> NULL)
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback