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

[i18n-Audio] Add DB schemas and shared API/store stubs #3987

Merged
merged 7 commits into from
Sep 21, 2023
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
14 changes: 12 additions & 2 deletions apps/mark-scan/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {
singlePrecinctSelectionFor,
} from '@votingworks/utils';

import { Usb, readBallotPackageFromUsb } from '@votingworks/backend';
import {
createUiStringsApi,
Usb,
readBallotPackageFromUsb,
} from '@votingworks/backend';
import { Logger } from '@votingworks/logging';
import { electionGeneralDefinition } from '@votingworks/fixtures';
import { useDevDockRouter } from '@votingworks/dev-dock-backend';
Expand All @@ -36,7 +40,8 @@ const debug = makeDebug('mark-scan:app-backend');

const defaultMediaMountDir = '/media';

function buildApi(
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function buildApi(
auth: InsertedSmartCardAuthApi,
usb: Usb,
logger: Logger,
Expand Down Expand Up @@ -232,6 +237,11 @@ function buildApi(

stateMachine.confirmInvalidateBallot();
},

...createUiStringsApi({
logger,
store: workspace.store.getUiStringsStore(),
}),
});
}

Expand Down
18 changes: 15 additions & 3 deletions apps/mark-scan/backend/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The durable datastore for configuration info.
//

import { UiStringsStore, createUiStringStore } from '@votingworks/backend';
import { Optional } from '@votingworks/basics';
import { Client as DbClient } from '@votingworks/db';
import {
Expand All @@ -21,7 +22,10 @@ const SchemaPath = join(__dirname, '../schema.sql');
* Manages a data store for imported election definition and system settings
*/
export class Store {
private constructor(private readonly client: DbClient) {}
private constructor(
private readonly client: DbClient,
private readonly uiStringsStore: UiStringsStore
) {}

getDbPath(): string {
return this.client.getDatabasePath();
Expand All @@ -31,14 +35,18 @@ export class Store {
* Builds and returns a new store whose data is kept in memory.
*/
static memoryStore(): Store {
return new Store(DbClient.memoryClient(SchemaPath));
const client = DbClient.memoryClient(SchemaPath);
const uiStringsStore = createUiStringStore(client);
return new Store(client, uiStringsStore);
}

/**
* Builds and returns a new store at `dbPath`.
*/
static fileStore(dbPath: string): Store {
return new Store(DbClient.fileClient(dbPath, SchemaPath));
const client = DbClient.fileClient(dbPath, SchemaPath);
const uiStringsStore = createUiStringStore(client);
return new Store(client, uiStringsStore);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! I like these patterns that you're setting for shared store and API code. I'd been with experimenting with similar ideas recently, e.g.

import {
clearCastVoteRecordHashes,
getCastVoteRecordRootHash,
updateCastVoteRecordHashes,
} from '@votingworks/auth';

getCastVoteRecordRootHash(): string {
return getCastVoteRecordRootHash(this.client);
}
updateCastVoteRecordHashes(cvrId: string, cvrHash: string): void {
updateCastVoteRecordHashes(this.client, cvrId, cvrHash);
}
clearCastVoteRecordHashes(): void {
clearCastVoteRecordHashes(this.client);
}

I like that what you're setting up is more automatic than what I'd set up. Looks like we can add new methods to libs/backend and the app stores and APIs will automatically update 🪄.

Re SQL schemas, I too just copied the schemas since we don't yet have a clear pattern for sharing those - that seems fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! I missed those - makes me feel better knowing that we have other instances where we're sharing the query code

}

/**
Expand Down Expand Up @@ -184,4 +192,8 @@ export class Store {
if (!result) return undefined;
return safeParseSystemSettings(result.data).unsafeUnwrap();
}

getUiStringsStore(): UiStringsStore {
return this.uiStringsStore;
}
}
14 changes: 12 additions & 2 deletions apps/mark/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
} from '@votingworks/types';
import { isElectionManagerAuth } from '@votingworks/utils';

import { Usb, readBallotPackageFromUsb } from '@votingworks/backend';
import {
createUiStringsApi,
Usb,
readBallotPackageFromUsb,
} from '@votingworks/backend';
import { Logger } from '@votingworks/logging';
import { electionGeneralDefinition } from '@votingworks/fixtures';
import { useDevDockRouter } from '@votingworks/dev-dock-backend';
Expand All @@ -37,7 +41,8 @@ function constructAuthMachineState(
};
}

function buildApi(
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function buildApi(
auth: InsertedSmartCardAuthApi,
usb: Usb,
logger: Logger,
Expand Down Expand Up @@ -139,6 +144,11 @@ function buildApi(

return ok(electionDefinition);
},

...createUiStringsApi({
logger,
store: workspace.store.getUiStringsStore(),
}),
});
}

Expand Down
18 changes: 15 additions & 3 deletions apps/mark/backend/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The durable datastore for configuration info.
//

import { UiStringsStore, createUiStringStore } from '@votingworks/backend';
import { Client as DbClient } from '@votingworks/db';
import {
ElectionDefinition,
Expand All @@ -17,7 +18,10 @@ const SchemaPath = join(__dirname, '../schema.sql');
* Manages a data store for imported election definition and system settings
*/
export class Store {
private constructor(private readonly client: DbClient) {}
private constructor(
private readonly client: DbClient,
private readonly uiStringsStore: UiStringsStore
) {}

getDbPath(): string {
return this.client.getDatabasePath();
Expand All @@ -27,14 +31,18 @@ export class Store {
* Builds and returns a new store whose data is kept in memory.
*/
static memoryStore(): Store {
return new Store(DbClient.memoryClient(SchemaPath));
const client = DbClient.memoryClient(SchemaPath);
const uiStringsStore = createUiStringStore(client);
return new Store(client, uiStringsStore);
}

/**
* Builds and returns a new store at `dbPath`.
*/
static fileStore(dbPath: string): Store {
return new Store(DbClient.fileClient(dbPath, SchemaPath));
const client = DbClient.fileClient(dbPath, SchemaPath);
const uiStringsStore = createUiStringStore(client);
return new Store(client, uiStringsStore);
}

/**
Expand Down Expand Up @@ -139,4 +147,8 @@ export class Store {
if (!result) return undefined;
return safeParseSystemSettings(result.data).unsafeUnwrap();
}

getUiStringsStore(): UiStringsStore {
return this.uiStringsStore;
}
}
9 changes: 8 additions & 1 deletion apps/scan/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '@votingworks/utils';
import express, { Application } from 'express';
import {
createUiStringsApi,
ExportDataError,
exportCastVoteRecordReportToUsbDrive,
ExportCastVoteRecordReportToUsbDriveError,
Expand Down Expand Up @@ -55,7 +56,8 @@ function constructAuthMachineState(
};
}

function buildApi(
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function buildApi(
auth: InsertedSmartCardAuthApi,
machine: PrecinctScannerStateMachine,
workspace: Workspace,
Expand Down Expand Up @@ -367,6 +369,11 @@ function buildApi(
supportsUltrasonic(): boolean {
return machine.supportsUltrasonic();
},

...createUiStringsApi({
logger,
store: workspace.store.getUiStringsStore(),
}),
});
}

Expand Down
25 changes: 20 additions & 5 deletions apps/scan/backend/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import { sha256 } from 'js-sha256';
import { DateTime } from 'luxon';
import { join } from 'path';
import { v4 as uuid } from 'uuid';
import { ResultSheet } from '@votingworks/backend';
import {
ResultSheet,
UiStringsStore,
createUiStringStore,
} from '@votingworks/backend';
import {
clearCastVoteRecordHashes,
getCastVoteRecordRootHash,
Expand Down Expand Up @@ -101,7 +105,10 @@ function resultSheetRowToResultSheet(row: ResultSheetRow): ResultSheet {
* interpreted by reading the sheets.
*/
export class Store {
private constructor(private readonly client: DbClient) {}
private constructor(
private readonly client: DbClient,
private readonly uiStringsStore: UiStringsStore
) {}

getDbPath(): string {
return this.client.getDatabasePath();
Expand All @@ -119,14 +126,18 @@ export class Store {
* Builds and returns a new store whose data is kept in memory.
*/
static memoryStore(): Store {
return new Store(DbClient.memoryClient(SchemaPath));
const client = DbClient.memoryClient(SchemaPath);
const uiStringsStore = createUiStringStore(client);
return new Store(client, uiStringsStore);
}

/**
* Builds and returns a new store at `dbPath`.
*/
static fileStore(dbPath: string): Store {
return new Store(DbClient.fileClient(dbPath, SchemaPath));
const client = DbClient.fileClient(dbPath, SchemaPath);
const uiStringsStore = createUiStringStore(client);
return new Store(client, uiStringsStore);
}

// TODO(jonah): Make this the only way to access the store so that we always
Expand Down Expand Up @@ -572,7 +583,7 @@ export class Store {
// Adding or deleting sheets would have updated the CVR count
const { maxSheetsCreatedAt, maxSheetsDeletedAt } = this.client.one(`
select
max(created_at) as maxSheetsCreatedAt,
max(created_at) as maxSheetsCreatedAt,
max(deleted_at) as maxSheetsDeletedAt
from sheets
`) as {
Expand Down Expand Up @@ -966,4 +977,8 @@ export class Store {
clearCastVoteRecordHashes(): void {
clearCastVoteRecordHashes(this.client);
}

getUiStringsStore(): UiStringsStore {
return this.uiStringsStore;
}
}