A ORM built on Pydantic models.
Project description
ns-orm
一个参考 Django ORM 设计的轻量级 ORM(不依赖 SQLAlchemy),以 Pydantic 作为模型层,支持同步/异步、CRUD、关联预取与最小建表能力。
数据库连接/连接池由 lesscode-database 负责,ns-orm 仅消费“执行器”来执行 SQL。
更多功能与完整用法见 doc/index.md。
安装
pip install ns-orm
数据库驱动与连接方式由 lesscode-database 决定。
快速开始(同步)
from typing import Optional
from typing_extensions import Annotated
from ns_orm import Database, ForeignKey, Int, Model, String, create_all
from ns_orm.dialects import dialect_from_url
class User(Model):
id: Annotated[Optional[int], Int(primary_key=True)] = None
name: Annotated[str, String(max_length=50)]
class Post(Model):
id: Annotated[Optional[int], Int(primary_key=True)] = None
user_id: Annotated[int, ForeignKey("User", on_delete="CASCADE")]
title: Annotated[str, String(max_length=200)]
executor = ... # 由 lesscode-database 创建
db = Database(executor=executor, dialect=dialect_from_url("sqlite"))
create_all(db, [User, Post])
u = User.objects.using(db).create(name="alice")
Post.objects.using(db).create(user_id=u.id, title="hello")
posts = Post.objects.using(db).prefetch_related("user").all()
print(posts[0].user.name)
快速开始(异步)
from ns_orm import AsyncDatabase, acreate_all
from ns_orm.dialects import dialect_from_url
executor = ... # 由 lesscode-database 创建(异步执行器)
db = AsyncDatabase(executor=executor, dialect=dialect_from_url("sqlite"))
await acreate_all(db, [User, Post])
u = await User.objects.using(db).acreate(name="alice")
posts = await Post.objects.using(db).filter(user_id=u.id).prefetch_related("user").all()
迁移命令
打包与发布
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ns_orm-0.0.0.tar.gz
(32.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
ns_orm-0.0.0-py3-none-any.whl
(36.1 kB
view details)
File details
Details for the file ns_orm-0.0.0.tar.gz.
File metadata
- Download URL: ns_orm-0.0.0.tar.gz
- Upload date:
- Size: 32.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f198dd0f4a033cac129d535f93750de2d2ebd5970e8dda68a7b9d670c9185f3
|
|
| MD5 |
1dfb935b4abe6b7fc85d1ebba8cc9f58
|
|
| BLAKE2b-256 |
88d0232d18664b6e9579e1ac7d9d337bd685efae966f53d79cb5b032b6869514
|
File details
Details for the file ns_orm-0.0.0-py3-none-any.whl.
File metadata
- Download URL: ns_orm-0.0.0-py3-none-any.whl
- Upload date:
- Size: 36.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0bdc28192989526f14c869604406f24e068af325b964230bf5cc0b4409e36676
|
|
| MD5 |
c605c7a4077c6846b93dda6d77720b51
|
|
| BLAKE2b-256 |
0e910c133ba900d672d1e09af92516d5ff10b4ce3f79bdcdab006db25f46fd03
|