skip to Main Content

This is a general question about how to deal with shifting data dependencies in an API.

I depend upon two data objects in my application:

1. mycompanyProducts
2. mycompanySettings

The products object looks like:

{
  "MYTHING01": {
    "sku": "MYTHING01",
    "name": "15-Pack",
    "price": 30,
    "shopifyData": {
      "id": "7987037995068",
      "product_id": "664292996940",
      "price": "30.00",
      "taxable": true,
      "inventory_quantity": 10,
      "weight": 2,
      "weight_unit": "kg",
      "lastFetched": {
        "$date": "2018-05-09T14:16:57.209Z"
      }
    }
  },
  "MYTHING02": {
    "sku": "MYTHING02",
    "name": "5-Pack",
    "price": 15,
    "shopifyData": {
      "id": "7836960168028",
      "product_id": "645959671756",
      "price": "15.00",
      "taxable": true,
      "inventory_quantity": 10,
      "weight": 2,
      "weight_unit": "kg",
      "lastFetched": {
        "$date": "2018-05-09T14:16:57.228Z"
      }
    }
  }
}

And the settings object simply contains application settings, like maximum image upload size in MB, etc.

Both of these objects are coming from MongoDb collections.

Here’s the idea: I want to be able to update my collections, and have my API update its dependencies without having to rebuild. It would be very cumbersome to have to trigger API rebuilds based on Shopify webhooks (where some of the changes are coming from), etc.

Each product has a shopifyData.lastFetched timestamp, and when accessing the data, if the timestamp is more than some period (10 minutes) in the past, I want to refresh the data before continuing:

// Loop through each Shopify product and update the product in the db
await Promise.all(res.data.products.map(async (shopifyProduct) => {
  const variant = shopifyProduct.variants.find(v => mySKUs.includes(v.sku))
  product.shopifyData = {
    ...variant,
    lastFetched: new Date(),
  }
  await product.save()
}))

The question is: how can I structure my API to use updating dependencies? Obviously ES6 modules export statically, and won’t change until the application is rebuilt.

I can of course export a function that returns the dependencies, and call this function when I need it for fresh deps, but the problem is that my function is necessarily async, so it’s hard to call:

import React from 'react'
const deps = generateDeps() // can't use await in this scope...

Moreover, putting const deps = generateDeps() in the top of my files doesn’t really solve the issue anyways, as this would be evaluated once, and only once.

How do people solve this problem?

2

Answers


  1. You can import the function and call it async in your code :

    import {generateDeps} from './depsGenerator'
    //...
    generateDeps().then(() => {
        //...
    })
    

    That way the timeout would be checked at every call

    Login or Signup to reply.
  2. Export a function and call it whenever you actually need to access the data, instead of at the top of a module.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search