Getting started with PolyNOM

Installation

Create a virtual environment with Python 3.11 or later and install PolyNOM 0.4.0. The package installs the compatible Python connector automatically.

python -m venv .venv
. .venv/bin/activate
python -m pip install 'polypheny-polynom==0.4.0'

In PowerShell, activate the environment with .\.venv\Scripts\Activate.ps1.

Insert and retrieve a model

Start a Polypheny server with a plain Prism 2.2 interface. The example reads POLYPHENY_HOST, POLYPHENY_PORT, POLYPHENY_USER, and POLYPHENY_PASSWORD from the environment. The defaults are 127.0.0.1, 20590, pa, and an empty password. Use a development database where the test user can create and drop a namespace.

Save the code as polynom_contacts.py and run python polynom_contacts.py. It creates an isolated relational namespace, writes a row, reads it in a new session, and cleans up the namespace and its own schema snapshot. It prints Ada Lovelace.

from contextlib import closing
from os import getenv
from uuid import uuid4

import polypheny
from polynom.application import Application
from polynom.model.model import BaseModel
from polynom.schema.field import Field
from polynom.schema.polytypes import VarChar
from polynom.schema.schema import BaseSchema
from polynom.schema.schema_registry import polynom_schema
from polynom.session import Session

address = (getenv("POLYPHENY_HOST", "127.0.0.1"), int(getenv("POLYPHENY_PORT", "20590")))
user = getenv("POLYPHENY_USER", "pa")
password = getenv("POLYPHENY_PASSWORD", "")
app_id = str(uuid4())
namespace = f"polynom_contacts_{uuid4().hex}"


@polynom_schema
class ContactSchema(BaseSchema):
    namespace_name = namespace
    entity_name = "contacts"
    fields = [Field("name", VarChar(80), nullable=False)]


class Contact(BaseModel):
    schema = ContactSchema()

    def __init__(self, name, _entry_id=None):
        super().__init__(_entry_id)
        self.name = name


with closing(polypheny.connect(address, username=user, password=password, transport="plain")) as cleanup:
    try:
        with Application(app_id, address, user=user, password=password) as app:
            with Session(app) as session:
                contact = Contact("Ada Lovelace")
                session.add(contact)
                contact_id = contact._entry_id
                session.commit()

            with Session(app) as session:
                saved_contact = Contact.query(session).get(contact_id)
                print(saved_contact.name)
    finally:
        with closing(cleanup.cursor()) as cursor:
            cursor.execute(f'DROP NAMESPACE IF EXISTS "{namespace}"')
            cursor.execute(
                'DELETE FROM "polynom_internal"."snapshot" WHERE "_entry_id" = ?',
                (app_id,),
            )
            cleanup.commit()

The schema decorator registers the table before the application starts. Application creates the table if it is missing. BaseModel assigns the row a UUID when you construct it. The first session commits the insert, and the second retrieves the saved row by that ID. A read-only session can exit without commit().

For a persistent application, keep a stable application UUID and namespace across runs. PolyNOM uses the UUID to find the previous schema snapshot. Applications and configuration

© Polypheny GmbH. All Rights Reserved.