要在Postgraphile中保留表的尾部的 's',可以使用Postgraphile的插件系统来自定义命名约定。
以下是一个示例解决方法:
创建一个名为 renameTablesPlugin.js
的文件。
在该文件中,使用Postgraphile的 makeExtendSchemaPlugin
函数来创建一个插件。
const { makeExtendSchemaPlugin, gql } = require("graphile-utils");
const renameTablesPlugin = makeExtendSchemaPlugin(build => {
const { pgSql: sql } = build;
return {
typeDefs: gql`
extend type Query {
renamedTables: [String]!
}
`,
resolvers: {
Query: {
renamedTables: async (_, args, { pgClient }) => {
const { rows } = await pgClient.query(
sql`
SELECT table_name || 's' AS renamed_table
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name NOT LIKE '%_pkey'
`
);
return rows.map(row => row.renamed_table);
},
},
},
};
});
module.exports = renameTablesPlugin;
const express = require("express");
const { postgraphile } = require("postgraphile");
const renameTablesPlugin = require("./renameTablesPlugin");
const app = express();
app.use(
postgraphile(process.env.DATABASE_URL, "public", {
appendPlugins: [renameTablesPlugin],
graphiql: true,
})
);
app.listen(3000, () => {
console.log("Server started on port 3000");
});
query {
renamedTables
}
该查询将返回一个具有表名的数组,每个表名都以 's' 结尾。
请注意,上面的示例假设Postgraphile服务器已经与数据库连接,并在端口3000上运行。你需要根据你自己的设置进行调整。
上一篇:保留片段导航状态与返回堆栈