Skip to content

Commit

Permalink
feat: add extension to transform result
Browse files Browse the repository at this point in the history
  • Loading branch information
Chacaponquin committed Nov 6, 2024
1 parent e0cd9ba commit e9a5446
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 89 deletions.
6 changes: 3 additions & 3 deletions src/Chaca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/core/dataset/index.ts
Original file line number Diff line number Diff line change
@@ -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<K = any> {
Expand Down
53 changes: 0 additions & 53 deletions src/core/export/resolvers/dump.ts

This file was deleted.

68 changes: 68 additions & 0 deletions src/core/export/resolvers/dump/dump.ts
Original file line number Diff line number Diff line change
@@ -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 };
11 changes: 11 additions & 0 deletions src/core/export/resolvers/dump/value-object/filename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class ExtensionFilename {
private readonly _value: string;

constructor(name: string, ext: string) {
this._value = `${name}.${ext}`;
}

value() {
return this._value;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChacaError } from "../../../errors";
import { ChacaUtils } from "../../utils";
import { ChacaError } from "../../../../errors";
import { ChacaUtils } from "../../../utils";
import {
CsvGenerator,
Generator,
Expand All @@ -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) {}
Expand Down
2 changes: 1 addition & 1 deletion src/core/export/value-object/name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/core/schema/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
21 changes: 11 additions & 10 deletions test/library/extensions/postgresql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']", () => {
Expand All @@ -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");
});

Expand All @@ -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");
});
});
Expand All @@ -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");
});
});
Expand All @@ -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");
});
});
Expand All @@ -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)",
);
Expand Down

0 comments on commit e9a5446

Please sign in to comment.