1

So I already have a database defined in a specific way, and am building a JSON:API API around it probably using Flask-REST-JSONAPI.

The problem I am having is that some of the endpoints the API generates need to be significantly different to the database structure, and I don't want to build entirely new tables for lots of the same data because it's very dynamic and changing.

Can I build a complex query in sqlalchemy and then use that as a model? For a simple example if I need to join some tables, then filter the results, could I then turn that into a schema model to use with flask-rest-jsonapi?

If not (or if other solutions may be better suited) what other solution is there to this?

Levi H
  • 3,426
  • 7
  • 30
  • 43
  • 1
    It sounds like you want to create a view or views? Some examples at https://stackoverflow.com/questions/9766940/how-to-create-an-sql-view-with-sqlalchemy – snakecharmerb Aug 11 '20 at 12:27
  • @snakecharmerb views will not work because sqlalchemy only supports them as raw tables, not as models. flask-rest-jsonapi needs models. And you can't just build a model over a view, I tried, it wouldn't make sense because the view is missing things like relationship information. – Levi H Aug 11 '20 at 14:54

1 Answers1

0

Short answer

Yes, you can use Flask-REST-JSONAPI.

Longer answer

I think you are confused about the difference between "data layer" and "logical data extraction" in Flask-REST-JSONAPI.

  • The "data layer" is an ORM of your database structure. You can't define something else.
  • The "logical abstraction layer" can be identical to your data layer. This is the case you are describing. Also, it is the case in the Quickstart example. However, the logical abstraction layer can be very different from the data layer. To quote the leading paragraph of logical abstraction layer in the documentation:

The first thing to do in Flask-REST-JSONAPI is to create a logical data abstraction. This part of the api discribes schemas of resources exposed by the api that is not the exact mapping of the data architecture.

  • Because you can define any relationship you want by defining a Schema with Marshmallow. It is hard to find a use case which can't be implemented with Marshmallow (though it may take a while until you figure it out...).
  • Then you map the "data layer" and the "logical abstraction layer" with the resource manager. After that you can declare your endpoints.

Flask-REST-JSONAPI explains this really good in its documentation (better than I did here).


Other options

You can also use:

above_c_level
  • 3,579
  • 3
  • 22
  • 37
  • Yes I understand all that already. But as far as I understand, with flask-rest-jsonapi when you get to defining the data layer, you only have the option of defining a single model to use (and relationships)? So even though I already have expressed what I want with marshmallow, I can't use it with flask-rest-jsonapi without building my entire own data layer from scratch and implementing pagination, filtering, etc again myself. Or moving all the database logic into the marshmallow schema. – Levi H Aug 11 '20 at 18:30
  • Yes, all the logic is in the marshmallow schema. But maybe I don't understand your use case. – above_c_level Aug 11 '20 at 20:01
  • @above_level_c could you create a small example then that simply joins two tables and works properly with flask-rest-jsonapi? – Levi H Aug 12 '20 at 11:05