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

dbrule

v1.2.0

Published

数据库rules管理

Downloads

9

Readme

node数据库操作

  • 基本增删改查操作
  • 增加rules校验
  • 根据rules进行数据库初始化

使用

const koa = require('koa');
const dbrule = require('dbrule');

const app = new koa();
// 数据库及字段配置
const rules = {
    // 表名称
    user: {
        // 字段名称
        username: {
            type: 'string',
            max: 20,
            name: '姓名',
            db: {
                type: 'float', // 数据库类型,和外部type可以不一致
                inc: false, // 是否自增字段
                primary: false, // 是否主键,和unique不能共用
                unique: false, // 是否创建唯一索引
            }
        }
    }
}

const config = {
    // 数据库连接配置,参考 https://github.com/laomu1988/ali-rds
    db: {
        host: 'your-rds-address.mysql.rds.aliyuncs.com',
        port: 3306,
        user: 'your-username',
        password: 'your-password',
        database: 'your-database-name',
    }
}

(async function() {
    app.dbs = await dbrule(rules, config, true); // 初始化数据库
    app.use(dbrule.koa(app.dbs, rules)); // 使用中间件,在ctx上增加dbs和rules

    app.use(function(ctx, next) {
        console.log('db:', ctx.dbs.user);
    });

    app.listen(8000);
})();

Todo

  • [x] 当未修改数据时,不执行alert table
  • [x] 当是primary或unique时,限制字段长度
  • [ ] unique且可以为null时简化处理
  • [ ] 补充使用文档
  • [ ] 仅生成更新sql语句脚本,不执行操作
  • [x] 增加extra配置

ChangeLog

  • v1.2.0(2020.09.09)
    • fix: db.create时不应该将db.default添加到默认值中,db.default默认值应该是数据库sql的默认值
  • v1.1.5(2020.09.08)
    • feat: 增加rule.db.extra属性,例如ON UPDATE CURRENT_TIMESTAMP
    • fix: 日期格式存在default值时,避免转换为字符串,例如CURRENT_TIMESTAMP
  • v1.1.4(2020.09.04)
    • fix: db.findOne未找到对象时不报错
  • v1.1.3
    • 升级async-validator到1.12.2版本,避免部分情况校验出错时无回调
  • v1.1.2
    • 使用rule.db.type作为默认类型
  • v1.1.1
    • 新增primary字段时语句修复
    • 更新时主键非id时处理修复
    • 字符串默认长度改为64字符,避免默认长度过长
  • v1.1.0
    • 当未修改数据时,不执行更新数据表操作
    • 增加部分测试case
  • v1.0.4
    • 数据库初始化完毕后关闭多余链接;
    • 增加数据库操作日志
  • v1.0.3
    • 修复重复创建索引问题
  • v1.0.1
    • 增加自动生成koa中间件方法
  • v1.0.0
    • 独立数据库操作,取消集成到koa内部