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.1.tar.gz
(37.1 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.1-py3-none-any.whl
(36.1 kB
view details)
File details
Details for the file ns_orm-0.0.1.tar.gz.
File metadata
- Download URL: ns_orm-0.0.1.tar.gz
- Upload date:
- Size: 37.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a7dfd31b6f03b0f088cfbb6c0e62abd70ea117628e9f66e284c6a5099e6b1a7
|
|
| MD5 |
dcaa69bd7ae9c70dabdd5adf58a9f9e5
|
|
| BLAKE2b-256 |
d03332c2aacc9a5a1635dd00b61a870558983ec9c24cb4dd55e8ddf99d9a3f83
|
File details
Details for the file ns_orm-0.0.1-py3-none-any.whl.
File metadata
- Download URL: ns_orm-0.0.1-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 |
db8ac0fb6f917d79cf8d9b27b8c2d490de0e4f8af978a06aa7671be351f6cdef
|
|
| MD5 |
131c20c98773774a3d007ee29225e35b
|
|
| BLAKE2b-256 |
5615a8a8d66c923af528578a9702bb1a302b44d4f9832960b9929a08ac19da9c
|