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

@fmfe/lib-sql

v1.0.2

Published

集成mysql,sqlserver等数据库的第三方库

Downloads

1

Readme

mysql,sql server等数据库连接集成库

Installation

$ npm install @fmfe/lib-sql

or

$ yarn add @fmfe/lib-sql

Usage

有两种传入配置信息的方式:

使用config来管理我们的配置文件.

假设我们的项目目录下有一个config目录,config目录里有一个dev.json文件.

config/dev.json


{
    "mysql": {
        "host": "127.0.0.1",
        "port": 3306,
        "database": "test",
        "user": "root",
        "password": "123456"
    },
    "mssql": {
        "user": "sa",
        "password": "123456",
        "server": "127.0.0.1",
        "database": "test",
        "port": 1433,
        "pool": {
            "min": 0,
            "max": 10,
            "idleTimeoutMillis": 3000
        }
    }
}

mysql.js

const { mysql } = require('@fmfe/lib-sql');

const mysqlPool = mysql.init();

const _getNewSqlParamEntity = mysql._getNewSqlParamEntity;

// 执行单条sql语句  
// mysql.exec(mysqlPool, sql, params);
async function exec() {
    const sql1 = 'select * from ?? limit 2';
    const data = await mysql.exec(mysqlPool, sql1, ['tbl_user']);
}

// 执行mysql事务,可以传入多条增/删/改sql语句 
// mysql.exectrans(mysqlpool, sqlParamsEntity);
async function execTrans() {
    const sqlParamsEntity = [];
    const sql1 = 'insert into ?? (name, age, sex) values (?, ?, ?)';
    const param1 = ['tbl_user', 'aaa', 20, 1];
    sqlParamsEntity.push(_getNewSqlParamEntity(sql1, param1));

    const sql2 = 'insert into ?? (name, age, sex) values (?, ?, ?)';
    const param2 = ['tbl_user', 'bbb', 22, 0];
    sqlParamsEntity.push(_getNewSqlParamEntity(sql2, param2));

    const sql3 = 'update ?? set age = ? where id = ?';
    const param3 = ['tbl_user', 10, 1];
    sqlParamsEntity.push(_getNewSqlParamEntity(sql3, param3));
    // ....
    const data = await mysql.execTrans(mysqlPool, sqlParamsEntity);  
}

mssql.js

const { mssql } = require('@fmfe/lib-sql');
const _getNewSqlParamEntity = mssql._getNewSqlParamEntity;

// 执行单条语句  
// mssql.exec(sql)
async function exec() {
    const sql1 = 'select Top 3 name, age, sex from tbl_user order by age desc';
    const data = await mssql.exec(sql1);  
}

// 执行sql server 事务, 最好执行增/删/改语句,这里只是用select演示使用方法
// mssql.exectrans(sqlParamsEntity); 
async function exectrans() {
  
    const sqlParamsEntity = [];
    const sql1 = 'select * from tbl_user where id = 1';
    sqlParamsEntity.push(_getNewSqlParamEntity(sql1));

    const sql2 = 'select * from tbl_user where id = 2';
    sqlParamsEntity.push(_getNewSqlParamEntity(sql2));
    // ...
    const data = await mssql.execTrans(sqlParamsEntity);  
}

由于使用config管理配置文件, 运行项目时通过使用命令: NODE_ENV=dev node ..., @fmfe/lib-sql即可自动获取到数据库相关配置.

通过传入配置文件来调用库

我们引用上边的代码示例,只需做一点改动:

mysql.js

只需在初始化时传入mysql数据库配置就好.

const { mysql } = require('@fmfe/lib-sql');

const mysqlPool = mysql.init({
    host: '127.0.0.1',
    port: 3306,
    database: 'test',
    user: 'root',
    password: '123456'
});

......

mssql.js

在每次调用方法时传入配置

const { mssql } = require('@fmfe/lib-sql');
const config = {
    user: 'sa',
    password: '123456',
    server: '127.0.0.1',
    database: 'test',
    port: 1433,
    pool: {
        min: 0,
        max: 10,
        idleTimeoutMillis: 3000
    }  
}

async function exec() {
    ......
    const data = await mssql.exec(sql1, config);  
}

async function exectrans() {
  
    ......
    const data = await mssql.execTrans(sqlParamsEntity, config);  
}

自动化脚本

| 脚本 | 描述信息 | |-------|----------| |npm run lint| eslint检测代码| |npm run lint:fix| 自动修复部分不符合eslint规范的代码| |npm run test| 运行测试用例| |npm run report| 生成html形式的测试报告 |

代码提交规范

git commit message 必须符合AngularJS Git Commit Message Conventions规范。

安装Commitizen工具

$  npm install -g commitizen  
or 
$  yarn global add commitizen

替换git commit 命令

$ git cz