Skip to content

Latest commit

 

History

History

netlify-postgresql-neon

Northwind Traders (by subZero)

Running on Netlify + PostgreSQL (Neon)

This is a demo of subZero library capabilities, leveraged in a NextJS app, to automatically expose a PostgREST compatible backend on top of the underlying database.

See the live version at northwind-postgresql.netlify.app and source code on GitHub.

Features / Advantages

  • Integrates in your codebase as a library (no need to deploy a separate service)
  • Runs in any context (Docker, AWS Lambda, Vercel, Netlify, Fly.io, Cloudflare Pages, Deno, Node, etc)
  • Implemented in Rust with JS/TypeScript bindings through WASM with no dependencies
  • Multiple databases supported:
    • SQLite (including Cloudflare D1)
    • PostgreSQL (including YugabyteDB, CockroachDB, TimescaleDB, etc)
    • ClickHouse
    • MySQL (PlanetScaleDB upcoming)
  • Supports advanced analytical queries (window functions, aggregates, etc)

Example details

  • Frontend is implemented in NextJS
  • Everything is deployed to Netlify
  • Data is stored in a PostgreSQL database hosted on Neon
  • The backend runs in a single serverless function. Most of the code deals with the configuration of the backend, and 99% of the functionality is within these lines:
    // .....
    // generate the SQL query from request object
    const { query, parameters } = await subzero.fmtStatement(publicSchema, `${urlPrefix}/`, role, req, queryEnv)
    // .....
    // execute the query
    result = (await db.query(query, parameters)).rows[0]
    // .....
    // send the response back to the client
    res.send(result.body)

Running locally

  • Clone the repo
    git clone https://github.com/subzerocloud/showcase.git
  • cd to the example directory
    cd showcase/netlify-postgresql-neon
  • Install dependencies
    yarn install
  • Copy .env.local file
    cp .env.local.example .env.local
  • Run in dev mode
    yarn dev
  • Open the app in your browser
    open http://localhost:3000

Deploying to Netlify

  • Setup a PostgreSQL database on Neon and get a connection string

  • Provision the database

    psql <db_connection_string> -f northwindtraders-postgresql.sql
  • Netlify init (we use --manual to avoid linking the Github repo to Netlify)

    netlify init --manual
  • Add db env variable

    netlify env:set DATABASE_URL <db_connection_string>
  • Deploy to Netlify (Due to NextJS plugin, build and deploy need to be done in one command when deploying manually)

    netlify deploy --build --prod

Credits