Skip to content

CRUDify is a command-line tool to kickstart a backend project by providing an ER Diagram.

License

Notifications You must be signed in to change notification settings

crudify-node/crudify-core

Repository files navigation


CRUDify

A command-line tool available as an NPM package which creates a starter backend project consisting of CRUD API endpoints and a configured PostgreSQL database (using Prisma ORM) from just an ER Diagram which the user needs to provide in JSON format.

Try it out · Report Bug · Request Feature

About The Project

CRUDify is a command-line tool to kickstart a backend project by just providing an ER Diagram.

The user needs to create a database schema in JSON format and then install the package. Next step is to invoke the package from the command line and pass the name of the schema file along with it.

This creates a backend project with the corresponding database schema file for Prisma ORM. Further, it also contains all the endpoints for CRUD operations for all database tables.


Installation

Install the NPM package

yarn add crudify-dtu
npm i crudify-dtu

Installation - Local Development

Install the dependencies

yarn install

Build the project in watch mode

yarn build -w

In a separate terminal, run the project

yarn dev

How To Use

Consider the following ER Diagram

BFS

Shown below will be the corresponding schema for CRUDify

{
  "Models": [
    {
      "name": "user",
      "attributes": {
        "StaticFields": [
          {
            "name": "email",
            "type": "String",
            "isUnique": true,
            "faker": {
              "module": "internet",
              "method": "email"
            }
          },
          {
            "name": "password",
            "type": "String",
            "toBeHashed": true,
            "faker": {
              "module": "internet",
              "method": "password"
            }
          },
          {
            "name": "name",
            "type": "String"
          }
        ],
        "RelationalFields": []
      }
    },
    {
      "name": "blog",
      "attributes": {
        "StaticFields": [
          {
            "name": "title",
            "type": "String"
          },
          {
            "name": "content",
            "type": "String"
          }
        ],
        "RelationalFields": [
          {
            "connection": "user",
            "foriegnKeyName": "id",
            "type": "ONETOMANY"
          }
        ]
      }
    },
    {
      "name": "review",
      "attributes": {
        "StaticFields": [
          {
            "name": "title",
            "type": "String"
          },
          {
            "name": "content",
            "type": "String"
          }
        ],
        "RelationalFields": [
          {
            "connection": "user",
            "foriegnKeyName": "id",
            "type": "ONETOMANY"
          },
          {
            "connection": "blog",
            "foriegnKeyName": "id",
            "type": "ONETOMANY"
          }
        ]
      }
    }
  ],
  "Authentication": {
    "model": "user",
    "userFieldName": "email",
    "passwordFieldName": "password"
  }
}

Step 1: Create a new folder for your project

Step 1: Create a new folder for your project

Step 2: Create your schema as a JSON file

Step 2: Create your schema as a JSON file

Step 3: Install the crudify-dtu NPM package

Step 3: Install the crudify-dtu NPM package

Step 4: CRUDify your ER Diagram using npx crudify-dtu “schema.json” command

Step 4: CRUDify your ER Diagram using npx crudify-dtu “schema.json” command

You can see the equivalent schema created in Prisma ORM in app/prisma/schema.prisma file This schema is converted into raw SQL queries after setup (after Step 5)

Equivalent schema created in Prisma ORM

You can see app/src/routes/ contains the APIs for blog, review and user models

APIs for blog, review and user models

Step 5: cd into app directory and follow the instructions shown below for setup

Instructions shown below for setup

Create a .env file at the root of the app and copy the content of .example.env file into it. Then, add your PostgreSQL username and password and replace the database name starter with a name of your choice. After creating the .env file, run the following commands:

yarn install
yarn prisma migrate dev
yarn build
yarn dev

Syntax For Creating JSON Schema File

{
  "Models": [
    {
      "name": "MODEL_NAME",
      "softDelete": false/true, // It is an optional field with default value as true
      "attributes": {
        "StaticFields": [
          {
            "name": "FIELD_NAME",
            "type": "FIELD_TYPE",
            "isUnique": true,
            "toBeHashed": true,
            "faker": {
              "module": "MODULE_NAME",
              "method": "FUNCTION_NAME"
            }
          }
        ],
        "RelationalFields": [
          {
            "connection": "RELATED_TABLE_NAME",
            "foriegnKeyName": "id",
            "type": "CONNECTION_TYPE"
          }
        ]
      }
    }
  ],
  "Enums": [
    {
      "name": "ENUM_NAME",
      "fields": ["SAMPLE_FIELD1", "SAMPLE_FIELD2"]
    }
  ],
  "Authentication": {
    "model": "YOUR_USER_MODEL_NAME",
    "userFieldName": "YOUR_USERNAME_FIELD_NAME",
    "passwordFieldName": "YOUR_PASSWORD_FIELD_NAME"
  }
}

MODEL_NAME: Name of the table (must be lowercase)
softDelete: False if you don't want soft deletes enabled on a particular model. See more about soft deletes here.
StaticFields: Array of JSON objects with each object representing a non-relational field
FIELD_NAME: Name of the field (must be lowercase)
FIELD_TYPE: Type of the field (can be either String , Boolean , Int , BigInt , Float , Decimal , DateTime , Json)
isUnique: Boolean that signifies whether the unique constraint should be applied to the field. Defaults to false, so can be omitted.
toBeHashed: Boolean that signifies whether the field's value should be hashed before saving to the database. Defaults to false, so can be omitted.
faker: Object representing the type of seed (fake) data that should be generated for the field. It is optional
module: Name of the module (e.g. lorem) from https://fakerjs.dev/api/
method: Name of the function to be called for the provided module [e.g. word (for lorem module)] from https://fakerjs.dev/api/
RelationalFields: Array of JSON objects with each object representing a relational field
RELATED_TABLE_NAME: Name of the table to which you want to create a relation to
foriegnKeyName: Name of the field in the RELATED_TABLE_NAME table which should be made the foreign key. It should be set as id to set the default auto-generated primary key of the RELATED_TABLE_NAME table as the foreign key
CONNECTION_TYPE: Can be either ONETOMANY or ONETOONE. In the case of ONETOMANY connection, one record in RELATED_TABLE_NAME will be related to many MODEL_NAME records
Enums: Specify to use enums in your database. See more about enums and their usage here

USER AUTHENTICATION DETAILS
Authentication: An object containing information regarding user authentication. It is optional and should be added only if user authentication API endpoints are required (login and getCurrentUser currently)
model: Name of the user model defined previously (case-sensitive)
userFieldName: Name of the field in the user model corresponding to username (Must be a unique field)
passwordFieldName: Name of the field in the user model corresponding to password


Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

Naman Gogia - Linkedin - namangogia2001@gmail.com
Abhinandan Sharma - Linkedin - abhi.moudgil15@gmail.com

(back to top)

About

CRUDify is a command-line tool to kickstart a backend project by providing an ER Diagram.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published