tortoise / tortoise/tortoise-orm

Full support BJson (BJSONField)

Open
#797 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Is your feature request related to a problem? Please describe.
I think it's a good idea to create a separate field - BJSONField, because postgres provides great features with bjson and a lot of queries, which are conveniently handled as an object

Describe the solution you'd like
Model:

class Settings(Model):
    id = fields.BigIntField(index=True)
    data = fields.BJSONField()

Code:

from tortoise.contrib.postgres.objects import bjson_object
...
>>> await Settings.create(data=bjson_object({"a": 1, "b": {"c": "d", "e": True}))
>>> settings = await Settings.all().first()
>>> data = settings.data
>>> data.a
1
>>> data.b.c
'd'
>>> data.b.e
True

And it is also convenient to make a lot of requests for orm, with bjson:

Settings.filter(data={"a":1})
Settings.filter(data__has='a')
Settings.filter(data__has_any=['a', 'b'])
Settings.filter(data__has_all=['a', 'b'])
# Sub/superset of key/value pair testing
Settings.filter(data__contains={'a': 1})
Settings.filter(data__in={'a': 1, 'b': 2})
# Get element/field (compare with json)
Settings.filter(data__get=(2, {'a': 1}))
# Get element/field (compare with scalar, including gt/lt comparisons)
Settings.filter(data__get=(2, 'a'))
Settings.filter(data__get__gt=('a', 1))
# key path traversal, compare with json or scalar.
Settings.filter(data__get=('{a,2}', {'foo': 'bar'}))
Settings.filter(data__get=('{a,2}', 2))
Settings.filter(data__get__lte=('{a,2}', 2))

It would also be nice to implement other postgresql data types

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or entry points. Start by mapping the existing PostgreSQL field and query support against the requested BJSONField behavior and listed filters; done means the supported scope, object access, and query semantics are covered and verified by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.