Databases & Migration

Demystifying complexity

At the end of the day a model is nothing more but the adapter between a database and business logic. Enabling easy of use without creating opinionated bottlenecks has always been the central goal of model interactions in neoan3. With the help of the neoan3-cli tool, "binding" SQL to code is easy, predictable and intuitive.

Migrate component

In order to get a better understanding of the details below, we recommend the following preparations:

  • create a new model with neoan3 new model post
  • prepare a database and credentials (e.g with neoan3 new database my_db)
  • prepare development server with neoan3 develop
  • open http://localhost:8080/migrate

The migrate.json

Each model has a migrate.json file indicating the structure of the database table(s) is it associated with. A model's name should align with the table(s) it interacts with.
This means that a model "post" assumes the existence of a table "post". Additionally, one to many relations are created by snake case naming convention.
This means that a table "post_comment" is assumed to have many comments in relation to a particular "post".

The comparison

SQL

        CREATE TABLE `post` (
          `id` binary(16) NOT NULL,
          `name` varchar(200) NOT NULL,
          `text` text DEFAULT NULL,
          `is_active` tinyint(1) DEFAULT NULL,
          `insert_date` timestamp NULL DEFAULT current_timestamp(),
          `delete_date` datetime DEFAULT NULL
        ) DEFAULT CHARSET=utf8mb4;
    
migrate.json

        {
          "post": {
            "id": {
              "type": "binary(16)",
              "key": "primary",
              "nullable": false,
              "default": false,
              "a_i": false
            },
            "name": {
              "type": "varchar(200)",
              "key": false,
              "nullable": false,
              "default": false,
              "a_i": false
            },
            "text": {
              "type": "text",
              "key": false,
              "nullable": true,
              "default": false,
              "a_i": false
            },
            "is_active": {
              "type": "tinyint(1)",
              "key": false,
              "nullable": true,
              "default": false,
              "a_i": false
            },
            "insert_date": {
              "type": "timestamp",
              "key": false,
              "nullable": true,
              "default": "current_timestamp()",
              "a_i": false
            },
            "delete_date": {
              "type": "datetime",
              "key": false,
              "nullable": true,
              "default": false,
              "a_i": false
            }
          }
        }
    

You will never have to manually construct either of these files/commands. Instead, your approach is always scenario based. If

  • Your table(s) exist/is up to date but not your migrate.json: neoan3 migrate models down
  • Your migrate exists/is up to date but your database isn't: neoan3 migrate models up
  • You just created a new model, and neither a migrate json nor table(s) in your database exists/are up to date: visit http://localhost:8080/migrate