数据查询
Query Param DTS
查询条件配置类型描述
ts
/**
* 表信息类型
*/
type TABLE_TYPE = DefineTableResult & { alias?: string }
/**
* 表信息配置项
*/
type TABLE_OPTION = TABLE_TYPE | TABLE_TYPE[]
interface ConditionOption {
/**
* 匹配值
*/
value?: VALUE_TYPE
/**
* 等式类型
*/
type?: CONDITION_EQUATION_TYPE
/**
* 是否为或语句
*/
or?: boolean
/**
* 表名或表别名
*/
t?: string
/**
* 子查询
*/
subquery?: ExecQueryParam
}
/**
* 查询条件配置项
*/
type CONDITION_TYPE = Record<
string,
VALUE_TYPE | ConditionOption | ConditionOption[]
>
/**
* 字段配置项
*/
interface FIELD_OPTION {
/**
* 字段名
*/
name: string
/**z
* 字段别名
*/
alias?: string
/**
* 表名或表别名
*/
t?: string
}
/**
* 字段配置类型
*/
type FIELDS_OPTION = string | FIELD_OPTION | (FIELD_OPTION | string)[]
/**
* 表连接查询配置项
*/
interface JOIN_PARAM_TYPE {
/**
* 连接方式,默认:INNER JOIN
*/
type?: 'LEFT' | 'RIGHT' | 'INNER'
/**
* 连接表配置信息
*/
t: TABLE_TYPE
/**
* 连接条件配置信息
*/
on: {
/**
* 连接字段名
*/
joinField: string
/**
* 主表字段名
*/
mainField: string
/**
* 主表表名或表别名
*/
mainTable: TABLE_TYPE
}
}
/**
* 排序配置项
*/
interface OrderByParam {
/**
* 数据库字段名
*/
field: string
/**
* 排序类型
* ASC | 升序
* DESC | 降序
*/
type?: 'ASC' | 'DESC'
/**
* 表名或表别名
*/
t?: string
}
interface ExecQueryBaseParam {
/**
* 表信息
*/
t: TABLE_OPTION
/**
* 查询条件
*/
condition?: CONDITION_TYPE
/**
* 查询字段
*/
fields?: FIELDS_OPTION
/**
* 表连接查询配置信息
*/
join?: JOIN_PARAM_TYPE | JOIN_PARAM_TYPE[]
/**
* 排序
*/
orderBy?: OrderByParam
}
/**
* 查询条目区间配置项
*/
interface LIMIT_PARAM {
/**
* 开始位置
*/
start: number
/**
* 查询数量
*/
length: number
}
/**
* 执行查询参数
*/
interface ExecQueryParam extends ExecQueryBaseParam {
/**
* 查询条目区间
*/
limit?: LIMIT_PARAM
}Example
ts
import emysql, { DefineTable } from '@aicblock/emysql'
// 数据库实例化
const mysql = new emysql({
password: '[db登录密码]',
user: '[db登录用户名]',
database: '访问数据库名称'
})
// 初始化数据库
await mysql.init()
// 创建表结构
const t_user = DefineTable({
tableName: 't_user',
columns: [
{
name: 'id',
dataType: 'INT',
primaryKey: true,
autoIncrement: true,
comments: '主键id'
},
{
name: 'name',
dataType: 'VARCHAR',
length: 45,
notNull: true,
comments: '名称'
},
{
name: 'age',
dataType: 'INT',
notNull: true,
comments: '年龄'
}
]
})
// 创建表
await mysql.table.create(t_user)
// t_user 数据
const user = [
{ name: 'name1', age: 20 },
{ name: 'name2', age: 21 },
{ name: 'name3', age: 30 },
{ name: 'name4', age: 31 },
{ name: 'name5', age: 18 }
]
// 查询年龄大于等于20的用户
const result = await mysql.query({
t: t_user,
fields: ['name'],
condition: { age: { value: 20, type: 'GREATER_THAN_OR_EQUAL' } }
})
// result:: [{ name: 'name1' },{ name: 'name2' },{ name: 'name3' },{ name: 'name4' }]