UNPKG

@lcap/nasl

Version:

NetEase Application Specific Language

109 lines (103 loc) 5.05 kB
// 基本类型定义 type Any = any; type Long = number; type Decimal = number; type Boolean = boolean; type String = string; /* 日期类型 */ type Date = globalThis.Date; /* 时间类型 */ class Time { accept: 'Time'; } /* 日期时间类型 */ class DateTime { accept: 'DateTime'; } type List<T> = Array<T>; /* * for...of 语句 * 这是唯一一个可以在逻辑中使用的循环语句,用于遍历列表中的元素,禁止使用for、while等其他循环语句。 * param index: Long,当前索引,必填 * param item: T,当前元素,必填 * [index, item] 为解构赋值,顺序不可颠倒。 * ListEntries 仅用于for...of循环,声明为: function ListEntries<T>(list: T[], start?: Long, end?: Long): [Long, T][]; * param list: List<T>,需要遍历的列表,必填 * param start: Long,开始索引,默认值 0 * param end: Long,结束索引,默认值 list.length * ListEntries([1, 2, 3]) => [[0, 1], [1, 2], [2, 3]] * ListEntries([1, 2, 3], 1, 2) => [[1, 2]] * @example for (const [index, item] of ListEntries(list, start, end)) { console.log(index, item); } * @example for (const [index, item] of ListEntries(nasl.util.ListRange(2, 5, 1))) { console.log(index, item); } */ /* 基本运算函数,用于数学计算 */ declare function plus(left: Decimal, right: Decimal): Decimal; declare function minus(left: Decimal, right: Decimal): Decimal; declare function multiply(left: Decimal, right: Decimal): Decimal; declare function divide(left: Decimal, right: Decimal): Decimal; declare function remainder(left: Decimal, right: Decimal): Decimal; /* 分页组件函数,用于分页 */ declare function PAGINATE<T>(list: List<T>, page: Long, size: Long): { list: List<T>, total: Long; }; /** * 数据库操作函数系列 * 平台提供两种数据库操作方式:SQL语言和数据实体接口。二者分别适用于不同的场景: * 使用SQL语言进行查询操作,使用数据实体接口进行增删改操作。 */ /** * SQL数据查询, 只使用该方法进行数据查询 * @param sql 数据查询需要的 SQL * @returns 返回查询结果列表 * * 关于T的类型说明: * 1. 如果查询字段均来自一个数据表,T为该数据表的类型 * 2. 如果查询字段来自多个数据表,那么T为匿名类型,包含所有查询字段, 如:{name: string, age: number} * 在调用时,T可以不指定,平台会自动进行推断 * * 优先使用数据库的查询和数据操纵能力,例如 JOIN、SUM、GROUP_BY 等 * 所有变量格式为:${variable},不能用''包裹,不能用函数处理。 * @example `SELECT * FROM Student WHERE id = ${id}` * @example `SELECT OrderProduct.*, Product.price as price FROM OrderProduct JOIN Product ON OrderProduct.productId = Product.id WHERE orderId = ${id}` * * 关于返回值类型, 可能是列表或单个值, 根据具体的SQL语句来确定。 * @example `SELECT SUM(price) FROM Goods` -> T * @example `SELECT SUM(price) FROM Goods GROUP BY user` -> List<T> * @example `SELECT * FROM Goods` -> List<T> */ declare namespace nasl.oql { export function query<T>(sql: string): List<T> | T; } /** * 数据实体接口 * 作用:数据库增删改操作 */ interface Entity<T> { /* 根据主键获取一条数据。注意:该方法仅支持使用主键进行查询, 且只返回一条数据 */ get(id: Long): T; /** * 将传入的数据实体插入到数据表中 * @param entity 数据实体,T类型。不包含id, createTime, updateTime, createdBy, updatedBy字段 * @returns 返回插入的数据实体,T类型。已自动生成id, createTime, updateTime, createdBy, updatedBy字段, 可以通过返回值获取插入数据在数据库中的id * @example const person = nasl.util.NewEntity<Person>({name: 'test', age: 18}; insertPerson = PersonEntity.create(person); // 可以根据insertPerson.id获取插入数据在数据库中的id */ create(entity: T): T; /* 根据传入的数据对象更新数据表中的数据 */ update(entity: T): T; /* 根据主键删除数据表中的数据 */ delete(id: Long): void; /* 根据传入的数据对象创建或更新数据表中的数据 */ createOrUpdate(body: T): T; /* 根据主键列表批量删除数据表中的数据 */ batchCreate(list: List<T>): List<T>; /* 根据传入的数据对象列表批量更新数据表中的数据 */ batchUpdate(list: List<T>): List<T>; /* 根据主键列表批量删除数据表中的数据 */ batchDelete(list: List<Long>): void; } /* * createEntity函数:根据数据表类型创建一个数据实体接口,然后通过该接口调用数据表中的数据。 * @example const LCAPUserEntity = createEntity<LCAPUser>(); LCAPUserEntity.delete(id); * 注意:对应接口已经在下面的实体列表中定义好了,不需要重新定义。 */ declare const createEntity: <T>() => Entity<T>; /* EntityRelation函数:用于定义数据表内的外键关联关系。*/ declare function EntityRelation(property: any): any;