0.0.3 • Published 10 months ago

@stroll/mysql v0.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

mysql

mysql 数据库模型封装

安装

npm i -S @stroll/mysql
// 只导出MySql
import mysql from '@stroll/mysql'
// OR
import {
  Model, // 模型
  verifyResults, // 验证结果
  createOrUpdateVerify, // 更新验证
  Mysql, // mysql 数据库 class, mysql2 二次封装
  MYSQL, // mysql2 数据库
  FieldTypes, // 字段验证
  FieldTypeKeys, // 字段类型范围
  arithmeticOperator, // 算术 运算符
  comparisonOperator, // 比较 运算符
  logicalOperators, // 逻辑 运算符
  bitwiseOperator, // 位 运算符
  Comparator, // 比较、逻辑、位、算术 运算符
} from '@stroll/mysql'

使用

const mysql = new Mysql({
  user: 'root',
  password: '123456',
  database: 'crm',
  // ...
})
// OR
const mysql = Mysql.inst({
  user: 'root',
  password: '123456',
  database: 'crm',
  // ...
})

// 创建模型
const user = Model.init({
  // 字段规则
  name: {
    type: FieldTypes.VARCHAR
  },
  age: {
    type: FieldTypes.TINYINT.UNSIGNED
  }
}, {
  db: mysql,
  table: 'user'
})

// 添加新数据
user.create(
  // 数据
  {
    // ...
  },
  // 需要保存的表字段
  tableKeys?: string[]
).then((res) => {
  // ...
})

// 更新数据
user.create(
  // 数据
  {
    // ...
  },
  // 需要更新的唯一标识 默认 id
  mark?: 'id'
).then((res) => {
  // ...
})

// 创建或更新数据
user.upsert(
  // 数据
  {
    // ...
  },
  // 需要更新的唯一标识 默认 id
  mark?: 'id'
).then((res) => {
  // ...
})

// 查询
user.find(
  // 查询的条件
  {
    // ...
  },
).then((res) => {
  // ...
})

// 分页查询
user.findPage(
  {
    // 页码
    pageNum: 1,
    // 条数
    pageSize: 20,
    // 查询的条件
    factor: {}
  },
).then((res) => {
  // ...
})

// 分页查询
user.findPage(
  {
    // 页码
    pageNum: 1,
    // 条数
    pageSize: 20,
    // 查询的条件
    factor: {}
  },
).then((res) => {
  // ...
})

// 表数据条数
user.count(
  {
    // 查询类型 默认 *
    column: '*',
    // 查询的条件
    factor: {}
  },
).then((res) => {
  // ...
})

// 删除数据(软删除)
user.delete(
  // 查询的条件
  {},
  // 唯一标识 默认 id
  'id'
).then((res) => {
  // ...
})

// 恢复数据(恢复软删除数据)
user.restore(
  // 查询的条件
  factor,
  // 唯一标识 默认 id
  'id'
).then((res) => {
  // ...
})

// 删除数据(物理删除)
user.destroy(
  // 查询的条件
  {},
  // 唯一标识 默认 id
  'id'
).then((res) => {
  // ...
})

// 获取表字段类型
user.type(
  // (默认当前表) 表名 或 {field: 字段, tableName: 表明, databaseName: 库名, where: where语句}
  {},
  // 字段列表 或 以逗号分隔的字符串
  ''
).then((res) => {
  // ...
})

// sql语句操作
user.query(
  // sql 语句
  '',
  // sql 语句的值
  {}
).then((res) => {
  // ...
})

// 查询表是否存在
user.showTable(
  // 表名称 type参数是WHERE 此值为检索条件
  '',
  // 检索类型
  ''
).then((res) => {
  // ...
})

// 创建表
user.buildTable(
  // 表名称 或 表信息的集合
  '',
  // 字段信息
  ''
).then((res) => {
  // ...
})

// 联表
user.linkedTable({ // 联表信息
  // 表名称 或 表信息
  tableInfo?: string | {[s: string]: string | string[]},
  // 次表 或 次表信息
  minorTable: string|{[s: string]: string|{[s:string]: string}},
  // 表关系
  relation?: string,
  // 条件
  where?: string,
  // 默认 true
  nestTables?: boolean | string,
}).then((res) => {
  // ...
})
0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago