cool-admin-midway

repository·8.x·Indexed 25 days ago

https://github.com/cool-team-official/cool-admin-midway

A high-efficiency Node.js backend management system built on the Midway.js framework. It is designed for rapid CRUD development and features AI-assisted coding, workflow orchestration, multi-tenancy, and a modular/plugin-based architecture. It includes tools for automated API generation via @CoolController, TypeORM entity management, i18n translation services, and system menu management.

Tokens
7.4K
Snippets
12
Records
68
Agent score
85%

What's inside cool-admin-midway

  1. Run cool-admin-midway locally

    8.x

    To run the project locally, first configure your database in src/config/config.local.ts. The project requires Node.js >= 18.x and MySQL >= 5.7 (8.0 recommended). On the first start, the system will automatically initialize and import data.

    After configuration, install dependencies and start the development server using npm or pnpm.

    $ npm i
    $ npm run dev
  2. Create a new data table using TypeORM entities

    8.x

    Define a new table by creating an entity file (e.g., src/modules/demo/entity/goods.ts). Extend BaseEntity from the project's base package. The table will be automatically created in the database upon project startup.

    import { BaseEntity } from '../../base/entity/base';
    import { Column, Entity, Index } from 'typeorm';
    
    /**
     * Product
     */
    @Entity('demo_app_goods')
    export class DemoAppGoodsEntity extends BaseEntity {
      @Column({ comment: 'title' })
      title: string;
    
      @Column({ comment: 'pic' })
      pic: string;
    
      @Column({ comment: 'price', type: 'decimal', precision: 5, scale: 2 })
      price: number;
    }
  3. Manage system menus with BaseSysMenuService

    8.x
    The BaseSysMenuService provides a comprehensive API for managing the application's menu structure, including retrieving menus based on user roles, importing/exporting menu hierarchies, and handling menu-related permissions. It is designed to work within a MidwayJS environment and integrates with BaseSysPermsService to refresh user permissions when menus change.
  4. Configure MySQL database in cool-admin-midway

    8.x

    Edit src/config/config.local.ts to set up your MySQL connection. The typeorm configuration block defines the connection details. Note that synchronize: true is used for automatic table creation; use this for development only, as it can lead to data loss in production environments.

    // mysql, driver is built-in
    typeorm: {
        dataSource: {
          default: {
            type: 'mysql',
            host: '127.0.0.1',
            port: 3306,
            username: 'root',
            password: '123456',
            database: 'cool',
            // Auto-create tables. WARNING: Do not use in production!
            synchronize: true,
            logging: false,
            charset: 'utf8mb4',
            cache: true,
            entities: ['**/modules/*/entity'],
          },
        },
      },
  5. Configure the local database environment with Docker Compose

    8.x

    Use the provided docker-compose.yml to set up a local development environment containing MySQL (coolDB) and Redis (coolRedis).

    Key Configuration Details:

    • Data Persistence: Database files are stored in the ./data/ directory relative to the project root.
    • Port Conflicts: If the default ports are already in use, modify the port mapping in the ports section (the number before the colon : is the host port).
    • Auto-restart: Containers are configured with restart: always. Comment this line out if you do not want containers to start automatically when the system boots.
    • MySQL Configuration:
      • MYSQL_ROOT_PASSWORD: Set the root user password.
      • MYSQL_DATABASE: Set the business database name (default: cool).
      • MYSQL_USER: Set the business database username.
      • MYSQL_PASSWORD: Set the business database password.
    • Redis Configuration:
      • To enable a password for Redis, uncomment the command: --requirepass "12345678" line in the coolRedis service.
    version: "3.1"
    
    services:
      coolDB:
        image: mysql
        command:
          --default-authentication-plugin=mysql_native_password
          --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
          --group_concat_max_len=102400
        restart: always
        volumes:
          - ./data/mysql/:/var/lib/mysql/
        environment:
          TZ: Asia/Shanghai
          MYSQL_ROOT_PASSWORD: "123456"
          MYSQL_DATABASE: "cool"
          MYSQL_USER: "root"
          MYSQL_PASSWORD: "123456"
        networks:
          - cool
        ports:
          - 3306:3306
    
      coolRedis:
        image: redis
        #command: --requirepass "12345678" # redis password
        restart: always
        environment:
          TZ: Asia/Shanghai
        volumes:
          - ./data/redis/:/data/
        networks:
          - cool
        ports:
          - 6379:6379
    
    networks:
      cool:
  6. Generate CRUD APIs with @CoolController

    8.x

    Use the @CoolController decorator to automatically generate standard CRUD endpoints for an entity. By specifying the api array and the entity, you can instantly create 6 endpoints: add, delete, update, info, list, and page.

    Example endpoints for a controller at /app/demo/goods:

    • POST /app/demo/goods/add (Create)
    • POST /app/demo/goods/delete (Delete)
    • POST /app/demo/goods/update (Update)
    • GET /app/demo/goods/info (Get single record)
    • POST /app/demo/goods/list (List records)
    • POST /app/demo/goods/page (Paginated query with fuzzy search)
    import { CoolController, BaseController } from '@cool-midway/core';
    import { DemoAppGoodsEntity } from '../../entity/goods';
    
    /**
     * Product Controller
     */
    @CoolController({
      api: ['add', 'delete', 'update', 'info', 'list', 'page'],
      entity: DemoAppGoodsEntity,
    })
    export class DemoAppGoodsController extends BaseController {
      /**
       * Custom endpoint
       */
      @Get('/other')
      async other() {
        return this.ok('hello, cool-admin!!!');
      }
    }