在 Postgres 中对所有打印的 JSONB 使用 jsonb_pretty
Use jsonb_pretty for all printed JSONB in Postgres
是否可以配置 .psqlrc
,以便将 jsonb_pretty
应用于打印到控制台的每个 JSONB 列?
扩展显示自动模式非常有用:
我想同时使用 jsonb_pretty
和扩展显示。 jsonb_pretty
是否通过在字符串中插入换行符来工作?我想如果应用于每个 JSONB select
.
这会导致问题
jsonb_pretty
: https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE
不,这是不可能的。 psql
不支持 JSON 类型的漂亮打印。
只有 psql 我猜 @Pavel_Stehule 是对的(所以不是)。
和SQLTT you can hack it with the use of hooks.
示例:
// users.sql.js
const sqltt = require("sqltt");
const getUserData = new sqltt({
hooks: {
// Prettier formatting on CLI output:
user_profile: (arg, eng) => eng.match(/(^|_)cli$/) && "jsonb_pretty("+arg+") as "+arg,
},
sql: $=>$`
select id, name, ${$.literal("user_profile")}
from users
where id = ${"userId"}
`,
});
sqltt.publish(module, {getUserData});
那么你可以...
安装sqltt:
user@host:~/sqltttest$ npm install sqltt
(...)
列出文件中的可用查询:
user@host:~/sqltttest$ node user.sql.js
Available queries:
✓ getUserData: (Undocumented)
获取 psql 合适的查询(参数已经插入):
user@host:~/sqltttest$ node user.sql.js getUserData 'John Smith'
\set userId '''John Smith'''
select id, name, jsonb_pretty(user_profile) as user_profile
from users
where id = :userId
...直接通过管道传递给 psql:
user@host:~/sqltttest$ node user.sql.js getUserData 'John Smith' | psql myDatabase
\set userId '''John Smith'''
select id, name, jsonb_pretty(user_profile) as user_profile
from users
where id = :userId
从 Node.js 项目中使用它:
const {getUserData, ...otherSQL} = require("./user.sql.js");
const sql = getUserData.sql();
// select id, name, user_profile
// from users
// where id =
获得non-clisql与其他语言一起使用:
user@host:~/sqltttest$ node user.sql.js --engine=_nocli getUserData
select id, name, user_profile
from users
where id =
...当然,将其存储在纯 sql 文件中:
user@host:~/sqltttest$ node user.sql.js --engine=_nocli getUserData > getUserData.sql
是否可以配置 .psqlrc
,以便将 jsonb_pretty
应用于打印到控制台的每个 JSONB 列?
扩展显示自动模式非常有用:
我想同时使用 jsonb_pretty
和扩展显示。 jsonb_pretty
是否通过在字符串中插入换行符来工作?我想如果应用于每个 JSONB select
.
jsonb_pretty
: https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE
不,这是不可能的。 psql
不支持 JSON 类型的漂亮打印。
只有 psql 我猜 @Pavel_Stehule 是对的(所以不是)。
和SQLTT you can hack it with the use of hooks.
示例:
// users.sql.js
const sqltt = require("sqltt");
const getUserData = new sqltt({
hooks: {
// Prettier formatting on CLI output:
user_profile: (arg, eng) => eng.match(/(^|_)cli$/) && "jsonb_pretty("+arg+") as "+arg,
},
sql: $=>$`
select id, name, ${$.literal("user_profile")}
from users
where id = ${"userId"}
`,
});
sqltt.publish(module, {getUserData});
那么你可以...
安装sqltt:
user@host:~/sqltttest$ npm install sqltt
(...)
列出文件中的可用查询:
user@host:~/sqltttest$ node user.sql.js
Available queries:
✓ getUserData: (Undocumented)
获取 psql 合适的查询(参数已经插入):
user@host:~/sqltttest$ node user.sql.js getUserData 'John Smith'
\set userId '''John Smith'''
select id, name, jsonb_pretty(user_profile) as user_profile
from users
where id = :userId
...直接通过管道传递给 psql:
user@host:~/sqltttest$ node user.sql.js getUserData 'John Smith' | psql myDatabase
\set userId '''John Smith'''
select id, name, jsonb_pretty(user_profile) as user_profile
from users
where id = :userId
从 Node.js 项目中使用它:
const {getUserData, ...otherSQL} = require("./user.sql.js");
const sql = getUserData.sql();
// select id, name, user_profile
// from users
// where id =
获得non-clisql与其他语言一起使用:
user@host:~/sqltttest$ node user.sql.js --engine=_nocli getUserData
select id, name, user_profile
from users
where id =
...当然,将其存储在纯 sql 文件中:
user@host:~/sqltttest$ node user.sql.js --engine=_nocli getUserData > getUserData.sql