`
sillycat
  • 浏览: 2486934 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

NodeJS MySQL Library and npmjs

 
阅读更多
NodeJS MySQL Library and npmjs

My friend publish a library based on mysql to support DAO, I forked that. It looks pretty good.

NodeJS method to convert keys in results
function convertResult(res, mappings) {
    if (res === undefined || mappings === undefined) {
        return res;
    }
    for (let i = 0; i < res.length; i++) {
        let item = res[i];
        let objKeys = Object.keys(item);
        let mappingKeys = Object.keys(mappings);
        for (let j = 0; j < objKeys.length; j++) {
            let sourceKey = objKeys[j];
            if (mappingKeys.includes(sourceKey)) {
                item[mappings[sourceKey]] = item[sourceKey];
                delete item[sourceKey];
            }
        }
    }
    return res;
}

I may use lodash to convert that
const _ = require('lodash');

class JSONUtil {

    static renamekey(obj, key, newKey) {
        if(_.includes(_.keys(obj), key)) {
            obj[newKey] = _.clone(obj[key], true);
            delete obj[key];
        }
        return obj;
    }

}

module.exports = JSONUtil

I clone my friends’ project. It seems useful to me.

If we want to publish the library to use, we need an account here https://www.npmjs.com/

Type command to login
> npm adduse
Logged in as luohuazju on https://registry.npmjs.org/.

Check who am I
> npm whoami
luohuazju

Publish to public
> npm publish --access=public

publish
> npm publish

Remove the package
> npm unpublish xpmysql@1.0.0


Or we can add codes in package.json as follow:
"dependencies": {
    "mysql": "2.18.1",
    "lodash": "4.17.15",
    "xpmysql": "https://github.com/baimeidaxia/xpmysql"
  }

Both ways are working fine.

References:
https://github.com/luohuazju/xpmysql
https://juejin.im/post/5d2708e26fb9a07f06559812

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics