This project demonstrates how to use api-pls to start up an API webserver in any project.
- Node.js v7+
- a PostgreSQL database
Clone this repository. Then, install the dependencies:
npm install
Next, create a file in the root of this project called .env
. Add the following
line to the file, replacing the database URL with your own:
DATABASE_URL='postgres://user@example.com:5432/example'
Next, you'll need to create Resource Models. These are the definitions that describe what tables and endpoints are created for you.
There are several examples already created for you in the ./resources
directory:
Review those, and make changes as you see fit.
Once you're satisfied with your resource models, run npm run sync
. This
will generate migrations from your models, and then apply them to the database.
You're now ready to start the API webserver.
Run npm run start
to get it running. Once it's up, navigate your browser to
localhost:5000
to begin CRUD'ing.
Eventually, api-pls will allow you to change your resources dynamically. It does
not support this now, however. If you change your resource models, you'll need
to clear all of the data in the database by running npm run reset
.
Once that completes, you can run npm run sync
to bootstrap the database
with your updated models.
To run the example, you'll need a PostgreSQL database URL. It is recommended that you create a database specifically for testing purposes, rather than using a database that you are using for other uses, too.
My preference is to create a free Heroku app, and then set up a free version of Heroku's PostgreSQL add-on. This provides you with 10,000 rows and 20 connections for free: more than enough than what you'll need to try out the example.
You can also set up a database locally on your machine. This will differ slightly between operating systems. I recommend searching StackOverflow for the best solution for your OS.
You can set the "ssl" option to be false
in your .plsrc
file.
{
"resourcesDirectory": "./resources",
"ssl": false
}