The Agricore
The Agricore is a NodeJS server exposing a robust REST API for various client applications to connect to.
Configuration
The agricore pulls its configuration from a .env
file at the root of the project.
These configuration variables are loaded into process.env.<VAR_NAME>
and are accessible accross the application.
To get started, this configuration file must be created with the following format:
# Application Configuration
PORT=
# Database Environment
DB_CLIENT=
DB_HOST=
DB_NAME=
DB_USER=
DB_PASS=
# Authentication
JWT_SECRET=
JWT_ISSUER=
JWT_AUDIENCE=
JWT_EXPIRES=
# Logging
LOG_LEVEL=
LOG_LEVEL
can be configured from levels 0-5:
-
error: 0
-
warn: 1
-
info: 2
-
verbose: 3
-
debug: 4
-
silly: 5
Development
Using Docker
The ./dockerize.sh
script can be used with the following arguments:
-
init
- creates a Docker image based on the Dockerfile -
install
- installs the node modules using yarn -
start
- starts the server with the port forwarded for access -
run
- runs any other npm scripts../dockerize.sh run command arg
is equivalent tonpm run command arg
Dependencies
To install javascript dependencies run the command yarn
. Yarn should also be used to add and remove dependencies, never npm.
Note
|
The knex configuration must also be altered to choose the right client .
|
Building
Important
|
npm start runs a file watcher already, no other build steps are required
|
To build and run for production run:
npm run start:prod
To just build for production run:
npm run build
An equivalent explicit command is also available:
npm run build:prod
To build and run for development:
npm run build:dev
To just watch the changes and build them when they change (for development), use:
npm run watch
npm start
already performs this so it’s not necessary to run both.
Testing
Naming Conventions
Unit tests go in agricore/test/unit/
.
Integration tests go in agricore/test/integration
.
To ensure the files are discovered, test files must have the suffix .test.ts
.
Running Test
To run the tests:
npm test
This lints the project, runs all the tests and generates a coverage report. Tests must meet the following coverage requirements to pass:
Type | Minimum |
---|---|
line |
80% |
statement |
80% |
function |
90% |
branch |
80% |
Running only Unit or Integration Test
To run just the unit tests:
npm run test:unit
To run just the integration tests:
npm run test:integration
To run a file watcher alongside these, append -- --watch
to the command. For example:
npm run test:unit -- --watch
Custom Test Filters
Each test has a series of descriptors before the actual test. These can be used to run only specific tests.
For example, The @slow
tag has been added to the descriptors of tests that take a while to complete.
To run just the slow tests:
npm run test:core -- --grep @slow
To skip the slow tests:
npm run test:core -- --grep @slow --invert
Custom tags can be added to the tests for filtering in the future.
Database
To set up a database for the Agricore, follow the steps below. Please start from the root directory of the project (ie. agricore/).
-
The Agricore uses PostgreSQL 10.1, although any version >=9.1 should work as well.
-
Create a user with correct permissions to create a database. The recommended default username is
boresha
.CREATE ROLE boresha WITH LOGIN PASSWORD '<pw>'; ALTER ROLE boresha CREATEDB;
-
Using the user you just created, create a database and grant the proper permissions to the user. The recommended default name for the database is
agricore_dev
.psql -U boresha CREATE DATABASE agricore_dev; GRANT ALL PRIVILEGES ON DATABASE agricore_dev TO boresha;
-
Connect to the database.
\connect agricore_dev
-
Install the
uuid-ossp
extension. This is already packaged, but not loaded for postgres >=9.1.CREATE EXTENSION "uuid-ossp"
-
Outside of the postgres shell, run the initialization commands for initializing the database.
psql -f dbscripts/db-init.sql agricore_dev boresha
-
See all tables using
\dt
-
Make sure that your user and database information matches to values in the `.env. file!
Database using Docker
To run the database in docker, run ./dockerize.sh initdb
and the database will be initialized and started.
-
To enter the
psql
shell, use./dockerize.sh shelldb
. -
To start the database container
./dockerize.sh startdb
. -
To stop the database container
./dockerize.sh stopdb
.