Page cover

Objection

@adminjs/objection

Before reading this article, make sure you have set up an AdminJS instance using one of the supported Plugins. Additionally, you should have installed @adminjs/objection as described in Getting started section.

This guide will assume you have set up Objection using it's documentation.

Before you start connecting your Objection models to AdminJS, we strongly advise to extend it's Model class to include format options and hooks for setting up your timestamps.

You might need to install an additional library ajv-formats:

$ yarn add ajv-formats
base-model.ts
import addFormats from 'ajv-formats';
import { AjvValidator, Model } from 'objection';

export abstract class BaseModel extends Model {
  createdAt: string;

  updatedAt: string;

  static createValidator(): AjvValidator {
    return new AjvValidator({
      onCreateAjv: (ajv) => {
        addFormats(ajv);
      },
      options: {
        allErrors: true,
        validateSchema: false,
        ownProperties: true,
      },
    });
  }

  $beforeInsert(): void {
    this.createdAt = new Date().toISOString();
  }

  $beforeUpdate(): void {
    this.updatedAt = new Date().toISOString();
  }
}

Office is an example model which extends BaseModel:

Please note that jsonSchema is necessary for AdminJS to determine your fields and their types.

The rest of the setup is similar to other adapters:

  • You must import AdminJSObjection adapter and register it

  • You must import the entities you want to use and pass them to AdminJS resources options, you cannot use databases because unfortunately Objection does not expose any connection with all models metadata.

Nest.js Support

A guide for Nest.js is a work in progress. If you would like to contribute to the documentation, please contact us.

Last updated