This is an example serverless API built on Platter Postgres, Node, and Netlify Functions/AWS Lambdas. It exposes a messages
handler for storing threads of messages to a Postgres database. The purpose of this example application is to show how to test handlers with and without a database.
This repository is heavily commented, but is most useful when paired with this blog post.
To start, make sure that you've created a Platter account and added a default payment method. Databases on the Platter platform are 1¢/hour per branch. You'll also need git
and Node.js installed for your operating system.
Once you have an account, do the following:
-
Clone this repository
-
Install the dependencies with
npm install
-
Authorize the
platter
CLI that was installed in step 2 to create Postgres instances on your behalf withnpx platter identity login
-
Create a new Postgres instance with:
npx platter postgres create testing-postgres-node-jest \ --platform node \ --directory ./platter
-
Create a
.env
file with aPLATTER_API_KEY
from the Platter developer dashboard and aPLATTER_POSTGRES_CLIENT
path that points to the path of your new Postgres client. If you used the command from step 4,PLATTER_POSTGRES_CLIENT
will be./platter/postgres/node/TestingPostgresNodeJest
. You can create that file with a single command like so:cat << EOF > .env PLATTER_API_KEY=<an API key from your user dashboard> PLATTER_POSTGRES_CLIENT=./platter/postgres/node/TestingPostgresNodeJest EOF
-
Run the migrations on your new instance with
npm run migrate:up
-
Start
platter watch
andnetlify dev
at the same time withnpm run watch
to test out your messages endpoint! It should acceptPOST
requests atlocalhost:8888/.netlify/functions/messages
. To insert a new message withcurl
, you would run:curl -s \ -X POST \ -H 'Content-Type: application/json' \ -d '{"text":"some message text"}' \ http://localhost:8888/.netlify/functions/messages
-
To run the test suite once, use
npm test
. To run the test suite in watch mode, usenpm run test:watch
.