/pg-apify

Automatically generate API (REST and GraphQL) endpoints directly from your PostgreSQL schema

Primary LanguageTypeScriptGNU Affero General Public License v3.0AGPL-3.0

GitHub License

NPM Version

npm bundle size

NPM Downloads


Postgres GraphQL TypeScript

pg-apify

Effortlessly transform your PostgreSQL database into a powerful REST and GraphQL API server with zero coding required

Features

  • Zero Coding Required: Instantly generate REST and GraphQL endpoints from your PostgreSQL schema without writing a single line of code.
  • Rapid Setup: Get your API server up and running in just a few seconds.
  • Seamless Integration: Easily integrates with your existing PostgreSQL databases, enabling quick and efficient data access.
  • Customizable Middleware: Enhance your API with custom logic using flexible middleware support.
  • Beautiful API: Automatically generate comprehensive API documentation using Scalar to ensure clear and detailed endpoint information.

Install and requirements

install pg-apify:

bun install pg-apify
// or
npm install pg-apify

local installation of postgREST is also required, follow official wiki to install correctly.

Usage

Zero-coding

creates and use .env file or passes the configuration directly via parameters and run server

import { pgApifyServer, postgraphileOptions, postgrestOptions } from "pg-apify";
import { dev as options } from "./postgraphileOptions";

const postgraphileServerOptions: postgraphileOptions = {
  enabled: true,
  databaseUrl: process.env.DATABASE_URL,
  schema: process.env.PGSCHEMA,
  options: options,
};

const postgrestServerOptions: postgrestOptions = {
  enabled: true,
  enableDocs: true,
};
pgApifyServer(postgraphileServerOptions, postgrestServerOptions);

a GraphQL server, RestAPI and OpenAPI documentation will be launched

🚀 GraphQL server available at http://localhost:5000/graphql
🚀 GraphiQL available at http://localhost:5000/graphiql
📖 API docs is running at http://localhost:3000/docs
🔥 API is running at http://localhost:3000

note

The postgraphileOptions file contains the Postgraphile options (docs), see the example file, copy it or create your own custom configuration file.

Request middleware

intercepts all GET requests

requestHandler.registerHandler("get", async (params, context, info) => {
  // your code

  return {
    success: false,
    status: 404,
    message: "Custom error message",
  };
});

or specific endpoint

requestHandler.registerRouteHandler(
  "get",
  "foo",
  async (params, context, info) => {
    // your code

    return {
      success: false,
      status: 404,
      message: "Custom error message",
    };
  }
);

Under the hood

PostGraphile

pg-apify leverages PostGraphile, a powerful tool that automatically generates a GraphQL API from your PostgreSQL schema. PostGraphile inspects your database schema, including tables, columns, relationships, and constraints, and creates a fully-functional GraphQL API. This allows you to take advantage of GraphQL's flexibility and efficiency in querying and mutating data.

PostgREST

In addition to GraphQL, pg-apify also utilizes PostgREST, a standalone web server that turns your PostgreSQL database directly into a RESTful API. PostgREST reads the database schema and creates RESTful endpoints that correspond to your tables and views, enabling CRUD operations with ease.

Scalar

Using scalar to generate interactive API documentation from OpenAPI/Swagger documents.

Do you want to support my work? Buy me an espresso coffee (I'm Italian)

Buy Me A Coffee