Skip to main content

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 lesscode_database.connection_info import ConnectionInfo
from lesscode_database.db_options import db_options

from ns_orm import ForeignKey, Int, Model, String, create_all, get_connection


class User(Model):
    class Meta:
        connect_name = "default"

    id: Annotated[Optional[int], Int(primary_key=True)] = None
    name: Annotated[str, String(max_length=50)]


class Post(Model):
    class Meta:
        connect_name = "default"

    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)]


db_options.conn_list = [
    ConnectionInfo(dialect="sqlite3", name="default", dsn="test.db", async_enable=False),
]

db = get_connection("default")
create_all(db, [User, Post])

u = User.objects.create(name="alice")
Post.objects.create(user_id=u.id, title="hello")

posts = Post.objects.prefetch_related("user").all()
print(posts[0].user.name)

connect, _connect_info = getattr(db_options, "default")
if not hasattr(connect, "cursor") and hasattr(connect, "connection"):
    connect = connect.connection()

qs = User.objects.filter(id=u.id)
sql, params = qs._select_sql()
prepared = db.dialect.prepare(sql, params)
cursor = connect.cursor()
cursor.execute(prepared.sql, prepared.params)
row = cursor.fetchone()
print(row)

快速开始(异步)

import asyncio

from lesscode_database.connection_info import ConnectionInfo
from lesscode_database.db_options import db_options

from ns_orm import acreate_all, get_connection


async def main():
    db_options.conn_list = [
        ConnectionInfo(dialect="sqlite3", name="default_async", dsn="test_async.db", async_enable=True),
    ]

    db = get_connection("default_async")
    await acreate_all(db, [User, Post])

    u = await User.objects.using("default_async").acreate(name="alice")
    posts = await (
        Post.objects.using("default_async").filter(user_id=u.id).prefetch_related("user").all()
    )
    print(posts[0].user.name)


asyncio.run(main())

迁移命令

doc/migrations.md

打包与发布

doc/release.md

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.3.tar.gz (41.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ns_orm-0.0.3-py3-none-any.whl (39.6 kB view details)

Uploaded Python 3

File details

Details for the file ns_orm-0.0.3.tar.gz.

File metadata

  • Download URL: ns_orm-0.0.3.tar.gz
  • Upload date:
  • Size: 41.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for ns_orm-0.0.3.tar.gz
Algorithm Hash digest
SHA256 2f78dcfa05952ba622ae3f12532ed3dc47f6eb66724e1f5f1a2f2e8f2d3b5f94
MD5 7a5019a383e5b7a17ccc33c88c323d4a
BLAKE2b-256 f7b296f84bd62992fc467aa23ee3cf929aeca858ad93c1df048ebde4f8e6fa34

See more details on using hashes here.

File details

Details for the file ns_orm-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: ns_orm-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 39.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for ns_orm-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 634d1e0954d2747b5b461fad8c0cae20fc9bf4cb3d800272bd790a7666842fff
MD5 9c90c01c5f807a907446b2029bbc1143
BLAKE2b-256 d0d6f8c24243f448cb3852f731788725b74f4637ea642959862ce3bc79deb5de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page