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 🙏

© 2025 – Pkg Stats / Ryan Hefner

nodejs-mysql-model

v1.0.1

Published

mysql model for nodejs

Downloads

3

Readme

node-mysql-model

nodejs mysql model

安装

使用npm安装

npm install nodejs-mysql-model -S

如何引用

先保证先安装了该模块

import { model, db } from 'nodejs-mysql-model'

API

初始化连接池

在项目入口文件创建一个数据库连接池

db.createPool({
  host: 'localhost', // localhost
  user: 'root',
  password: 'qwer123456', // qwer123456
  database: 'apidoc',
  port: 3306
})

配置model

根据每个表配置model

// User.js

import { model } from 'nodejs-mysql-model'

export default new model({
  tableName: 'user',
  attributes: {
    id: {
      type: String
    },
    username: {
      type: String
    },
    password: {
      type: String
    },
    created_at: {
      type: String,
      default: '0'
    },
    updated_at: {
      type: String,
      default: '0'
    },
    user_state: {
      type: Number,
      default: 1
    }
  }
})

// tableName
tableName和数据库表名一致

// attributes
attributes必须和相应表键值一致,type是数据类型(和javascript几种数据类型一致)会在删除或者新增的时候触发检测, default只有在用户触发save方法的时候触发

方法

使用之前必须先引入相应的model

import User from 'xxx/xxx/User'

所有的方法都是返回promise对象

User.find().then(res => {}).catch(res => {})

find

User.find();
User.find(method);
User.find(method, conditions);

参数:

  • string method: 方法名有(all, count, first, field)
  • object conditions: 查询条件有(fields, where, group, having, order, limit)

例子:

User.find('all', {where: "year > 2001"}).then(res => {
  // 成功回调
}).catch(res => {
  // 错误回调
})
find各种方式使用
  • all 获取相应条件所有的记录
  • return
{
  qerr: qerr, 
  vals: vals, 
  fields: fields
}

例子:

User.find('all', {where: "id > '1'", limit: [0, 30]}).then(res => {}).catch(res => {})
  • count 获取相应条件记录的条数
  • return
{
  qerr: qerr, 
  vals: vals, 
  fields: fields
}

例子:

User.find('count', {where: "id > '1'", limit: [0, 30]}).then(res => {}).catch(res => {})
  • first 获取相应条件的数据第一条
  • return
{
  qerr: qerr, 
  vals: vals, 
  fields: fields
}

例子:

User.find('first', {where: "id > '1'", limit: [0, 30]}).then(res => {}).catch(res => {})
  • field 自定义返回数据的字段
  • return
{
  qerr: qerr, 
  vals: vals, 
  fields: fields
}

例子:

User.find('field', {fields: ['username', 'user_state'], where: "id = 3"}).then(res => {}).catch(res => {})
find方法conditions使用
  • fields 获取相应条件记录并且返回指定字段 例子:
User.find('all', {fields: ['id', 'username', 'password']}).then(res => {}).catch(res => {})
// 等同于 select id, username, password from user
  • where 查询条件 例子:
User.find('all', {where: `username="xxx"`}).then(res => {}).catch(res => {})
// 等同于 select * from user where username="xxx"
  • group 以分组的方式进行查询 例子:
User.find('all', {group: ['id', 'username']}).then(res => {}).catch(res => {})
// 等同于 select * from user group by id, username
User.find('all', {group: 'id'}).then(res => {}).catch(res => {})
// 等同于 select * from user group by id
  • groupDESC 查询条件group 并且desc排序 例子:
User.find('all', {group: ['id', 'username'], groupDESC:true}).then(res => {}).catch(res => {})
// 等同于 select * from user group by id, username desc
  • having 查询条件havaing运算 例子:
User.find('all', {fields: ['name', 'COUNT(name)'], group: "name", having: "COUNT(name) = 1"}).then(res => {}).catch(res => {})
// 等同于 select name, count(name) from user group by name having count(name) = 1
  • order order排序 例子:
User.find('all', {order: ['id', 'username']}).then(res => {}).catch(res => {})
// 等同于 select * from user order by id, username
User.find('all', {order: 'id'}).then(res => {}).catch(res => {})
// 等同于 select * from user order by id
  • orderDESC order排序 例子:
User.find('all', {order: ['id', 'username'], orderDESC:true}).then(res => {}).catch(res => {})
// 等同于 select * from user order by id, username desc
User.find('all', {order: 'id', orderDESC:true}).then(res => {}).catch(res => {})
// 等同于 select * from user order by id desc
  • limit 查询限制条数 例子:
User.find('all', {limit: [0, 30]}).then(res => {}).catch(res => {})
// 等同于 select * from user limit 0, 30
User.find('all', {limit: '0, 30'}).then(res => {}).catch(res => {})
// 等同于 select * from user limit 0, 30

save

User.save();
User.save(where);

参数:

  • string where: 设置保存的条件(传了该参数会触发数据库的update)

例子:

User.set({
  username: "xxx",
  password: "xxx",
  user_state: "xxx"
})
User.save()
// 新增一条记录

User.set({
  username: "xxx",
  password: "xxx",
  user_state: "xxx"
})
User.save(`id="12"`)
// 修改记录

remove

User.remove();
User.remove(where);

参数:

  • string where: 删除条件

案例:

User.set({
   id: 1
})
User.remove()
User.remove('id = 1');
// 等同于delete from user where id=1

query

User.query(query);

参数:

  • string query: 需要运行的sql语句

案例:

User.query("select * from user").then(res => {}).catch(res => {});
// 等同于 select * from user

saveAll

User.saveAll(list);

参数:

  • array list: 需要一次性添加的数据

案例:

const list = [
      {
        id: "1",
        username: "xxx"
      },
      {
        user_id: "2",
        username: "xxxx"
      }
    ]
User.saveAll(list).then(res => {}).catch(res => {});
// 等同于 insert into user (id, username) values ('1', 'xxx'), ('2', 'xxxx')