Key Value Store

Key value store (KVS)

Introduction

KVS stands for Key Value Store and it's a way to store data following a key-value structure

Structure of a KVS

For the different projects, to standardize the KVS, the IDs of fields (setted with pfqcn) will be used as keys of KVS to become more precise, facilitate and standardize the KVS structures.

  • key : id of the field + . + action
    • Example: pfqcn_phpros_posBundle_informationForm_client_input.show
{
    "id": 3,
    "key": "pfqcn_phprpos_posBundle_demo_informationForm_deliveryDate_field.show",
    "value": "1",
    "description": "Hide/Show the delivery date input (0) to hide (1) to show"
}

Example of usage in app

Because they aren't often updated, it's better to store them in localstorage

In your hooks (Eg. GetKVS)

  • Create a function to get the list of user specific KVS
    • Get the list of KVS from localstorage
    • If there is no KVS in localstorage, get the list KVS of the specific user from API Link : open-api/v3/users/logged_user_kvs/get

Example :

    const GetKVS = async () => {
        let kvs: KVS[] = [];
        if (typeof window !== "undefined" && localStorage.kvs) {
            kvs = JSON.parse(localStorage.kvs);
        }

        if (!kvs || kvs.length === 0) {
            kvs = await fetchKVS();
            localStorage.kvs = JSON.stringify(kvs);
        }

        return kvs;
    }

In your component

  • Get that KVS list

Example :

    const kvsArray = await GetKVS();
  • Get the KVS value that you need

Example :

  const showDeliveryKVS = kvsArray?.find(
    (kvs) =>
      kvs.key === pfqcn_phprpos_posBundle_demo_informationForm_deliveryDate_field + "." + show
  );
  • Use that value to restrict what you need to restrict

Example :

{showDeliveryKVS === "1" && (
    <div> Delivery date </div>
)}

Example of implementation in app