Git Product home page Git Product logo

red-bird's Introduction

Red Bird

Repository Patterns for Python

Generic database implemetation for SQL, MongoDB and in-memory lists


Pypi version build codecov Documentation Status PyPI pyversions

Repository pattern is a technique to abstract the data access from the domain/business logic. In other words, it decouples the database access from the application code. The aim is that the code runs the same regardless if the data is stored to an SQL database, NoSQL database, file or even as an in-memory list.

Read more about the repository patterns in the official documentation.

Why?

Repository pattern has several benefits over embedding the database access to the application:

  • Faster prototyping and development
  • Easier to migrate the database
  • More readable code, Pythonic
  • Unit testing and testing in general is safer and easier

Features

Main features:

  • Support for Pydantic models for data validation
  • Identical way to create, read, update and delete (CRUD)
  • Pythonic and simple syntax
  • Support for more complex queries (greater than, not equal, less than etc.)

Supported repositories:

  • SQL
  • MongoDB
  • In-memory (Python list)
  • JSON files
  • CSV file

Examples

First, we create a simple repo:

from redbird.repos import MemoryRepo
repo = MemoryRepo()

Note: the following examples work on any repository, not just in-memory repository.

Adding/creating items:

repo.add({"name": "Anna", "nationality": "British"})

Reading items:

repo.filter_by(name="Anna").all()

Updating items:

repo.filter_by(name="Anna").update(nationality="Finnish")

Deleting items:

repo.filter_by(name="Anna").delete()

See more from the official documentation.

Author

red-bird's People

Contributors

miksus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

red-bird's Issues

Issue on page /versions.html

from scheduler import app as app_rocketry

raise error:

raise PydanticUserError('`const` is removed, use `Literal` instead', code='removed-kwargs')
pydantic.errors.PydanticUserError: `const` is removed, use `Literal` instead

and it's in file: redbird/base.py, line 153

ordered: bool = Field(default=False, const=True)

Mongo error

need to comment the json["_id"] out for it to work as self.id_field is none and likely the same for all the operations

def item_to_data(self, item:BaseModel):
json = self.item_to_dict(item, exclude_unset=False)
# Rename whatever is as id_field to _id
#json["_id"] = json.pop(self.id_field)
return json

Field `id_field` is not optional. Missing pagination?

Awesome work!

I tried redbird with mongorepo and noticed that the id_field is not optional.
Is there any reason behind that?

The id_field is set to _id field on mongo which turns of some features of mongo (mongo generates faster the _id and makes it easier to sort).

I assume that this is because ObjectId from mongo is not json parsable?

Also, I didn't found any options to use limit and skip together for pagination (there is just limit).

Feature request: add_many method

First of all, kudos for making this library! I'm evaluating the usages in one of my project, it can use a better separation between the data layer and the logic!

I think that one thing that is missing is the add_many method to leverage the (often more performant) backend methods.

The base implementation can be simply a series of calls to the add method.

Pydantic v2 support

Copying here an issue posted originally on Rocketry, just for the integrity and the tracking - since the problem stems from red-bird.
Miksus/rocketry#210

Is your feature request related to a problem? Please describe.
red-bird is not compatible with Pydantic V2

Describe the solution you'd like
Update Red-Bird (if it is still maintained) to use Pydantic V2

Describe alternatives you've considered
None

Additional context
Pydantic V2 was released on June 30th, and ever since red-bird and Rocketry are not usable.
Traceback for example:

Traceback (most recent call last):
  File "/app/main.py", line 5, in <module>
    from rocketry import Rocketry
  File "/usr/local/lib/python3.9/site-packages/rocketry/__init__.py", line 1, in <module>
    from .session import Session
  File "/usr/local/lib/python3.9/site-packages/rocketry/session.py", line 18, in <module>
    from rocketry.log.defaults import create_default_handler
  File "/usr/local/lib/python3.9/site-packages/rocketry/log/defaults.py", line 1, in <module>
    from redbird.logging import RepoHandler
  File "/usr/local/lib/python3.9/site-packages/redbird/__init__.py", line 2, in <module>
    from .base import BaseRepo, BaseResult
  File "/usr/local/lib/python3.9/site-packages/redbird/base.py", line 116, in <module>
    class BaseRepo(ABC, BaseModel):
  File "/usr/local/lib/python3.9/site-packages/redbird/base.py", line 153, in BaseRepo
    ordered: bool = Field(default=False, const=True)
  File "/usr/local/lib/python3.9/site-packages/pydantic/fields.py", line 675, in Field
    raise PydanticUserError('`const` is removed, use `Literal` instead', code='removed-kwargs')
pydantic.errors.PydanticUserError: `const` is removed, use `Literal` instead

JSON repository appears to call item serialization twice on update vs. replace

Building on the JSONDirectoryRepo framework, I implemented a model with a Pydantic field_serializer to convert a Decimal value to string. That worked well when calling the BaseRepo methods to add or replace files in the repository, but I turned up an unusual error when attempting to call update. I haven't tried to track it down more because I am using replace as a workaround.

From what I can tell, it appears that the update call attempts to serialize the model twice, causing a failure when the field_serializer is handed a string instead of the expected Decimal value. That points to a likely unnecessary call to item_to_dict somewhere along the way, probably in the JSONDirectoryRepo override of update.

I'm new to the codebase, and using @ManiMozaffar's fork to support Pydantic v2 - but the error appears to be in the mainline codebase. I'll attach the traceback output as a comment for reference.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.