Skip to content

Refactor - Services should be initiated once and not require callers to fetch DB Connection #116

@joneubank

Description

@joneubank

Description

Services were exported as functions so that they can be initialized on server startup. They are requesting a DB instance be passed in as an argument. This is acceptable, but this initialization can be done once, and then the service with its known DB connection should be available for use throughout the code.

It is a common pattern that the db connection needs to be retrieved immediately before a service is called, with code such as:

const database = getDbInstance();
const studySvc = await studyService(database);

All uses of the study are done this same way, with a call to getDbInstance that means that knowledge of the DB connection is spread throughout the app. This should instead be handled a single time on server start up. This is similar to how we initialize the Lyric Provider.

Affected Services

  • DAC Service
  • ID Manager Service
  • Study Service

Desired Behaviour

These services should be initialized a single time. This is easily accomplished by making the services singleton and having them grab the system's db connection on their own.

For example, we want to use the study service like:

// As singleton
const studySvc = await getStudyService();

or

// as const module export
const result = await studyService.getStudy(id);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions