1.0.4 • Published 8 months ago

nest-genie v1.0.4

Weekly downloads
-
License
BSD-3-Clause
Repository
-
Last release
8 months ago

Nest Genie 🧞

Nest Genie is a CLI tool for generating modules, services, controllers, and more in NestJS projects that follow a clean architecture structure.

Installation

To install Nest Genie, you can use npm and then install the dependencies:

# Use npm
$ npm install -g nest-genie

# Use yarn
$ yarn add -g nest-genie

# Use pnpm
$ pnpm install -g nest-genie

Use

Nest Genie provides several generators to help you create different parts of your NestJS application. The available generators are:

  • doc: Easily generate error codes for later use in different places in the application.
  • single: Generates individual controllers, CRUDs, providers, schemas and use cases.

The main command includes this same documentation and can be consulted at any time:

$ genie


  _   _           _      ____            _      _ 
 | \ | | ___  ___| |_   / ___| ___ _ __ (_) ___| |
 |  \| |/ _ \/ __| __| | |  _ / _ \ '_ \| |/ _ \ |
 | |\  |  __/\__ \ |_  | |_| |  __/ | | | |  __/_|
 |_| \_|\___||___/\__|  \____|\___|_| |_|_|\___(_)

  

  Usage: genie <generator> <action>

  Available generators and actions:
  - doc: doc errors
  - single: single controller, single crud, single provider, single schema, single use-case

  Examples:
  - genie doc errors
  - genie single controller

  For more information, visit our documentation at https://docs

Commands

To use Nest Genie, run the genie command followed by the generator and the action you want to perform. Here are some examples:

$ genie doc errors
$ genie single controller
$ genie single crud
$ genie single provider
$ genie single schema
$ genie single use-case

Examples

1. Generate a controller:

To generate a driver, use the following command:

$ genie single controller
  √ Controller module name (singular)? · car

  Loaded templates: ....
        inject: src/app.module.ts
        inject: src/app.module.ts
        inject: src/infraestructure/controllers/index.ts
        added: src/infraestructure/controllers/cars.controller.ts
        added: src/infraestructure/controllers/docs/cars/cars.docs.decorator.ts
        inject: src/infraestructure/controllers/docs/index.ts
        added: src/infraestructure/controllers/docs/cars/cars.responses.ts       
        added: src/infraestructure/controllers/tests/cars/cars.controller.spec.ts

2. Generate a Use Case

$ genie single use-case
  √ Use case module name (singular)? · car

  Loaded templates: ....
        inject: src/application/application.module.ts
        inject: src/application/application.module.ts
        inject: src/application/application.module.ts
        added: src/application/cars/cars.use-cases.ts
        added: src/application/cars/tests/cars.use-cases.mocks.ts
        added: src/application/cars/tests/cars.use-cases.spec.ts

Note that it asks for the name of the module it will take to build the required structure.

3. Generate a CRUD

To generate a CRUD, use the following command:

$ genie single crud
  √ Entity name (singular)? · car
  √ Pick a database · MongoDB

  Loaded templates: ....
        inject: src/application/application.module.ts
        inject: src/application/application.module.ts
        inject: src/application/application.module.ts
        added: src/application/cars/tests/cars.use-cases.mocks.ts
        added: src/application/cars/tests/cars.use-cases.spec.ts 
        added: src/application/cars/cars.use-cases.ts
        added: src/domain/cars/cars.adapter.ts
        added: src/domain/cars/index.ts
        .......

You can select between MongoDB and PostgreSQL according to your particular case.

4. Generate a new schema

We can easily generate a new database schema within the framework layer according to the selected database:

$ genie single schema
  √ Schema's name · car
  √ Pick a database · PostgreSQL

  Loaded templates: ....
        added: src/domain/cars/cars.adapter.ts
        added: src/domain/cars/index.ts
        added: src/domain/cars/cars.entity.ts
        added: src/domain/cars/cars.types.ts
        inject: src/framework/providers/data.provider.ts
        inject: src/framework/providers/data.provider.ts
        inject: src/framework/postgresql/models/index.ts
        added: src/framework/postgresql/models/cars.model.ts
        inject: src/framework/postgresql/postgresql.service.ts
        inject: src/framework/postgresql/postgresql.module.ts
        inject: src/framework/postgresql/postgresql.service.ts
        inject: src/framework/postgresql/postgresql.service.ts
        inject: src/framework/postgresql/postgresql.service.ts
        .......

5. Generate a new provider based on an abstraction

This generator allows you to create an abstraction that represents something tangible, for example, a mailer provider. This can be implemented by a framework:

$ genie single provider
  √ Abstraction's name · email
  √ Library's name · nodemailer
  √ Abstraction's name · email
  √ Library's name · nodemailer

  Loaded templates: ....
        added: src/framework/providers/email.provider.ts
        added: src/framework/nodemailer/nodemailer.module.ts
        added: src/framework/nodemailer/nodemailer.repository.ts
        added: src/framework/nodemailer/tests/nodemailer.repository.spec.ts
        inject: src/framework/providers/providers.module.ts
        inject: src/framework/providers/providers.module.ts
        inject: src/framework/providers/providers.module.ts

6. Generate error definition easily

You can generate a complete error definition from the CLI without needing to visit the configuration files directly.

  $ genie doc errors
  √ Error name (simple and short)? · car not registered
  √ HTTP Status code: · 405
  √ Message: · This message is an example
  √ Description (for swagger docs): · Description for swagger documentation

  Loaded templates: ....
        inject: libs/common/src/exceptions/codes.ts
        inject: libs/common/src/exceptions/codesDefinition.ts

The modified files look like this:

// libs/common/src/exceptions/codes.ts
export enum ErrorCodes {
  // Add custom codes | Don't remove this line
  CAR_NOT_REGISTERED = 'CAR_NOT_REGISTERED',
  ...
}

// libs/common/src/exceptions/codesDefinition.ts
export const ErrorCodesDefinition: ErrorFromEnumObject<typeof ErrorCodes> = {
  // Add custom codes definition | Don't remove this line
  CAR_NOT_REGISTERED: {
    message: 'This message is an example',
    statusCode: 405,
    description: 'Description for swagger documentation',
  },
}

Contributors

  • Manuel Cutie M.
  • Gabriel Barrera O.

License

This project is licensed under the BSD-3-Clause license.

This documentation provides an overview of the tool, installation instructions, usage examples, and other relevant information.

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago