如何在pg-promise中设置架构 [英] How to set schema in pg-promise

查看:140
本文介绍了如何在pg-promise中设置架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索 pg-promise 的文档,尤其是在创建客户。但是我找不到设置用于连接的默认模式的选项,它始终使用 public 模式。如何设置它?

I was searching the documentation of pg-promise specifically in the creation of the client. But I wasn't able to find the option to set the default schema to be used in the connection, it always uses public schema. How do I set it?

推荐答案

通常,如所解释的那样,通常设置数据库或角色的默认架构。此处:

Normally, one sets the default schema(s) for the database or the role, as explained here:

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

It is only if you want to do so without persisting the change, you might want to set the schema(s) dynamically, just for the current process.

该库支持选项 schema 初始化选项中:

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

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

使设置动态模式更加容易。

making it easier to set the dynamic schema(s).

示例


  • 显示您自己的架构以及默认的 public 模式:

const initOptions = {
    schema: ['public', 'my_schema'] /* make both schemas visible */
};

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


  • 使用回调基于数据库上下文设置架构(请参见数据库构造函数):

    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);
    


  • 这篇关于如何在pg-promise中设置架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆