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.
Prerequisites#
You 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 Project#
To make sure that platter clients are rebuilt on every push to GitHub, create a
buildcommand in yourpackage.json'sscriptsthat looks like this:The
buildcommand takes aBRANCHenvironment variable (useful in the absence ofgit), and Netlify provides aHEADenvironment variable during builds that maps to the name of the head branch in Deploy Preview build contexts. TheBRANCH=$HEADmapping means that Postgres clients are always built for the correct branch.Functions are JavaScript modules that export AWS lambda-style
handlermethods. They are automatically deployed from a configured directory. Start by creating afunctionsdirectory in your project root:...and then adding the following to a
netlify.tomlin your project root:For local development, you'll need an API key from the User Dashboard included in an
.envfile asPLATTER_API_KEY. Create this.envfile with:Do not commit
.envfiles or Platter API keys togit! Instead, use a.gitignorefile with the following contents:After all of this, your project should have the following structure:
Building Functions#
In the
functionsdirectory, create aposts.jsmodule 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 port8888by default), you should be able toGETyourpostswith: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
masterand go to your Netlify Dashboard to watch those functions deploy!
Branching Deploys#
At 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 watchand change branches to a branch calleddeploy-preview-test, like so:This will create a
deploy-preview-testbranch of yourmy-new-databaseinstance, rebuilding your Postgres client to point towards thedeploy-preview-testdatabase along the way.There are now two database branches (
masteranddeploy-preview-test), and we need to seed thedeploy-preview-testbranch with some blog posts and authors. To fake those posts, installfakeras adevDependencywith:Create a seed file with the following command:
Then add the following to your new
add-posts.jsfile:Populate your database branch with:
In addition to changing the data in the database, create an
authors.jsfile in yourfunctionsdirectory 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-testand 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 themasterbranch lacks both the new feature and the seed data.
Migrations#
To see how this branching would incoporate schema migrations, take a look at the Migrations Guide.