API Star + DynamoDB / NoSQL database?


#1

Hi,
I understand that API Star can use the django ORM but it seems that it cannot support NoSQL databases.
I would like to use DynamoDB as a database for API Star, I’ve found PynamoDB that can be helpful, but I’m not sure this can be used with API star in some way.

Anyone have used API Star + DynamoDB or other NoSQL databases?

p.s. I’m planning to deploy api star with zappa

Thanks


#2

you could always make your own third party extension apistar-dynamodb or something similar, you can look at how the integrations with SQLAlchemy and Django’s ORM were done and utilize PynamoDB as you stated above to implement it.


#3

Thanks for the reply,
I cannot just create a db-related file and load it when I need to update/create documents?
or this approach is inefficient and will not work correctly with zappa?


#4

No idea sorry :frowning:


#5

I would create a component and inject that into your views. It worked for me with mongodb, so I would image it would work for you.


#6

Can you please show me an example? mongodb is also fine

p.s. Are you running api star with zappa?


#7

First, I am not running api star with zappa, but I think this might work.
My code looks like this.

database.py

from pymongo import MongoClient
from apistar.types import Settings
from bson import ObjectId

class Database(Component):
    def __init__(self, uri):
        self._db = MongoClient(uri).get_default_database()

    def get_thing_by_id(self,id):
        return self._db.things.find_one({
            '_id':ObjectId(id)
        })

def init_database(settings: Settings):
    return Database(settings['MONGO_URI'])
    

app.py

from apistar import Include, Route, Component
from apistar.frameworks.wsgi import WSGIApp as App
from apistar.handlers import docs_urls
from database import Database, init_database



def get_thing(id:str, db:Database):
    """
    The view that is interacting with the database
    """
    return db.get_thing_by_id(id)


settings = {
    'MONGO_URI': 'mongodb://localhost:27017/InvestmentClubManager',
}

components = [
    Component(Database, init=init_database, preload=True)
]

routes = [
    Route('/things/{id}', 'GET', get_thing),
    Include('/docs', docs_urls)
]

app = App(
    settings=settings,
    routes=routes,
    components=components
    )


if __name__ == '__main__':
    app.main()

It should be easy to convert this pattern to DynamoDB (although I have not looked at the docs).
Hope this helps!


#8

Thank you VERY much for this code snippet, this is what I was looking for!

Actually mongodb is fine, the only issue could be w/ zappa/lambda, I will do some tests and maybe update the thread.

Thanks again


#9

I currently have API* on serverless/zappa with DynamoDB in production. Basically i use boto3, so nothing fancy. I would be glad to spin up a library for this.


#10

Any chance you could share an example of how you’re using dynamodb with Apistar?


#11

I think the way i do it is quite simple, hence i do not utilize the models functionality in any way whatsoever. I basically utilize boto3 directly and do the writing to the DB through this channel. This can be done much better and i would be more than glad to jump into constructing a component/helper function to utilize this much more ideally in api*


#12

If anyone is interested, it could be interesting to build a plugin or component with DynamoDB that is compatible with apistar typesystem.


#13

Dataset works with Apistar and you dont even need a component,plugin,etc. Works as nosql with postgres.