UNPKG

jh-common-dataaccess

Version:
179 lines (144 loc) 3.24 kB
# jh-common-dataaccess 通用数据库操作 > 支持常见数据库操作,如 sqlserver、mysql、postgresql、~~sqlite~~ 等,可实现通用查询和数据映射 _暂时屏蔽对`sqlite`的支持,使用率不高,下载速度慢_ ## Installation ```bash $ 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 | ```js 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 ```json { "type": "mssql", "user": "sa", "password": "123456", "server": "127.0.0.1", "database": "ECommerce" } ``` ### mssql ```json { "type": "mysql", "host": "127.0.0.1", "user": "sa", "password": "******", "port": "3506", "database": "******", "multipleStatements": true, "useConnectionPooling": true } ``` ### postgre ```json { "type": "postgre", "user": "postgres", "password": "******", "database": "******", "caselow": true, "port": 5432 } ``` ### sqlite ```json { "type": "sqlite", "database": "./ECommerce.db" } ``` _对于大小写敏感的数据库,如postgresql,需要在配置中传入`caselow`配置,否者有可能无法匹配表或字段_ ## Common Query ```js 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 ```js 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`)