grunt-generate-history-model

Code generation history models by models description

Usage no npm install needed!

<script type="module">
  import gruntGenerateHistoryModel from 'https://cdn.skypack.dev/grunt-generate-history-model';
</script>

README

Grunt plugin for code generation history models by models description

Build Status

This repository provides a grunt plugin for code generation history models by models description.

Установка

npm install grunt-generate-history-model

Как начать использовать

  • Установить typeorm.

    npm install typeorm

  • Создайте gencofig.json в корневом каталоге.

{
    "check":
        {
            "folders":[
                "./models"
            ]
        }
}

Свойство "folders" показывает для каких папок(и их внутренних папок) нужны модели логирования.

  • Установите декораты на нужные модели.
import { InnerClass } from "./innerClass";
import { GenerateHistory, IgnoredInHistory, HistoryIndex } from "grunt-generate-history-model";
import { Column } from "typeorm";

@GenerateHistory({
    "historyPath":"./generated/models"
})
export class Class {

    @Column('text', {'nullable': true })
    public property1: string;
    @Column('integer', {'nullable': true })
    @HistoryIndex()
    public indexProperty: number;
    @Column('text', {'nullable': true })
    @IgnoredInHistory()
    public ignoredProperty: any;
}
  • В package.json добавьте инициализирующую команду в свойство "scripts":
  "scripts": {
    "generation": "generateHistory"
  }

где "generateHistory" - строка для запуска плагина.

  • npm run generation

  • после завершения работы плагина по пути, указанному в декораторе GenerateHistory, появятся файлы с расширением ".ts" :

history model

import {Entity, Column, PrimaryColumn, ColumnOptions, Index, PrimaryGeneratedColumn} from 'typeorm';
import 'reflect-metadata';

@Entity('h_class')
export class hClass {
    @PrimaryGeneratedColumn()
    public __id?: number;

    @Column()
    public __operation: string;

    @Column('timestamp with time zone')
    @Index('ind_hClass_changed_date')
    public __changedate: Date;
   @Column('text', {'nullable': true})
    public property1: string;
   @Column('integer', {'nullable': true})
    @Index('ind_hClass_indexProperty')
    public indexProperty: number;
}

Декораторы

В этом плагине используются 3 декоратора: 1 для классов и 2 для свойств.

Декораторы для классов

GenerateHistory

Основной декоратор для создания моделей логирования.

+-------------+--------------+-------------------------------------------------------+
|                        @GenerateHistory                                            |
+------------------------------------------------------------------------------------+
|   property  |  Mandatory   |                      definition                       |
+-------------+--------------+-------------------------------------------------------+
| options     | true         | options, which used to creare history                 |- complex object
+-------------+--------------+-------------------------------------------------------+

+-------------+--------------+-------------------------------------------------------+
|                        options                                                     |
+------------------------------------------------------------------------------------+
|   property  |  Mandatory   |                      definition                       |
+-------------+--------------+-------------------------------------------------------+
| historyPath | true         | path,where history class will create                  |
+-------------+--------------+-------------------------------------------------------+
@GenerateHistory({'historyPath':'./generated/models'})

Декораторы для свойств

HistoryIndex

Декоратор, который используется для создания декоратора Index из библиотеки typeorm у свойства в модели логирования.

+------------------------------------------------------------------------------------------------+
|                        @HistoryIndex                                                           |
+------------------------------------------------------------------------------------------------+
|   property      |  Mandatory   |                      definition                               |
+-----------------+--------------+---------------------------------------------------------------+
| indexName(param)| false        | add name param to typeorm decorator Index in history model    |
+-----------------+--------------+---------------------------------------------------------------+
@HistoryIndex()

@HistoryIndex("name_index")

IgnoredInHistory

Декоратор, который используется, что бы свойство не было перенесено в модель логирования.

@IgnoredInHistory()

Связь с декораторами typeORM

Column

  • Все поля с декораторм Column, если не отмечены декоратором IgnoredInHistory, создаются и в модели логирования.
  • Если декоратор Column имеет свойство nullable, то это свойство переносится и в модель логирования.

Join Column

  • Если поле имеет составной тип и декоратор JoinColumn, то создается поле с типом number и именем, которое описано в декораторе.