Skip to content

Commit

Permalink
Merge pull request #9 from pagopa/IOPLT-368_refactor_enrichment_ds_model
Browse files Browse the repository at this point in the history
[#IOPLT-368] Refactor DB Enrichment Datasource
  • Loading branch information
AleDore authored Mar 13, 2024
2 parents a93b7ad + 8a2e445 commit eadbae3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 19 deletions.
15 changes: 6 additions & 9 deletions src/types/__tests__/enrichment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ describe("DataEnrichment", () => {

const aCosmosDBParams = {
connectionString: "cosmos_db_connection_string",
query: "SELECT * FROM container",
partitionKey: "pk",
id: "id",
dbResourceKeyFieldName: "foo",
dbResourceName: "container",
queryType: "FIND_BY_KEY",
streamKeyFieldName: "foo"
};

const aMongoDBParams = {
connectionString: "mongo_db_connection_string",
query: "db.collection.find({})",
id: "id",
...aCosmosDBParams
};

const aPostgresDBParams = {
connectionString: "postgres_db_connection_string",
query: "SELECT * FROM table",
id: "id",
...aCosmosDBParams
};

const anAPIParams = {
Expand Down
100 changes: 90 additions & 10 deletions src/types/enrichment/enrichment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,108 @@ export const API = t.type({

const CommonDBParams = t.type({
connectionString: NonEmptyString,
id: NonEmptyString,
query: NonEmptyString,
dbResourceName: NonEmptyString,
});

const CosmosDBParams = t.intersection([
CommonDBParams,
export const CommonFindDbParams = t.intersection([
t.type({
dbResourceKeyFieldName: NonEmptyString,
streamKeyFieldName: NonEmptyString,
}),
t.partial({
selectFields: t.readonlyArray(NonEmptyString),
}),
]);

export type CommonFindDbParams = t.TypeOf<typeof CommonFindDbParams>;

export const RelationalModelFindByKeyParams = t.intersection([
CommonFindDbParams,
t.type({
queryType: t.literal("FIND_BY_KEY"),
}),
]);
export type RelationalModelFindByKeyParams = t.TypeOf<
typeof RelationalModelFindByKeyParams
>;

export const DocumentModelQueryParams = t.intersection([
CommonFindDbParams,
t.partial({
dbResourcePkFieldName: NonEmptyString,
streamPkFieldName: NonEmptyString,
}),
]);

export type DocumentModelQueryParams = t.TypeOf<
typeof DocumentModelQueryParams
>;

export const DocumentModelFindByKeyQueryParams = t.intersection([
DocumentModelQueryParams,
t.type({
partitionKey: NonEmptyString,
queryType: t.literal("FIND_BY_KEY"),
}),
]);

const DB = t.union([
t.type({ type: t.literal("CosmosDB"), params: CosmosDBParams }),
t.type({ type: t.literal("MongoDB"), params: CommonDBParams }),
t.type({ type: t.literal("PosgresDB"), params: CommonDBParams }),
export type DocumentModelFindByKeyQueryParams = t.TypeOf<
typeof DocumentModelFindByKeyQueryParams
>;

export const DocumentModelVersionedQueryParams = t.intersection([
DocumentModelQueryParams,
t.type({
queryType: t.literal("FIND_LAST_VERSION"),
dbResourceVersionFieldName: NonEmptyString,
}),
]);

export type DocumentModelVersionedQueryParams = t.TypeOf<
typeof DocumentModelVersionedQueryParams
>;

export const RelationalModelVersionedQueryParams = t.intersection([
CommonFindDbParams,
t.type({
queryType: t.literal("FIND_LAST_VERSION"),
dbResourceVersionFieldName: NonEmptyString,
}),
]);
export type RelationalModelVersionedQueryParams = t.TypeOf<
typeof RelationalModelVersionedQueryParams
>;

export const DocumentDBParams = t.intersection([
CommonDBParams,
t.union([
DocumentModelFindByKeyQueryParams,
DocumentModelVersionedQueryParams,
]),
]);

export type DocumentDBParams = t.TypeOf<typeof DocumentDBParams>;

export const RelationalDBParams = t.intersection([
CommonDBParams,
t.union([
RelationalModelFindByKeyParams,
RelationalModelVersionedQueryParams,
]),
]);
export type RelationalDBParams = t.TypeOf<typeof RelationalDBParams>;

export const DBEnrichmentDataSource = t.union([
t.type({ type: t.literal("CosmosDB"), params: DocumentDBParams }),
t.type({ type: t.literal("MongoDB"), params: DocumentDBParams }),
t.type({ type: t.literal("PosgresDB"), params: RelationalDBParams }),
]);
export type DBEnrichmentDataSource = t.TypeOf<typeof DBEnrichmentDataSource>;

export const EnrichmentDataSource = t.union([
BlobStorage,
TableStorage,
API,
DB,
DBEnrichmentDataSource,
]);

export type EnrichmentDataSource = t.TypeOf<typeof EnrichmentDataSource>;

0 comments on commit eadbae3

Please sign in to comment.