feat(repo): add @tools/typeorm:tomg generator

This commit is contained in:
Francesco Spilla 2025-02-05 22:17:45 +01:00
parent e95117f3a9
commit 6526a42f46
22 changed files with 12080 additions and 58 deletions

View File

@ -1,24 +1,17 @@
{
"name": "@insiemesalute-3p/source",
"version": "0.0.0",
"private": true,
"license": "MIT",
"scripts": {
"start": "nx serve",
"build": "nx build",
"test": "nx test"
},
"nx": {
"includedScripts": []
},
"dependencies": {
"@nestjs/common": "^10.0.2",
"@nestjs/core": "^10.0.2",
"@nestjs/platform-express": "^10.0.2",
"@nestjs/typeorm": "^11.0.0",
"@nx/devkit": "20.4.0",
"axios": "^1.6.0",
"mssql": "^10.0.4",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0"
"rxjs": "^7.8.0",
"typeorm": "^0.3.20"
},
"devDependencies": {
"@eslint/js": "^9.8.0",
@ -42,15 +35,31 @@
"@types/node": "~18.16.9",
"eslint": "^9.8.0",
"eslint-config-prettier": "^9.0.0",
"execa": "5.1.1",
"jest": "^29.7.0",
"jest-environment-node": "^29.7.0",
"nx": "20.4.0",
"patch-package": "^8.0.0",
"prettier": "^2.6.2",
"syncpack": "^13.0.2",
"ts-jest": "^29.1.0",
"ts-node": "10.9.1",
"tslib": "^2.3.0",
"typeorm-model-generator": "^0.4.6",
"typescript": "~5.7.2",
"typescript-eslint": "^8.19.0",
"webpack-cli": "^5.1.4"
},
"license": "MIT",
"nx": {
"includedScripts": []
},
"private": true,
"scripts": {
"build": "nx build",
"g": "nx g",
"postinstall": "patch-package",
"start": "nx serve",
"test": "nx test"
}
}

File diff suppressed because one or more lines are too long

2979
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@ import {
import { LibGeneratorSchema } from './schema';
import { libraryGenerator } from '@nx/nest';
import path from 'path';
import path from 'node:path';
export async function libGenerator(tree: Tree, options: LibGeneratorSchema) {
const opts: Parameters<(typeof libraryGenerator)>[1] = {

11
tools/typeorm/README.md Normal file
View File

@ -0,0 +1,11 @@
# typeorm
This library was generated with [Nx](https://nx.dev).
## Building
Run `nx build typeorm` to build the library.
## Running unit tests
Run `nx test typeorm` to execute the unit tests via [Jest](https://jestjs.io).

View File

@ -0,0 +1,34 @@
import baseConfig from '../../eslint.base.config.mjs';
export default [
...baseConfig,
{
files: ['**/*.json'],
rules: {
'@nx/dependency-checks': [
'error',
{
ignoredFiles: ['{projectRoot}/eslint.config.{js,cjs,mjs}'],
},
],
},
languageOptions: {
parser: await import('jsonc-eslint-parser'),
},
},
{
files: [
'**/package.json',
'**/executors.json',
'**/package.json',
'**/generators.json',
'**/executors.json',
],
rules: {
'@nx/nx-plugin-checks': 'error',
},
languageOptions: {
parser: await import('jsonc-eslint-parser'),
},
},
];

View File

@ -0,0 +1,16 @@
{
"generators": {
"typeorm-model-generator": {
"factory": "./src/generators/typeorm-model-generator/typeorm-model-generator",
"schema": "./src/generators/typeorm-model-generator/schema.json",
"aliases": ["tomg"],
"description": "Creates typeorm entities via typeorm-model-generator."
},
"typeorm-model-generator-scaffold": {
"factory": "./src/generators/typeorm-model-generator-scaffold/typeorm-model-generator-scaffold",
"schema": "./src/generators/typeorm-model-generator-scaffold/schema.json",
"aliases": ["tomg-scaffold"],
"description": "Scaffolds a project with typeorm-model-generator config files."
}
}
}

View File

@ -0,0 +1,10 @@
export default {
displayName: 'typeorm',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/tools/typeorm',
};

View File

@ -0,0 +1,13 @@
{
"name": "@tools/typeorm",
"version": "0.0.1",
"private": true,
"type": "commonjs",
"main": "./src/index.js",
"types": "./src/index.d.ts",
"dependencies": {
"@nx/devkit": "20.4.0",
"tslib": "^2.3.0"
},
"generators": "./generators.json"
}

View File

@ -0,0 +1,41 @@
{
"name": "typeorm",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "tools/typeorm/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/tools/typeorm",
"main": "tools/typeorm/src/index.ts",
"tsConfig": "tools/typeorm/tsconfig.lib.json",
"assets": [
"tools/typeorm/*.md",
{
"input": "./tools/typeorm/src",
"glob": "**/!(*.ts)",
"output": "./src"
},
{
"input": "./tools/typeorm/src",
"glob": "**/*.d.ts",
"output": "./src"
},
{
"input": "./tools/typeorm",
"glob": "generators.json",
"output": "."
},
{
"input": "./tools/typeorm",
"glob": "executors.json",
"output": "."
}
]
}
}
}
}

View File

@ -0,0 +1,22 @@
[
{
"pluralizeNames": true,
"noConfigs": true,
"convertCaseFile": "none",
"convertCaseEntity": "pascal",
"convertCaseProperty": "camel",
"convertEol": "LF",
"propertyVisibility": "none",
"lazy": false,
"activeRecord": false,
"generateConstructor": true,
"customNamingStrategyPath": ".tomg-naming-strategy.js",
"relationIds": false,
"strictMode": "none",
"skipSchema": true,
"indexFile": false,
"exportType": "named",
"skipNonPrimaryKeyIndexes": true,
"removeColumnsInRelation": false
}
]

View File

@ -0,0 +1,39 @@
/**
* @typedef {import('typeorm-model-generator').Column} Column
* @typedef {import('typeorm-model-generator').Entity} Entity
*/
/**
* Customizes the entity name.
* @param {string} oldEntityName - The default entity name.
* @param {Entity} entity - The entity.
* @returns {string} The new entity name.
*/
function entityName(oldEntityName, entity) {
return oldEntityName + 'Entity';
}
/**
* Customizes the column name.
* @param {string} oldColumnName - The default column name.
* @param {Column} column - The column.
* @returns {string} The new column name.
*/
function columnName(oldColumnName, column) {
return oldColumnName;
}
/**
* Customizes the file name.
* @param {string} oldFileName - The default file name.
* @returns {string} The new file name.
*/
function fileName(oldFileName) {
return oldFileName.replace('Entity', '.entity');
}
module.exports = {
entityName,
columnName,
fileName
};

View File

@ -0,0 +1,3 @@
export interface TomgGeneratorSchema {
project: string;
}

View File

@ -0,0 +1,17 @@
{
"$schema": "https://json-schema.org/schema",
"$id": "typeorm-model-generator-scaffold",
"title": "",
"description": "Scaffolds a project with typeorm-model-generator config files.",
"type": "object",
"properties": {
"project": {
"description": "The project to scaffold.",
"type": "string",
"alias": "p",
"x-prompt": "Which project to scaffold?",
"x-dropdown": "projects"
}
},
"required": []
}

View File

@ -0,0 +1,29 @@
import {
formatFiles,
generateFiles,
joinPathFragments,
OverwriteStrategy,
readProjectConfiguration,
Tree,
} from '@nx/devkit';
import { TomgGeneratorSchema } from './schema';
export async function tomgScaffoldGenerator(
tree: Tree,
options: TomgGeneratorSchema
) {
const libraryRoot = readProjectConfiguration(tree, options.project).root;
generateFiles(
tree,
joinPathFragments(__dirname, './files'),
libraryRoot,
options,
{ overwriteStrategy: OverwriteStrategy.KeepExisting }
);
await formatFiles(tree);
}
export default tomgScaffoldGenerator;

View File

@ -0,0 +1,10 @@
export interface TomgGeneratorSchema {
driver: string;
host: string;
username: string;
password: string;
database: string;
project: string;
directory: string;
quiet: boolean;
}

View File

@ -0,0 +1,68 @@
{
"$schema": "https://json-schema.org/schema",
"$id": "typeorm-model-generator",
"title": "Creates typeorm entities via typeorm-model-generator.",
"description": "Creates typeorm entities via typeorm-model-generator.",
"type": "object",
"examples": [
{
"command": "nx g tomg --host 192.168.0.0 --database database -u sa -x pa$$w0rd -e mssql -q",
"description": "Generate typeorm entities for the database identified by the specified connection, in quiet mode"
}
],
"properties": {
"project": {
"description": "The project in which to place the entities.",
"type": "string",
"alias": "p",
"x-prompt": "Which project to scaffold?",
"x-dropdown": "projects"
},
"directory": {
"description": "A directory where the entities are placed.",
"type": "string",
"alias": "dir",
"x-prompt": "Which directory do you want to create the entities in?",
"default": "typeorm-model-generator-output",
"x-priority": "important"
},
"driver": {
"description": "The database driver that should be used by the generator.",
"type": "string",
"alias": "e",
"x-prompt": "Database driver?",
"default": "mssql"
},
"database": {
"description": "The name of the database whose tables should be scaffolded.",
"type": "string",
"x-prompt": "Database name?"
},
"host": {
"description": "The host of the database whose tables should be scaffolded.",
"type": "string",
"x-prompt": "Database host?"
},
"username": {
"description": "The username of database whose tables should be scaffolded.",
"type": "string",
"alias": "u",
"x-prompt": "Database username?"
},
"password": {
"description": "The password of database whose tables should be scaffolded.",
"type": "string",
"alias": "x",
"x-prompt": "Database password?"
},
"quiet": {
"description": "Enables verbose logging.",
"type": "boolean",
"alias": "q",
"default": "true",
"x-priority": "internal",
"x-prompt": "Hide typeorm stdout/stderr?"
}
},
"required": []
}

View File

@ -0,0 +1,59 @@
import {
formatFiles,
generateFiles,
joinPathFragments,
OverwriteStrategy,
readProjectConfiguration,
Tree,
} from '@nx/devkit';
import execa from 'execa';
import os from 'node:os';
import fs from 'node:fs/promises';
import path from 'node:path';
import { TomgGeneratorSchema } from './schema';
export async function tomgGenerator(tree: Tree, options: TomgGeneratorSchema) {
const libraryRoot = readProjectConfiguration(tree, options.project).root;
const tmpDirPath = await fs.mkdtemp(path.join(os.tmpdir(), 'tomg-'));
const { stdout, stderr } = await execa(
`typeorm-model-generator`,
[
'-o',
tmpDirPath,
'-e',
options.driver,
'-h',
options.host,
'-d',
options.database,
'-u',
options.username,
'-x',
options.password,
],
{ cwd: libraryRoot }
);
if (!options.quiet) {
console.log(stdout);
console.error(stderr);
}
generateFiles(
tree,
tmpDirPath,
joinPathFragments(libraryRoot, options.directory),
options,
{ overwriteStrategy: OverwriteStrategy.Overwrite }
);
await formatFiles(tree);
await fs.rm(tmpDirPath, { recursive: true, force: true });
console.log();
console.log('Generation complete.');
console.log();
}
export default tomgGenerator;

View File

@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}

View File

@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}

View File

@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"moduleResolution": "node10",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}

View File

@ -15,6 +15,7 @@
"baseUrl": ".",
"paths": {
"@tools/nest": ["tools/nest/src/index.ts"],
"@tools/typeorm": ["tools/typeorm/src/index.ts"]
},
"esModuleInterop": true
},