Map a data model#

This guide maps the SQLAlchemy models of an application to SCIM resources, case by case. It is for developers who have followed the Overview and map a model that it does not cover: an extension, a computed value, a custom resource type, or tables with other identifiers. It does not cover the choice of the schemas the server publishes: for this choice, read Publish the stored attributes.

The examples extend the intranet of the Overview.

Prepare the SQLAlchemy model#

Give each SQLAlchemy model the columns the storage needs:

  • a primary key, mapped to id. A ResourceMapping without it raises a ValueError;

  • a creation date and a modification date, mapped to meta.created and meta.lastModified. Without them, the resources have no such date;

  • an integer version counter, declared as the version_id_col of the mapper, and passed as the version of the mapping. Without it, meta.version is a hash of the resource, keyed with the version_key of the storage.

The project model of the intranet has these columns:

class ProjectRecord(Base):
    __tablename__ = "projects"

    id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
    code: Mapped[str] = mapped_column(unique=True)
    title: Mapped[str | None]
    created_at: Mapped[datetime.datetime]
    updated_at: Mapped[datetime.datetime]
    revision: Mapped[int] = mapped_column()
    owner_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("accounts.id"))

    owner: Mapped[Account | None] = relationship()
    members: Mapped[list[Account]] = relationship(secondary=project_members)
    tags: Mapped[list["ProjectTag"]] = relationship(
        cascade="all, delete-orphan", order_by="ProjectTag.id"
    )

    __mapper_args__ = {"version_id_col": revision}

Per RFC 7643 §3.1, two resources never share an id, even of different types. A search at the root of the server returns the resources of every type together, and clients tell them apart by their id. The integers of an autoincrement column start again in each table, so they break this rule as soon as the server serves two resource types. Use a UUID, with the Uuid type, or a sequence that every table shares.

The primary key may be an integer, a string or a UUID, and the storage returns it as a string. Give its column a default, such as default=uuid.uuid4, a server default, or an autoincrement, so that the database fills the key of each new resource.

Map a column#

Map an attribute to a column of the model. Map a complex attribute holding a single value, such as name, by its sub-attributes:

{
    "userName": Account.login,
    "name.givenName": Account.first_name,
    "name.familyName": Account.last_name,
}

A path to a complex attribute alone, such as name, raises a ValueError. Leave out the attributes the model has no column for: the storage refuses a value for them. To keep clients from sending such values, read Publish the stored attributes.

A new resource needs a value for each column that cannot be NULL and has no default. The storage refuses a resource without this value with a 400 error. It also refuses an entry of a Many without this value. The storage does not see the values the application sets itself, such as in the constructor of the model or in a before_insert event. Declare such a value as the default or the server_default of the column.

The mapping warns about a column of the record that needs a value but that the storage never writes, such as a column left out of the mapping or mapped as read-only. Creating a resource would then fail in the database. The setter of a property is taken to write the columns that its expression reads.

Map an extension#

Map the attribute of an extension by its full path, the URN of the schema then the attribute name. The mapping takes the model with its extensions, such as User[Workplace]:

class Workplace(Extension):
    __schema__ = URN("urn:example:params:scim:schemas:extension:intranet:2.0:User")
    office: str | None = None


ACCOUNTS = ResourceMapping(
    User[Workplace],
    Account,
    {
        "id": Account.id,
        "userName": Account.login,
        "urn:example:params:scim:schemas:extension:intranet:2.0:User:office": Account.office,
        "meta.created": Account.created_at,
        "meta.lastModified": Account.updated_at,
    },
    version=Account.revision,
)


Map a computed value#

Map an attribute to a hybrid_property when the model computes its value. The storage reads the property, and filters and sorts with its expression. Without a setter, the attribute is read-only: the storage never writes it, and refuses a different value with a 400 error. The displayName of the intranet joins the first and the last name:

@hybrid_property
def full_name(self):
    return f"{self.first_name or ''} {self.last_name or ''}".strip() or None

@full_name.expression
def full_name(cls):
    return func.nullif(
        func.trim(
            func.coalesce(cls.first_name, "")
            + " "
            + func.coalesce(cls.last_name, "")
        ),
        "",
    )

Convert a value#

Map an attribute to a hybrid_property with a setter and an expression when the column holds the value in another form. The getter and the setter convert the value in Python. The expression converts it in SQL, so that filters and sorts compare SCIM values. The following model stores active in a status column, which holds enabled or disabled:

class Account(Base):
    status: Mapped[str | None]

    @hybrid_property
    def active(self):
        return None if self.status is None else self.status == "enabled"

    @active.inplace.setter
    def _active_setter(self, value):
        self.status = None if value is None else ("enabled" if value else "disabled")

    @active.inplace.expression
    @classmethod
    def _active_expression(cls):
        return cls.status == "enabled"

Map the attribute to the property, with "active": Account.active. The filter active eq true then returns the accounts whose status is enabled.

Keep the expression NULL when the column is NULL, as status == "enabled" does. The filter active pr then holds only for the accounts with a status. A request removing the attribute passes None to the setter.

A string attribute is read from a column of strings, or of bytes, which the storage encodes in base64. The mapping refuses a string attribute in a column of another type, such as an integer, except for id. Convert such a column with a property, whose expression casts it to a string.

Map an enumerated column#

Map a string attribute to a column of type Enum directly, without a property. The attribute holds the string the column stores: the name of the member, or its value when the column has values_callable. Take this enumeration:

class Color(enum.Enum):
    Red = "Crimson"
    blue = "azure"

A column Enum(Color) gives the strings Red and blue. With values_callable=lambda members: [member.value for member in members], it gives Crimson and azure.

Filters and sorts compare these strings, as on any string column, and not the order of the members. This also holds for the native enumerations of PostgreSQL. The storage refuses a string outside the enumeration with a 400 error.

Map a write-only or a read-only attribute#

Wrap a column or a property in an Attribute with writable=False to make its attribute read-only, even when the column or the property can be written. A property without setter, or a Computed column, is read-only without it.

An attribute that the schema makes writeOnly, such as password, or never returns, holds a value that clients never read back. The storage still reads it, in the form its column holds, such as a hash, and the server never shows it to clients. A PUT without the value gives the stored one back to the storage, which keeps it as it is: a value equal to the stored one is not written again. A PATCH that removes the value, or a PUT that sets it to null, clears it. The storage neither filters nor sorts on such a value, and does not check that it is unique: give its column a unique constraint instead.

The intranet hashes the password in the setter of a property, whose getter returns the hash:

@hybrid_property
def password(self):
    return self.password_hash

@password.inplace.setter
def _password_setter(self, value):
    self.password_hash = None if value is None else hash_password(value)


The schema makes password write-only, so the mapping is plain:

{"password": Account.password}

A column of type Hashed hashes the values the same way, without a property: mapped_column(Hashed(hash_password)). The mapping refuses it for an attribute that clients read, since they would read the hash. It also refuses it for an immutable attribute: a client sending the value again would get a 400 error, since the value never matches its hash.

When a setter turns None into a value, such as a hash that matches no password, the getter should give None back for this value. Otherwise, a removed password still reads as a value.

A write-only sub-attribute of an immutable attribute can change, as the schema makes it write-only.

The mapping refuses an attribute that clients could neither read nor write, such as a write-only attribute on a property without setter, or on a computed column.

Map a list of values#

Map a multi-valued attribute holding plain values, such as a list of strings, with Many. Give it the relationship() to the child model, and the column holding the values. The projects of the intranet have tags, one per row of a child table:

class ProjectTag(Base):
    __tablename__ = "project_tags"

    id: Mapped[int] = mapped_column(primary_key=True)
    project_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("projects.id"))
    name: Mapped[str]


{"tags": Many(ProjectRecord.tags, ProjectTag.name)}

Declare the relationship with cascade="all, delete-orphan": writing the resource replaces its values, and the delete-orphan cascade deletes the former ones. A relationship without this cascade raises a ValueError. The order_by of the relationship gives the order of the values, and the storage keeps their duplicates.

A filter such as tags eq "moon" holds when one value matches, and tags[value eq "moon"] selects the same projects. A sort on the attribute uses the first value.

A multi-valued sub-attribute of a complex attribute cannot be mapped, and raises a ValueError.

Map entries of a child table#

Map a multi-valued complex attribute whose entries belong to the resource, such as emails, with Many too. Give it the relationship to the child model, and the column of each sub-attribute:

{
    "emails": Many(
        Account.emails,
        {
            "value": EmailAddress.address,
            "type": EmailAddress.kind,
            "primary": EmailAddress.preferred,
        },
    )
}

The relationship needs the same cascade as for a list of values, and its order_by gives the order of the entries. A sort on the attribute uses the entry marked primary, or else the first entry.

Writing the resource creates its entries again, so the mapping refuses a sub-attribute that clients never read back, such as a write-only one. Its value comes back in the form its column holds, such as a hash, and would be written again in a new entry. The mapping also refuses a read-only sub-attribute next to written ones: the sub-attributes are all written or all read-only.

Map a custom resource type#

Map a resource type of the application the same way as a standard one. Declare its SCIM model first: for the steps, read Define custom SCIM models. The intranet serves its projects, whose owner and members are users, and which have tags:

class UserReference(ComplexAttribute):
    value: Annotated[str | None, Mutability.immutable] = None
    ref: Annotated[Reference["User"] | None, Mutability.immutable] = Field(
        None, serialization_alias="$ref", validation_alias="$ref"
    )
    display: str | None = None


class Project(Resource):
    __schema__ = URN("urn:example:params:scim:schemas:intranet:2.0:Project")
    code: Annotated[str | None, Required.true, Uniqueness.server] = None
    title: str | None = None
    owner: UserReference | None = None
    members: list[UserReference] | None = None
    tags: list[str] | None = None


Map it to the project model:

PROJECTS = ResourceMapping(
    Project,
    ProjectRecord,
    {
        "id": ProjectRecord.id,
        "code": ProjectRecord.code,
        "title": ProjectRecord.title,
        "owner": Link(ProjectRecord.owner, "User"),
        "members": Link(ProjectRecord.members, "User"),
        "tags": Many(ProjectRecord.tags, ProjectTag.name),
        "meta.created": ProjectRecord.created_at,
        "meta.lastModified": ProjectRecord.updated_at,
    },
    version=ProjectRecord.revision,
)


Serve it next to the users. The mappings of the storage are keyed by the name of each resource type:

def create_storage(session, provider):
    return SqlAlchemyStorage(
        {"User": ACCOUNTS, "Project": PROJECTS}, lambda: session, provider=provider
    )

Create a project owned by a user:

>>> from scim2_server.applications.wsgi import WSGIApplication
>>> from sqlalchemy import create_engine
>>> from sqlalchemy.orm import Session
>>> from werkzeug.test import Client
>>> from intranet.models import Base
>>> from intranet.projects import create_provider
>>> from intranet.projects import create_storage

>>> engine = create_engine("sqlite://")
>>> Base.metadata.create_all(engine)
>>> session = Session(engine)
>>> provider = create_provider()
>>> client = Client(WSGIApplication(create_storage(session, provider), provider))
>>> user = client.post("/v2/Users", json={"userName": "bjensen"}).json
>>> project = client.post(
...     "/v2/Projects",
...     json={"code": "APOLLO", "owner": {"value": user["id"]}, "tags": ["moon"]},
... ).json
>>> project["owner"]
{'value': '...', '$ref': 'http://localhost/v2/Users/...', 'display': 'bjensen'}
>>> project["tags"]
['moon']
>>> session.close()
>>> engine.dispose()

For every parameter of the mapping and of the storage, read Mapping and Storages.