javascript - 如何在 pg-promise 中设置模式

标签 javascript postgresql pg-promise node-postgres

我正在搜索 pg-promise 的文档特别是在客户端的创建。但是我找不到设置要在连接中使用的默认架构的选项,它始终使用 public 架构。如何设置?

最佳答案

通常,为数据库或 Angular 色设置默认模式,如下所述:

只有在不保留更改的情况下,您才可能希望动态设置模式,仅针对当前进程。

该库在 Initialization Options 中支持选项 schema :

const initOptions = {
    schema: 'my_schema' /* can also be an array of strings or a callback */
};

const pgp = require('pg-promise')(initOptions);

更容易设置动态模式。

示例

  • 使您自己的架构与默认的public 架构一起可见:

    const initOptions = {
        schema: ['public', 'my_schema'] /* make both schemas visible */
    };
    
    const pgp = require('pg-promise')(initOptions);
    
  • 使用回调根据数据库上下文设置架构(参见 Database 构造函数):

    const initOptions = {
        schema(dc) {
            if(dc === /* whatever Database Context was used */) {
                return 'my_schema'; /* or an array of strings */
            }
            /* other provisions, if multiple databases are used. */
    
            /* can return null/undefined, if no schema change is needed. */
        }
    };
    
    const pgp = require('pg-promise')(initOptions);
    

关于javascript - 如何在 pg-promise 中设置模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41730971/

相关文章:

javascript - pg-promise task and map with multiple same level nested queries

postgresql - Pg-promise 客户端无法从 ElephantSQL 云数据库中正确检索特殊字符

node.js - pg-promise 中的查询超时

sql - 如何在 postgresql 查询中将行转换为列

javascript - 如何重新加载我的数据表?

javascript - 使用 React 和 axios 进行 curl

javascript - 如何在wordpress的Javascript函数中传递用户ID?

postgresql - 增加查询参数直到达到 LIMIT

python - 将数据从 DB2 DB 传输到 greenplum DB

javascript - 如何使用 typescript 向对象添加属性?