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.4.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.4-py3-none-any.whl (39.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ns_orm-0.0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 9b39fe8a4c5f2d29e6b346b2ac4c3fd16cf2a6d59cb1e14da06901dc61b6a409
MD5 6768a397dee3bcb932bd310c4176f15a
BLAKE2b-256 94f8ac197950da5826cfa2a2a4da7101ef5dc69fd13996ef4384e9bf986f3553

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ns_orm-0.0.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b378fe328937f0c9cb037e10cf6b094eec329bd1579e19cdbd83e8e3789ea04e
MD5 5a9ff43f07f4802cc70edd94e3d6cbcd
BLAKE2b-256 43574376ae4b23473b532e1d88182db2adb42bdc33c6a0c32f45a8b4b374f32b

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