Custom plugin fetching backend data from API instead of DB

I have a need to build a plugin where, the model does CURD operations on an API data instead of data from Database.

Is it possible to do it?

Please give examples.

Here is one idea: First, determine the immutable data for each record and save a twin record in your local database. Then use the ExpandoModel type to capture and cache the record’s data locally. Use the model events to replenish and broadcast values.

This is a basic example, without caching:

class ExternalRecord extends \October\Rain\Database\ExpandoModel
    // Store dynamic data as json in the "value" column on the table
    protected $expandoColumn = 'value';

    // These attributes are immutable and stored directly as table columns
    protected $expandoPassthru = ['uuid'];

    // Request the attributes from the server
    public function afterFetch()
        $data = Http::get('https://myendpoint.tld/fetch')->json();

        $this->attributes = array_merge($this->attributes, $data);

    // Post the saved data to the API endpoint
    public function afterSave()
        Http::post('https://myendpoint.tld/save', $this->attributes);

@daft Thank you so much.

Can you please explain me a bit. It looks way too technical for me. It will be a lot helpful if you can explain it a bit.

The code above is just an idea. It still needs work.

The idea is you make a ‘digital twin’ for each item in your API. There should be some identifier, some immutable value, that can be used to guarantee that a record in your database matches a record in the API.

You can make copies of the API records and use the database as a caching layer. The database records can also be used as backend data.

There should be some worker that checks the API for new records against the database records.

Model events (afterFetch, afterSave) can keep the data in synchronisation.

I hope this helps.