typeid


Nametypeid JSON
Version 0.4.1 PyPI version JSON
download
home_pageNone
SummaryUUID-compatible Typed IDs with prefixes
upload_time2024-09-06 07:04:36
maintainerNone
docs_urlNone
authorFabian Schuh
requires_python<4.0,>=3.10
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # UUID-compatible Typed IDs with prefixes.

[![Documentation](https://readthedocs.org/projects/typeid/badge/?version=latest)](https://typeid.readthedocs.io/en/latest/?badge=latest)

`typid` is a python library that allows to create specific id types much like
stripe uses them across their platform to distinguish different data models from
their id already. `typeid` is compatible to the UUID library and as such can act
as a drop-in replacement.

A type id has the following string representation:

    user_nnv5rr3rfk3hgry6xrygr
    └──┘ └───────────────────┘
    type    uuid (base58)

## How to use `typeid`

First generate the Types using generate(...), then use them like so:

```python
from uuid_extensions import uuid7
from typeid import generate

typeids = generate("sk", "pk", "user", "apikey", enable_sqla=True)

# Now they can be used with SkId, PkId, UserId, and ApikeyId
assert str(typeids.SkId(uuid7().hex)).startswith("sk_")
assert str(typeids.PkId(uuid7().hex)).startswith("pk_")
assert str(typeids.UserId(uuid7().hex)).startswith("user_")
assert str(typeids.ApikeyId(uuid7().hex)).startswith("apikey_")

# Obviously, loading a prefixed type id in the correct Type works just fine
assert (typeids.UserId("user_nnv5rr3rfk3hgry6xrygr").hex == "0664c4135ed83faabd4bc0dc33839c9f")

# Use custom suffixes, just in case you need them
other_ids.generate("admin", suffix="IdUseWithCare")
assert str(other_ids.AdminIdUseWithCare(uuid7().hex)).startswith("admin_")
```

## SqlAlchemy Support

The library also allows to create classes specifically for the use with
sqlalchemy.

#### Example

```python
from typeid import generate

typeids = generate("user", enable_sqla=True)

class User(Base):
    __tablename__ = "user"

    # use typeids.UserId in the Mapper[]
    id: Mapped[typeids.UserId] = mapped_column(
        # use typeids.UserIdSQLA in the mapped_column!
        typeids.UserIdSQLA, primary_key=True, default=uuid.uuid4
    )

    name: str = Column(String(128), unique=True)

user = User(name="John Doe")
(db,) = get_session()
db.add(user)
db.commit()
db.refresh(user)
print(type(user.id))
# <class 'typeid.UserId'>

print((user.id))
# user_qvnrsem3skqho2hgni2mfk
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "typeid",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Fabian Schuh",
    "author_email": "info@chainsquad.com",
    "download_url": "https://files.pythonhosted.org/packages/bf/96/4effc69fb355cc0f008e6f9750e3c1fa690578fbc676fada7b96216a9122/typeid-0.4.1.tar.gz",
    "platform": null,
    "description": "# UUID-compatible Typed IDs with prefixes.\n\n[![Documentation](https://readthedocs.org/projects/typeid/badge/?version=latest)](https://typeid.readthedocs.io/en/latest/?badge=latest)\n\n`typid` is a python library that allows to create specific id types much like\nstripe uses them across their platform to distinguish different data models from\ntheir id already. `typeid` is compatible to the UUID library and as such can act\nas a drop-in replacement.\n\nA type id has the following string representation:\n\n    user_nnv5rr3rfk3hgry6xrygr\n    \u2514\u2500\u2500\u2518 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n    type    uuid (base58)\n\n## How to use `typeid`\n\nFirst generate the Types using generate(...), then use them like so:\n\n```python\nfrom uuid_extensions import uuid7\nfrom typeid import generate\n\ntypeids = generate(\"sk\", \"pk\", \"user\", \"apikey\", enable_sqla=True)\n\n# Now they can be used with SkId, PkId, UserId, and ApikeyId\nassert str(typeids.SkId(uuid7().hex)).startswith(\"sk_\")\nassert str(typeids.PkId(uuid7().hex)).startswith(\"pk_\")\nassert str(typeids.UserId(uuid7().hex)).startswith(\"user_\")\nassert str(typeids.ApikeyId(uuid7().hex)).startswith(\"apikey_\")\n\n# Obviously, loading a prefixed type id in the correct Type works just fine\nassert (typeids.UserId(\"user_nnv5rr3rfk3hgry6xrygr\").hex == \"0664c4135ed83faabd4bc0dc33839c9f\")\n\n# Use custom suffixes, just in case you need them\nother_ids.generate(\"admin\", suffix=\"IdUseWithCare\")\nassert str(other_ids.AdminIdUseWithCare(uuid7().hex)).startswith(\"admin_\")\n```\n\n## SqlAlchemy Support\n\nThe library also allows to create classes specifically for the use with\nsqlalchemy.\n\n#### Example\n\n```python\nfrom typeid import generate\n\ntypeids = generate(\"user\", enable_sqla=True)\n\nclass User(Base):\n    __tablename__ = \"user\"\n\n    # use typeids.UserId in the Mapper[]\n    id: Mapped[typeids.UserId] = mapped_column(\n        # use typeids.UserIdSQLA in the mapped_column!\n        typeids.UserIdSQLA, primary_key=True, default=uuid.uuid4\n    )\n\n    name: str = Column(String(128), unique=True)\n\nuser = User(name=\"John Doe\")\n(db,) = get_session()\ndb.add(user)\ndb.commit()\ndb.refresh(user)\nprint(type(user.id))\n# <class 'typeid.UserId'>\n\nprint((user.id))\n# user_qvnrsem3skqho2hgni2mfk\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "UUID-compatible Typed IDs with prefixes",
    "version": "0.4.1",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "49b6db661d5d7c229624f498c39611524dcb00625df917b780547db5276d4a7f",
                "md5": "c54c09ecf7aa1692ca78965874aba738",
                "sha256": "f84c3ddc5f25716ef04260ad04520b355dbbbc6e3327b841033c70a9894e7027"
            },
            "downloads": -1,
            "filename": "typeid-0.4.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c54c09ecf7aa1692ca78965874aba738",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 5629,
            "upload_time": "2024-09-06T07:04:35",
            "upload_time_iso_8601": "2024-09-06T07:04:35.635959Z",
            "url": "https://files.pythonhosted.org/packages/49/b6/db661d5d7c229624f498c39611524dcb00625df917b780547db5276d4a7f/typeid-0.4.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bf964effc69fb355cc0f008e6f9750e3c1fa690578fbc676fada7b96216a9122",
                "md5": "5563d6941a8edcd6efa75ee2b3aebca3",
                "sha256": "cc6fa20282ec947994d638b0b76f169504e3f08dbaf69e0bb63ca87c09efc74c"
            },
            "downloads": -1,
            "filename": "typeid-0.4.1.tar.gz",
            "has_sig": false,
            "md5_digest": "5563d6941a8edcd6efa75ee2b3aebca3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 4778,
            "upload_time": "2024-09-06T07:04:36",
            "upload_time_iso_8601": "2024-09-06T07:04:36.566122Z",
            "url": "https://files.pythonhosted.org/packages/bf/96/4effc69fb355cc0f008e6f9750e3c1fa690578fbc676fada7b96216a9122/typeid-0.4.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-06 07:04:36",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "typeid"
}
        
Elapsed time: 0.33922s