Records

The Records component of the Genomcore API allows you to create, update, query and delete a diversity of records.

Get more information about the Records API here

Create

Example of how to create records. The records body must be a JSON object with a list of dictionaries containing the records to create, presenting the following structure:

{
    "items": [
                {
                    "code": "TestCode",
                    "data": {
                                "RecordParameter1": {
                                    "Parameter1": "Value1",
                                    "Parameter2": "Value2"
                                },
                                "RecordParameter2": {
                                    "Parameter3": "Value3",
                                    "Parameter4": "Value4"
                                }
                            }
                },
                ...
            ]
}

The following code starts the sdk, the logging module and loads an .env file. Then, it creates records using a template slug (or template Id) and the body containing the records.

import logging
import os
from dotenv import load_dotenv
from genomcore.client import GenomcoreApiClient

load_dotenv(override=True)

logging.basicConfig(
    level="INFO",
    format="[%(asctime)s][%(levelname)s][%(name)s] -- %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
)

body = {
            "items": [
                    {
                        "code": "Testcode",
                        "data": {
                                    "RecordParameter1": {
                                        "Parameter1": "Value1",
                                        "Parameter2": "Value2"
                                    }
                                }
                    },
                    {
                        "code": "TestCode2",
                        "data": {
                                    "RecordParameter1": {
                                        "Parameter1": "Value3",
                                        "Parameter2": "Value4"
                                    }
                                }
                    }
            ]
        }

api = GenomcoreApiClient(
    token=os.getenv("TOKEN"),
    refresh_token=os.getenv("REFRESH_TOKEN")
)

api.records.create_records(
    template = "TestTemplate"
    body = body
)

Warning

The method does not check if the parameters provided for each record meet the requirements of the template, nor does it verify if the code for any of them already exists. It is the user’s responsibility to ensure that each record code to be created is unique and that the records comply with the structure and requirements of the specified template.

Update

This example updates a record with the Id “TestID”, containing the new data of the record on the update body.

from genomcore.client import GenomcoreApiClient

api = GenomcoreApiClient(token="A_VALID_TOKEN", refresh_token="A_VALID_REFRESH_TOKEN")

body = {
    "data": {
        "RecordParameter": {
            "Parameter1": "Value1",
            "Parameter2": "Value2"
        }
    }
}

api.records.update_records(id = "TestID", body = body)

#Alternativelly, you can update records by the template slug and record code

api.records.update_records(templateSlug = "TestTemplate", code = "TestCode", body = body)

Note

You can update records by providing either the record ID or the template slug and code of the record. If you are not providing an ID, then you must provide a template slug and a code.

Get

This example retrieves the information of a record with the Id “TestID”. As another option, you can search for a record by the template slug and record code.

from genomcore.client import GenomcoreApiClient

api = GenomcoreApiClient(token="A_VALID_TOKEN", refresh_token="A_VALID_REFRESH_TOKEN")

api.records.get_records(id = "TestID")

#Alternativelly, you can search for a record by the template slug and record code

api.records.get_records(templateSlug = "TestTemplate", code = "TestCode")

Delete

The following example shows how to deletes a single record or a series of records.

To delete multiple records at once, you must provide a body with the structure of a dictionary that contains a list of records to delete, each identified by its id or by the template slug and code. As an example, it must have the following structure:

{   "records": [
                    {
                        "id": "TestID"
                    },
                    {
                        "id": "TestID2"
                    }
                ]
}

The following code deletes records, either by the record ID, by template slug and record code or by using a body object.

from genomcore.client import GenomcoreApiClient

api = GenomcoreApiClient(token="A_VALID_TOKEN", refresh_token="A_VALID_REFRESH_TOKEN")

body = {"records": [
                    {
                        "id": "TestID"
                    },
                    {
                        "id": "TestID2"
                    }
                ]
        }

api.records.delete_records(id = "TestID")

#Alternativelly, you can delete records by the template slug and record code

api.records.delete_records(templateSlug = "TestTemplate", code = "TestCode")

#For delete multiple records you need to use a body object

api.records.delete_records(body = body)

Note

For delete an individual record, you can use either the record ID or the template slug and code of the record. Remember, if you are not providing an ID, then you must provide a template slug and a code.

For deleting multiple records, you have to create a body with the delete records format.

Query

The following example shows how do a query over records

The following code search records in all templates.

from genomcore.client import GenomcoreApiClient

api = GenomcoreApiClient(token="A_VALID_TOKEN", refresh_token="A_VALID_REFRESH_TOKEN")

filters = {
    "code": "HPO",
    "type": "term"
}

api.records.query_records(filters)


#Alternativelly, you can query records by the template slug and record code

api.records.query_records(filters,templateId = "templateId")

#You can search and sort
sort= {
    "_id": 1
}

#You can specified project, page and pageSize:
project='111'
page=2
pageSize=50

#At finished, you can do groups and operate with its
group={
    "_id": "$type",
    "count": {
    "$sum": 1
}
api.records.delete_records(filters,templateId = "templateId",group=group,sort=sort,project=project,page=page,pageSize=pageSize)