Emm*_*pos 2 javascript postgresql node-postgres pg-promise
我在创建客户端时专门搜索pg-promise的文档。但是我找不到设置要在连接中使用的默认架构的选项,它总是使用public架构。我该如何设置?
通常,为数据库或角色设置默认架构,如下所述:
仅当您不想保留更改时才这样做,您可能希望动态设置模式,仅针对当前进程。
该库支持选项schema中初始化选项:
const initOptions = {
schema: 'my_schema' /* can also be an array of strings or a callback */
};
const pgp = require('pg-promise')(initOptions);
Run Code Online (Sandbox Code Playgroud)
更容易设置动态模式。
例子
使您自己的架构与默认public架构一起可见:
const initOptions = {
schema: ['public', 'my_schema'] /* make both schemas visible */
};
const pgp = require('pg-promise')(initOptions);
Run Code Online (Sandbox Code Playgroud)使用回调根据数据库上下文设置架构(请参阅数据库构造函数):
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);
Run Code Online (Sandbox Code Playgroud)