UNPKG

ts-api-core

Version:

Nodejs api framework core

255 lines (254 loc) 9.47 kB
import { SrcConverterBase, SrcParams } from '../utils/model.helper'; import { ClassType, TargetType } from '../context/base.object'; export declare class ColumnParams extends SrcParams { /** 是否是主键 */ primary: boolean; /** 是否可以修改 */ updated: boolean; /** 是否可以插入 */ inserted: boolean; /** 是否允许查询 */ query: boolean; /** 是否存在默认值(为true时,插入字段为空时也允许继续) */ default: boolean; /** 是否允许使用累加的方式更新 */ accumulation: boolean; /** 字段别名 */ axios?: string; /** 字段名 */ _field?: string; /** 字段名称 */ get field(): string; } /** * pdate的处理方式定义 */ export declare enum UpdateOperatorType { /** * 覆盖 即用新的值覆盖调原有值 */ cover = 1, /** * 累加 即用原有值的基础上增加传入的值 */ accumulation = 2 } /** * SQL 脚本生成器 */ export declare class DBScript { /** * sql脚本 */ sql: string; /** * 执行参数 */ params: any[]; private static emptyFieldError; private static joinFields; /** 获取指定 entity 类型对应的表名称 */ static getTableName(target: TargetType<any>, tableName?: string): string | undefined; /** * 生成删除数据脚本 * @param table 表名 或 entity 类型 * @param fieldName ID字段名称 * @param id ID列表 * @returns */ static deleteByIds(table: string | TargetType<any>, fieldName: string, id: string | string[]): string | undefined; /** * 生成删除数据脚本 * @param condition 条件语句 * @returns */ static delete<T>(target: TargetType<T>, condition: string, options?: { /** 表名 */ table?: string; }): string | undefined; /** * 生成查询SQL语句 * @param target 目标 entity 类 * @param condition 条件语句 * @param options 选项 * @param options.alias 表的别名 * @param options.joinSql Join SQL 语句 * @param options.table 自定义表名称 * @param options.page 分页 页码,从 1 开始 * @param options.pageSize 分页大小 * @returns */ static query<T>(target: TargetType<T>, condition?: string, options?: { /** 表的别名 */ alias?: string; /** Join SQL 语句 */ joinSql?: string; /** 附加的字段信息 */ extFields?: string; /** 自定义表名称 */ table?: string; /** 页码 */ page?: number; /** 分页大小 */ pageSize?: number; }): string; static queryByTableColumns(tableName: string, columns: (ColumnParams | string)[] | string, condition?: string, options?: { /** 表的别名 */ alias?: string; /** Join SQL 语句 */ joinSql?: string; /** 附加的字段信息 */ extFields?: string; /** 页码 */ page?: number; /** 分页大小 */ pageSize?: number; }): string; /** * 生成插入脚本 * @param options 选项 * @param entity 数据对象 */ static insert<T>(target: TargetType<T>, entity: T | undefined, options?: { /** 表名 */ table?: string; }): DBScript | undefined; static insertByTableColumns<T>(tableName: string, columns: (ColumnParams | string)[], entity: T | undefined): DBScript | undefined; /** * 生成插入或更新脚本 * @param options 选项 * @param entity 数据对象 * @param operatorType 更新操作类型 */ static insertUpdate<T>(target: TargetType<T>, entity: T | undefined, options?: { /** 表名 */ table?: string; /** 更新操作类型, 默认 `UpdateOperatorType.cover` */ operatorType?: UpdateOperatorType; }): DBScript | undefined; /** * 生成批量插入脚本 * @param options 选项 * @param entity 数据对象数组 */ static batchInsert<T>(target: TargetType<T>, entity: T[] | undefined, options?: { /** 表名 */ table?: string; }): DBScript | undefined; static batchInsertByTableColumns<T>(tableName: string, columns: (ColumnParams | string)[], entity: T[] | undefined): DBScript | undefined; /** * 生成插入或更新脚本 */ static insertUpdateByTableColumns<T>(tableName: string, columns: (ColumnParams | string)[], entity: T | undefined, options?: { /** 更新操作类型, 默认 `UpdateOperatorType.cover` */ operatorType?: UpdateOperatorType; }): DBScript | undefined; /** * 生成批量插入或更新脚本 * @param options 选项 * @param entity 数据对象数组 */ static batchInsertUpdate<T>(target: TargetType<T>, entity: T[] | undefined, options?: { /** 表名 */ table?: string; /** 更新操作类型, 默认 `UpdateOperatorType.cover` */ operatorType: UpdateOperatorType; }): DBScript | undefined; /** * 生成批量插入或更新脚本 */ static batchInsertUpdateByTableColumns<T>(tableName: string, columns: (ColumnParams | string)[], entity: T[] | undefined, options?: { /** 更新操作类型, 默认 `UpdateOperatorType.cover` */ operatorType?: UpdateOperatorType; }): DBScript | undefined; /** * 生成更新脚本 * @param options 选项 * @param entity 数据对象数组 * @param condition 更新的包含条件 */ static update<T>(target: TargetType<T>, entity: T | undefined, condition: string, options?: { /** 表名 */ table?: string; /** 更新操作类型, 默认 `UpdateOperatorType.cover` */ operatorType?: UpdateOperatorType; /** 条件参数 */ conditionArgs?: any[]; }): DBScript | undefined; static updateByTableColumns<T>(tableName: string, columns: (ColumnParams | string)[], entity: T | undefined, condition: string, options?: { /** 更新操作类型, 默认 `UpdateOperatorType.cover` */ operatorType?: UpdateOperatorType; /** 条件参数 */ conditionArgs?: any[]; }): DBScript | undefined; /** * 获取插入更新的sql * @param updateColumns * @param entity * @param operatorType 更新操作类型, 默认 `UpdateOperatorType.cover` * @returns */ private static getInsertUpdateSql; private static readonly _cacheEntityKeys; private static getEntityKeys; /** * 获取数据对象对应的列信息 * @param entity 数据对象 * @param allowInsert 列必须允许插入, 排除掉不允许插入的列 * @param allowUpdate 列必须允许更新, 排除掉不允许更新的列 * @returns */ static getColumns<T>(entity: TargetType<T>, allowInsert?: boolean, allowUpdate?: boolean, excludePrimary?: boolean): (ColumnParams | string)[]; /** * 获取数据对象对应的列信息字符串 * @param entity 数据对象 * @param alias 表别名 * @param allowInsert 列必须允许插入, 排除掉不允许插入的列 * @param allowUpdate 列必须允许更新, 排除掉不允许更新的列 * @returns */ static getColumnsStr<T>(entity: TargetType<T>, alias?: string, allowInsert?: boolean, allowUpdate?: boolean, excludePrimary?: boolean): string; static columnsToStr(columns: (ColumnParams | string)[], alias?: string): string; /** * 生成分页需要的 Limit 条件 SQL 语句 * @param pageIndex 页码,从1开始 * @param pageSize 分页大小 */ static limit(pageIndex: number | undefined, pageSize: number | undefined): string; /** * 将字符串或数字的数组转化为 SQL 语句 IN 的可选值字符串 * @param ids 字符串数组 * @returns ` ('n','n1','n2',...) ` */ static tranArrayToInSql(ids: (string | number)[]): string; /** 正则查询最后一个 */ private static lastRegFindStr; /** * 获取总行数的SQL,总行数字段名 `rowCount` * @param sql sql 语句 * @param mini 简化统计行数的 sql , 默认 `0` * @returns */ static rowCountSql(sql: string, mini?: number): string; } /** 列定义选项 */ export declare class ColumnOption { /** 是否是主键,默认 `false` */ primary?: boolean; /** 是否允许更新,默认 `true` */ updated?: boolean; /** 是否允许插入,默认 `true` */ inserted?: boolean; /** 是否允许查询, 默认 `true` */ query?: boolean; /** 是否存在默认值,默认 `false` */ default?: boolean; /** 是否允许使用累加的方式更新值, 默认 `false` */ accumulation?: boolean; /** 别名,默认 `undefined` */ axios?: string; } /** 表定义 */ export declare function Table(tableName: string): (target: any) => void; /** 列定义,适用于 entity 类 */ export declare function column(field?: string | undefined, options?: ColumnOption, convert?: ClassType<SrcConverterBase>, ...args: any | undefined): any;