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

@cic-digital/node-dao-db2

v0.1.0

Published

## node-db2数据访问组件,方便用户快速构建db2数据库访问模块,提高程序的可读性

Downloads

9

Readme

node-db2数据访问组件

node-db2数据访问组件,方便用户快速构建db2数据库访问模块,提高程序的可读性

用法

install

npm install --save @cic-digital/node-dao-db2

QuickStart

这里的example可以在example目录下找到

Step1 创建业务模型,比如我们需要一个模型:用户,有如下的ddl

CREATE TABLE user (
   userid    VARCHAR(100) NOT NULL,
   password  VARCHAR(50) NOT NULL,
   username  VARCHAR(200) NOT NULL,
   userlevel CHAR(1) NOT NULL,
   updatetimestamp TIMESTAMP NOT NULL GENERATED ALWAYS FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP,
   PRIMARY KEY (userid));

Step2 根据业务模型,创建对应的数据模型(bizModel.js)

// 引入schema配置包
const Joi = require('joi');
// 引入数据模型基础类
const { DataModel } = require('../dataModel');

// 创建用户类继续基础模型
class User extends DataModel {
  constructor(database) {
    super(database);
    // 根据ddl创建schema
    this.fullschema = {
      USERID: Joi.string().max(100).required(),
      PASSWORD: Joi.string().max(50).required(),
      USERNAME: Joi.string().max(50).required(),
      USERLEVEL: Joi.string().valid('0', '1', '2', '3'),
      UPDATETIMESTAMP: Joi.string(),
    };
    // 指定主key
    this.keyfieldlist = ['USERID'];
    // 指定数据库设定字段
    this.autosetlist = ['UPDATETIMESTAMP'];
    // 指定表名
    this.tablename = 'USER';
  }

  // 复杂处理可以自行定义方法
  async mycustomfunction() {
    const sql = `select * from ${this.tablename} where USERID = ?`;
    const result = await this.database.executeSql(sql, ['userid']);
    return result;
  }
}

module.exports.User = User;

schema定义方法参考

Step3 创建业务处理(bizProcess.js)

const { transaction } = require('../index');
const { log } = require('../util/log');
const { User } = require('./bizModel');
const { Where } = require('../index');

async function bizProcess(database, uuid, data) {
  log.info(uuid, 'bizProcess start');
  const user = new User(database);
  // 设定用户记录信息
  const newuser = {
    USERID: data.userid,
    PASSWORD: data.password,
    USERNAME: data.username,
    USERLEVEL: '1',
  };
  // 创建用户的记录
  const newuserdata = await user.create(newuser);
  log.info('newuserdata', newuserdata);
  // 设置检索key, 本例为 USERLEVEL >0 AND USERLEVEL < 10
  const usersearchkey = {
    USERLEVEL: Where.base().greater(0).less(10),
  };
  // 根据检索条件搜索数据库获得返回结果
  const searchresult = await user.retrieve(usersearchkey);
  log.info(uuid, 'searchresult', searchresult);
  log.info(uuid, 'bizProcess normal end');
  return searchresult;
}

// 申明业务处理需要transaction
module.exports.bizProcess = transaction(bizProcess);

Step4 创建业务处理入口(main.js)

const uuidv4 = require('uuid/v4');
const { bizProcess } = require('./bizProcess');
const { log } = require('../util/log');

// 调用参数
const testdata = {
  userid: 'testuser',
  password: '12345',
  username: 'testusername',
};

// 调用业务处理
bizProcess(uuidv4(), testdata)
  .then(result => log.info(result))
  .catch(error => log.error(error));

注意:使用前请定义以下环境变量

export DB2DATABASE=yourdatabasename
export DB2HOSTNAME=databaseip
export DB2UID=userid
export DB2PWD=password
export DB2PORT=port

类似retrieve, create等等内置方法使用说明 参考API文档