您当前的位置: 首页 > 技术文章 > 前端开发

node.js+Express+SQLServer封装模块

作者: 时间:2023-10-11阅读数:人阅读

为一个小项目开发的后端API框架,要求在现有SQLserver数据库上进行一些数据查询工作。使用express框架,并调用了微软官方的node-mssql,封装了SQLServer操作模块。
安装node-mssql

npm install mssql

db.mod.js模块

'use strict';
const mssql = require('mssql');
const conf = {
    ******************* 数据库配置 ********************/
    user: 'devuser',
    password: 'xxxxxx',
    server: 'localhost',
    database: 'dataname',
    port: 1433,
    options: {
        encrypt: false, // Use this if you're on Windows Azure
        enableArithAbort: true
    },
    pool: {
        min: 30,
        max: 128,
        idleTimeoutMillis: 30000
    }
};
/**
 * 原生查询
 * @param {sql语句} strsql 
 * @param {where参数对象} params 
 * @returns 返回所有结果对象
 */
var query = function (strsql,params) {
    return new Promise(function (resolve, reject) {
        mssql
            .connect(conf)
            .then(function () {
                var ser = new mssql.Request()
                if (params != "") {
                    for (var index in params) {
                        
                        if (typeof params[index] == "number") {
                            ser.input(escape(index), mssql.Int, params[index]);
                        } else if (typeof params[index] == "string") {
                            ser.input(escape(index), mssql.NVarChar, params[index]);
                        }
                    }
                }
                ser.query(strsql)
                    .then(function (recordset) {
                        resolve(recordset);
                    })
                    .catch(function (err) {
                        reject(conf.debug ? err : err.code)
                    });
            })
            .catch(function (err) {
                reject(conf.debug ? err : err.code)
            });
    });
};
/**
 * select 查询 
 * @param {sql语句} strsql 
 * @param {where参数对象} params 
 * @returns 返回查询数据对象
 */
var select = function (strsql,params) {
    return new Promise(function (resolve, reject) {
        mssql
            .connect(conf)
            .then(function () {
                var ser = new mssql.Request()
                if (params != "") {
                    for (var index in params) {
                        
                        if (typeof params[index] == "number") {
                            ser.input(escape(index), mssql.Int, params[index]);
                        } else if (typeof params[index] == "string") {
                            ser.input(escape(index), mssql.NVarChar, params[index]);
                        }
                    }
                }
                ser.query(strsql)
                    .then(function (recordset) {
                        var redata = [];
                        redata = recordset.recordset
                        resolve(redata);
                    })
                    .catch(function (err) {
                        reject(conf.debug ? err : err.code)
                    });
            })
            .catch(function (err) {
                reject(conf.debug ? err : err.code)
            });
    });
};
/**
 * find 查询一行数据
 * @param {sql语句} strsql 
 * @param {where参数对象} params 
 * @returns 返回一行数据对象
 */
var find = function (strsql,params) {
    return new Promise(function (resolve, reject) {
        mssql
            .connect(conf)
            .then(function () {
                var ser = new mssql.Request()
                if (params != "") {
                    for (var index in params) {
                        if (typeof params[index] == "number") {
                            ser.input(escape(index), mssql.Int, params[index]);
                        } else if (typeof params[index] == "string") {
                            ser.input(escape(index), mssql.NVarChar, params[index]);
                        }
                    }
                }
                ser.query(strsql)
                    .then(function (recordset) {
                        if (recordset.rowsAffected[0] >= 1){
                            var redata = recordset.recordset[0]
                        }
                        else {
                            var redata = false;
                        }
                        resolve(redata);
                    })
                    .catch(function (err) {
                        reject(conf.debug ? err : err.code)
                    });
            })
            .catch(function (err) {
                reject(conf.debug ? err : err.code)
            });
    });
};
/**
 * value 查询一个值
 * @param {sql语句} strsql 
 * @param {where参数对象} params 
 * @returns 返回第一行第一列数据值
 */
var value = function (strsql,params) {
    return new Promise(function (resolve, reject) {
        mssql
            .connect(conf)
            .then(function () {
                var ser = new mssql.Request()
                if (params != "") {
                    for (var index in params) {
                        if (typeof params[index] == "number") {
                            ser.input(escape(index), mssql.Int, params[index]);
                        } else if (typeof params[index] == "string") {
                            ser.input(escape(index), mssql.NVarChar, params[index]);
                        }
                    }
                }
                ser.query(strsql)
                    .then(function (recordset) {
                        var redata = '';
                        //console.log(recordset.recordset);
                        if (recordset.rowsAffected[0] >= 1){
                            var valueField = Object.keys(recordset.recordset[0]) 
                            //console.log(valueField); 
                            redata = recordset.recordset[0][valueField];
                        }
                        resolve(redata);
                    })
                    .catch(function (err) {
                        reject(conf.debug ? err : err.code)
                    });
            })
            .catch(function (err) {
                reject(conf.debug ? err : err.code)
            });
    });
};
/**
 * alter 修改数据 可以执行 插入 更新 删除等语句
 * @param {sql语句} strsql 
 * @param {参数对象} params 
 * @returns 返回false or true
 */
var alter = function (strsql,params) {
    return new Promise(function (resolve, reject) {
        mssql
            .connect(conf)
            .then(function () {
                var ser = new mssql.Request()
                if (params != "") {
                    for (var index in params) {
                        
                        if (typeof params[index] == "number") {
                            ser.input(escape(index), mssql.Int, params[index]);
                        } else if (typeof params[index] == "string") {
                            ser.input(escape(index), mssql.NVarChar, params[index]);
                        }
                    }
                }
                
                ser.query(strsql)
                    .then(function (recordset) {
                        if (recordset.rowsAffected[0] >= 1){
                            var redata = true;
                        }
                        else {
                            var redata = false;
                        }
                        resolve(redata);
                    })
                    .catch(function (err) {
                        reject(conf.debug ? err : err.code)
                    });
            })
            .catch(function (err) {
                reject(conf.debug ? err : err.code)
            });
    });
};
module.exports = {
    mssql,
    conf,
    query,
    select,
    find,
    value,
    alter
}

本站所有文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:licqi@yunshuaiweb.com

标签: sqlserver node.js
加载中~