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 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-postgres

v1.0.2

Published

PostgreSQL plugin for egg

Downloads

110

Readme

egg-postgres

PostgreSQL 插件是为 egg 提供 PostgreSQL 数据库访问的功能

此插件基于 node-postgres 实现一个简单的配置封装,具体使用方法你还需要阅读 node-postgres 的文档。

安装

$ npm i egg-postgres --save

配置

通过 config/plugin.js 配置启动 PostgreSQL 插件:

exports.postgres = {
  enable: true,
  package: 'egg-postgres',
};

config/config.${env}.js 配置各个环境的数据库连接信息:

单数据源

exports.postgres = {
  // 单数据库信息配置
  client: {
    // host
    host: 'pg.com',
    // 端口号
    port: '15432',
    // 用户名
    user: 'test_user',
    // 密码
    password: 'test_password',
    // 数据库名
    database: 'test',    
  },
  // 是否加载到 app 上,默认开启
  app: true,
  // 是否加载到 agent 上,默认关闭
  agent: false,
};

使用方式:

app.postgres.query(sql, values); // 单实例可以直接通过 app.postgres 访问

多数据源

exports.postgres = {
  clients: {
    // clientId, 获取client实例,需要通过 app.mysql.get('clientId') 获取
    db1: {
      // host
      host: 'pg.com',
      // 端口号
      port: '15432',
      // 用户名
      user: 'test_user',
      // 密码
      password: 'test_password',
      // 数据库名
      database: 'test',
    },
    // ...
  },
  // 所有数据库配置的默认值
  default: {
    // 连接池最大数量
    max: 20,
    // 查询超时
    idleTimeoutMillis: 30000,
    // 连接超时
    connectionTimeoutMillis: 2000,
  },

  // 是否加载到 app 上,默认开启
  app: true,
  // 是否加载到 agent 上,默认关闭
  agent: false,
};

使用方式:

const client1 = app.postgres.get('db1');
client1.query(sql, values);

const client2 = app.postgres.get('db2');
client2.query(sql, values);

扩展

app.js

app.postgres

如果开启了 config.postgres.app = true,则会在 app 上注入 node-postgres 客户端 的 Singleton 单例

app.postgres.query(sql);
app.postgres.get('db1').query(sql);

agent.js

agent.postgres

如果开启了 config.postgres.agent = true,则会在 agent 上注入 node-postgres 客户端 的 Singleton 单例

agent.postgres.query(sql);
agent.postgres.get('db1').query(sql);

CRUD 使用指南

Create

// 插入
const result = await app.postgres.query('insert into t_user(name,age) values ($1,$2)',['张三',13]);
//计算数据库受影响记录
const insertSuccess = result.rowCount === 1;

Read

const {rows} = await app.postgres.query('select * from t_user where age=$1', [13]);

Update

const result= await app.postgres.query('update t_user set age=$1 where name=$2', [15,'张三']);
//计算数据库受影响记录
const updateSuccess = result.rowCount === 1;

Delete

const result= await app.postgres.query('delete from t_user');
//计算数据库受影响记录
const deleteSuccess = result.rowCount === 1;

License

MIT