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 🙏

© 2026 – Pkg Stats / Ryan Hefner

database-config

v1.0.1

Published

Quick and easy connection to the database

Readme

database-config

目录

下载安装

npm i database-config

cnpm i database-config

使用

create

创建一个目录用来存放两个配置文件,以 mongodb 为例

目录结构

  • mongodb //目录名随意
    • index.js
    • config.yml

index.js

用于加载并导出数据库对象

const myDataBase = require('database-config');

let db = new myDataBase(__dirname);
//创建一个数据库连接对象
//设置__dirname 之后,可以自动读取同目录下的配置文件 config.yml,或者可以自行设置目录位置
db.Create();
//初始化数据库对象并连接

module.exports = db;
//导处该对象并在您想用的地方使用它

config.yml

配置数据库及其集合、表的相关信息

以 mongodb 为例

# 数据库配置
DATABASE:
  name: mongodb
  # 指定数据库,目前支持 mongodb、mysql
  url: mongodb://localhost:27017/my_database
  # 连接地址 
  # mysql 可以设置 host: 127.0.0.1 及 poet: 3306
  authorization:
  # 认证信息
    enable: true
    user: greatiga
    pass: 123456

# 集合映射 针对 mongodb
TABLE:
  
  # 集合名
  user:
    # 集合 user,存储用户信息的集合
    # 设置字段及其对应的类型,以数组的形式
    String: [ user_name, user_pwd, user_email ]
    Number: [ user_phone ]
    Array: [ user_friend ]
    ObjectId: [ _id ]

开始使用

导入上述例子中目录下的 index.js

const db = require('./mongodb/index');

db.user.find({ }, {_id: 0}, function(err, data) {
  if(err) return;
  console.log(data);
})

// [
//   {
//     user_name: 'tom',
//     user_pwd: 123,
//     user_email: '[email protected]',
//     user_phone: 111111,
//     user_friend: [
//       'jack', 'bob'
//     ]
//   },
//     {
//     user_name: 'jack',
//     user_pwd: 12344,
//     user_email: '[email protected]',
//     user_phone: 77777,
//     user_friend: [
//       'bob', 'tom'
//     ]
//   }
// ]

完整配置示例

mongodb

  • index.js
const myDataBase = require('database-config');

let db = new myDataBase(__dirname);
db.Create();

module.exports = db;
  • config.yml
DATABASE:
  name: mongodb
  url: mongodb://localhost:27017/my_database
  authorization:
    enable: true
    user: greatiga
    pass: 123456

TABLE:
  user:
    String: [ user_name, user_pwd, user_email ]
    Number: [ user_phone ]
    Array: [ user_friend ]
    ObjectId: [ _id ]

mysql

  • index.js
const myDataBase = require('database-config');

let db = new myDataBase(__dirname);
db.Create();

module.exports = db;
  • config.yml
DATABASE:
  name: mysql
  host: localhost
  port: 3306
  authorization:
    enable: true
    user: greatiga
    pass: 123456