Code Monkey home page Code Monkey logo

little-mermaid-2-the-sql's Introduction

little-mermaid-2-the-sql

  • a command line tool for generating create sql from mermaid erDiagram

Getting Started

  1. npm install -g @funktechno/little-mermaid-2-the-sql
  2. littleMermaid2SQL --help
  3. littleMermaid2SQL -d dbType --src markdownFilePath

Generating Mermaid erDiagram

Ala Cart

  1. npm install --save @funktechno/little-mermaid-2-the-sql
  2. Example
    • from markdown content

      import { GenerateSqlFromMermaid } from "@funktechno/little-mermaid-2-the-sql/lib/src/Library";
      import { MarkdownContentResponseI } from "@funktechno/little-mermaid-2-the-sql/lib/src/types";
      import { DbEntityAttributesDefinition, DbRelSpec } from "@funktechno/little-mermaid-2-the-sql/lib/src/types";
      
      
      const markdownContent:MarkdownContentResponseI ={
        settings: {
            database: "postgres",
            outputName: "result",
            isRaw: false,
            src: ""
        },
        content:`
        #Test
        \`\`\`mermaid
        erDiagram
            %% comment 1
            Persons {
                int PersonID PK "NOT NULL"
                varchar255 LastName
                varchar255 FirstName
                varchar255 Address
                varchar255 City
            }
      
            %% comment 2
            Orders {
                int OrderID PK "NOT NULL"
                int PersonID FK "NOT NULL"
            }
      
            Persons ||--o{ Orders : "[Persons.PersonId] to [Orders.PersonId]"
        \`\`\`
        `
      };
      
      const sqlOutputs = GenerateSqlFromMermaid(markdownContent);
    • manual mermaid db model

      import { DbParser } from "@funktechno/little-mermaid-2-the-sql/lib/src/generate-sql-ddl";
      import erDb from "@funktechno/little-mermaid-2-the-sql/lib/src/mermaid/src/diagrams/er/erDb";
      
      const db = erDb;
      
      let entityName = "Persons";
      // load mermaid db with entities
      db.addEntity(entityName);
      let attributes:DbEntityAttributesDefinition[] = [
        {
          attributeName: "PersonID",
          attributeKeyType: "PK",
          attributeType: "int",
          attributeComment: "NOT NULL"
        },
        {
          attributeName: "LastName",
          attributeType: "varchar255"
        },
        {
          attributeName: "FirstName",
          attributeType: "varchar255"
        },
        {
          attributeName: "Address",
          attributeType: "varchar255"
        },
        {
          attributeName: "City",
          attributeType: "varchar255"
        }
      ];
      db.addAttributes(entityName,attributes);
      
      entityName = "Orders";
      db.addEntity(entityName);
      attributes = [
        {
          attributeName: "OrderID",
          attributeKeyType: "PK",
          attributeType: "int",
          attributeComment: "NOT NULL"
        },
        {
          attributeName: "PersonID",
          attributeKeyType: "FK",
          attributeType: "int",
          attributeComment: "NOT NULL"
        }
      ];
      db.addAttributes(entityName,attributes);
      
      const relSpec:DbRelSpec= {
        cardA: "ZERO_OR_MORE",
        cardB: "ONLY_ONE",
        relType: "IDENTIFYING"
      };
      
      db.addRelationship("Persons", `[Persons.PersonId] to [${entityName}.PersonId]`, entityName, relSpec);
      const ddlSyntax = new DbParser('sqlite', db).getSQLDataDefinition();
    • sql output

      CREATE TABLE "Persons" (
        "City" varchar(255),
        "Address" varchar(255),
        "FirstName" varchar(255),
        "LastName" varchar(255),
        "PersonID" int NOT NULL,
        PRIMARY KEY("PersonID")
      );
      
      CREATE TABLE "Orders" (
        "PersonID" int NOT NULL,
        "OrderID" int NOT NULL,
        PRIMARY KEY("OrderID"),
        FOREIGN KEY ("PersonId") REFERENCES "Persons"("PersonId")
      );

Development

  1. git clone repo
    1. run git submodule update --init --recursive --progress to clone submodule dependencies
  2. run npm install inside directory.
  3. Examples:
    1. sqlite npm run littleMermaid2SQL -- -d sqlite --src samples/chinook-database-2.0.1.md
    2. postgres npm run littleMermaid2SQL -- -d postgres --src samples/Adventureworks_pg.md
    3. mysql npm run littleMermaid2SQL -- -d mysql --src samples/Adventureworks_mysql.md
  4. Force uninstall
    • npm uninstall -g little-mermaid-2-the-sql
    • npm list -g
    • littleMermaid2SQL --help
  5. test npm packaging
    • update .npmignore
    • run npm pack unzip contents and verify, node_modules are not required b/c they will be installed with package.json
    • npm unpublish @funktechno/[email protected] can only work within 1 week of publish, not very useful b/c not possible to republish an unpublished version

Technologies

little-mermaid-2-the-sql's People

Contributors

dependabot[bot] avatar lastlink avatar mbuchoff avatar smoreira1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

little-mermaid-2-the-sql's Issues

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.