From e9a5446db592a54bfe8cba5aa45ff92dfcd1ecb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Angel=20Gomez?= Date: Tue, 5 Nov 2024 22:16:05 -0500 Subject: [PATCH] feat: add extension to transform result --- src/Chaca.ts | 6 +- src/core/dataset/index.ts | 6 +- src/core/export/resolvers/dump.ts | 53 --------------- src/core/export/resolvers/dump/dump.ts | 68 +++++++++++++++++++ .../resolvers/dump/value-object/filename.ts | 11 +++ .../export/resolvers/{ => export}/export.ts | 22 +++--- .../generator-filter.ts | 8 +-- src/core/export/value-object/name.ts | 2 +- src/core/schema/index.ts | 6 +- src/index.ts | 2 +- test/library/extensions/postgresql.test.ts | 21 +++--- 11 files changed, 116 insertions(+), 89 deletions(-) delete mode 100644 src/core/export/resolvers/dump.ts create mode 100644 src/core/export/resolvers/dump/dump.ts create mode 100644 src/core/export/resolvers/dump/value-object/filename.ts rename src/core/export/resolvers/{ => export}/export.ts (66%) rename src/core/export/resolvers/{ => generator-filter}/generator-filter.ts (92%) diff --git a/src/Chaca.ts b/src/Chaca.ts index 73483fa9..ad70f4dc 100644 --- a/src/Chaca.ts +++ b/src/Chaca.ts @@ -13,7 +13,7 @@ import { import { ChacaUtils } from "./core/utils"; import { SchemaInput } from "./core/schema/interfaces/schema"; import { DatasetSchema } from "./core/dataset-resolver/interfaces/resolver"; -import { ExportResolver } from "./core/export/resolvers/export"; +import { ExportResolver } from "./core/export/resolvers/export/export"; import { DumpConfig, FileConfig } from "./core/export/interfaces/export"; import { ProbabilityOption, @@ -22,9 +22,9 @@ import { import { PickField, PickFieldProps } from "./core/fields/core/pick"; import { Dataset } from "./core/dataset"; import { DatatypeModule } from "./modules/datatype"; -import { GeneratorFilter } from "./core/export/resolvers/generator-filter"; +import { GeneratorFilter } from "./core/export/resolvers/generator-filter/generator-filter"; import { DumpFile } from "./core/export/generators/generator"; -import { DumpResolver } from "./core/export/resolvers/dump"; +import { DumpResolver } from "./core/export/resolvers/dump/dump"; export class Chaca { constructor( diff --git a/src/core/dataset/index.ts b/src/core/dataset/index.ts index 5f9f9c05..00896c92 100644 --- a/src/core/dataset/index.ts +++ b/src/core/dataset/index.ts @@ -1,11 +1,11 @@ import { DatatypeModule } from "../../modules/datatype"; import { DatasetSchema } from "../dataset-resolver/interfaces/resolver"; import { DatasetResolver } from "../dataset-resolver/resolver"; -import { ExportResolver } from "../export/resolvers/export"; +import { ExportResolver } from "../export/resolvers/export/export"; import { DumpConfig, FileConfig } from "../export/interfaces/export"; import { ChacaUtils } from "../utils"; -import { GeneratorFilter } from "../export/resolvers/generator-filter"; -import { DumpResolver } from "../export/resolvers/dump"; +import { GeneratorFilter } from "../export/resolvers/generator-filter/generator-filter"; +import { DumpResolver } from "../export/resolvers/dump/dump"; import { DumpFile } from "../export/generators/generator"; export class Dataset { diff --git a/src/core/export/resolvers/dump.ts b/src/core/export/resolvers/dump.ts deleted file mode 100644 index 39b4bc50..00000000 --- a/src/core/export/resolvers/dump.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { DatatypeModule } from "../../../modules/datatype"; -import { DatasetSchema } from "../../dataset-resolver/interfaces/resolver"; -import { DatasetResolver } from "../../dataset-resolver/resolver"; -import { ChacaUtils } from "../../utils"; -import { Filename } from "../generators/file-creator/filename"; -import { DumpFile } from "../generators/generator"; -import { DumpConfig } from "../interfaces/export"; -import { FileFormat } from "../value-object/format"; -import { FileName } from "../value-object/name"; -import { Verbose } from "../value-object/verbose"; -import { GeneratorFilter } from "./generator-filter"; - -export class DumpResolver { - private readonly format: FileFormat; - private readonly filename: FileName; - private readonly verbose: Verbose; - - constructor( - private readonly utils: ChacaUtils, - private readonly datatypeModule: DatatypeModule, - private readonly filter: GeneratorFilter, - config: DumpConfig, - ) { - this.format = new FileFormat(config.format); - this.filename = new FileName(config.filename); - this.verbose = new Verbose(config.verbose); - } - - data(data: any): DumpFile[] { - const gen = this.filter.execute(this.format.value()); - - return gen.dump({ - data: data, - filename: new Filename(this.filename.value()), - }); - } - - relational(schemas: DatasetSchema[]): DumpFile[] { - const gen = this.filter.execute(this.format.value()); - - const resolver = new DatasetResolver(this.utils, this.datatypeModule, { - schemas: schemas, - verbose: this.verbose.value(), - }); - - return gen.dumpRelational({ - resolver: resolver, - filename: new Filename(this.filename.value()), - }); - } -} - -export type { DumpFile, DumpConfig }; diff --git a/src/core/export/resolvers/dump/dump.ts b/src/core/export/resolvers/dump/dump.ts new file mode 100644 index 00000000..15129713 --- /dev/null +++ b/src/core/export/resolvers/dump/dump.ts @@ -0,0 +1,68 @@ +import { DatatypeModule } from "../../../../modules/datatype"; +import { DatasetSchema } from "../../../dataset-resolver/interfaces/resolver"; +import { DatasetResolver } from "../../../dataset-resolver/resolver"; +import { ChacaUtils } from "../../../utils"; +import { Filename } from "../../generators/file-creator/filename"; +import { DumpFile } from "../../generators/generator"; +import { DumpConfig } from "../../interfaces/export"; +import { FileFormat } from "../../value-object/format"; +import { FileName } from "../../value-object/name"; +import { Verbose } from "../../value-object/verbose"; +import { GeneratorFilter } from "../generator-filter/generator-filter"; +import { ExtensionFilename } from "./value-object/filename"; + +export class DumpResolver { + private readonly format: FileFormat; + private readonly filename: FileName; + private readonly verbose: Verbose; + + constructor( + private readonly utils: ChacaUtils, + private readonly datatypeModule: DatatypeModule, + private readonly filter: GeneratorFilter, + config: DumpConfig, + ) { + this.format = new FileFormat(config.format); + this.filename = new FileName(config.filename); + this.verbose = new Verbose(config.verbose); + } + + data(data: any): DumpFile[] { + const generator = this.filter.execute(this.format.value()); + + const result = generator.dump({ + data: data, + filename: new Filename(this.filename.value()), + }); + + return result.map((r) => { + return { + content: r.content, + filename: new ExtensionFilename(r.filename, generator.ext).value(), + }; + }); + } + + relational(schemas: DatasetSchema[]): DumpFile[] { + const generator = this.filter.execute(this.format.value()); + + const resolver = new DatasetResolver(this.utils, this.datatypeModule, { + schemas: schemas, + verbose: this.verbose.value(), + }); + + const result = generator.dumpRelational({ + resolver: resolver, + filename: new Filename(this.filename.value()), + }); + + return result.map((r) => { + return { + content: r.content, + filename: new ExtensionFilename(r.filename, generator.ext).value(), + }; + }); + } +} + +export type { DumpFile, DumpConfig }; diff --git a/src/core/export/resolvers/dump/value-object/filename.ts b/src/core/export/resolvers/dump/value-object/filename.ts new file mode 100644 index 00000000..e8f92a17 --- /dev/null +++ b/src/core/export/resolvers/dump/value-object/filename.ts @@ -0,0 +1,11 @@ +export class ExtensionFilename { + private readonly _value: string; + + constructor(name: string, ext: string) { + this._value = `${name}.${ext}`; + } + + value() { + return this._value; + } +} diff --git a/src/core/export/resolvers/export.ts b/src/core/export/resolvers/export/export.ts similarity index 66% rename from src/core/export/resolvers/export.ts rename to src/core/export/resolvers/export/export.ts index 9d4756f8..c1bba50a 100644 --- a/src/core/export/resolvers/export.ts +++ b/src/core/export/resolvers/export/export.ts @@ -1,14 +1,14 @@ -import { FileConfig } from "../interfaces/export"; -import { DatasetSchema } from "../../dataset-resolver/interfaces/resolver"; -import { DatasetResolver } from "../../dataset-resolver/resolver"; -import { FileName } from "../value-object/name"; -import { Location } from "../value-object/location"; -import { FileFormat } from "../value-object/format"; -import { Verbose } from "../value-object/verbose"; -import { ChacaUtils } from "../../utils"; -import { DatatypeModule } from "../../../modules/datatype"; -import { GeneratorFilter } from "./generator-filter"; -import { FileCreator } from "../generators/file-creator/file-creator"; +import { FileConfig } from "../../interfaces/export"; +import { DatasetSchema } from "../../../dataset-resolver/interfaces/resolver"; +import { DatasetResolver } from "../../../dataset-resolver/resolver"; +import { FileName } from "../../value-object/name"; +import { Location } from "../../value-object/location"; +import { FileFormat } from "../../value-object/format"; +import { Verbose } from "../../value-object/verbose"; +import { ChacaUtils } from "../../../utils"; +import { DatatypeModule } from "../../../../modules/datatype"; +import { GeneratorFilter } from "../generator-filter/generator-filter"; +import { FileCreator } from "../../generators/file-creator/file-creator"; export class ExportResolver { private readonly format: FileFormat; diff --git a/src/core/export/resolvers/generator-filter.ts b/src/core/export/resolvers/generator-filter/generator-filter.ts similarity index 92% rename from src/core/export/resolvers/generator-filter.ts rename to src/core/export/resolvers/generator-filter/generator-filter.ts index 761d963f..7bfb6095 100644 --- a/src/core/export/resolvers/generator-filter.ts +++ b/src/core/export/resolvers/generator-filter/generator-filter.ts @@ -1,5 +1,5 @@ -import { ChacaError } from "../../../errors"; -import { ChacaUtils } from "../../utils"; +import { ChacaError } from "../../../../errors"; +import { ChacaUtils } from "../../../utils"; import { CsvGenerator, Generator, @@ -10,8 +10,8 @@ import { SQLGenerator, TypescriptGenerator, YamlGenerator, -} from "../generators"; -import { ExportFormat } from "../interfaces/export"; +} from "../../generators"; +import { ExportFormat } from "../../interfaces/export"; export class GeneratorFilter { constructor(private readonly utils: ChacaUtils) {} diff --git a/src/core/export/value-object/name.ts b/src/core/export/value-object/name.ts index 3d904de2..3ccb0599 100644 --- a/src/core/export/value-object/name.ts +++ b/src/core/export/value-object/name.ts @@ -5,7 +5,7 @@ export class FileName { constructor(name?: string) { if (typeof name !== "string" || name.trim() === "") { - throw new ChacaError("A file name is necesary to export the data"); + throw new ChacaError("A file name is necesary to export your data"); } this._value = name; diff --git a/src/core/schema/index.ts b/src/core/schema/index.ts index d82f1956..56115453 100644 --- a/src/core/schema/index.ts +++ b/src/core/schema/index.ts @@ -1,11 +1,11 @@ -import { ExportResolver } from "../export/resolvers/export"; +import { ExportResolver } from "../export/resolvers/export/export"; import { SchemaInput } from "./interfaces/schema"; import { DumpConfig, FileConfig } from "../export/interfaces/export"; import { SchemaResolver } from "../schema-resolver"; import { ChacaUtils } from "../utils"; import { DatatypeModule } from "../../modules/datatype"; -import { GeneratorFilter } from "../export/resolvers/generator-filter"; -import { DumpResolver } from "../export/resolvers/dump"; +import { GeneratorFilter } from "../export/resolvers/generator-filter/generator-filter"; +import { DumpResolver } from "../export/resolvers/dump/dump"; import { DumpFile } from "../export/generators/generator"; import { DEFAULT_SCHEMA_NAME } from "./core/default-name"; diff --git a/src/index.ts b/src/index.ts index e7b455f9..643350d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,7 +95,7 @@ import { import { ChacaModules } from "./modules"; import { Dataset } from "./core/dataset"; -import { DumpConfig, DumpFile } from "./core/export/resolvers/dump"; +import { DumpConfig, DumpFile } from "./core/export/resolvers/dump/dump"; const utils = new ChacaUtils(); diff --git a/test/library/extensions/postgresql.test.ts b/test/library/extensions/postgresql.test.ts index b2b21686..5688b3aa 100644 --- a/test/library/extensions/postgresql.test.ts +++ b/test/library/extensions/postgresql.test.ts @@ -37,12 +37,13 @@ describe("Postgresql", () => { }), }); - expect(() => { - chaca.transform(schema.array(10), { - filename: "schema", - format: { ext: "postgresql", declarationOnly: true, keys: ["id"] }, - }); + const result = chaca.transform(schema.array(10), { + filename: "schema", + format: { ext: "postgresql", declarationOnly: true, keys: ["id"] }, }); + + expect(result).toHaveLength(1); + expect(result[0].filename).toBe("schema.sql"); }); it("keys = ['id']", () => { @@ -56,7 +57,7 @@ describe("Postgresql", () => { }); expect(result).toHaveLength(1); - expect(result[0].filename).toBe("schema"); + expect(result[0].filename).toBe("schema.sql"); expect(result[0].content).include("id INTEGER PRIMARY KEY"); }); @@ -71,7 +72,7 @@ describe("Postgresql", () => { }); expect(result).toHaveLength(1); - expect(result[0].filename).toBe("schema"); + expect(result[0].filename).toBe("schema.sql"); expect(result[0].content).include("id SERIAL PRIMARY KEY"); }); }); @@ -89,7 +90,7 @@ describe("Postgresql", () => { }); expect(result).toHaveLength(1); - expect(result[0].filename).toBe("schema"); + expect(result[0].filename).toBe("schema.sql"); expect(result[0].content).not.include("username VARCHAR(255) NOT NULL"); }); }); @@ -107,7 +108,7 @@ describe("Postgresql", () => { }); expect(result).toHaveLength(1); - expect(result[0].filename).toBe("schema"); + expect(result[0].filename).toBe("schema.sql"); expect(result[0].content).include("username VARCHAR(255) UNIQUE"); }); }); @@ -133,7 +134,7 @@ describe("Postgresql", () => { }); expect(result).toHaveLength(1); - expect(result[0].filename).toBe("schema"); + expect(result[0].filename).toBe("schema.sql"); expect(result[0].content).include( "object_id INTEGER PRIMARY KEY REFERENCES Schema(id)", );