npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stroll/mysql

v0.0.3

Published

mysql

Downloads

1

Readme

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) => {
  // ...
})