Netlify Functions
Postgres' branching capabilities were built with Netlify's Branch Deploys and Deploy Previews in mind. This makes Postgres a perfect fit for working with Netlify Functions.
In this guide, you'll learn how to deploy the blog project from the Migrations guide to a Netlify Function.
#
PrerequisitesYou must have a Netlify account connected to your GitHub account and a site configured to deploy the master
branch of the repo you've been using for the Quick Start and Migrations guide. See Getting Started With GitHub for help setting up your project as a GitHub-hosted repository, and see the Netlify Docs for help setting up your repository as a Netlify site.
Lastly, install the encoding
package. This is currently required by Netlify.
#
Setting up the ProjectTo make sure that platter clients are rebuilt on every push to GitHub, create a
build
command in yourpackage.json
'sscripts
that looks like this:The
build
command takes aBRANCH
environment variable (useful in the absence ofgit
), and Netlify provides aHEAD
environment variable during builds that maps to the name of the head branch in Deploy Preview build contexts. TheBRANCH=$HEAD
mapping means that Postgres clients are always built for the correct branch.Functions are JavaScript modules that export AWS lambda-style
handler
methods. They are automatically deployed from a configured directory. Start by creating afunctions
directory in your project root:...and then adding the following to a
netlify.toml
in your project root:For local development, you'll need an API key from the User Dashboard included in an
.env
file asPLATTER_API_KEY
. Create this.env
file with:Do not commit
.env
files or Platter API keys togit
! Instead, use a.gitignore
file with the following contents:After all of this, your project should have the following structure:
#
Building FunctionsIn the
functions
directory, create aposts.js
module with the following contents:For local development, use the Netlify CLI. The dev server for Functions can be invoked without installation with
npx netlify dev
. Once that is running (on port8888
by default), you should be able toGET
yourposts
with:info
At this point, requests for posts should return an empty Array as a response.
If everything is working as expected locally, then push your changes to
master
and go to your Netlify Dashboard to watch those functions deploy!
#
Branching DeploysAt this point, you have a public API that mirrors the master
branch of your project. With the build script in package.json
configured above, you can now take advantage of Deploy Previews.
Run
npx platter watch
and change branches to a branch calleddeploy-preview-test
, like so:This will create a
deploy-preview-test
branch of yourmy-new-database
instance, rebuilding your Postgres client to point towards thedeploy-preview-test
database along the way.There are now two database branches (
master
anddeploy-preview-test
), and we need to seed thedeploy-preview-test
branch with some blog posts and authors. To fake those posts, installfaker
as adevDependency
with:Create a seed file with the following command:
Then add the following to your new
add-posts.js
file:Populate your database branch with:
In addition to changing the data in the database, create an
authors.js
file in yourfunctions
directory to handle returning all authors, a single author by ID, or all of the posts associated with an author. That file should have the following content:If everything is working as expected locally, then push your changes to
deploy-preview-test
and go to your Netlify Dashboard to watch those functions deploy!Now your new deploy has both a new endpoint (
/authors
) and new data, while the "production" deploy of themaster
branch lacks both the new feature and the seed data.
#
MigrationsTo see how this branching would incoporate schema migrations, take a look at the Migrations Guide.