{"id":15927760,"url":"https://github.com/thrau/notion-objects","last_synced_at":"2025-03-24T15:32:06.411Z","repository":{"id":61120277,"uuid":"548108241","full_name":"thrau/notion-objects","owner":"thrau","description":"A Python library that makes it easy to work with objects in a notion database. 🚧 under development!","archived":false,"fork":false,"pushed_at":"2024-10-29T19:26:13.000Z","size":49,"stargazers_count":24,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T04:09:14.567Z","etag":null,"topics":["notion","notion-database","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thrau.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-10-08T23:55:00.000Z","updated_at":"2025-03-04T14:26:18.000Z","dependencies_parsed_at":"2024-03-30T18:31:00.495Z","dependency_job_id":null,"html_url":"https://github.com/thrau/notion-objects","commit_stats":{"total_commits":19,"total_committers":1,"mean_commits":19.0,"dds":0.0,"last_synced_commit":"f7000a83e93820f2b141424ce59cc460e1c9c0f4"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thrau%2Fnotion-objects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thrau%2Fnotion-objects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thrau%2Fnotion-objects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thrau%2Fnotion-objects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thrau","download_url":"https://codeload.github.com/thrau/notion-objects/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245298227,"owners_count":20592565,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["notion","notion-database","python"],"created_at":"2024-10-06T23:04:33.630Z","updated_at":"2025-03-24T15:32:01.363Z","avatar_url":"https://github.com/thrau.png","language":"Python","readme":"# notion-objects\n\n[![Build Status](https://github.com/thrau/notion-objects/actions/workflows/test.yml/badge.svg)](https://github.com/thrau/notion-objects/actions/workflows/test.yml)\n[![PyPI Version](https://badge.fury.io/py/notion-objects.svg)](https://badge.fury.io/py/notion-objects)\n[![PyPI License](https://img.shields.io/pypi/l/notion-objects.svg)](https://img.shields.io/pypi/l/notion-objects.svg)\n[![Codestyle](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\nA Python library that makes it easy to work with notion databases, built on top of [notion-sdk-py](https://github.com/ramnes/notion-sdk-py).\nIt provides a higher-level API with a data mapper, allowing you to define custom mappings between notion database records and your Python objects.\n\nWith notion-objects you can:\n* [Transform properties](#defining-models)\n* [Query databases](#querying-databases)\n* [Update records](#updating-records)\n* [Create records](#creating-records)\n\n## User guide\n\n### Defining models\n\nSuppose your database `tasks` has four fields, the title `Task`, a date range `Date`, and a person `Assigned to`, and a status field `Status`.\nYou want to transform notion database queries into records of:\n\n```json\n{\n  \"task\": \"my task\",\n  \"date_start\": \"2022-01-01\",\n  \"date_end\": \"2022-01-02\",\n  \"assigned_to\": \"Thomas\",\n  \"status\": \"In progress\"\n}\n```\n\nFirst, declare a model that contains all the necessary transformations as descriptors:\n\n```python\nfrom notion_objects import *\n\nclass Task(NotionObject):\n    task = TitleText(\"Task\")\n    assigned_to = Person(\"Assigned to\")\n    date_start = DateRangeStart(\"Date\")\n    date_end = DateRangeEnd(\"Date\")\n    closed_at = Date(\"Closed at\")\n    status = Status(\"Status\")\n```\n\nNow, when you have queried a database, you can instantiate `Task` objects with the results of the API call:\n\n```python\nresponse = requests.post(\"https://api.notion.com/v1/databases/{database_id}/query\", ...)\n\nfor item in response.json()['results']:\n    t = Task(item)\n    print(t.task)  # access attribute values\n    print(t.to_json())  # prints the record in the json format show earlier\n```\n\n### Querying Databases\n\nnotion-objects adds data-mapping around [notion-sdk-py](https://github.com/ramnes/notion-sdk-py). The `Database` class\nis uses a type parameter to map notion objects to the data models you defined.\n\nHere's a code snippet showing how to iterate over all pages in a databases that were updated after 2022-10-08, using\nour built-in `Page` model that holds the root page attributes.\n\n```python\nfrom notion_client import Client\nfrom notion_objects import Database, Page\n\nnotion = Client(auth=os.environ['NOTION_TOKEN'])\n\ndatabase: Database[Page] = Database(Page, database_id=\"123456789abcdef1234567890abcdef1\", client=notion)\n\nresult = database.query({\n    \"filter\": {\n        \"timestamp\": \"last_edited_time\",\n        \"last_edited_time\": {\n            \"after\": \"2022-10-08\"\n        }\n    }\n})\nfor page in result:\n    print(page.id, page.created_time, page.last_edited_time)\n```\n\nYou could also use `DynamicNotionObject` if you're too lazy to create a model for your database. notion-objects will map\nthe data types in a best-effort way. You can also iterate directly over the database to fetch all records:\n\n```python\nfrom notion_objects import Database, DynamicNotionObject\n\ndatabase = Database(DynamicNotionObject, ...)\n\nfor record in database:\n    print(record.to_json())  # will print your database record as JSON\n```\n\n**NOTE** not all types have yet been implemented. Type mapping is very rudimentary.\n\n### Updating records\n\nYou can update database records by simply calling attributes with normal python assignments.\nThe data mapper will map the types correctly to Notion's internal format.\nYou can then call `Database.update(...)` to run an update API call.\nnotion-objects keeps track of all the changes that were made to the object, and only sends the changes.\n\n```python\ndatabase: Database[Task] = Database(Task, ...)\n\ntask = database.find_by_id(\"...\")\ntask.status = \"Done\"\ntask.closed_at = datetime.utcnow()\ndatabase.update(task)\n```\n\n**Note** not all properties can be set yet.\n\n### Creating records\n\nSimilarly, you can also create new pages.\nYou can use `NotionObject.new()` on any subclass to create new unmanaged instances of that type.\nThen, call `Database.create(...)` to create a new item in the database.\n\n```python\ndatabase: Database[Task] = Database(Task, ...)\n\ntask = Task.new()\ntask.task = \"My New Task\"\ntask.status = \"In progress\"\ntask.assigned_to = \"6aa4d3cd-3928-4f61-9072-f74a3ebfc3ca\"\n\ntask = database.create(task)\nprint(task.id)  # will print the page ID that was created\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthrau%2Fnotion-objects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthrau%2Fnotion-objects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthrau%2Fnotion-objects/lists"}