0.0.9 • Published 5 years ago

jh-common-dataaccess v0.0.9

Weekly downloads
19
License
ISC
Repository
-
Last release
5 years ago

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

FieldType
idstring
createbystring
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
0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago