Models in pod structure under subfolder


#1

Hello, I have the following structure of a grown project where we need to implement a few bigger changes. We want to separate the old code base from the new features, but unfortunately we have a few difficulties. We decided to use pods at some point so we thought we create the following structure

app
  models
    model1.js
  pods
    subfolder
      model1
      index

In a normal app you probably only access to one api, unfortunately we have 2 at this stage and to make matters worse, the old one uses the ActiveRecordAdapter and the new one will use a JsonAPIAdapter. So all models in the pod subfolder should use JSONAPI and the rest just use the activeModelAdapter. This can be done pretty easy by placing an adapter file into each model folder, but I am wondering if there is a better way, like the application adapter which sets the default, but for pod a folder? We also need to use serializers for each model, which should also use a default serializer, but current has to be placed in every single model folder. I hope I could explain this good enough.

Cheers Thomas


#2

Hey Thomas, I haven’t used pods so I’m not sure if this would work any differently with a pods structure, but what I would do is have two “base” adapters/serializers, your ActiveRecord “default” adapter/serializer and your JSON API “default” adapter/serializer, then each model-specific serializer/adapter extends the base. For example if you had a model called model1 and it was using the JSON API backend, and you had a model called model2 using the ActiveRecord backend, you could have the following adapters:

  • application - extends JSON API adapter
  • `activerecord - extends ActiveRecord adapter
  • model1 - extends application adapter
  • model2 - extends activerecord adapter

And then likewise for serializers. This way you don’t need to make copies of the “base” serializers in each model serializer which I think is what you were saying. Obviously you still need an adapter/serializer file for each model then, but it would only be a few lines specifying which of your base adapters you are extending and then exporting.

In one of our apps we use two backends: our custom API and firebase. Our application adapter is extended from REST Adapter and then most of our models extend that adapter but we also have a few models that use firebase, and those we extend from our “base” firebase adapter.

Hope that makes sense/is helpful. Let me know if you have any questions about it.


#3

Thats exactly what I did in the end. I wanted to write it down today as a possible solution, but you did it for me :slight_smile: Thx.