jh-common-dataaccess

0.0.9 • Public • Published

jh-common-dataaccess 通用数据库操作

支持常见数据库操作,如 sqlserver、mysql、postgresql、sqlite 等,可实现通用查询和数据映射

暂时屏蔽对sqlite的支持,使用率不高,下载速度慢

Installation

$ npm install jh-common-dataaccess

Quick Example

Please make sure that the following information exists in your databases.

database:ECommerce、table:userinfo

Field Type
id string
createby string
const { DataAccess } = require('jh-common-dataaccess');
 
const ds = new DataAccess({
    type: 'mysql',
    host: '127.0.0.1',
    user: 'sa',
    password: '123456',
    port: '3506',
    database: 'ECommerce',
    multipleStatements: true,
    useConnectionPooling: true,
});
 
ds.getTable('select * from userinfo')
    .then(dt => {
        console.log(dt);
    })
    .catch(err => {
        console.log(err);
    });

Config

mysql

{
    "type": "mssql",
    "user": "sa",
    "password": "123456",
    "server": "127.0.0.1",
    "database": "ECommerce"
}

mssql

{
    "type": "mysql",
    "host": "127.0.0.1",
    "user": "sa",
    "password": "******",
    "port": "3506",
    "database": "******",
    "multipleStatements": true,
    "useConnectionPooling": true
}

postgre

{
    "type": "postgre",
    "user": "postgres",
    "password": "******",
    "database": "******",
    "caselow": true,
    "port": 5432
}

sqlite

{
    "type": "sqlite",
    "database": "./ECommerce.db"
}

对于大小写敏感的数据库,如postgresql,需要在配置中传入caselow配置,否者有可能无法匹配表或字段

Common Query

const { DataAccess, EmtityClass, Single } = require('jh-common-dataaccess');
 
class USERINFO extends EmtityClass {
    constructor() {
        super(arguments[0], ['id', 'createby']);
    }
}
 
// table & emtity orm
const tbconf = {
    userinfo: {
        name: 'userinfo',
        pk: 'id',
    },
};
 
const ds = new DataAccess({
    type: 'mysql',
    host: '127.0.0.1',
    user: 'sa',
    password: '123456',
    port: '3506',
    database: 'ECommerce',
    multipleStatements: true,
    useConnectionPooling: true,
});
const single = new Single(dbconf, tbconf);
 
single.query('userinfo', [], 0, 0, false, null, true).then(ms => {
    console.log(ms.result);
});

Translate Execute

const { DataAccess, EmtityClass, Public, OperationEnum } = require('jh-common-dataaccess');
 
class USERINFO extends EmtityClass {
    constructor() {
        super(arguments[0], ['id', 'createby']);
    }
}
 
// table & emtity orm
const tbconf = {
    userinfo: {
        name: 'userinfo',
        pk: 'id',
    },
};
 
const ds = new DataAccess({
    type: 'mysql',
    host: '127.0.0.1',
    user: 'sa',
    password: '123456',
    port: '3506',
    database: 'ECommerce',
    multipleStatements: true,
    useConnectionPooling: true,
});
const public = new Public(tbconf);
 
const user = new USERINFO({ id: new Date().getTime().toString(), createby: '123' });
ds.transExecute(public.operationSQLParams(user, OperationEnum.Create)).then(dt => {
    console.log('create', dt);
    user.createby = '456';
    ds.transExecute(public.operationSQLParams(user, OperationEnum.Update)).then(dt => {
        console.log('update', dt);
    });
});

Bugs

  • 修复逻辑非无法插入问题(数字0布尔false

Package Sidebar

Install

npm i jh-common-dataaccess

Weekly Downloads

0

Version

0.0.9

License

ISC

Unpacked Size

55.1 kB

Total Files

23

Last publish

Collaborators

  • moshenggui