SecJS / Generator

🧬 Simple code generator for any framework using Node.js
MIT License
10 stars 2 forks source link

Add CLI questions to create model with attributes #8

Open txsoura opened 2 years ago

meerhamzadev commented 2 years ago

hey, can you explain this a bit more? I'm interested

jlenon7 commented 2 years ago

Hey @meerhamzadev, this issue will add some questions to the CLI after choosing a framework to add an attribute to the CRUD.

Example:

Add attribute ? yes/no

-> yes

Attribute name:

-> email

Attribute type:

-> string

Attribute length:

-> 100

Attribute is required ? yes/no

-> yes

Attribute is unique ? yes/no

-> yes

Attribute "email" add successfully!

Add attribute ? yes/no

-> no

// Generate the files...
meerhamzadev commented 2 years ago

So, I was supposed to provide the interface right? or functionality as well?

jlenon7 commented 2 years ago

You can help however you can. Count on me to help you with anything you need, I can help you with functionality and the CLI too.

jlenon7 commented 2 years ago

To facilitate this, we can make a brainstorm for what we will need to finalize this issue

Model

// NestJS TypeORM Model example

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'

export class <%= namePascal %>  {
    @PrimaryGeneratedColumn()
    id: string

    <% for (attribute of attributes) { %>
         @Column({ 
             unique: <%= attribute.unique %>,
             length: <%= attribute.length %>,
             nullable: <%= !attribute.required %>,
          })
         <%= attribute.name %>: <%= attribute.type %>;
    <% } %>

    @CreateDateColumn({ name: 'created_at' })
    createdAt: Date

    @UpdateDateColumn({ name: 'updated_at' })
    updatedAt: Date

    @Column({ name: 'deleted_at' })
    deletedAt?: Date

    @Column({ enum: ['pendent', 'active'] })
    status: string

    /**
     * Relations
     */
}

Validator

// NestJS TypeORM Validator example

import { Injectable } from '@nestjs/common'
import { Validator } from '@secjs/validator'

@Injectable()
export class <%= namePascal %>Validator extends Validator {
  get schema() {
    return {
      <% for (attribute of attributes) { %>
           <%= attribute.name %>: '<%= attribute.required ? 'required' : '' %>|<%= attribute.unique ? 'unique:<%= namePlural %>' : '' %>';
      <% } %>
    }
  }
}

These are just examples, I didn't test it, but I think that should work.

meerhamzadev commented 2 years ago

Ok, I would give it a try, this weekend 🙌

meerhamzadev commented 2 years ago

hey, @jlenon7 how to run CLI locally?

jlenon7 commented 2 years ago

Hey @meerhamzadev, sorry about that, I will document how to run locally later.

First install yo globally using npm.

npm install -g yo

Then in the project root, install the dependencies

npm install

Then link the project using npm

npm link

Now every time that you run yo secjs ..., it will use the local project because of npm link.

yo secjs Foo --path=./Bar