Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The LiteFarm monorepo defines two packages: webapp and api. As we will see, there is a kind of a third package, embedded within the api folder, that provides a certification export service, variously known as "exports", "scheduler", or "job scheduler".

The webapp package

We use React as our primary frontend libraryThis is the React-based frontend application. If you haven't seen React before we strongly recommend you go through the React Documentation and the tutorial. They are both extremely helpful getting started sources.

We use Redux for state management. If you haven't heard about Redux before, we strongly recommend you study the materials on their site. Or if you are more of a visual learner, Redux creator Dan Abramov has a free tutorial.

Conceptually, we divide the frontend into three categories.

“Pure” components are found in packages/webapp/src/components. These simple React components hold no logic and simply control the layout and rendering of their props.

“Container” components, sometimes called “smart” components, are found in packages/webapp/src/containers. These complex React components hold logic, connect to the Redux store, dispatch actions, and render the pure components with the required props. Within the container folders we will also define sagas, actions and reducers in case there are any of those that relate to that particular component logic.

Finally we have stories, which are tests of our containers that are rendered through Storybook. A story can import both pure and container components. In the case of testing isolated views, the most likely scenario is importing pure components. When rendering complete flows, however, you might use container components to test user interactions with the store or to test changes on events.

To do that, create a new folder and corresponding file that ends in ".stories.js" in the stories folder, and import the pure component in that file.

The api package

The API uses Node along with Express to run the backend and routing logic. You will need to be pretty familiar with Express routing before getting into any code.

To access the database and to query it we use Knex, a query builder for multiple SQL implementations; we use PostgreSQL. Please go through Knex documentation on migrations and the api/db/migration folder to get an understanding of both the library and how the project goes about using it. The migration folder contains all the project's migrations-- the evolving definitions of the database’s schema from day one.

We use ObjectionJS to define the models in api/src/models.

The exports “package”

Within the api folder, the Express. Under packages/api/src there are folders for routes, middleware, controllers, and models.

Express defines the approach used for routes and middleware. Our routes and middleware generally follow RESTful API conventions for URLs and response codes.

Controllers and models are concepts borrowed from the MVC architecture pattern.

Controllers act as a kind of translator between the model(s) and the user interface. (The V in MVC is for View, an older approach to UI issues that React handles for us.) In general terms, a controller accepts input from the UI, converts it to commands to the model(s), transforms the results and responds to the UI. In terms specific to LiteFarm, a controller is a group of Express route handler functions that handle all routes related to a type of entity, such as farms. Via Express, the controller receives requests from the frontend in HTTP, following REST conventions. The farm controller makes appropriate calls to the farm model (and perhaps other models) to retrieve and/or modify database contents. The controller then sends the results back to the frontend via Express, usually in HTTP/JSON format.

A model is a dynamic data structure that directly manages the data, logic and rules of the application. We use ObjectionJS to define the models. Objection builds on Knex, a query builder for multiple SQL database platforms. We use the PostgreSQL database management system.

The exports “package”

The file packages/api/src/jobs/index.js is the entry point for a Node service that is run separately, as if it were a third package. This service monitors Redis-based work queues to receive and process requests for exports of certification information.

...