loadConfig()

loadConfig centralizes JSON config retrieval, streamlining backend access with robust error handling for graceful responses in cases of missing values, ensuring enhanced reliability.

Example:

import { createBackendFunction, data, loadConfig } from "@magicjs.dev/backend";
import configJson from './config.json';

const config = loadConfig(configJson);

export default createBackendFunction(async function (keyword) {
  try {
    const itemCollection = data(config.getValue('mongoDbCollectionName'));
    const items = await itemCollection.find({}).toArray();
    return items;
  } catch (error) {}
})

And the config.json file:

{
  "properties": [
    {
      "name": "mongoDbCollectionName",
      "value": "items"
    },
    {
      "name": "label",
      "value": "All Products"
    }
  ],
  "label": "Item",
  "aliases": []
}

Click here to refer GitHub.

Last updated