Sugam Devare
Sugam Devare

Reputation: 86

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

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.

Upvotes: 0

Views: 344

Answers (1)

Jura Brazdil
Jura Brazdil

Reputation: 1100

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()

Upvotes: 0

Related Questions