Data Models
The data models that form the core of the Registry are located in the pgmodels directory. These models map directly to database tables or database views.
Table-backed models support inserts and usually updates and deletions. (Some records, such as PREMIS events, cannot be updated or deleted.) Most models implementing deletion support soft-deletion only.
View-backed models are read-only. These are generally used to gather data for detail and list pages with a single query.
The Base Model
The base models, BaseModel and TimestampModel, are defined in pgmodels/pgmodel.go, which also defines base methods for insertion, updating, and transactions.
This file has two other important functions:
initFilters()
defines a whitelist of allowed filters for each model type.InstIDFor()
returns the ID of the insitution that owns any given resource.
InstIDFor()
is especially important for the security checks run by the resource authorization middleware, which checks permissions against roles and ensures institutional users and admins can access only those resources belonging to their own institutions.
Allowed Filters
Each model defines a list of allowed filters that users can use to refine results in list queries. The filters are often defined on the view version of a model. For example, pgmodels/work_item_view.go defines a number of filters for the WorkItem list.
These filters become the names of form controls in the related filter form. See, for example, the WorkItemFilterForm.
That form will be rendered by the work item filters view.
When a user submits a query to the WorkItem list page, the GetFilterCollection()
function of the web request object parses the query string into a FilterCollection object, whose ToQuery()
function converts it to a
Query object.
The effect of all this is that we can add new query fields for any table or view simply by adding items to the allowed filters list. As long as the filter name follows a defined pattern, the Registry code will know what to do with it. This system also ensures that Registry won't accept unknown or dangerous filters, only those that are whitelisted.
The QueryOp map in pgmodels/query.go defines how filter name suffixes map to SQL operators. For example, query string parameters size=99
and size__eq=99
both turn into the SQL condition size = 99
. Similarly, size__gt=99
turns into size > 99
, while size__gteq=99
turns into size >= 99
.
The QueryOp map supports numeric operators as well as string and boolean equality, ILIKE
, IN
, NOT IN
, IS NULL
and IS NOT NULL
.
List of Models
Below is a list of models in the pgmodels directory, along with a brief description of each. For more information, click on a model name to view the souce.
Model | Description |
---|---|
Alert | An alert sent to one or more users. E.g. password-reset email, deletion request review notice, etc. |
AlertView | Read-only model containing additional fields from records related to the alert. This is used for querying and display. |
Checksum | A checksum record for an individual GenericFile. Checksums cannot be deleted or updated. |
ChecksumView | Read-only model containing additional fields from records related to the checksum. This is used for querying and display. |
Counts | Includes several objects used to represent object, file, event, and work item counts. |
DeletionRequest | Contains information about a request to delete a GenericFile or IntellectualObject. |
DeletionRequestView | Read-only model containing additional fields from records related to the deletion request. This is used for querying and display. |
DepositFormatStats | Contains stats about an institution's deposits. Possibly obsolete. This may have been superseded by DepositStats. |
DepositStats | Contains repository-wide stats used in Registry's Reports page. |
GenericFile | Represents a single file in preservation storage. |
GenericFileView | Read-only model containing additional fields from records related to the file. This is used for querying and display. |
Institution | Describes an institution (aka depositor). |
InstitutionView | Read-only model containing additional fields from records related to the institution. This is used for querying and display. |
IntellectualObject | Represents an object in preservation storage. An IntellectualObject is a collection of GenericFiles, plus some metadata, such as Title, Access settings, etc. |
IntellectualObjectView | Read-only model containing additional fields from records related to the object. This is used for querying and display. |
PremisEvent | Describes an action that the system or one of its users has performed on an IntellectualObject or GenericFile. PREMIS events cannot be updated or deleted. |
PremisEventView | Read-only model containing additional fields from records related to the event. This is used for querying and display. |
StorageRecord | Describes where in preservation storage a file can be found. |
User | A Registry User. |
UserView | Read-only model containing additional fields from records related to the user. This is used for querying and display. |
WorkItem | Describes a task that is, has been, or will be carried out by Preserv's ingest, restoration or deletion services. |
WorkItemView | Read-only model containing additional fields from records related to the WorkItem. This is used for querying and display. |
List of Non-Model Objects in pgmodels
The following files in the pgmodels folder are not models but are related to models and database access.
File | Description |
---|---|
factory.go | Like Rails' FactoryGirl, this file contains functions to generate model instances for unit and integration tests. |
filter_collection.go | Contains information about filters a user wants to apply when querying a list of records. |
json_types.go | Contains specially-defined serialization models for some pgmodel types. These are for cases in which some front-end component requires a custom (usually lightweight) serialization of an object. |
pgmodel.go | Contains base fields and functions inherited by other pgmolde.s |
query.go | Contains information necessary to generate a database query, including where clause, sorting, offsets, and limits. This can be constructed from a FilterCollection object, which in turn is parsed from query string params. |