【问题标题】:How to set schema in pg-promise如何在 pg-promise 中设置模式
【发布时间】:2017-06-03 12:19:20
【问题描述】:

我专门在创建客户端时搜索了pg-promise 的文档。但是我找不到设置要在连接中使用的默认模式的选项,它总是使用public 模式。如何设置?

【问题讨论】:

标签: javascript postgresql pg-promise node-postgres


【解决方案1】:

通常,为数据库或角色设置默认架构,如下所述:

只有当您想要这样做而不持久化更改时,您可能想要动态设置架构,仅用于当前进程。

该库支持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);
    

【讨论】:

  • 为简单起见并避免混淆,删除了使用过时的手动方法的示例。只能使用新选项 schema
猜你喜欢
  • 2017-10-10
  • 2018-02-25
  • 1970-01-01
  • 2016-04-13
  • 2018-05-07
  • 1970-01-01
  • 2019-01-04
  • 2022-11-10
  • 1970-01-01
相关资源
最近更新 更多