Skip to content

Upgrading to 0.30.0

This version contains some major and breaking changes. You can easily upgrade your project by using this documentation.

Step 1. Serialization folder

Axe API will not support model serialize method anymore with this version.

You should create a Serialization folder under the app directory instead of that model function.

Before

ts
class User extends Model {
  serialize(item: any, request: Request) {
    return {
      ...item,
      fullname: `${item.name} ${item.surname}`,
    };
  }
}

After

ts
class User extends Model {
  // no serialize method anymore
}

app/Serialization/UserSerialization.ts

ts
import { Request } from "express";

export default (item: any, request: Request) => {
  return {
    ...item,
    fullname: `${item.name || ""} ${item.surname || ""}`.trim(),
  };
};

Step 2. Setup version folder

All of your application files should be in a version folder with the new version. Your files should be looking like the following example before 0.30.0;

bash
├── app
  ├── Config
  ├── Events
  ├── Hooks
  ├── Models
  ├── init.ts

Your app folder structure should be changed like this;

bash
├── app
  ├── v1
    ├── Config
    ├── Events
    ├── Hooks
    ├── Models
    ├── init.ts

TIP

You can use any version name such as v1, v2, beta, etc.

Step 3. Setup application config

Your API should have two different configurations;

  • Application config (/app/config.ts)
  • Version config (/app/v1/config.ts)

TIP

The main configuration file has been split into two different parts because both applications and versions should have different configuration files to configure the application and version separately.

You can see the application configuration file content in the following example;

app/config.ts

ts
import path from "path";
import { LogLevels, IApplicationConfig } from "axe-api";

const config: IApplicationConfig = {
  prefix: "api",
  env: process.env.NODE_ENV || "production",
  port: process.env.APP_PORT ? parseInt(process.env.APP_PORT) : 3000,
  logLevel: LogLevels.INFO,
  database: {
    client: process.env.DB_CLIENT || "mysql",
    connection: {
      host: process.env.DB_HOST || "localhost",
      user: process.env.DB_USER || "user",
      password: process.env.DB_PASSWORD || "password",
      database: process.env.DB_DATABASE || "database",
      filename: path.join(__dirname, "..", "..", "mydb.sqlite"),
    },
    pool: {
      min: 2,
      max: 10,
    },
    migrations: {
      tableName: "knex_migrations",
    },
  },
};

export default config;

Step 4. Setup version config

Each of your API versions should have a configuration file like the following example;

app/v1/config.ts

ts
import { IVersionConfig, QueryFeature, allow } from "axe-api";

const config: IVersionConfig = {
  transaction: [],
  serializers: [],
  supportedLanguages: ["en-GB", "en", "tr", "de"],
  defaultLanguage: "en-GB",
  query: {
    limits: [allow(QueryFeature.All)],
    defaults: {
      perPage: 10,
      minPerPage: 5,
      maxPerPage: 25,
    },
  },
};

export default config;

Step 4. Remove old configuration folder

The app/Config folder should be removed. All configurations in that folder should be moved to the app/config.ts and app/v1/config.ts files.

Step 5. Setting new hooks and event folders

Hook and Event folder structure is redesigned. Let's assume that you have the following hook file for the User model.

app/v1/Events/UserEvents.ts

ts
import { IContext } from "axe-api";

const onAfterInsert = async ({ formData }: IContext) => {
  // You can send an email to the user in here...
};

const onBeforePaginate = async ({ formData }: IContext) => {
  // do something cool in here
};

export { onAfterInsert, onBeforePaginate };

You should have two different files with the new file structure;

app/v1/Events/User/onAfterInsert.ts

ts
import { IContext } from "axe-api";

export default async ({ formData }: IContext) => {
  // You can send an email to the user in here...
};

app/v1/Events/User/onBeforePaginate.ts

ts
import { IContext } from "axe-api";

export default async ({ formData }: IContext) => {
  // do something cool in here
};

TIP

The changes you must do;

  • Create subfolders for each model that have hooks or events.
  • Create a hook or event file by the hook or event name.
  • Export the hook or event function as default.

You've made it! 🎉

It looks like you have made it. Congrats! 👏 👏 👏

If you have any question please contact us via GitHub Issues.

Released under the MIT License.