# template-generator **Repository Path**: dongjob/template-generator ## Basic Information - **Project Name**: template-generator - **Description**: template-generator - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-01 - **Last Updated**: 2026-02-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Template Generator A powerful code generation tool that automatically creates Java code templates from SQL CREATE TABLE statements, following Google Java Style Guide conventions. ## Features - **SQL Parsing**: Automatically parses SQL CREATE TABLE statements to extract fields and table names - **Dynamic Code Generation**: Generates code based on the parsed SQL schema - **Google Java Style Guide**: Follows Google's Java coding conventions - **Flexible Configuration**: Customizable package names, entity names, and output paths - **Comprehensive Templates**: Generates controllers, services, mappers, persistence classes, and more - **Dynamic Table Names**: Extracts table names from SQL statements for use in generated code - **Smart Field Handling**: Converts snake_case SQL fields to camelCase Java fields - **Base Class Integration**: Excludes fields already defined in base classes ## Installation 1. **Clone the repository**: ```bash git clone https://gitee.com/dongjob/template-generator.git cd template-generator ``` 2. **Install dependencies**: ```bash npm install ``` ## Configuration Create a `config.json` file in the project root with the following structure: ```json { "packageName": "com.example.project", "entityName": "DataSource", "path": "internal.product", "sqlFile": "schema.sql", "templates": [ { "template": "controller.ejs" }, { "template": "adminRequest.ejs" }, { "template": "keyRequest.ejs" }, { "template": "response.ejs" }, { "template": "service.ejs" }, { "template": "serviceInterface.ejs" }, { "template": "outputService.ejs" }, { "template": "cacheService.ejs" }, { "template": "tbaServiceInterface.ejs" }, { "template": "tbaServiceImpl.ejs" }, { "template": "tbaoServiceInterface.ejs" }, { "template": "tbaoServiceImpl.ejs" }, { "template": "tbsServiceInterface.ejs" }, { "template": "tbsServiceImpl.ejs" }, { "template": "dataMapper.ejs" }, { "template": "tbaMapper.ejs" }, { "template": "tbaoMapper.ejs" }, { "template": "tbsMapper.ejs" }, { "template": "activePersistence.ejs" }, { "template": "requestPersistence.ejs" }, { "template": "snapshotPersistence.ejs" }, { "template": "pricingModel.ejs" } ] } ``` ### Configuration Options - `packageName`: The base package name for generated code - `entityName`: The name of the entity (used to generate class names) - `path`: The subpackage path (e.g., "internal.product") - `sqlFile`: Path to the SQL file containing CREATE TABLE statements - `templates`: List of templates to generate ## Usage 1. **Prepare your SQL file** (`schema.sql`) with CREATE TABLE statements: ```sql CREATE TABLE tba_data_source ( data_source_id VARCHAR(64) NOT NULL, data_type VARCHAR(32) NOT NULL, source VARCHAR(64) NOT NULL, data_id VARCHAR(64) NOT NULL, is_chain_ric VARCHAR(1) NOT NULL, filename_pattern VARCHAR(256) NOT NULL, business_date DATE NOT NULL, maker VARCHAR(32) NOT NULL, checker VARCHAR(32), data_source VARCHAR(32) NOT NULL, countersign_request_id VARCHAR(64), created_by VARCHAR(32) NOT NULL, created_time TIMESTAMP NOT NULL, updated_by VARCHAR(32) NOT NULL, updated_time TIMESTAMP NOT NULL, activation_type VARCHAR(1) NOT NULL, activation_date DATE, schedule_time TIMESTAMP, record_status VARCHAR(1) NOT NULL, version INTEGER NOT NULL, PRIMARY KEY (data_source_id, business_date) ); ``` 2. **Run the generator**: ```bash node cli.js ./config.json ``` 3. **Generated files** will be created in the `output` directory. ## Template Structure The generator uses EJS templates to generate code. Templates are located in the `templates` directory: - **Controller Templates**: - `controller.ejs` - REST controller - **Request/Response Templates**: - `adminRequest.ejs` - Admin request model - `keyRequest.ejs` - Key request model - `response.ejs` - Response model - **Service Templates**: - `service.ejs` - Service implementation - `serviceInterface.ejs` - Service interface - `outputService.ejs` - Output service implementation - `cacheService.ejs` - Cache service implementation - **Persistence Templates**: - `activePersistence.ejs` - Active persistence class (tba_*) - `requestPersistence.ejs` - Request persistence class (tbao_*) - `snapshotPersistence.ejs` - Snapshot persistence class (tbs_*) - **Mapper Templates**: - `tbaMapper.ejs` - Active data mapper - `tbaoMapper.ejs` - Request data mapper - `tbsMapper.ejs` - Snapshot data mapper - `dataMapper.ejs` - MapStruct data mapper - **Additional Templates**: - `pricingModel.ejs` - Pricing model class - `tbaServiceInterface.ejs` - Active service interface - `tbaServiceImpl.ejs` - Active service implementation - `tbaoServiceInterface.ejs` - Request service interface - `tbaoServiceImpl.ejs` - Request service implementation - `tbsServiceInterface.ejs` - Snapshot service interface - `tbsServiceImpl.ejs` - Snapshot service implementation ## SQL Parsing The generator uses two SQL parsing methods: 1. **pg-query-parser** (preferred): Uses the PostgreSQL query parser for accurate parsing 2. **Regex Fallback**: Uses regex patterns to parse SQL when pg-query-parser is not available ### Parsing Features - Extracts table names from CREATE TABLE statements - Extracts column definitions with data types and constraints - Identifies primary key fields - Converts snake_case field names to camelCase - Determines field sizes based on data types ## Generated Files Structure ``` output/ ├── com/ │ └── example/ │ └── project/ │ ├── controller/ │ │ └── DataSourceController.java │ ├── model/ │ │ └── internal/ │ │ └── product/ │ │ ├── request/ │ │ │ ├── AdminDataSourceRequest.java │ │ │ └── DataSourceKeyRequest.java │ │ ├── response/ │ │ │ └── AdminDataSourceResponse.java │ │ ├── persistence/ │ │ │ ├── TbaDataSourcePersistence.java │ │ │ ├── TbaoDataSourcePersistence.java │ │ │ └── TbsDataSourcePersistence.java │ │ └── mapper/ │ │ └── DataSourceDataMapper.java │ ├── service/ │ │ └── internal/ │ │ └── product/ │ │ ├── DataSourceService.java │ │ ├── TbaDataSourceService.java │ │ ├── TbaoDataSourceService.java │ │ ├── TbsDataSourceService.java │ │ └── impl/ │ │ ├── DataSourceServiceImpl.java │ │ ├── DataSourceCacheServiceImpl.java │ │ ├── DataSourceOutputServiceImpl.java │ │ ├── TbaDataSourceServiceImpl.java │ │ ├── TbaoDataSourceServiceImpl.java │ │ └── TbsDataSourceServiceImpl.java │ └── repository/ │ └── mapper/ │ ├── internal/ │ │ ├── TbaDataSourceMapper.java │ │ └── TbaoDataSourceMapper.java │ └── snapshot/ │ └── TbsDataSourceMapper.java └── base/ └── model/ └── refdata/ └── product/ └── PricingDataSource.java ``` ## Output Path Calculation The generator automatically calculates output paths based on the package declaration in each template. For example: - Template with `package com.example.project.service;` will be generated to `output/com/example/project/service/` - Custom output paths can be specified in the config file for individual templates ## Example ### Input SQL ```sql CREATE TABLE tba_user ( user_id VARCHAR(64) NOT NULL, username VARCHAR(32) NOT NULL, email VARCHAR(256) NOT NULL, business_date DATE NOT NULL, PRIMARY KEY (user_id, business_date) ); ``` ### Config ```json { "packageName": "com.example.project", "entityName": "User", "path": "internal.core", "sqlFile": "user_schema.sql", "templates": [ {"template": "activePersistence.ejs"}, {"template": "service.ejs"}, {"template": "tbaMapper.ejs"} ] } ``` ### Generated Files #### TbaUserPersistence.java ```java package com.example.project.model.internal.core.persistence; import com.baomidou.mybatisplus.annotation.TableName; import com.example.project.model.common.base.persistence.ActiveReferenceData; import com.example.project.config.annotation.CompositeKey; import lombok.Getter; import lombok.Setter; import lombok.ToString; @TableName("tba_user") @Getter @Setter @ToString public class TbaUserPersistence extends ActiveReferenceData { @CompositeKey private String userId; private String username; private String email; } ``` ## Technical Details ### Field Exclusion The generator excludes fields that are already defined in base classes: - `maker` - `checker` - `dataSource` - `countersignRequestId` - `createdBy` - `createdTime` - `updatedBy` - `updatedTime` - `activationType` - `activationDate` - `scheduleTime` - `recordStatus` - `version` - `withdrawRequestId` - `referenceDataSnapshotId` - `businessDate` ### SQL Parsing Fallback If the pg-query-parser package is not available, the generator falls back to regex-based parsing. This ensures the tool works even without PostgreSQL dependencies. ### Template Variables Templates can use the following variables: - `${packageName}` - Base package name - `${entityName}` - Entity name - `${path}` - Subpackage path - `${fields}` - Generated fields string - `${keyFields}` - Generated key fields string - `${persistenceFields}` - Generated persistence fields string - `${pricingFields}` - Generated pricing fields string - `${mapperFieldsString}` - Generated snake_case fields for SQL - `${activeTableName}` - Table name for active data (tba_*) - `${requestTableName}` - Table name for request data (tbao_*) - `${snapshotTableName}` - Table name for snapshot data (tbs_*) ## Contributing Contributions are welcome! Please feel free to submit a Pull Request. ## License MIT License