Skip to content

Latest commit

 

History

History
77 lines (53 loc) · 2.57 KB

README.md

File metadata and controls

77 lines (53 loc) · 2.57 KB

api-pls Example

This project demonstrates how to use api-pls to start up an API webserver in any project.

Prerequisites

Getting Started

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.

FAQ

How can I change my resource models after the initial migration?

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.

How can I start a PostgreSQL database to run this example?

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.

My database does not use SSL; how can I connect to it?

You can set the "ssl" option to be false in your .plsrc file.

{
  "resourcesDirectory": "./resources",
  "ssl": false
}