Quantcast
Channel: DHIS2 Community - Latest topics
Viewing all articles
Browse latest Browse all 1859

How can I use app-runtime to get and post data from the datastore

$
0
0

I saw the method below using getInstance from ‘d2’ get and post data to the datastore

import { getInstance } from 'd2'

export const NAMESPACE = 'dashboard'

export const hasDashboardNamespace = async (d2) =>
    await d2.currentUser.dataStore.has(NAMESPACE)

const getNamespace = async (d2) => {
    const hasNamespace = await hasDashboardNamespace(d2)

    return hasNamespace
        ? await d2.currentUser.dataStore.get(NAMESPACE)
        : await d2.currentUser.dataStore.create(NAMESPACE)
}

export const apiPostUserDataStoreValue = async (key, value) => {
    const d2 = await getInstance()
    const ns = await getNamespace(d2)

    return ns.set(key, value)
}

export const apiGetUserDataStoreValue = async (key, defaultValue) => {
    const d2 = await getInstance()
    const ns = await getNamespace(d2)
    const hasKey = ns?.keys?.find((k) => k === key)

    if (hasKey) {
        return await ns.get(key)
    } else {
        await apiPostUserDataStoreValue(key, defaultValue, ns)
        console.log('(These errors to /userDataStore can be ignored)')
        return defaultValue
    }
}

How can I use the useDataQuery, useDataMutation from app-runtime to do same? I mean the resource endpoint to call and if mutation is allowed in datastore

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 1859

Trending Articles