3

node.js 如何高效的操作数据库

 2 years ago
source link: https://segmentfault.com/a/1190000040766415
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

node.js 如何高效的操作数据库

发布于 10 月 2 日

access-db

统一各类数据库的连接

目前支持的数据库有:

数据库说明支持版本FastDB本地json文件数据库[由access_db团队开发]0.0.6MongoDB分布式文件存储数据库4.xMysql关系型数据库8.x, 5.xredis高性能的 key-value 数据库6.x, 5.x

yarn add access-db
yarn add dotenv

在项目根目录新建.env文件,然后填写配置信息

然后在项目最开始,尽可能早的引入require('dotenv').config()
.env配置如下

MYSQL_HOST MONGODB_HOST REDIS_HOST FASTDB_DIR 你使用什么数据库,则对应的地址就必填,不使用则不填

  MYSQL_HOST=localhost   // 非必填,不填则表示不使用该数据库
  MYSQL_USER=root
  MYSQL_PASSWORD=123456
  MYSQL_PORT=3306
  MYSQL_DATABASE=
  # 默认UTF8_GENERAL_CI
  # MYSQL_CHARSET=
  # 最大连接数,默认10
  # MYSQL_CONNECTION_LIMIT=


  MONGODB_HOST=localhost    // 非必填,不填则表示不使用该数据库
  MONGODB_USER=
  MONGODB_PASSWORD=
  MONGODB_DATABASE=
  MONGODB_PORT=

  REDIS_HOST=localhost    // 非必填,不填则表示不使用该数据库
  REDIS_PORT=


  FASTDB_DIR=   // fastdb的数据存储目录,默认项目根目录下的fastdb目录
import {mysql, mongodb} from 'access-db'

async function exp() {
  let {data} = await mongodb.get('tableName1', id)
  await mysql.find('tableName2', {
    p0: ['num', '=', data.num],
    r: 'p0'
  })
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK