{"id":18797589,"url":"https://github.com/imyizhang/orm-sqlite","last_synced_at":"2025-04-13T16:32:11.835Z","repository":{"id":57449618,"uuid":"371397693","full_name":"imyizhang/ORM-SQLite","owner":"imyizhang","description":"A lightweight ORM for SQLite","archived":false,"fork":false,"pushed_at":"2023-08-20T05:31:16.000Z","size":8,"stargazers_count":14,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-27T07:48:01.324Z","etag":null,"topics":["orm","sqlite","sqlite3"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/ORM-SQLite/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imyizhang.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}},"created_at":"2021-05-27T14:14:47.000Z","updated_at":"2024-09-25T08:40:29.000Z","dependencies_parsed_at":"2023-08-29T21:23:13.607Z","dependency_job_id":"b425a705-9a5c-42a3-929b-51300e3cf2ed","html_url":"https://github.com/imyizhang/ORM-SQLite","commit_stats":null,"previous_names":["yzhang-dev/orm-sqlite"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imyizhang%2FORM-SQLite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imyizhang%2FORM-SQLite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imyizhang%2FORM-SQLite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imyizhang%2FORM-SQLite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imyizhang","download_url":"https://codeload.github.com/imyizhang/ORM-SQLite/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248743902,"owners_count":21154765,"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":["orm","sqlite","sqlite3"],"created_at":"2024-11-07T22:08:50.843Z","updated_at":"2025-04-13T16:32:11.574Z","avatar_url":"https://github.com/imyizhang.png","language":"Python","readme":"# ORM-SQLite\n\nORM-SQLite is\n\n* a lightweight ORM for SQLite\n* written in [Python (3.7+) Standard Library](https://docs.python.org/3.7/library/)\n\n\n\n## Quickstart\n\n1. Define a `Pomodoro` model\n\n   ```python\n   import orm_sqlite  \n\n   class Pomodoro(orm_sqlite.Model):  \n\n       id = orm_sqlite.IntegerField(primary_key=True) # auto-increment\n       task = orm_sqlite.StringField()\n       interval = orm_sqlite.IntegerField()\n   ```\n\n2. Create a `Database` instance\n\n   ```python\n   db = orm_sqlite.Database('example.db')\n   ```\n\n3. Set it as backend of `Pomodoro` model objects, CRUD operations thus can be performed later\n\n   ```python\n   Pomodoro.objects.backend = db\n   ```\n\n4. **Create** a Pomodoro timer record (primary key is auto-increment) and **insert** it to backend (**auto-commited**)\n\n   ```python\n   pomodoro = Pomodoro({'task': 'do something', 'interval': 25})\n   pomodoro.save()\n   ```\n\n   which is equivalent to\n\n   ```python\n   pomodoro = Pomodoro({'task': 'do something', 'interval': 25})\n   Pomodoro.objects.add(pomodoro)   \n   ```\n\n5. **Retrieve** all the records in the backend\n\n   ```Python\n   Pomodoro.objects.all()\n   ```\n\n6. **Retrieve** single record by its primary key and **update** it (**auto-commited**)\n\n   ```python\n   obj = Pomodoro.objects.get(pk=1)\n   obj['task'] = 'do something else'\n   obj.update()\n   ```\n\n   which is equivalent to\n\n   ```python\n   obj = Pomodoro.objects.get(pk=1)\n   obj['task'] = 'do something else'\n   Pomodoro.objects.update(obj)\n   ```\n\n7. **Retrieve** single record by its primary key and **delete** it (**auto-commited**)\n\n   ```python\n   Pomodoro.objects.get(pk=1).delete()\n   ```\n\n   which is equivalent to\n\n   ```python\n   obj = Pomodoro.objects.get(pk=1)\n   Pomodoro.objects.remove(obj)\n   ```\n\n8. Disconnect the backend\n\n   ```python\n   Pomodoro.objects.backend.close()\n   ```\n\n\n\n### Install ORM-SQLite\n\n```bash\n$ pip install orm-sqlite\n```\n\n\n\n## Documentation\n\n### Module `Database`\n\n```python\nclass orm_sqlite.Database(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri])\n```\n\n* `connected`\n\n  **Instance attribute**: Whether or not the SQLite database is connected.\n\n* `connection`\n\n  **Instance attribute**: `sqlite3.Connection` object\n\n* `cursor`\n\n  **Instance attribute**: `sqlite3.Cursor` object\n\n* `connect()`\n\n  **Instance method**: Connects the SQLite database if disconnected.\n\n* `close()`\n\n  **Instance method**: Disconnects the SQLite database if connected.\n\n* `select(sql, *args, size=None)`\n\n  **Instance method**: Returns query results, a list of `sqlite3.Row` objects.\n\n* `execute(sql, *args, autocommit=True)`\n\n  **Instance method**: Executes an SQL statement and returns rows affected.\n\n* `commit()`\n\n  **Instance method**: Commits the staged transaction.\n\n\n\n### Module `StringField`\n\n```python\nclass orm_sqlite.StringField(name=None, default='')\n```\n\n* `name`\n\n  **Instance attribute**: Column name. Default: `None`.\n\n* `type`\n\n  **Instance attribute**: Column type. Default: `TEXT`.\n\n* `default`\n\n  **Instance attribute**: Column default value. Default: `''`.\n\n* `primary_key`\n\n  **Instance attribute**: Whether or not it is the primary key. Default: `False`.\n\n\n\n### Module `IntegerField`\n\n```python\nclass orm_sqlite.IntegerField(name=None, default=0, primary_key=False)\n```\n\n* `name`\n\n  **Instance attribute**: Column name. Default: `None`.\n\n* `type`\n\n  **Instance attribute**: Column type. Default: `INTEGER`.\n\n* `default`\n\n  **Instance attribute**: Column default value. Default: `0`.\n\n* `primary_key`\n\n  **Instance attribute**: Whether or not it is the primary key. Default: `False`.\n\n\n\n### Module `FloatField`\n\n```python\nclass orm_sqlite.StringField(name=None, default=0.0)\n```\n\n* `name`\n\n  **Instance attribute**: Column name. Default: `None`.\n\n* `type`\n\n  **Instance attribute**: Column type. Default: `REAL`.\n\n* `default`\n\n  **Instance attribute**: Column default value. Default: `0.0`.\n\n* `primary_key`\n\n  **Instance attribute**: Whether or not it is the primary key. Default: `False`.\n\n\n\n### Module `Model`\n\n```python\nclass orm_sqlite.Model(*args, **kwargs)\n```\n\n* `__table__`\n\n  **Class attribute**: Table.\n\n* `__mappings__`\n\n  **Class attribute**: Object Relational Mappings (ORMs).\n\n* `__primary_key__`\n\n  **Class attribute**: Primary key.\n\n* `__fields__`\n\n  **Class attribute**: Fields except primary key.\n\n* `objects`\n\n  **Class-only attribute**: `orm_sqlite.Manager` object used to manage corresponding `Model` objects.\n\n* `exists()`\n\n  **Class-only method**: Whether or not the table exists in the connected database.\n\n* `create()`\n\n  **Class-only method**: Create the table if not exists in the connected database.\n\n* `drop()`\n\n  **Class-only method**: Drop the table if exists in the connected database, `objects.backend.commit()` needed to confirm. ***Extremely Dangerous***.\n\n* `save()`\n\n  **Instance method**: Inserts `Model` object to table in the connected database and returns rows affected (1 or -1).\n\n* `update()`\n\n  **Instance method**: Updates `Model` object from table in the connected database and returns rows affected (1 or -1). ***Dangerous***.\n\n* `delete()`\n\n  **Instance method**: Deletes `Model` object from table in the connected database and returns rows affected (1 or -1). ***Dangerous***.\n\n* `keys()`, `values()`, `items()`,  `copy()`, etc. methods inherited from standard `dict`.\n\n\n\n**Practical template for customized model**:\n\n```python\nimport orm_sqlite\n\nclass MyModel(orm_sqlite.Model):\n\n    id = orm_sqlite.IntegerField(primary_key=True) # auto-increment\n    # TODO:   \n```\n\n\n\n### Module `Manager`\n\n```python\nclass orm_sqlite.Manager()\n```\n\n* `backend`\n\n  **Instance attribute**: `orm_sqlite.Database` object serving as backend of  `Model` objects.\n\n* `all()`\n\n  **Instance method**: Returns all `Model` objects from table in the connected database.\n\n* `find(filter=None, order_by=None, **extra)`\n\n  **Instance method**: Returns all `Model` objects satified the conditions from table in the connected database.\n\n* `get(pk)`\n\n  **Instance method**: Returns single `Model` object by its primary key from table in the connected database.\n\n* `exists(pk)`\n\n  **Instance method**: Whether or not a primary key exists in the table from the connected database.\n\n* `aggregate(self, expression, filter=None)`\n\n  **Instance method**: Returns the result of an aggregate function performed on table in the connected database.\n\n* `add(obj)`\n\n  **Instance method**: Inserts `Model` object to table in the connected database and returns rows affected (1 or -1).\n\n* `update(obj)`\n\n  **Instance method**: Updates `Model` object from table in the connected database and returns rows affected (1 or -1). ***Dangerous***.\n\n* `remove(obj)`\n\n  **Instance method**: Deletes `Model` object from table in the connected database and returns rows affected (1 or -1). ***Dangerous***.\n\n* `clear()`\n\n  **Instance method**: Deletes **all** `Model` objects from table in the connected database and returns rows affected (1 or -1), `backend.commit()` needed  to confirm. ***Extremely Dangerous***.\n\n\n\n### Helper Function\n\n* `root_logger(log_dir='.')`\n\n  Returns root logger, a `logging.logger` object, logging in main module.\n\n* `child_logger(child)`\n\n  Returns child logger, a `logging.logger` object, logging in auxiliary module.\n\n\n\n## Related Projects\n\n* Inspired by [Django](https://www.djangoproject.com/) ORM\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimyizhang%2Form-sqlite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimyizhang%2Form-sqlite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimyizhang%2Form-sqlite/lists"}