Skip to main content

A common crud framework for web.

Project description

pycrud

codecov

A common crud framework for web.

Features:

  • Generate query by json or dsl

  • Role based permission system

  • Easy to integrate with web framework

  • Tested

Examples:

Define

from typing import Optional

from playhouse.db_url import connect
from pycrud.crud.ext.peewee_crud import PeeweeCrud
from pycrud.types import RecordMapping

class User(RecordMapping):
    id: Optional[int]
    nickname: str
    username: str
    password: str = 'password'


db = connect("sqlite:///:memory:")

c = PeeweeCrud(None, {
    User: 'users'
}, db)

Create

from pycrud.values import ValuesToWrite

v = ValuesToWrite({'nickname': 'wwww', 'username': 'u2'})
lst = await c.insert_many(User, [v])

print(lst)

Read

from pycrud.query import QueryInfo

# from dsl
lst = await c.get_list(QueryInfo.from_table_raw(User, where=[
    User.id != 1
]))

# from json
lst = await c.get_list(QueryInfo.from_json(User, {
    'id.eq': 1
}))

print([x.to_dict() for x in lst])

Update

from pycrud.query import QueryInfo
from pycrud.values import ValuesToWrite

v = ValuesToWrite({'nickname': 'bbb', 'username': 'u2'})

# from dsl
lst = await c.update(QueryInfo.from_table_raw(User, where=[
    User.id.in_([1, 2, 3])
]))

# from json
lst = await c.update(QueryInfo.from_json(User, {
    'id.in': [1,2,3]
}), v)

print(lst)

Delete

from pycrud.query import QueryInfo

lst = await c.delete(QueryInfo.from_json(User, {
    'id.in': [1,2,3]
}))

print(lst)

Query by json

from pycrud.query import QueryInfo

# $or: (id < 3) or (id > 5)
QueryInfo.from_json(User, {
    '$or': {
        'id.lt': 3,  
        'id.gt': 5 
    }
})

# $and: 3 < id < 5
QueryInfo.from_json(User, {
    '$and': {
        'id.gt': 3,  
        'id.lt': 5 
    }
})


# $not: not (3 < id < 5)
QueryInfo.from_json(User, {
    '$not': {
        'id.gt': 3,  
        'id.lt': 5 
    }
})

# multiple same operator: (id == 3) or (id == 4) or (id == 5)
QueryInfo.from_json(User, {
    '$or': {
        'id.eq': 3,  
        'id.eq.2': 4,
        'id.eq.3': 5, 
    }
})

# multiple same operator: (3 < id < 5) or (10 < id < 15)
QueryInfo.from_json(User, {
    '$or': {
        '$and': {
            'id.gt': 3,
            'id.lt': 5
        },
        '$and.2': {
            'id.gt': 10,
            'id.lt': 15
        }
    }
})

Query by DSL

# $or: (id < 3) or (id > 5)
(User.id < 3) | (User.id > 5)

# $and: 3 < id < 5
(User.id > 3) & (User.id < 5)

# $not: not (3 < id < 5)
~((User.id > 3) & (User.id < 5))

# multiple same operator: (id == 3) or (id == 4) or (id == 5)
(User.id != 3) | (User.id != 4) | (User.id != 5)

# multiple same operator: (3 < id < 5) or (10 < id < 15)
((User.id > 3) & (User.id < 5)) | ((User.id > 10) & (User.id < 15))

Operators

type operator text
compare EQ ('eq', '==')
compare NE ('ne', '!=')
compare LT ('lt', '<')
compare LE ('le', '<=')
compare GE ('ge', '>=')
compare GT ('gt', '>')
relation IN ('in',)
relation NOT_IN ('notin', 'not_in')
relation IS ('is',)
relation IS_NOT ('isnot', 'is_not')
relation PREFIX ('prefix',)
relation CONTAINS ('contains',)
relation CONTAINS_ANY ('contains_any',)
logic AND ('and',)
logic OR ('or',)
// usage:
{
  'time.ge': 1,
  '$or': {
    'id.in': [1, 2, 3],
    '$and': {
      'time.ge': 100,
      'time.le': 500,
    }
  }
}

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

pycrud-0.3.1.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

pycrud-0.3.1-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file pycrud-0.3.1.tar.gz.

File metadata

  • Download URL: pycrud-0.3.1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.3 Windows/10

File hashes

Hashes for pycrud-0.3.1.tar.gz
Algorithm Hash digest
SHA256 0585181a6139844a33ca1ecfced08956c7597dac2dc8bf7588d9997f19f57534
MD5 16a245f08ea1e0cabae2ab0e0daa66a7
BLAKE2b-256 93f3579a4e1e653a4bc951b2dad4b0b1e4eae4360a050b8fb2db89ad3a06f805

See more details on using hashes here.

File details

Details for the file pycrud-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pycrud-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 25.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.3 Windows/10

File hashes

Hashes for pycrud-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7606a8436522814c5868860abba792a16067fe936acc95ecd4e929952d0f0a7e
MD5 61d90d6ca4dc47c9af64933cf2678e36
BLAKE2b-256 f7c6f26b27e36a4f92272dcb14287c07af2fdceafb6733b45a254aaf7829b1f0

See more details on using hashes here.

Supported by

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