Skip to content

Commit

Permalink
Update Lib (Untested Changes) (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueleite42 authored Oct 26, 2021
1 parent 24a06a3 commit ba3fbee
Show file tree
Hide file tree
Showing 24 changed files with 519 additions and 92 deletions.
37 changes: 19 additions & 18 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
".prettierignore": "ignore",
".lintstagedrc": "json",
".env.*": "env",
"src/templates/*": "plaintext"
"src/templates/**": "plaintext"
},
"material-icon-theme.languages.associations": {
"env": "tune"
Expand Down Expand Up @@ -108,30 +108,31 @@

// Spell Checker
"cSpell.words": [
"techmmunity",
"nestjs",
"vercel",
"sonarjs",
"tsbuildinfo",
"prebuild",
"nodemodules",
"dotenv",
"conta",
"Diozin",
"dotenv",
"fastify",
"henriqueleite",
"injectables",
"jitterbit",
"jwks",
"webfactory",
"Khudoiberdiev",
"nestjs",
"nodemodules",
"nosql",
"Oláh",
"plugable",
"prebuild",
"sonarjs",
"symb",
"techmmunity",
"transpiled",
"Diozin",
"Zaetic",
"nosql",
"tsbuildinfo",
"typeorm",
"jitterbit",
"henriqueleite",
"Khudoiberdiev",
"Oláh",
"Umed",
"upsert"
"upsert",
"vercel",
"webfactory",
"Zaetic"
]
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@techmmunity/symbiosis-cli",
"version": "0.0.1",
"version": "0.0.2",
"main": "index.js",
"types": "index.d.ts",
"license": "Apache-2.0",
"author": "Techmmunity",
"description": "CLI for @techmmunity/symbiosis",
"homepage": "https://github.com/techmmunity/symbiosis-cli#readme",
"bin": {
"symb": "cli/bin/index.js"
"symb": "./cli/bin/index.js"
},
"bugs": {
"url": "https://github.com/techmmunity/symbiosis-cli/issues"
Expand All @@ -28,17 +28,17 @@
"techmmunity-symbiosis"
],
"peerDependencies": {
"@techmmunity/symbiosis": "^0.0.20",
"@techmmunity/symbiosis": "^0.0.21",
"reflect-metadata": "^0.1.13"
},
"dependencies": {
"@techmmunity/utils": "^1.3.0",
"@techmmunity/utils": "^1.4.0",
"chalk": "^4.1.2",
"commander": "^8.3.0"
},
"devDependencies": {
"@techmmunity/eslint-config": "^5.0.4",
"@techmmunity/symbiosis": "^0.0.20",
"@techmmunity/symbiosis": "^0.0.21",
"@types/jest": "^27.0.1",
"eslint": "^7.32.0",
"husky": "^7.0.4",
Expand All @@ -56,7 +56,7 @@
"dev": "env-cmd ts-node src/index.ts",
"test": "jest --passWithNoTests",
"test:cov": "jest --coverage --passWithNoTests",
"build": "rimraf ./dist && tsc && rimraf ./dist/tests",
"build": "rimraf ./dist && tsc --build --force && rimraf ./dist/tests",
"build:ci": "tsc",
"format": "eslint . --fix --quiet",
"lint": "eslint . --quiet",
Expand Down
117 changes: 117 additions & 0 deletions src/cli/commands/gen-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* eslint-disable @typescript-eslint/naming-convention */

import { existsSync, writeFileSync } from "fs";

import { getConfigFile } from "../utils/get-config-file";
import { getRootPath } from "../utils/get-root-path";
import { Logger } from "../../utils/logger";

import { getMigrationsPath } from "../utils/get-migrations-path";
import { MigrationGenerator } from "../../lib/migration-generator";
import { Plugin } from "./types/plugin";
import { getTemplate } from "../utils/get-template";
import { getSymbVersion } from "../utils/get-symb-version";

export const genMigrations = () => {
const migrationsPath = getMigrationsPath();

const { connectionConfig, plugin } = getConfigFile();

if (!existsSync(getRootPath(`node_modules/${plugin}`))) {
Logger.error(`Plugin not found: ${plugin}`);

process.exit(1);
}

const { Connection } = require(plugin) as Plugin;

const connection = new Connection(connectionConfig);

const template = getTemplate("migration:generate");
const symbVersion = getSymbVersion();

connectionConfig.entities.forEach(entity => {
const entityMetadata = connection.entityManager.getEntityMetadata(entity);

const migration = new MigrationGenerator();

/**
* Entity
*/

migration.createEntity({
databaseName: entityMetadata.databaseName,
extras: entityMetadata.extras,
});

/**
* Enums
*/

entityMetadata.columns
.filter(col => Boolean(col.enumValues))
.forEach(col => {
migration.createEnum({
entityDatabaseName: entityMetadata.databaseName,
columnDatabaseName: col.databaseName,
enumName: col.enumName!,
enumValues: col.enumValues!,
extras: col.extras,
});
});

/**
* Columns
*/

entityMetadata.columns.forEach(col => {
migration.createColumn({
entityDatabaseName: entityMetadata.databaseName,
columnDatabaseName: col.databaseName,
enumName: col.enumName,
primary: col.primary,
comment: col.comment,
type: col.databaseType,
extras: col.extras,
});
});

/**
* Indexes
*/

entityMetadata.indexes?.forEach(idx => {
const columnsDatabaseNames = connection.entityManager.convertColumnsNames(
{
entity,
columnsNames: idx.columns.map(col => col.name),
},
);

const extras = Object.fromEntries(
idx.columns.map(col => [col.name, col.extras]),
);

migration.createIndex({
entityDatabaseName: entityMetadata.databaseName,
indexDatabaseName: idx.databaseName,
columnsDatabaseNames,
extras,
});
});

const code = `${Date.now()}-${entityMetadata.name}`;

const { up, down } = migration.format();

const formattedTemplate = template
.replace(/\$\{CODE\}/g, code)
.replace(/\$\{DESCRIPTION\}/g, `${entityMetadata.name} Migration`)
.replace(/\$\{PLUGIN\}/g, plugin)
.replace(/\$\{SYMB_VERSION\}/g, symbVersion)
.replace(/\$\{MIGRATION_UP_LOGIC\}/g, up)
.replace(/\$\{MIGRATION_DOWN_LOGIC\}/g, down);

writeFileSync(`${migrationsPath}/${code}.ts`, formattedTemplate);
});
};
8 changes: 8 additions & 0 deletions src/cli/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Command } from "commander";
import { Logger } from "../../utils/logger";

import { genConfig } from "./gen-config";
import { genMigrations } from "./gen-migration";
import { createMigration } from "./migration-create";

const handleInvalidCommand = (program: Command) => {
Expand Down Expand Up @@ -34,5 +35,12 @@ export const loadCommands = (program: Command) => {

/** ------- */

program
.command("migration:generate")
.description("Generate migrations.")
.action(() => genMigrations());

/** ------- */

handleInvalidCommand(program);
};
6 changes: 5 additions & 1 deletion src/cli/commands/migration-create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { writeFileSync } from "fs";
import { getTemplate } from "../utils/get-template";
import { getMigrationsPath } from "../utils/get-migrations-path";
import { getSymbVersion } from "../utils/get-symb-version";

interface Args {
description: string;
Expand All @@ -11,10 +12,13 @@ export const createMigration = ({ description }: Args) => {

const template = getTemplate("migration:create");
const code = Date.now().toString();
const symbVersion = getSymbVersion();

const formattedTemplate = template
.replace(/\$\{CODE\}/g, code)
.replace(/\$\{DESCRIPTION\}/g, description);
.replace(/\$\{DESCRIPTION\}/g, description)
.replace(/\$\{PLUGIN\}/g, "MANUAL")
.replace(/\$\{SYMB_VERSION\}/g, symbVersion);

writeFileSync(`${migrationsDirPath}/${code}.ts`, formattedTemplate);
};
21 changes: 4 additions & 17 deletions src/cli/commands/run-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
/* eslint-disable @typescript-eslint/naming-convention */

import { existsSync, readdirSync } from "fs";
import { BaseConnection, BaseConnectionOptions } from "@techmmunity/symbiosis";
import { isEmptyArray } from "@techmmunity/utils";

import { getConfigFile } from "../utils/get-config-file";
import { getRootPath } from "../utils/get-root-path";
import { Logger } from "../../utils/logger";

import { SyncManager as SyncManagerType } from "../../lib/sync-manager";
import { QueryRunner as QueryRunnerType } from "../../lib/query-runner";
import { BaseQueryRunner } from "../../lib/query-runner";
import { getMigrationsPath } from "../utils/get-migrations-path";

interface Plugin {
Connection: {
new (connectionConfig: BaseConnectionOptions): BaseConnection;
};
QueryRunner: {
new (connection: any): QueryRunnerType;
};
SyncManager: {
new (connection: any): SyncManagerType;
};
}
import { Plugin } from "./types/plugin";

interface MigrationFile {
Migration: {
new (): {
up: (queryRunner: QueryRunnerType) => Promise<void>;
down: (queryRunner: QueryRunnerType) => Promise<void>;
up: (queryRunner: BaseQueryRunner) => Promise<void>;
down: (queryRunner: BaseQueryRunner) => Promise<void>;
};
};
}
Expand Down
16 changes: 16 additions & 0 deletions src/cli/commands/types/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable @typescript-eslint/naming-convention */

import { BaseConnectionOptions, BaseConnection } from "@techmmunity/symbiosis";
import { BaseQueryRunner, BaseSyncManager } from "../../..";

export interface Plugin {
Connection: {
new (connectionConfig: BaseConnectionOptions): BaseConnection;
};
QueryRunner: {
new (connection: any): BaseQueryRunner;
};
SyncManager: {
new (connection: any): BaseSyncManager;
};
}
3 changes: 3 additions & 0 deletions src/cli/templates/migration:create
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Migration ${CODE}
*
* ${DESCRIPTION}
*
* @plugin ${PLUGIN}
* @symbVersion ${SYMB_VERSION}
*/
import { BaseMigration, QueryRunner } from "@techmmunity/symbiosis-cli";

Expand Down
22 changes: 22 additions & 0 deletions src/cli/templates/migration:generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Migration ${CODE}
*
* ${DESCRIPTION}
*
* @plugin ${PLUGIN}
* @symbVersion ${SYMB_VERSION}
*/
import { BaseMigration, QueryRunner } from "@techmmunity/symbiosis-cli";

export class Migration implements BaseMigration {
public code = "${CODE}";
public description = "${DESCRIPTION}";

public async up(queryRunner: QueryRunner) {
${MIGRATION_UP_LOGIC}
}

public async down(queryRunner: QueryRunner) {
${MIGRATION_DOWN_LOGIC}
}
}
2 changes: 1 addition & 1 deletion src/cli/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { BaseConnectionOptions } from "@techmmunity/symbiosis";
export interface ConfigFile<Opts = BaseConnectionOptions> {
plugin: string;
connectionConfig: Opts;
migrationsDir?: string;
migrationsDir: string;
}
Loading

0 comments on commit ba3fbee

Please sign in to comment.