README
crud-generator
is a tool that generates automatically a module with its entity, repository, service, conversion service, controller and main CRUD API entrypoints.
Usage
Go in the directory where you want to write the module and simply start crud-generator
. The module's name and destination path will be prompted, and the files will be generated after confirmation.
Example of the output directory:
test-module
├── test-module.controller.ts
├── test-module.conversion.service.ts
├── test-module.entity.ts
├── test-module.module.ts
├── test-module.repository.ts
└── test-module.service.ts
Programmatic usage
It's possible to not be prompted the module's name and destination path by providing them as arguments and using the --auto
flag:
# Generate module "test-module" in current directory
npm start -- test-module . --auto
How does it work
This package's source code contains a template
directory to generate the module's files at runtime.
If you want to add your own template, create a TypeScript file in this directory (with any name), and write the following inside:
import { Template } from '../template'
const tpl: Template = p => ({
fileName: 'genius.ts',
script: true,
source: `
console.log("I'm a genius!");
`
})
export default tpl
In the exported callback's returned object, fileName
is the name of the file to write and source
its content. The p
argument contains the callback's parameters, whose type is defined in src/cli/template.ts
.
Then, register the template in the exported object of src/cli/templates/index.ts
.
The related file will then be automatically generated at runtime.
Code formatting
The generated files are automatically formatted using ESLint and Prettier, by looking at the .eslintrc.json
and .prettierrc
files located in the destination path or one of its parent directories.
If no ESLint / Prettier configuration file is found, a warning is displayed to indicate no formatting will occur.
Refactoring
If a template needs to be removed or changed, it can be very simply edited in the dedicated file in src/cli/templates
.
New parameters can easily be added by extending the related type in src/cli/template.ts
.
They can also be renamed easily, as most IDEs support automatic refactoring, performing the renaming in all templates that already use this parameter.
TODO
- Handle relations correctly in default CRUD DTOs generics