Coder Social home page Coder Social logo

adminjs-extension's Introduction

adminjs-typeorm

This is an official AdminJS adapter which integrates TypeORM into AdminJS. (originally forked from Arteha/admin-bro-typeorm)

Installation: yarn add @adminjs/typeorm

Usage

The plugin can be registered using standard AdminJS.registerAdapter method.

import { Database, Resource } from '@adminjs/typeorm'
import AdminJS from 'adminjs'

AdminJS.registerAdapter({ Database, Resource });

// Optional: if you use class-validator you have to inject this to resource.
import { validate } from 'class-validator'
Resource.validate = validate

Example

import {
  BaseEntity,
  Entity, PrimaryGeneratedColumn, Column,
  ManyToOne,
  RelationId
} from 'typeorm'
import { MyDataSource } from './my-data-source'
import * as express from 'express'
import { Database, Resource } from '@adminjs/typeorm'
import { validate } from 'class-validator'

import AdminJS from 'adminjs'
import * as AdminJSExpress from '@adminjs/express'

Resource.validate = validate
AdminJS.registerAdapter({ Database, Resource })

@Entity()
export class Person extends BaseEntity
{
  @PrimaryGeneratedColumn()
  public id: number;

  @Column({type: 'varchar'})
  public firstName: string;

  @Column({type: 'varchar'})
  public lastName: string;

  @ManyToOne(type => CarDealer, carDealer => carDealer.cars)
  organization: Organization;

  // in order be able to fetch resources in adminjs - we have to have id available
  @RelationId((person: Person) => person.organization)
  organizationId: number;

  // For fancy clickable relation links:
  public toString(): string
  {
    return `${firstName} ${lastName}`;
  }
}

( async () =>
{
  await MyDataSource.initialize();

  const adminJs = new AdminJS({
    // databases: [MyDataSource],
    resources: [
      { resource: Person, options: { parent: { name: 'foobar' } } }
      ],
    rootPath: '/admin',
  })

  const app = express()
  const router = AdminJSExpress.buildRouter(adminJs)
  app.use(adminJs.options.rootPath, router)
  app.listen(3000)
})()

ManyToOne

Admin supports ManyToOne relationship but you also have to define @RealationId as stated in the example above.

Contribution

Running the example app

If you want to set this up locally this is the suggested process:

  1. fork the repo
  2. Install dependencies
yarn install
  1. register this package as a (linked package)[https://classic.yarnpkg.com/en/docs/cli/link/]
yarn link
  1. Setup example app

Install all dependencies and use previously linked version of @adminjs/typeorm.

cd example-app
yarn install
yarn link @adminjs/typeorm

Optionally you might want to link your local version of adminjs package

  1. Make sure you have all the envs set (which are defined in example-app/ormconfig.js)

  2. Build the package in watch mode

(in the root folder)

yarn dev
  1. run the app in the dev mode
cd example-app
yarn dev

Pull request

Before you make a PR make sure all tests pass and your code wont causes linter errors. You can do this by running:

yarn lint
yarn test

or with proper envs: POSTGRES_USER=yourtestuser POSTGRES_DATABASE="database_test" yarn test

License

AdminJS is copyrighted © 2023 rst.software. It is a free software, and may be redistributed under the terms specified in the LICENSE file.

About rst.software

We’re an open, friendly team that helps clients from all over the world to transform their businesses and create astonishing products.

  • We are available for hire.
  • If you want to work for us - check out the career page.

adminjs-extension's People

Contributors

elfacu0 avatar frsel99 avatar dependabot[bot] avatar fran-merce avatar leotammaro avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.