npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dhis2/app-service-datastore

v1.0.0-beta.3

Published

> **WARNING**: THIS SERVICE IS STILL A WORK-IN-PROGRESS, THE API WILL PROBABLY CHANGE!

Downloads

1,310

Readme

DataStore App Service

WARNING: THIS SERVICE IS STILL A WORK-IN-PROGRESS, THE API WILL PROBABLY CHANGE!

This DataStore app service support persistent user and global application settings as well as saved (and sharable) objects, such as visualization configurations.

This library was bootstrapped with DHIS2 Application Platform.

Installation

yarn add @dhis2/app-service-datastore

Features

  • Save and load application settings from a well-known dataStore (or userDataStore) key
  • Create, read, update, and delete saved objects (i.e. visualizations) from a managed key-value store in the dataStore or userDataStore
  • Client-side syncronized state - automatically re-render all components which use a setting or object when that setting or object is updated somewhere else in the application (no refetch required)
  • Optimistic updates - propagate "provisional" data to all consumers while mutation is in-transit, roll back changes if the mutation fails
  • Optionally encrypt settings data at rest

API

DataStoreProvider props

| Name | Type | Required | Default | Description | | --------------------- | ----------------- | ------------ | ----------- | -------------------------------------------------------------------------------- | | namespace | Boolean | REQUIRED | | The namespace to use | | loadingComponent | React Component | | null | A component to render during initial load | | defaultGlobalSettings | Object | | {} | Default settings to save in the dataStore | | defaultUserSettings | Object | | {} | Default settings to save in the userDataStore | | encryptSettings | boolean | | false | If true, encrypt all settings at rest (important if credentials could be stored) |

Hooks

This library provides four main hooks:

type useSetting = (
  id: string,
  options?: HookOptions
) => [value: any, { set: (newValue: any) => Promise<void> }];

type useAllSettings = (
  options?: HookOptions
) => [
  settings: Record<string, any>,
  { set: (key: string, value: any) => Promise<void> }
];

type useSavedObject = (
  id: string,
  options?: HookOptions
) => [
  obj: object,
  {
    update: (obj: object) => Promise<object>;
    replace: (obj: object) => Promise<void>;
    remove: () => Promise<void>;
  }
];

type useSavedObjectList = (
  options?: HookOptions
) => [
  list: object[],
  {
    add: (obj: object) => Promise<void>;
    update: (id: string, obj: object) => Promise<object>;
    replace: (id: string, obj: object) => Promise<void>;
    remove: (id: string) => Promise<void>;
  }
];

Each of the hooks accepts an optional options object:

type HookOptions = {
  // If true, store this setting or object in the dataStore instead of userDataStore
  global: boolean;

  // If true, do NOT rerender this component when the value is changed somewhere else in the application
  ignoreUpdates: boolean;
};

There is one additional hook which exposes the DataStore controller for imperative access (advanced):

type useDataStore = () => DataStore;

Usage

Wrap the application in a DataStore provider

import React from "react";
import { DataStoreProvider } from "@dhis2/app-service-datastore";
import AppRouter from "./AppRouter";

const App = () => (
  <DataStoreProvider namespace="myAppName">
    <AppRouter />
  </DataStoreProvider>
);

export default App;

Reading settings

import React from "react";
import { useSetting, useAllSettings } from "@dhis2/app-service-datastore";

const MyComponent = () => {
  // All data-store settings for the current user
  const [allUserSettings] = useAllSettings();
  // All data-store settings within the namespace
  const [allGlobalSettings] = useAllSettings({ global: true });
  // A specific setting for the current user
  const [aUserSetting] = useSetting("id-1");
  // A specific global setting
  const [aGlobalSetting] = useSetting("id-1", { global: true });

  return "Something";
};

export default MyComponent;

Reading saved objects

import React from "react";
import {
  useSavedObject,
  useSavedObjectList,
} from "@dhis2/app-service-datastore";

const MyComponent = () => {
  // All saved objects for the current user
  const [allUserSavedObjects] = useSavedObjectList();
  // All saved objects within the namespace
  const [allGlobalSavedObjects] = useSavedObjectList({ global: true });
  // A specific saved object for the current user
  const [aUserSavedObject] = useSavedObject("id-1");
  // A specific global saved object
  const [aGlobalSavedObject] = useSavedObject("id-1", { global: true });

  return "Something";
};

export default MyComponent;

Mutating settings and saved objects

import React from "react";
import {
  useSavedObject,
  useSavedObjectList,
  useSetting,
  useAllSettings,
} from "@dhis2/app-service-datastore";

const MyComponent = () => {
  // A setting for the current user
  const [userSetting, { set }] = useSetting("id-1");

  // All settings for the current user
  const [userSettings, { set }] = useAllSettings();

  // A saved object for the current user
  const [savedObject, { update, replace, remove }] = useSavedObject("id-1");

  // All saved objects for the current user
  const [
    allUserSavedObjects,
    { add, update, replace, remove },
  ] = useSavedObjectList();

  return "Something";
};

Report an issue

The issue tracker can be found in DHIS2 JIRA under the LIBS project.

Deep links: