Skip to main content

Command Line Interface命令行工具(CLI)

The easiest way to get up and running with PostGraphile is to use the Command Line Interface.

快速启动 PostGraphile 最简单的方式就是使用命令行工具(CLI)。

To try without installing, run the pgl command via the npx command that comes bundled with Node.js:

如果不想预先安装,可以直接通过 Node.js 自带的 npx 运行 pgl 命令:

npx pgl -P pgl/amber -e -c postgres:///mydb
info说明

You should replace postgres:///mydb with a PostgreSQL connection string pointing at your database, and if you use a non-standard PostgreSQL schema you should indicate the schema name via -s my_schema_name_here.

请将 postgres:///mydb 替换为指向你数据库的 PostgreSQL 连接字符串;如果你使用的不是默认的 public schema,可通过 -s my_schema_name_here 指定 schema 名称。

For a more permanent installation we suggest you create yourself a new directory for your project, we'll refer to this as ~/postgraphile. Open a terminal in this directory and install PostGraphile:

若需长期使用,建议新建一个项目目录(下文以 ~/postgraphile 为例),在该目录下打开终端并安装 PostGraphile:

cd ~/postgraphile
npm install postgraphile

You can then run PostGraphile equivalent to above via:

安装完成后,即可执行与上面等效的命令启动 PostGraphile:

npx postgraphile -P postgraphile/presets/amber -e -c postgres:///mydb
warning警告

The -e option should only be used in development.

-e 选项仅应在开发环境中使用。

CLI flagsCLI 命令行参数​

The CLI accepts a small number of configuration options to get you started. It also reads options from a graphile.config.js file from the current working directory (if it exists); by specifying all options in this file you can run PostGraphile with no (or minimal) CLI flags.

CLI 本身仅提供少量常用配置选项帮助你快速起步,同时还会读取当前工作目录下的 graphile.config.js 配置文件(如果存在)。将所有选项写入该配置文件后,你就可以不传或只传极少参数来运行 PostGraphile。

  • --version
    output the version number

    --version
    输出版本号

  • --help
    output usage information

    --help
    输出帮助与用法说明

  • --preset <string> (alias: -P)

    --preset <string>(简写:-P)

    { extends: [...] }

    can be used to specify a comma-separated list of presets to add to the configuration

    指定要加入配置的 preset 列表(多个之间用逗号分隔)

  • --allow-explain (alias: -e)

    --allow-explain(简写:-e)

    { grafast: { explain: true } }

    enables "explain" mode, you won't want this in production but it's very helpful in development to know what's going on

    启用 "explain" 模式;切勿在生产环境开启,但在开发阶段对排查执行细节非常有帮助

  • --connection <string> (alias: -c)

    --connection <string>(简写:-c)

    { pgServices: [makePgService({ connectionString: '...' })] }

    the PostgreSQL connection string. If omitted, inferred from

    PostgreSQL 连接字符串。如果省略,则自动从以下位置读取:

    environmental variables

    环境变量

    .
    Examples: db, postgres:///db, postgres://user:password@domain:port/db?ssl=true

    。
    示例:db、postgres:///db、postgres://user:password@domain:port/db?ssl=true

  • --superuser-connection <string> (alias: -S)

    --superuser-connection <string>(简写:-S)

    { pgServices: [makePgService({ superuserConnectionString:'...' })] }

    the PostgreSQL superuser connection string to use for installing watch fixtures (if --watch is enabled). If omitted, the regular connection string will be used.

    启用 --watch 时,用于安装 watch 监听组件的 PostgreSQL 超级用户连接字符串。若省略,则复用普通连接字符串。

  • --schema <string> (alias: -s)

    --schema <string>(简写:-s)

    { pgServices: [makePgService({ schemas: ['...'] })] }

    a comma-separated list of PostgreSQL schemas to be introspected.

    需要执行 introspection 的 PostgreSQL schema 列表,以逗号分隔。

  • --watch (alias: -w)

    --watch(简写:-w)

    { grafserv: { watch: true } }

    automatically updates your GraphQL schema when your database schema changes (NOTE: requires DB superuser to install postgraphile_watch schema)

    当数据库 schema 变更时自动刷新 GraphQL schema(注意:需要数据库超级用户权限来安装 postgraphile_watch schema)

  • --host <string> (alias: -n)

    --host <string>(简写:-n)

    { grafserv: { host: 'localhost' } }

    the hostname to be used. Defaults to localhost

    服务监听的主机名,默认为 localhost

  • --port <number> (alias: -p)

    --port <number>(简写:-p)

    { grafserv: { port: 5678 } }

    the port to be used. Defaults to 5678

    服务监听的端口号,默认为 5678

pgl vs postgraphilepgl 与 postgraphile 的区别​

New to V5 of PostGraphile is the pgl command. This is almost identical to the postgraphile command (it actually defers to postgraphile under the hood), with the following differences:

PostGraphile V5 新增了 pgl 命令。它在底层实际直接调用 postgraphile,功能几乎完全一致,仅有以下两点差异:

  • pgl has no peerDependencies - as soon as you start installing plugins that need peerDependencies you should consider moving to postgraphile

    pgl 不包含任何 peerDependencies——一旦开始安装依赖 peerDependencies 的插件,就建议改用 postgraphile

  • pgl exposes postgraphile/presets/amber as pgl/amber (and similar for pgl/v4 and pgl/relay), so it's more concise

    pgl 将 postgraphile/presets/amber 简写暴露为 pgl/amber(pgl/v4 和 pgl/relay 同理),命令行更简洁

tip提示

pgl is most suitable for running from npx without the need to permanently install anything. If you want to start using PostGraphile in your project you should install postgraphile instead.

pgl 最适合用 npx 免安装临时体验;若要在正式项目中引入 PostGraphile,仍应安装 postgraphile 包。