Database

What is Shypyard database?

Every app comes with a simple managed key/value database system you can use in your task. The data are fully independent and private. This allows you to persist state across different task runs so it can do more complicated automations.

For example, lets say you want to automatically update product description when it is out of stock. You may want to store the original description in the database when the inventory reaches 0, and then upon re-stocking, read from the database and re-store the old description.

Usage Example

Using the example from earlier, here's a pseudo code to show how database can be used.

const { auths, libs } = require('platform');

module.exports = async (event, options) => {
  const shopify = libs.shopify.get();
  const db = libs.shypyard.get().db();
  
  // get full product detail
  const product = await shopify.product.get(event.data.id);
  const key = `old-product-description:${product.id}`;
  
  if (product.inventory == 0) {
    // when product is out of stock, change description and 
    await db.set(key, { val: product.description_html });
    
    await shopify.product.update(
      product.id, 
      { description_html: '....some new description' },
    );
  } else {
    // restore old description
    const {val: oldVal} = await db.get(key);
    await shopify.product.update(
      product.id, 
      { description_html: oldVal },
    );
  }
};

Web UI

You can see the database value by going to Storage > Database section. Here you can do basic filtering on key prefix. Note that every database key also automatically track a createdAt attribute.

See detailed documentation

Please see the Shypyard SDK documention on shypyard database.

Last updated