Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define models needed for HPC-7904 #30

Merged
merged 16 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import expiredData from './models/expiredData';
import form from './models/form';
import governingEntity from './models/governingEntity';
import governingEntityVersion from './models/governingEntityVersion';
import location from './models/location';
import operation from './models/operation';
import operationCluster from './models/operationCluster';
import participant from './models/participant';
Expand Down Expand Up @@ -44,6 +45,7 @@ export default (conn: Knex) => ({
form: form(conn),
governingEntity: governingEntity(conn),
governingEntityVersion: governingEntityVersion(conn),
location: location(conn),
operation: operation(conn),
operationCluster: operationCluster(conn),
participant: participant(conn),
Expand Down
37 changes: 37 additions & 0 deletions src/db/models/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as t from 'io-ts';

import { brandedType } from '../../util/io-ts';
import type { Brand } from '../../util/types';
import { defineIDModel } from '../util/id-model';

export type LocationId = Brand<
number,
Expand All @@ -10,3 +11,39 @@ export type LocationId = Brand<
>;

export const LOCATION_ID = brandedType<number, LocationId>(t.number);

const LOCATION_STATUS = {
active: null,
expired: null,
};

export default defineIDModel({
tableName: 'location',
fields: {
generated: {
id: { kind: 'branded-integer', brand: LOCATION_ID },
},
optional: {
externalId: { kind: 'checked', type: t.string },
name: { kind: 'checked', type: t.string },
latitude: { kind: 'checked', type: t.number },
longitude: { kind: 'checked', type: t.number },
iso3: { kind: 'checked', type: t.string },
pcode: { kind: 'checked', type: t.string },
// Even though this column is defined as int8, it is
// fetched as a string by knex, since it is bigint
validOn: { kind: 'checked', type: t.string },
parentId: { kind: 'branded-integer', brand: LOCATION_ID },
},
accidentallyOptional: {
adminLevel: { kind: 'checked', type: t.number },
status: {
kind: 'enum',
values: LOCATION_STATUS,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum should only be used when the property uses an enum defined in sql, this particular field is a string, so instead we should use a checked string with t.keyof to validate upon read, and restrict the type when writing.

The reason for this is that at some point I'd like to extend the validation function / unit tests to check that enum types are defined correctly, and allow the same values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense even now, without enum definition checks

itosSync: { kind: 'checked', type: t.boolean },
},
},
idField: 'id',
softDeletionEnabled: false,
});