1.4.0 • Published 2 years ago

@myunisoft/typeorm-file-parser v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

📢 About

This package has been created to parse TypeORM file and return informations on a given Entity (decorators, columns and properties). The main idea is to be able to retrieve information for a CLI or documentation.

🚧 Requirements

🚀 Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @myunisoft/typeorm-file-parser
# or
$ yarn add @myunisoft/typeorm-file-parser

📚 Usage example

import { fileURLToPath } from "url";
import path from "path";

import * as TypeORMFileParser from "@myunisoft/typeorm-file-parser";

// CONSTANTS
const __dirname = path.dirname(fileURLToPath(import.meta.url));

const result = await TypeORMFileParser.readFile(
  path.join(__dirname, "EntityFile.ts")
);
console.log(result);

It will return an Object like the following one

{
  "properties": {
    "id": {
      "type": "number",
      "decorators": {
        "PrimaryGeneratedColumn": {
          "name": "PrimaryGeneratedColumn"
        }
      }
    },
    "note": {
      "type": "string",
      "decorators": {
        "Column": {
          "name": "Column",
          "type": "text",
          "properties": {
            "nullable": true
          }
        }
      }
    },
    "billed": {
      "type": "boolean",
      "decorators": {
        "Column": {
          "name": "Column",
          "type": "boolean",
          "properties": {
            "default": "false"
          }
        }
      }
    }
  },
  "unique": {
    "constraintName": "GiArticle_reference",
    "columns": [
      "reference"
    ]
  }
}

📜 API

The response of the readFile method is described by the following interfaces:

export interface TypeORMProperty {
  /** TypeScript/JavaScript type */
  type: string;

  /** TypeScript (TypeORM) decorators attached to the property */
  decorators: Record<string, TypeORMDecoratorBase>;
}

export interface ParsedTypeORMResult {
  /** Entity Unique decorator (without root name property) */
  unique?: DecoratorExWithoutName;

  /** Entity properties as a plainObject */
  properties: Record<string, TypeORMProperty>;
}

TypeORMDecoratorBase is a composition of multiple types

export type TypeORMDecoratorExtended =
  UniqueDecorator |
  RelationDecorator |
  ColumnDecorator |
  JoinDecorator;

export type TypeORMDecoratorBase =
  { name: "Entity" | "PrimaryGeneratedColumn" } |
  TypeORMDecoratorExtended;

Each of them can be found in ./src/decorator/parsers

export interface UniqueDecorator {
  name: "Unique";
  constraintName: string | null;
  columns: string[];
}
export interface RelationDecorator {
  name: RelationKind;
  table: string;
  tableColumn: string;
  properties: Properties;
}
export type ColumnKind = "PrimaryColumn" | "Column" | "Generated";

export interface ColumnDecorator {
  name: ColumnKind;
  type: string;
  properties: Properties;
}
export type JoinKind = "JoinTable" | "JoinColumn";

export interface JoinDecorator {
  name: JoinKind;
  properties: Properties;
}

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):

License

MIT

1.4.0

2 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago