我正在搜索 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/