deta/deta-python

Suggestion. Allow to create a local Deta Base

legioz opened this issue · 6 comments

Congratulations for this amazing project!
I would like to make a suggestion for the Deta Base.
It will be great if you allow users to create a local database using the same Deta class, this could be helpful in some cases, like the user user has no internet connection to deploy, but still want to keep up coding his project, or just want to keep the files locally.

Could be something like this:

deta = Deta('my_db', local=True)
# creates a local db confi/serving folder
books = deta.Base("books")
# creates books.json file on my_db/

Thanks for your support ;)

I also think this would be an important feature to implement from a testing perspective. As it stands if someone were to use deta base it would be expected you would have to make a test project to have a test database, meaning anyone in the future who wants to run database tests has to have a deta account and internet access. It would be nice if there was a local mock of the database to test against.

Thanks @luizfelipevbll and @boanergies for bringing this to our attention!
We think this feature is very useful but rarely a blocker. In a addition, it will require a bit more work to make sure it woks across our JS/TS/Browser/HTTP/etc SDKs. As such, this feature sits low on our priority list.

Any updates on this? I can help if you're looking to add this feature.

If it is only for Deta Base then you can write a tiny wrapper with this boolean flag local. When you set it to true, then create or access from the local JSON file. Example:

from deta import Deta
import json, os

deta = Deta("")
class DetaBase:
    def __init__(self, db_name: str, local: bool=False):
        self.db = deta.Base(db_name)
        self.local = local

    def get(self, key: str):
        if self.local:
            with open(f"local/{self.db_name}/{key}.json", "r") as f:
                return json.load(f)
        return self.db.get(key=key)

    def put(self, key: str, data: dict):
        if self.local:
            with open(f"local/{self.db_name}/{key}.json", "w") as f:
                json.dump(data, f)
        return self.db.put(key=key, data=data)

    def delete(self, key: str):
        if self.local:
            os.remove(f"local/{self.db_name}/{key}.json")
        return self.db.delete(key=key)

I quickly wrote this. So didn't get to add other methods like insert, fetch, put_many and all that. But basically, this should help you with immediate requirements.

hi @harshitsinghai77 & @navhits

your comments demonstrate why this is not such a small task:

  1. we need to model our Base backend, especially regarding consistency and support fetch queries
  2. make sure this local "Base" works across all SDKs of other languages.

the best way to test and dev with Base is to use the actual live service.

closing this for now as we don't have any immediate plans to implement it and because the community won't be able to replicate the behavior of Base reliably.