Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Tom committed Aug 1, 2024
0 parents commit 7fd2b24
Show file tree
Hide file tree
Showing 14 changed files with 593 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "daily"
17 changes: 17 additions & 0 deletions .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: release
on:
release:
types: [published]
jobs:
publish:
environment: release # needed for PyPI OIDC
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install build && python -m build # build
- uses: pypa/gh-action-pypi-publish@release/v1 # publish
70 changes: 70 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '42 15 * * 5'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'python' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
31 changes: 31 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: pr
on:
pull_request:
types: [opened, reopened, synchronize]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
py: ['3.9', '3.10', '3.11', '3.12' ]
steps:
- uses: actions/checkout@v4
- uses: eifinger/setup-rye@v3
- run: rye pin $ # pin Python version
- run: | # abort if the lockfile changes
rye sync
[[ -n $(git diff --stat requirements.lock) ]] && exit 1
- run: rye fmt --check # check formatting is correct
- run: rye lint # and linting
- run: rye run check # typecheck too
- run: rye run test # then run your tests!
- name: Publish Test Report
uses: mikepenz/action-junit-report@v3
if: always() # always run even if the previous step fails
with:
report_paths: "junit/report.xml"
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# python generated files
__pycache__/
*.py[oc]
build/
dist/
wheels/
*.egg-info
.pytest_cache

# venv
.venv

# pycharm
.idea/

# pdm
.pdm-build
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Daniël Tom

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Sadel

Sadel simplifies upserting records to your database
by extending [SQLModel](https://sqlmodel.tiangolo.com/). It adds a single line of code to upsert records to your database.
It also supports batch upserting records. In addition, it automatically adds and manages `created_on` and `modified_on`
columns to your table for auditing purposes and validates the record before upserting.


### Features
- (Batch) upsert records with a single line of code.
- Asyncio support
- For auditing, automatically adds and manages `created_on` and `modified_on` columns to your table
- Validates your data before upserting using Pydantic validate_model method (not supported in SQLModel)

#### Example upsert
```python
from sadel import Sadel
from sqlalchemy.ext.asyncio import create_async_engine
from sqlmodel import Field, create_engine, select, or_
from sqlmodel.ext.asyncio.session import AsyncSession

class Hero(Sadel, table=True):
__tablename__ = "hero" # type: ignore
_upsert_index_elements = {"id"}

id: int | None = Field(default=None, primary_key=True)
name: str
secret_name: str
age: int | None = None

sqlite_url = f"sqlite+aiosqlite::///database.db"
engine = create_engine(sqlite_url, echo=True, future=True)

async_engine = create_async_engine(sqlite_url_async, echo=True, future=True)
hero = Hero(name="Deadpond", secret_name="Dive Wilson")

async with AsyncSession(async_engine) as session:
# Upsert the record
await Hero.upsert(hero, session)

# Fetch the upserted record
result = (
(await session.exec(select(Hero).where(Hero.name == "Deadpond")))
.all()
)

print(result)
```
Output:
```text
[Hero(id=1, name='Deadpond', secret_name='Dive Wilson', created_on=datetime.datetime(2024, 8, 1, 19, 39, 7), modified_on=None)]
```
### Example batch upsert
```python
hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)

async with AsyncSession(async_engine) as session:
await Hero.batch_upsert([hero_1, hero_2, hero_3], session)
result = (
(
await session.exec(
select(Hero).where(or_(Hero.name == "Deadpond", Hero.name == "Spider-Boy", Hero.name == "Rusty-Man"))
)
)
.all()
)
print(result)
```
Output:
```text
[Hero(id=1, name='Deadpond', secret_name='Dive Wilson', created_on=datetime.datetime(2024, 8, 1, 19, 39, 7), modified_on=None),
Hero(id=2, name='Spider-Boy", secret_name='Pedro Parqueador', created_on=datetime.datetime(2024, 8, 1, 19, 39, 7), modified_on=None),
Hero(id=3, name='Rusty-Man', secret_name='Tommy Sharp', created_on=datetime.datetime(2024, 8, 1, 19, 39, 7), modified_on=None)]
```

### Installation
```bash
pip install sadel
```

### Contributing
- Fork the repository
- Create a new branch
- Make your changes
- Raise a PR

### Development
```bash
# install dependencies
rye sync
# run tests, linting, formatting, and type checking,
rye run all
```

### License
This project is licensed under the terms of the [MIT License](LICENSE)
61 changes: 61 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[project]
name = "sadel"
dynamic = ["version"]
description = "Simplifies upserting records to database"
authors = [
{ name = "Daniël Tom", email = "[email protected]" }
]
dependencies = [
"sqlmodel~=0.0.21",
]
readme = "README.md"
requires-python = ">= 3.9"

[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"

[tool.rye]
managed = true
dev-dependencies = [
"pyright~=1.1.374",
"pytest~=8.3.2",
"setuptools~=72.1.0",
"pytest-asyncio~=0.23.8",
"aiosqlite~=0.20.0",
"greenlet~=3.0.3",
]

[tool.rye.scripts]
fmt = "rye fmt"
lint = "rye lint --fix"
check = "pyright"
test = "rye test"
all = { chain = ["fmt", "lint", "check", "test"] }


[tool.pyright]
venvPath = "."
venv = ".venv"
strict = ["**/*.py"]
pythonVersion = "3.9"
include = ["sadel/**/*.py"]

[tool.ruff]
target-version = "py39"
line-length = 100

[tool.ruff.lint]
select = [
"A", # warn about shadowing built-ins
"E", # style stuff, whitespaces
"F", # important pyflakes lints
"I", # import sorting
"N", # naming
"T100", # breakpoints (probably don't want these in prod!)
]


[tool.ruff.lint.isort]
known-first-party = ["sadel"]

42 changes: 42 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# generated by rye
# use `rye lock` or `rye sync` to update this lockfile
#
# last locked with the following flags:
# pre: false
# features: []
# all-features: false
# with-sources: false
# generate-hashes: false
# universal: false

-e file:.
aiosqlite==0.20.0
annotated-types==0.7.0
# via pydantic
greenlet==3.0.3
iniconfig==2.0.0
# via pytest
nodeenv==1.9.1
# via pyright
packaging==24.1
# via pytest
pluggy==1.5.0
# via pytest
pydantic==2.8.2
# via sqlmodel
pydantic-core==2.20.1
# via pydantic
pyright==1.1.374
pytest==8.3.2
# via pytest-asyncio
pytest-asyncio==0.23.8
setuptools==72.1.0
sqlalchemy==2.0.31
# via sqlmodel
sqlmodel==0.0.21
# via sadel
typing-extensions==4.12.2
# via aiosqlite
# via pydantic
# via pydantic-core
# via sqlalchemy
Loading

0 comments on commit 7fd2b24

Please sign in to comment.