1.0.10 ā€¢ Published 1 year ago

models-generator-express-nestjs v1.0.10

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Features

  • Config file generate models
  • Support for nestjs and express ts
  • Generate models, interface, entities, dto from your database postgres

package install

  $ npm i  @nestjs/swagger dotenv

Installation

NPM

$ npm i  models-generator-express-nestjs

Getting started

Once the installation process is complete, create file config in the root project for generate models

Ā 

File configuration

const models = require('models-generator-express-nestjs/src/index')
require('dotenv').config()
const main = async () => {
    const connection = {
      host: process.env.DATABASE_HOST || 'localhost',
      port: process.env.DATABASE_PORT || '5432',
      user: process.env.DATABASE_USER || 'user_db',
      password: process.env.DATABASE_PASSWORD || 'pass_db',
      database: process.env.DATABASE || 'your_database'
    };

    const config = {
    database:'mysql', //mysql | postgres 
    orm: 'SEQUELIZE', // TYPEORM | SEQUELIZE,
    configMigrations: true,
    installPackage:true,
    outputModelFile: '.app/modules/',
    tables: {
      exclude: '',
      include: '',
    },
    auth:{
      generate:true,
      table: 'users',
      fields:{
        user:'email',
        password:'password',
      },
      uninstallPackage:false
    },
    generatorForModule: {
      dto: true,
      models: true,
      entitie: true,
      interface: true,
      module: true,
      controller: true,
      rest: true,
      service: true,
    },
    deleteAlways:true
  };

    const modelsPromise = await models(connection.database, connection, config);
    const all = await Promise.all([modelsPromise]);
    process.exit();
}

Add command package.json

 "generator": "node main.js"

Running

npm run generator

json config paramters description

database

  • Select database mysql or postgress

orm

  • Select orm sequelize or typeorm

configMigrations true | false

  • configure in the json package the necessary commands to run migrations

installPackage true | false

-Depending on the orm you select, it will install the corresponding packages

#package typeorm
  -typeorm
  -@nestjs/typeorm
  -@nestjs/config

#package sequelize
  -sequelize
  -sequelize-typescript
  -@nestjs/sequelize

outputModelFile

  • path where the modules will be created

tables

  • configuration to exclude or include tables in the generation of modules

    exclude

    -tables not included

    include

    -tables that only include

auth

  • We can generate a login from a table and its respective field of username and password

    generate true | false

    -validate if we are going to generate the auth module

    table

    -name of the table where the auth module will be generated

    fields

    • username and password fields for login

      user

      password

generatorForModule

dto true | false

models true | false

entitie true | false

interface true | false

module true | false

controller true | false

rest true | false

service true | false

deleteAlways true | false

  • šŸ“¢ important this parameter should only be in tru only in the initial part of the generator then it should be left in false, it already eliminates the folders

Example modules generators sequelize

ā”œā”€ā”€ ./app
ā”‚   ā””ā”€ā”€ main.ts
ā”‚   ā””ā”€ā”€ app.module.ts
ā”‚   ā”œā”€ā”€ modules
ā”‚   ā”‚   ā”œā”€ā”€ users
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ dtos
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.dto.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ entities
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.entity.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ interface
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.interface.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ models
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.model.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ rest
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.json
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.service.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.module.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.controller.ts
ā”‚   ā”œā”€ā”€ database
ā”‚   ā”‚   ā”œā”€ā”€ database.models.ts
ā”‚   ā”‚   ā”œā”€ā”€ database.module.ts
ā”‚   ā”‚   ā”œā”€ā”€ database.providers.ts
ā”‚   ā”‚   ā”œā”€ā”€ sequalize.constants.ts
ā”‚   ā”‚   ā””ā”€ā”€ modules.providers.ts
ā”‚   ā”œā”€ā”€ sequelize
ā”‚   ā”‚   ā”œā”€ā”€ config
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ config.js
ā”‚   ā”‚   ā”œā”€ā”€ migrations
ā”‚   ā”‚   ā””ā”€ā”€ seeders

Example modules generators typeorm

ā”œā”€ā”€ ./app
ā”‚   ā””ā”€ā”€ main.ts
ā”‚   ā””ā”€ā”€ app.module.ts
ā”‚   ā”œā”€ā”€ modules
ā”‚   ā”‚   ā”œā”€ā”€ users
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ dtos
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.dto.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ entities
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.entity.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ interface
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.interface.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ models
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.model.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ rest
ā”‚   ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.json
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.service.ts
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ user.module.ts
ā”‚   ā”‚   ā”‚   ā””ā”€ā”€ user.controller.ts
ā”‚   ā”œā”€ā”€ config
ā”‚   ā”‚   ā”œā”€ā”€ config.typeorm.module.ts
ā”‚   ā”‚   ā”œā”€ā”€ database.typeorm.config.ts
ā”‚   ā”‚   ā””ā”€ā”€ migrations.typeorm.config.ts
ā”‚   ā”œā”€ā”€ typeorm
ā”‚   ā”‚   ā””ā”€ā”€ migrations

File configuration .env

 TYPE_CONNECTION=mysql
 DATABASE_PORT=3306
 DATABASE_HOST=localhost
 DATABASE_USER=root
 DATABASE_PASSWORD=admin
 DATABASE=payment_backoffice

# generate orm is sequelize, add variables
 DATABASE_DIALECT=mysql
 DATABASE_SCHEMA=public
 DATABASE_LOGGING=true
 NODE_ENV=development

 #generate auth is true, add variable
 JWT_SECRET='mysecret'
1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.10

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago