LogoLogo
Join our community
  • AdminJS
  • Contribute
  • Demo
  • Addons Marketplace
  • Installation
    • Getting started
    • Plugins
      • Adonis
      • Express
      • Nest
      • Fastify
      • Hapi
      • Koa
      • Community Plugins
        • FeathersJS
        • AdonisJS
      • Matrix
    • Adapters
      • TypeORM
      • Sequelize
      • Prisma
      • MikroORM
      • Objection
      • SQL
      • Mongoose
      • Community Adapters
        • AdonisJS
    • What's new in v7?
    • Migration Guide v7
  • Basics
    • Resource
    • Action
    • Property
    • Features
      • Relations
      • Upload
      • Logger
      • Import & Export
      • Password
      • Leaflet Maps
      • Writing your own features
    • API
      • List
      • New
      • Search
      • Show
      • Edit
      • Delete
      • Bulk Delete
    • Themes
    • Authentication
      • FirebaseAuthProvider
      • MatrixAuthProvider
  • How to write an addon?
  • UI Customization
    • Writing your own Components
    • Overwriting CSS styles
    • Dashboard customization
    • Changing the form view
    • Storybook
  • Tutorials
    • Role-Based Access Control
    • Internationalization (i18n)
    • Content Management System
    • Custom components library
    • Custom component internationalization
  • FAQ
    • PDF Generator
    • Charts
    • Forgot Password
  • ⚠️Legacy documentation
Powered by GitBook
On this page
  1. Installation
  2. Adapters

Objection

@adminjs/objection

PreviousMikroORMNextSQL

Last updated 2 years ago

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

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

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:

office.entity.ts
import { BaseModel } from '../base-model.js';
import Manager from './manager.entity.js';

export interface OfficeAddress {
  street: string;
  city: string;
  zipCode: string;
}

class Office extends BaseModel {
  id: number;

  name: string;

  address?: OfficeAddress;

  static tableName = 'offices';

  static jsonSchema = {
    type: 'object',
    required: ['name'],
    properties: {
      id: { type: 'integer' },
      name: { type: 'string', minLength: 1, maxLength: 255 },
      address: {
        type: 'object',
        properties: {
          street: { type: 'string' },
          city: { type: 'string' },
          zipCode: { type: 'string' },
        },
      },
      createdAt: { type: 'string', format: 'date-time' },
      updatedAt: { type: 'string', format: 'date-time' },
    },
  };
}

export default Office;

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.

app.ts
// ... other imports
import * as AdminJSObjection from '@adminjs/objection'

import { Office } from './office.entity.js'

AdminJS.registerAdapter({
  Resource: AdminJSObjection.Resource,
  Database: AdminJSObjection.Database,
})

// ... other code
const start = async () => {
  const adminOptions = {
    // We pass Office to `resources`
    resources: [Office],
  }
  // Please note that some plugins don't need you to create AdminJS instance manually,
  // instead you would just pass `adminOptions` into the plugin directly,
  // an example would be "@adminjs/hapi"
  const admin = new AdminJS(adminOptions)
  // ... other code
}

start()

Make sure you have followed Objection documentation and have setup your knexfile and knex instance.

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.

Plugins
Getting started
documentation
Page cover image