Skip to content

Commit

Permalink
Add dataset insert function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
HenrikHartiala committed Sep 20, 2023
1 parent 1f7a3f6 commit 7346c92
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 42 deletions.
4 changes: 2 additions & 2 deletions test/hasura/timetables-data-inserter/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>,
Expand Down
33 changes: 33 additions & 0 deletions test/hasura/timetables-data-inserter/generic/data-insert.ts
Original file line number Diff line number Diff line change
@@ -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;
};
19 changes: 0 additions & 19 deletions test/hasura/timetables-data-inserter/generic/json-parser.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions test/hasura/timetables-data-inserter/hsl/data-insert.ts
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/hasura/timetables-data-inserter/hsl/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './data-insert';
export * from './dataset';
export * from './models';
export * from './table-data';
Expand Down
19 changes: 0 additions & 19 deletions test/hasura/timetables-data-inserter/hsl/json-parser.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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;
};

0 comments on commit 7346c92

Please sign in to comment.