carecentive

How do I add data to a new database table?

In a previous article, we showed you how to create a new database in carecentive.

This article explains how to add actual data to the database.

Background: Object-relationship mappers

In order to retrieve and save data, carecentive uses a Object-relationship mapper. This improves clarity and reduces the amount of code you have to program. Carecentive uses Objection.js for this purpose.

A ORM makes it easy to define relationships such as "one user can submit many questionnaires". Carecentive creates a model for a user and a questionnaire, and assigns a relationship between them.

You can finde a nice write-up about ORMs and their advantages here.

Saving new data

In order to be able to save new data in the database, you will have to perform the following steps:

Create a new model

Create a new Javascript file in the /model-directory. Carecentive framework ships with an example "CustomModel.js"-file. You can find more information on the required code in the Objection.js documentation.

A basic model should implement the function "tableName()". This links the newly created model to the previously created SQL table.

Creating a new route

In order to access the data, you must create a new route. An example route can be found in the "routes/example.js" folder.