pg-apify
v1.0.4
Published
<img alt="GitHub License" src="https://img.shields.io/github/license/edsol/pg-apify"> <img alt="NPM Version" src="https://img.shields.io/npm/v/pg-apify"> <img alt="npm bundle size" src="https://img.shields.io/bundlephobia/min/pg-apify"> <img alt="NPM Down
Downloads
11
Readme
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.