Code Monkey home page Code Monkey logo

dozer-react's Introduction


Connect any data source, combine them in real-time and instantly get low-latency gRPC and REST APIs.
⚡ All with just a simple configuration! ⚡️


CI Coverage Status Docs Join on Discord License


Overview

This repository is a react helpers for using Dozer as data provider. It contains 3 hooks useCount, useQueryCommon, useOnEvent

Installation

yarn add @dozerjs/dozer-react

Usage

useCount(endpoint: string)

This hook returns number of records in endpoint.

import { useCount } from "@dozerjs/dozer-react";
// ...

const AirportComponent = () => {
    const [count] = useCount('airports');

    return <span>Total airports count: {count}</span>
}

useQueryCommon(endpoint: string, query: string | null = null)

This hook can be used for getting data from cache. It allows to pass query. Query is json object serialized as string.

import { useQueryCommon } from "@dozerjs/dozer-react";
// ...

const AirportComponent = () => {
    let query = {
      orderBy: {
        start: Order.ASC
      }
    }
    const [records, fields] = useQueryCommon('airports', query);
    
    return <>{records.map(r => <div>{ r.name }</div>)}</>
}

useOnEvent(endpoint: string, cb: (data, fields, primaryIndexKeys, mapper) => ()

UseOnEvent hook can be used for getting data updates from dozer. It uses callback to pass operations. Callback has 4 arguments:

  • data - Operation event. Reference to structure can be found here
  • fields - Fields array, which are used for mapping operation data.
  • primaryIndexKeys - Array of primary key fields indexes
  • mapper - Mapper instance, which can be used for converting data.
const AirportsComponent = () => {
  const [airports, setAirports] = useState([]);
  
  useOnEvent('airports', (data, fields, primaryIndexKeys, mapper) => {
    if (fields.length) {
      setAirports(records => {
        if (data.getTyp() === OperationType.UPDATE) {
          let oldValue = mapper.mapRecord(data.getOld().getValuesList());
          let existingIndex = records.findIndex(v => primaryIndexKeys.every(index => v[index] === oldValue[index]));

          if (records.length > 0) {
            if (existingIndex > -1) {
              records[existingIndex] = mapper.mapRecord(data.getNew().getValuesList());
              return [...records];
            } else {
              return [...records, mapper.mapRecord(data.getNew().getValuesList())];
            }
          }
        } else if (data.getTyp() === OperationType.INSERT) {
          return [...records, mapper.mapRecord(data.getNew().getValuesList())];
        }

        return records
      });
    }
  });
  
  return <>{airports.map(airport => <div>{ airport.name }</div>)}</>
}

dozer-react's People

Contributors

karolisg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.