This is example project of simple blog REST API. This API contains two entities with 1:N association (Post and Comment under the post). The whole app is built on the top of express web server and prisma ORM. As the database engine is used sqlite3.
Feel free to start your own project from this example :-).
-
Clone project to your computer by executing command:
git clone [email protected]:jan.havlena/example-project.git cd example-project/
or download it as a standalone archive from here.
-
Install all required packages by running:
npm install
. You can find their names inpackage.json
file underdependencies
field. -
Create migration and initialize databases with example data from
seed.js
file by running prepared scriptnpm run prisma:init
. -
Now you can start app in development mode by running
npm start
ornpm run start:dev
.
Environment files allow as to add custom variables into nodejs process for later use. With this functionality is possible to run application in different modes based on used .env file.
Available modes of this app are development (.env.development), production (.env.production) and test (.env.test).
Each mode use different database file.
You can start you application by prepared scripts in production npm run start:pro
or development npm run start:dev
mode.
The .env files are loaded by dotenv-cli package. You can load certain .env file with your own command simply as this dotenv -e .env.production -- {your command}
.
Don't forget that each mode has its own database file with different data.
All important application settings are located in main config file config.js
.
It allows you to set host, port, custom response headers and CORS origins.
Prettier is awesome tool which formats your code automatically into human readable form. Pretty cool, right?
Simply run npm run fix
and prettier will do the rest. You can define your own settings in .prettierrc
file.
Prisma is great ORM which simplifies database use.
This project uses for simplicity Sqlite3 but many other databases are available.
Prisma-cli allows you to manage you database from CLI by actions under prisma
command.
Database is created from schema file prisma/schema.prisma
.
- To create your first migration and generate database schema run
dotenv -e .env.development -- prisma migrate dev --name init
. - To apply all waiting migrations run
dotenv -e .env.development -- prisma migrate deploy
. - To push demo data generated by
prisma/seed.js
file into database rundotenv -e .env.development -- prisma db seed
Application contains few simple tests build with use of mocha.
All test sets are located under test/
directory.
There are server REST API tests based on supertest package
and database tests based on chai package.
You can execute them all by running npm test
.
See their source files to find out how to write you own tests.
- This project is set to
module
type inpackage.json
. That's why you have to use JS ES6import
/export
statement instead of older ES5require()
/module.exports
. Learn more here. - Always run this project with npm or yarn and desired .env file to avoid errors.
MIT