database

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2024 License: GPL-3.0 Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const TABLE_ALIAS = "fhir"

Variables

This section is empty.

Functions

func AggregationParameterToClause

func AggregationParameterToClause(aggParameter AggregationParameter) string

func NamedParameterWithSuffix

func NamedParameterWithSuffix(parameterName string, suffix string) string

func SearchCodeToFromClause

func SearchCodeToFromClause(searchParam SearchParameter) (string, error)

func SearchCodeToWhereClause

func SearchCodeToWhereClause(searchParam SearchParameter, searchParamValue SearchParameterValue, namedParameterSuffix string) (string, map[string]interface{}, error)

SearchCodeToWhereClause converts a searchCode and searchCodeValue to a where clause and a map of named parameters

Types

type AggregationParameter added in v0.1.7

type AggregationParameter struct {
	SearchParameter
	Function string //count, sum, avg, min, max, etc
}

func ProcessAggregationParameter

func ProcessAggregationParameter(aggregationFieldWithFn models.QueryResourceAggregation, searchParamTypeLookup map[string]string) (AggregationParameter, error)

ProcessAggregationParameter processes the aggregation parameters which are fields with optional properties: Fields that are primitive types (number, uri) must not have any property specified: eg. `probability`

Fields that are complex types (token, quantity) must have a property specified: eg. `identifier:code`

if the a property is specified, its set as the modifier, and used when generating the SQL query groupBy, orderBy, etc clause

type DatabaseRepository

type DatabaseRepository interface {
	Close() error
	Migrate() error

	CreateUser(context.Context, *models.User) error
	GetUserCount(context.Context) (int, error)
	GetUserByUsername(context.Context, string) (*models.User, error)
	GetCurrentUser(ctx context.Context) (*models.User, error)
	DeleteCurrentUser(ctx context.Context) error

	GetSummary(ctx context.Context) (*models.Summary, error)

	GetResourceByResourceTypeAndId(context.Context, string, string) (*models.ResourceBase, error)
	GetResourceBySourceId(context.Context, string, string) (*models.ResourceBase, error)
	QueryResources(ctx context.Context, query models.QueryResource) (interface{}, error)
	ListResources(context.Context, models.ListResourceQueryOptions) ([]models.ResourceBase, error)
	GetPatientForSources(ctx context.Context) ([]models.ResourceBase, error)
	AddResourceAssociation(ctx context.Context, source *models.SourceCredential, resourceType string, resourceId string, relatedSource *models.SourceCredential, relatedResourceType string, relatedResourceId string) error
	RemoveResourceAssociation(ctx context.Context, source *models.SourceCredential, resourceType string, resourceId string, relatedSource *models.SourceCredential, relatedResourceType string, relatedResourceId string) error
	FindResourceAssociationsByTypeAndId(ctx context.Context, source *models.SourceCredential, resourceType string, resourceId string) ([]models.RelatedResource, error)
	GetFlattenedResourceGraph(ctx context.Context, graphType pkg.ResourceGraphType, options models.ResourceGraphOptions) (map[string][]*models.ResourceBase, error)

	// Deprecated:This method has been deprecated. It has been replaced in favor of Fasten SourceCredential & associations
	AddResourceComposition(ctx context.Context, compositionTitle string, resources []*models.ResourceBase) error

	CreateSource(context.Context, *models.SourceCredential) error
	GetSource(context.Context, string) (*models.SourceCredential, error)
	GetSourceSummary(context.Context, string) (*models.SourceSummary, error)
	GetSources(context.Context) ([]models.SourceCredential, error)
	UpdateSource(ctx context.Context, sourceCreds *models.SourceCredential) error
	DeleteSource(ctx context.Context, sourceId string) (int64, error)

	CreateGlossaryEntry(ctx context.Context, glossaryEntry *models.Glossary) error
	GetGlossaryEntry(ctx context.Context, code string, codeSystem string) (*models.Glossary, error)

	//background jobs
	CreateBackgroundJob(ctx context.Context, backgroundJob *models.BackgroundJob) error
	GetBackgroundJob(ctx context.Context, backgroundJobId string) (*models.BackgroundJob, error)
	UpdateBackgroundJob(ctx context.Context, backgroundJob *models.BackgroundJob) error
	ListBackgroundJobs(ctx context.Context, queryOptions models.BackgroundJobQueryOptions) ([]models.BackgroundJob, error)

	//settings
	LoadSystemSettings(ctx context.Context) (*models.SystemSettings, error)
	SaveSystemSettings(ctx context.Context, newSettings *models.SystemSettings) error
	LoadUserSettings(ctx context.Context) (*models.UserSettings, error)
	SaveUserSettings(context.Context, *models.UserSettings) error
	PopulateDefaultUserSettings(ctx context.Context, userId uuid.UUID) error

	//used by fasten-sources Clients
	BackgroundJobCheckpoint(ctx context.Context, checkpointData map[string]interface{}, errorData map[string]interface{})
	UpsertRawResource(ctx context.Context, sourceCredentials sourcePkg.SourceCredential, rawResource sourcePkg.RawResourceFhir) (bool, error)
	UpsertRawResourceAssociation(
		ctx context.Context,
		sourceId string,
		sourceResourceType string,
		sourceResourceId string,
		targetSourceId string,
		targetResourceType string,
		targetResourceId string,
	) error
}

func NewRepository

func NewRepository(appConfig config.Interface, globalLogger logrus.FieldLogger, eventBus event_bus.Interface) (DatabaseRepository, error)

type GormRepository added in v0.1.8

type GormRepository struct {
	AppConfig config.Interface
	Logger    logrus.FieldLogger

	GormClient *gorm.DB

	EventBus event_bus.Interface
}

func (*GormRepository) AddResourceAssociation added in v0.1.8

func (gr *GormRepository) AddResourceAssociation(ctx context.Context, source *models.SourceCredential, resourceType string, resourceId string, relatedSource *models.SourceCredential, relatedResourceType string, relatedResourceId string) error

func (*GormRepository) AddResourceComposition deprecated added in v0.1.8

func (gr *GormRepository) AddResourceComposition(ctx context.Context, compositionTitle string, resources []*models.ResourceBase) error

AddResourceComposition this will group resources together into a "Composition" -- primarily to group related Encounters & Conditions into one semantic root. algorithm: - find source for each resource - (SECURITY) ensure the current user and the source for each resource matches - check if there is a Composition resource Type already.

  • if Composition type already exists:
  • update "relatesTo" field with additional data.
  • else:
  • Create a Composition resource type (populated with "relatesTo" references to all provided Resources)

- add AddResourceAssociation for all resources linked to the Composition resource - store the Composition resource TODO: determine if we should be using a List Resource instead of a Composition resource

Deprecated: This method has been deprecated. It has been replaced in favor of Fasten SourceCredential & associations

func (*GormRepository) BackgroundJobCheckpoint added in v0.1.8

func (gr *GormRepository) BackgroundJobCheckpoint(ctx context.Context, checkpointData map[string]interface{}, errorData map[string]interface{})

func (*GormRepository) CancelAllLockedBackgroundJobsAndFail added in v0.1.8

func (gr *GormRepository) CancelAllLockedBackgroundJobsAndFail() error

when server restarts, we should unlock all locked jobs, and set their status to failed SECURITY: this is global, and effects all users.

func (*GormRepository) Close added in v0.1.8

func (gr *GormRepository) Close() error

func (*GormRepository) CreateBackgroundJob added in v0.1.8

func (gr *GormRepository) CreateBackgroundJob(ctx context.Context, backgroundJob *models.BackgroundJob) error

<editor-fold desc="Background Job & Checkpoints">

func (*GormRepository) CreateGlossaryEntry added in v0.1.8

func (gr *GormRepository) CreateGlossaryEntry(ctx context.Context, glossaryEntry *models.Glossary) error

<editor-fold desc="Glossary">

func (*GormRepository) CreateSource added in v0.1.8

func (gr *GormRepository) CreateSource(ctx context.Context, sourceCreds *models.SourceCredential) error

func (*GormRepository) CreateUser added in v0.1.8

func (gr *GormRepository) CreateUser(ctx context.Context, user *models.User) error

<editor-fold desc="User">

func (*GormRepository) DeleteCurrentUser added in v1.0.2

func (gr *GormRepository) DeleteCurrentUser(ctx context.Context) error

SECURITY: this should only be called after the user has confirmed they want to delete their account.

func (*GormRepository) DeleteSource added in v0.1.8

func (gr *GormRepository) DeleteSource(ctx context.Context, sourceId string) (int64, error)

func (*GormRepository) FindResourceAssociationsByTypeAndId added in v0.1.8

func (gr *GormRepository) FindResourceAssociationsByTypeAndId(ctx context.Context, source *models.SourceCredential, resourceType string, resourceId string) ([]models.RelatedResource, error)

func (*GormRepository) GetBackgroundJob added in v0.1.8

func (gr *GormRepository) GetBackgroundJob(ctx context.Context, backgroundJobId string) (*models.BackgroundJob, error)

func (*GormRepository) GetCurrentUser added in v0.1.8

func (gr *GormRepository) GetCurrentUser(ctx context.Context) (*models.User, error)

TODO: check for error, right now we return a nil which may cause a panic. TODO: can we cache the current user? //SECURITY:

func (*GormRepository) GetFlattenedResourceGraph added in v0.1.8

func (gr *GormRepository) GetFlattenedResourceGraph(ctx context.Context, graphType pkg.ResourceGraphType, options models.ResourceGraphOptions) (map[string][]*models.ResourceBase, error)

Retrieve a list of all fhir resources (vertex), and a list of all associations (edge) Generate a graph return list of root nodes, and their flattened related resources.

func (*GormRepository) GetGlossaryEntry added in v0.1.8

func (gr *GormRepository) GetGlossaryEntry(ctx context.Context, code string, codeSystem string) (*models.Glossary, error)

func (*GormRepository) GetPatientForSources added in v0.1.8

func (gr *GormRepository) GetPatientForSources(ctx context.Context) ([]models.ResourceBase, error)

Get the patient for each source (for the current user)

func (*GormRepository) GetResourceByResourceTypeAndId added in v0.1.8

func (gr *GormRepository) GetResourceByResourceTypeAndId(ctx context.Context, sourceResourceType string, sourceResourceId string) (*models.ResourceBase, error)

TODO: should this be deprecated? (replaced by ListResources)

func (*GormRepository) GetResourceBySourceId added in v0.1.8

func (gr *GormRepository) GetResourceBySourceId(ctx context.Context, sourceId string, sourceResourceId string) (*models.ResourceBase, error)

we need to figure out how to get the source resource type from the source resource id, or if we're searching across every table :(

func (*GormRepository) GetSource added in v0.1.8

func (gr *GormRepository) GetSource(ctx context.Context, sourceId string) (*models.SourceCredential, error)

func (*GormRepository) GetSourceSummary added in v0.1.8

func (gr *GormRepository) GetSourceSummary(ctx context.Context, sourceId string) (*models.SourceSummary, error)

func (*GormRepository) GetSources added in v0.1.8

func (gr *GormRepository) GetSources(ctx context.Context) ([]models.SourceCredential, error)

func (*GormRepository) GetSummary added in v0.1.8

func (gr *GormRepository) GetSummary(ctx context.Context) (*models.Summary, error)

func (*GormRepository) GetUserByUsername added in v0.1.8

func (gr *GormRepository) GetUserByUsername(ctx context.Context, username string) (*models.User, error)

func (*GormRepository) GetUserCount added in v1.1.1

func (gr *GormRepository) GetUserCount(ctx context.Context) (int, error)

func (*GormRepository) InflateSelectedResourcesInResourceGraph added in v0.2.2

func (gr *GormRepository) InflateSelectedResourcesInResourceGraph(currentUser *models.User, resourcePlaceholderListDictionary map[string][]*VertexResourcePlaceholder, options models.ResourceGraphOptions) (map[string][]*models.ResourceBase, error)

InflateSelectedResourcesInResourceGraph - this function will take a dictionary of placeholder "sources" graph and load the selected resources (and their descendants) from the database. - first, it will load all the "source" resources (eg. Encounter, Condition, etc) - sort the root resources by date, desc - use the page number + page size to determine which root resources to return - return a dictionary of "source" resource lists

func (*GormRepository) ListBackgroundJobs added in v0.1.8

func (gr *GormRepository) ListBackgroundJobs(ctx context.Context, queryOptions models.BackgroundJobQueryOptions) ([]models.BackgroundJob, error)

func (*GormRepository) ListResources added in v0.1.8

func (gr *GormRepository) ListResources(ctx context.Context, queryOptions models.ListResourceQueryOptions) ([]models.ResourceBase, error)

func (*GormRepository) LoadSystemSettings added in v1.1.1

func (gr *GormRepository) LoadSystemSettings(ctx context.Context) (*models.SystemSettings, error)

LoadSystemSettings will retrieve settings from the database, and return a SystemSettings struct

func (*GormRepository) LoadUserSettings added in v0.1.8

func (gr *GormRepository) LoadUserSettings(ctx context.Context) (*models.UserSettings, error)

LoadSettings will retrieve settings from the database, store them in the AppConfig object, and return a Settings struct

func (*GormRepository) Migrate added in v0.1.8

func (gr *GormRepository) Migrate() error

func (*GormRepository) PopulateDefaultUserSettings added in v0.1.8

func (gr *GormRepository) PopulateDefaultUserSettings(ctx context.Context, userId uuid.UUID) error

func (*GormRepository) PopulateGraphTypeReciprocalRelationships added in v0.1.8

func (gr *GormRepository) PopulateGraphTypeReciprocalRelationships(graphType pkg.ResourceGraphType, relationships []models.RelatedResource) []models.RelatedResource

We need to support the following types of graphs: - Medical History - AddressBook (contacts) - Medications - Billing Report edges are always "strongly connected", however "source" nodes (roots, like Condition or Encounter -- depending on ) are only one way. add an edge from every resource to its related resource. Keep in mind that FHIR resources may not contain reciprocal edges, so we ensure the graph is rooted by flipping any related resources that are "Condition" or "Encounter"

func (*GormRepository) QueryResources added in v0.1.8

func (gr *GormRepository) QueryResources(ctx context.Context, query models.QueryResource) (interface{}, error)

Allows users to use SearchParameters to query resources Can generate simple or complex queries, depending on the SearchParameter type:

eg. Simple

eg. Complex SELECT fhir.* FROM fhir_observation as fhir, json_each(fhir.code) as codeJson WHERE (

(codeJson.value ->> '$.code' = "29463-7" AND codeJson.value ->> '$.system' = "http://loinc.org")
OR (codeJson.value ->> '$.code' = "3141-9" AND codeJson.value ->> '$.system' = "http://loinc.org")
OR (codeJson.value ->> '$.code' = "27113001" AND codeJson.value ->> '$.system' = "http://snomed.info/sct")

) AND (user_id = "6efcd7c5-3f29-4f0d-926d-a66ff68bbfc2") GROUP BY `fhir`.`id`

func (*GormRepository) RemoveResourceAssociation added in v0.1.8

func (gr *GormRepository) RemoveResourceAssociation(ctx context.Context, source *models.SourceCredential, resourceType string, resourceId string, relatedSource *models.SourceCredential, relatedResourceType string, relatedResourceId string) error

func (*GormRepository) SaveSystemSettings added in v1.1.1

func (gr *GormRepository) SaveSystemSettings(ctx context.Context, newSettings *models.SystemSettings) error

testing SaveSystemSettings will update save the settings to the database.

func (*GormRepository) SaveUserSettings added in v0.1.8

func (gr *GormRepository) SaveUserSettings(ctx context.Context, newSettings *models.UserSettings) error

testing curl -d '{"metrics": { "notify_level": 5, "status_filter_attributes": 5, "status_threshold": 5 }}' -H "Content-Type: application/json" -X POST http://localhost:9090/api/settings SaveSettings will update settings in AppConfig object, then save the settings to the database.

func (*GormRepository) UpdateBackgroundJob added in v0.1.8

func (gr *GormRepository) UpdateBackgroundJob(ctx context.Context, backgroundJob *models.BackgroundJob) error

func (*GormRepository) UpdateSource added in v0.1.8

func (gr *GormRepository) UpdateSource(ctx context.Context, sourceCreds *models.SourceCredential) error

func (*GormRepository) UpsertRawResource added in v0.1.8

func (gr *GormRepository) UpsertRawResource(ctx context.Context, sourceCredential sourceModel.SourceCredential, rawResource sourceModel.RawResourceFhir) (bool, error)

This function will create a new resource if it does not exist, or update an existing resource if it does exist. It will also create associations between fhir resources This function is called directly by fasten-sources

func (*GormRepository) UpsertRawResourceAssociation added in v1.0.2

func (gr *GormRepository) UpsertRawResourceAssociation(
	ctx context.Context,
	sourceId string,
	sourceResourceType string,
	sourceResourceId string,
	targetSourceId string,
	targetResourceType string,
	targetResourceId string,
) error

func (*GormRepository) UpsertResource added in v0.1.8

func (gr *GormRepository) UpsertResource(ctx context.Context, wrappedResourceModel *models.ResourceBase) (bool, error)

UpsertResource this method will upsert a resource, however it will not create associations. UPSERT operation - call FindOrCreate

  • check if the resource exists
  • if it does not exist, insert it

- if no error during FindOrCreate && no rows affected (nothing was created)

  • update the resource using Updates operation

type SearchParameter

type SearchParameter struct {
	Name     string
	Type     SearchParameterType
	Modifier string
}

func ProcessSearchParameter

func ProcessSearchParameter(searchCodeWithModifier string, searchParamTypeLookup map[string]string) (SearchParameter, error)

SearchParameters are made up of parameter names and modifiers. For example, "name" and "name:exact" are both valid search parameters This function will parse the searchCodeWithModifier and return the SearchParameter

type SearchParameterType

type SearchParameterType string
const (
	//simple types
	SearchParameterTypeNumber  SearchParameterType = "number"
	SearchParameterTypeDate    SearchParameterType = "date"
	SearchParameterTypeUri     SearchParameterType = "uri"
	SearchParameterTypeKeyword SearchParameterType = "keyword" //this is a literal/string primitive.

	//complex types
	SearchParameterTypeString    SearchParameterType = "string"
	SearchParameterTypeToken     SearchParameterType = "token"
	SearchParameterTypeReference SearchParameterType = "reference"
	SearchParameterTypeQuantity  SearchParameterType = "quantity"
	SearchParameterTypeComposite SearchParameterType = "composite"
	SearchParameterTypeSpecial   SearchParameterType = "special"
)

type SearchParameterValue

type SearchParameterValue struct {
	Prefix          string
	Value           interface{}
	SecondaryValues map[string]interface{}
}

func ProcessSearchParameterValue

func ProcessSearchParameterValue(searchParameter SearchParameter, searchValueWithPrefix string) (SearchParameterValue, error)

ProcessSearchParameterValue searchValueWithPrefix may or may not have a prefix which needs to be parsed this function will parse the searchValueWithPrefix and return the SearchParameterValue for example, "eq2018-01-01" would return a SearchParameterValue with a prefix of "eq" and a value of "2018-01-01" and "2018-01-01" would return a SearchParameterValue with a value of "2018-01-01"

some query types, like token, quantity and reference, have secondary values that need to be parsed for example, code="http://loinc.org|29463-7" would return a SearchParameterValue with a value of "29463-7" and a secondary value of { "codeSystem": "http://loinc.org" }

type SearchParameterValueOperatorTree

type SearchParameterValueOperatorTree [][]SearchParameterValue

Lists in the SearchParameterValueOperatorTree are AND'd together, and items within each SearchParameterValueOperatorTree list are OR'd together For example, the following would be AND'd together, and then OR'd with the next SearchParameterValueOperatorTree

{
 {SearchParameterValue{Value: "foo"}, SearchParameterValue{Value: "bar"}}
 {SearchParameterValue{Value: "baz"}},
}

This would result in the following SQL:

(value = "foo" OR value = "bar") AND (value = "baz")

func ProcessSearchParameterValueIntoOperatorTree

func ProcessSearchParameterValueIntoOperatorTree(searchParameter SearchParameter, searchParamCodeValueOrValuesWithPrefix interface{}) (SearchParameterValueOperatorTree, error)

ProcessSearchParameterValueIntoOperatorTree searchParamCodeValueOrValuesWithPrefix may be a single string, or a list of strings each string, may itself be a concatenation of multiple values, seperated by a comma so we need to do three stages of processing: 1. split the searchParamCodeValueOrValuesWithPrefix into a list of strings 2. split each string into a list of values 3. use the ProcessSearchParameterValue function to split each value into a list of prefixes and values these are then stored in a multidimentional list of SearchParameterValueOperatorTree top level is AND'd together, and each item within the lists are OR'd together

For example, searchParamCodeValueOrValuesWithPrefix may be:

"code": "29463-7,3141-9,27113001"
"code": ["le29463-7", "gt3141-9", "27113001"]

type VertexResourcePlaceholder added in v0.1.7

type VertexResourcePlaceholder struct {
	UserID                     string
	SourceID                   string
	ResourceID                 string
	ResourceType               string
	RelatedResourcePlaceholder []*VertexResourcePlaceholder
}

func (*VertexResourcePlaceholder) ID added in v0.1.7

Directories

Path Synopsis
migrations
Package mock_database is a generated GoMock package.
Package mock_database is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL