From 7346c9272732e61efe8209406693422db5ccef51 Mon Sep 17 00:00:00 2001 From: Henrik Hartiala Date: Tue, 19 Sep 2023 08:35:53 +0300 Subject: [PATCH] Add dataset insert function add functions that take datasets as parameter instead of json. These functions can be used for example from front end. - Also move the inserter functions for clearance: "json-parser" does not indicate of any db insertions. --- test/hasura/timetables-data-inserter/cli.ts | 4 +-- .../generic/data-insert.ts | 33 +++++++++++++++++++ .../generic/json-parser.ts | 19 ----------- .../generic/timetables-data-inserter.spec.ts | 2 +- .../hsl/data-insert.ts | 33 +++++++++++++++++++ .../hsl/hsl-timetables-data-inserter.spec.ts | 2 +- .../timetables-data-inserter/hsl/index.ts | 1 + .../hsl/json-parser.ts | 19 ----------- 8 files changed, 71 insertions(+), 42 deletions(-) create mode 100644 test/hasura/timetables-data-inserter/generic/data-insert.ts create mode 100644 test/hasura/timetables-data-inserter/hsl/data-insert.ts diff --git a/test/hasura/timetables-data-inserter/cli.ts b/test/hasura/timetables-data-inserter/cli.ts index 84b4e5e9..de31dd8a 100644 --- a/test/hasura/timetables-data-inserter/cli.ts +++ b/test/hasura/timetables-data-inserter/cli.ts @@ -3,8 +3,8 @@ import { program } from 'commander'; import fs from 'fs'; import { ConnectionConfig } from 'pg'; -import { insertDatasetFromJson as insertGenericDatasetFromJson } from './generic/json-parser'; -import { insertDatasetFromJson as insertHslDatasetFromJson } from './hsl/json-parser'; +import { insertDatasetFromJson as insertGenericDatasetFromJson } from './generic/data-insert'; +import { insertDatasetFromJson as insertHslDatasetFromJson } from './hsl/data-insert'; const buildDbConfig = ( options: Record, diff --git a/test/hasura/timetables-data-inserter/generic/data-insert.ts b/test/hasura/timetables-data-inserter/generic/data-insert.ts new file mode 100644 index 00000000..c2c6d926 --- /dev/null +++ b/test/hasura/timetables-data-inserter/generic/data-insert.ts @@ -0,0 +1,33 @@ +import { timetablesDbConfig } from '@config'; +import { closeDbConnection, createDbConnection } from '@util/db'; +import { setupDb } from '@util/setup'; +import { ConnectionConfig } from 'pg'; +import { buildGenericTimetablesDataset } from './dataset'; +import { parseGenericDatasetJson } from './json-parser'; +import { createGenericTableData } from './table-data'; +import { GenericTimetablesDatasetInput } from './types'; + +export const insertDatasetFromJson = async ( + input: string, + dbConfig: ConnectionConfig = timetablesDbConfig, +) => { + const result = parseGenericDatasetJson(input); + const builtDataset = buildGenericTimetablesDataset(result); + const dbConnection = createDbConnection(dbConfig); + const tableData = createGenericTableData(builtDataset); + await setupDb(dbConnection, tableData); + closeDbConnection(dbConnection); + return builtDataset; +}; + +export const insertGenericDataset = async ( + input: GenericTimetablesDatasetInput, + dbConfig: ConnectionConfig = timetablesDbConfig, +) => { + const builtDataset = buildGenericTimetablesDataset(input); + const tableData = createGenericTableData(builtDataset); + const dbConnection = createDbConnection(dbConfig); + await setupDb(dbConnection, tableData); + closeDbConnection(dbConnection); + return builtDataset; +}; diff --git a/test/hasura/timetables-data-inserter/generic/json-parser.ts b/test/hasura/timetables-data-inserter/generic/json-parser.ts index fe17dc10..40bd1bc5 100644 --- a/test/hasura/timetables-data-inserter/generic/json-parser.ts +++ b/test/hasura/timetables-data-inserter/generic/json-parser.ts @@ -1,10 +1,4 @@ -import { timetablesDbConfig } from '@config'; -import { closeDbConnection, createDbConnection } from '@util/db'; -import { setupDb } from '@util/setup'; -import { ConnectionConfig } from 'pg'; -import { buildGenericTimetablesDataset } from './dataset'; import { genericTimetablesJsonSchema } from './json-schemas'; -import { createGenericTableData } from './table-data'; import { GenericTimetablesDatasetInput } from './types'; export const parseGenericDatasetJson = ( @@ -14,16 +8,3 @@ export const parseGenericDatasetJson = ( const parsedDatasetInput = genericTimetablesJsonSchema.parse(parsedJson); return parsedDatasetInput; }; - -export const insertDatasetFromJson = async ( - input: string, - dbConfig: ConnectionConfig = timetablesDbConfig, -) => { - const result = parseGenericDatasetJson(input); - const builtDataset = buildGenericTimetablesDataset(result); - const dbConnection = createDbConnection(dbConfig); - const tableData = createGenericTableData(builtDataset); - await setupDb(dbConnection, tableData); - closeDbConnection(dbConnection); - return builtDataset; -}; diff --git a/test/hasura/timetables-data-inserter/generic/timetables-data-inserter.spec.ts b/test/hasura/timetables-data-inserter/generic/timetables-data-inserter.spec.ts index ff60b40c..9f5e116a 100644 --- a/test/hasura/timetables-data-inserter/generic/timetables-data-inserter.spec.ts +++ b/test/hasura/timetables-data-inserter/generic/timetables-data-inserter.spec.ts @@ -4,8 +4,8 @@ import { queryTable } from '@util/setup'; import { genericTimetablesDbSchema } from 'generic/timetablesdb/datasets/schema'; import { get } from 'lodash'; import { defaultDayTypeIds } from 'timetables-data-inserter/day-types'; +import { insertDatasetFromJson } from './data-insert'; import * as testDatasetJson from './example.json'; -import { insertDatasetFromJson } from './json-parser'; import { GenericTimetablesDatasetOutput, GenericVehicleScheduleFrameOutput, diff --git a/test/hasura/timetables-data-inserter/hsl/data-insert.ts b/test/hasura/timetables-data-inserter/hsl/data-insert.ts new file mode 100644 index 00000000..e1a3ba92 --- /dev/null +++ b/test/hasura/timetables-data-inserter/hsl/data-insert.ts @@ -0,0 +1,33 @@ +import { timetablesDbConfig } from '@config'; +import { closeDbConnection, createDbConnection } from '@util/db'; +import { setupDb } from '@util/setup'; +import { ConnectionConfig } from 'pg'; +import { buildHslTimetablesDataset } from './dataset'; +import { parseHslDatasetJson } from './json-parser'; +import { createHslTableData } from './table-data'; +import { HslTimetablesDatasetInput } from './types'; + +export const insertDatasetFromJson = async ( + input: string, + dbConfig: ConnectionConfig = timetablesDbConfig, +) => { + const result = parseHslDatasetJson(input); + const builtDataset = buildHslTimetablesDataset(result); + const dbConnection = createDbConnection(dbConfig); + const tableData = createHslTableData(builtDataset); + await setupDb(dbConnection, tableData); + closeDbConnection(dbConnection); + return builtDataset; +}; + +export const insertHslDataset = async ( + input: HslTimetablesDatasetInput, + dbConfig: ConnectionConfig = timetablesDbConfig, +) => { + const builtDataset = buildHslTimetablesDataset(input); + const tableData = createHslTableData(builtDataset); + const dbConnection = createDbConnection(dbConfig); + await setupDb(dbConnection, tableData); + closeDbConnection(dbConnection); + return builtDataset; +}; diff --git a/test/hasura/timetables-data-inserter/hsl/hsl-timetables-data-inserter.spec.ts b/test/hasura/timetables-data-inserter/hsl/hsl-timetables-data-inserter.spec.ts index 2db44e54..f2e0e94c 100644 --- a/test/hasura/timetables-data-inserter/hsl/hsl-timetables-data-inserter.spec.ts +++ b/test/hasura/timetables-data-inserter/hsl/hsl-timetables-data-inserter.spec.ts @@ -2,8 +2,8 @@ import * as config from '@config'; import { DbConnection, closeDbConnection, createDbConnection } from '@util/db'; import { queryTable } from '@util/setup'; import { hslTimetablesDbSchema } from 'hsl/timetablesdb/datasets/schema'; +import { insertDatasetFromJson } from './data-insert'; import * as testDatasetJson from './example.json'; -import { insertDatasetFromJson } from './json-parser'; import { HslTimetablesDatasetOutput, HslVehicleScheduleFrameOutput, diff --git a/test/hasura/timetables-data-inserter/hsl/index.ts b/test/hasura/timetables-data-inserter/hsl/index.ts index f16be848..0bbbc121 100644 --- a/test/hasura/timetables-data-inserter/hsl/index.ts +++ b/test/hasura/timetables-data-inserter/hsl/index.ts @@ -1,3 +1,4 @@ +export * from './data-insert'; export * from './dataset'; export * from './models'; export * from './table-data'; diff --git a/test/hasura/timetables-data-inserter/hsl/json-parser.ts b/test/hasura/timetables-data-inserter/hsl/json-parser.ts index 3d46236d..a923044e 100644 --- a/test/hasura/timetables-data-inserter/hsl/json-parser.ts +++ b/test/hasura/timetables-data-inserter/hsl/json-parser.ts @@ -1,10 +1,4 @@ -import { timetablesDbConfig } from '@config'; -import { closeDbConnection, createDbConnection } from '@util/db'; -import { setupDb } from '@util/setup'; -import { ConnectionConfig } from 'pg'; -import { buildHslTimetablesDataset } from './dataset'; import { hslTimetablesJsonSchema } from './json-schemas'; -import { createHslTableData } from './table-data'; import { HslTimetablesDatasetInput } from './types'; export const parseHslDatasetJson = ( @@ -14,16 +8,3 @@ export const parseHslDatasetJson = ( const parsedDatasetInput = hslTimetablesJsonSchema.parse(parsedJson); return parsedDatasetInput; }; - -export const insertDatasetFromJson = async ( - input: string, - dbConfig: ConnectionConfig = timetablesDbConfig, -) => { - const result = parseHslDatasetJson(input); - const builtDataset = buildHslTimetablesDataset(result); - const dbConnection = createDbConnection(dbConfig); - const tableData = createHslTableData(builtDataset); - await setupDb(dbConnection, tableData); - closeDbConnection(dbConnection); - return builtDataset; -};