What is the most efficient method to synchronize my database and the data received from an API?

309 Views Asked by At

I'm storing user's data and articles using the Pocket API into a SQLite Database using Django Framework. How can I efficiently maintain consistency between the database and the data received from the API? I'm planning to update the database once every 24 hours.

1

There are 1 best solutions below

0
Jura Brazdil On

Should be simple with chron jobs. If you already have a model with logic to fetch data from the Pocket API and parse it, it's as simple as:

class MyCronJob(CronJobBase):
    RUN_EVERY_MINS = 120 # every 2 hours

    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
    code = 'my_app.my_cron_job'    # a unique code

    def do(self):
        data = fetch_your_data()
        save_data(data)

If you don't have the parsing logic yet, I'd suggest Django Rest Framework serializers. Using the serializers is simple and saving the data shrinks down to:

def save_data(json_data):
    serializer = YourPocketDataSerializer(json_data)
    serializer.save()