Giter Club home page Giter Club logo

vexdb's Introduction

VexDB

Deprecation Notice: This module is no longer recieving new features. If you need to access data from RobotEvents, use robotevents which uses the new RobotEvents API v2.

Build Status codecov npm

vexdb is a wrapper for VexDB.

Features

Installation

Install this package with yarn:

yarn add vexdb

or, if you want to use npm:

npm i vexdb

API Primer

GET

Retrieves data from an endpoint with the specified parameters. These parameters can include any that can be specified to VexDB, as well as final values in the response object.

Normally, vexdb limits responses to 5000 items per request. vexdb will make enough requests to ensure that all applicable matches are returned

// Get all events in StarStruck
var vexdb = require("vexdb");
vexdb.get("events", { season: "StarStruck" }).then(console.log);

// Get all teams from California
vexdb
  .get("teams", {
    region: "California",
  })
  .then(console.log);

Size

This works basically identically to .get(), but returns the number of items that fit this result.

// Get the number of all teams in California
vexdb.size("teams", { region: "California" }).then(console.log);

// All examples from .get() above would work here...

Depending on the parameters specified, size() may or may not send nodata requests. In order to minimize bandwidth, you'll want to only include parameters that can be passed directly to VexDB

Defaults

In many cases, you'll want to share headers and parameters across requests. This can be done using vexdb.constants.header and vexdb.constants.param, respectively:

vexdb.constants.param({
  region: "California",
});

vexdb.constants.header({
  "User-Agent": "<custom user agent string>",
});

Warning: Because of the Cross Origin Policy, setting headers using vexdb.constants.header may cause the browser to automatically block requests to https://api.vexdb.io. It is not reccomended to use headers in the browser.

Caching

Since VexDB only updates every 4 minutes, this module will prevent repeat requests by resolving them with the previous value immediately. You can control this behavior with vexdb.cache

Note: vexdb uses my own keya module to handle cache. In Node, caching will take place using sqlite, while in the browser, caches will be stored first in IndexedDB

Update the Time To Live for new caches

vexdb.cache.setTTL(60 * 1000);

See if a cache is present

vexdb.cache
  .has("teams", {
    region: "South Carolina",
  })
  .then(console.log); // Boolean

Directly resolve a cached value

vexdb.cache.resolve("skills", { region: "Utah" }).then(console.log); // The resolved value, or undefined if the cache isn't present

Clear the cache

vexdb.cache.clear().then(() => console.log("Cache has been cleared"));

Live

This module also supports basic live features. Specify an endpoint and parameters (passed through to get()) and recieve updates on new items that fit that criteria

vexdb
  .live("matches", {
    scored: 1,
    sku: "RE-VRC-17-3805",
  })
  .on("item", console.log);

Note that the item event will trigger for every result on the inital poll. This means that every item that fits the parameters will be passed to item. If you do not want this to be the case, specify prefetch: true in your listed parameters

// Only new matches will trigger item
vexdb
  .live("matches", {
    scored: 1,
    sku: "RE-VRC-17-3805",
    prefetch: true,
  })
  .on("item", console.log);

Events

fetch

  • newItems Object[]

Emitted on a successful fetch

prefetch

  • results Object[]

Emitted when a prefetch has been completed

item

  • item Object

Emitted for each new item

close

Emitted for each new item


API

close(): undefined Stop new polls

params(newParams: Object): Object Override request parameters for future requests. Note: this does not change the current index of results

current(): Object[] Returns the current cache of results

vexdb's People

Contributors

brenapp avatar dependabot[bot] avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

vexdb's Issues

getAll errors if no params are passed

.getAll() will throw an error if no parameters are passed

  1 failed

  getAll » .getAll() resolves for correct requests
  C:\Users\bmcgui7575\Documents\vexdb\main.js:185


   185:             (v, i) => get(endpoint, Object.assign(params, { limit_start: 5000 * i }))


  Error: Cannot convert undefined or null to object

  Promise.all.map (main.js:185:44)
  size.then.items (main.js:184:12)

This is because of the lack of || {} after params

Increase intelligence of `size()`

Right now, size is really stupid:

async function size(endpoint, params) {
    return get(endpoint, params)
        .then(res => res.length)
}

This can be improved by:

  • Sorting parameters so more nodata: true requests can be made
  • Making more raw requests

Not Caching nodata requests?

Steps to reproduce:
Insert a debug line into the request function to see what network requests are made:

async function request(endpoint, params) {
    if (cache.has(endpoint, params)) return cache.resolve(endpoint, params);
    console.log("GET", endpoint, params);
const vexdb = require("vexdb")
vexdb.get("matches", {
    sku: "RE-VRC-17-3805",
    team: async team =>
        (await vexdb.size("teams", {
            team,
            region: "South Carolina"
        })) > 0
}).then(console.log)

Run the code and see that the request for GET teams { region: 'South Carolina', nodata: true } is being called for every match evaluated

Move away from axios

This library is designed to be isomorphic and in order to do that, I have used axios, which is an isomorphic Promise-based fetch library. However, it is 10Kb big built and gzipped and has many features which are not required for this application. I will look to create a tiny promise-based library to replace it

API design is too complicated right now

vexdb tries to do too much for the user. vexbot, which is one of the most in-depth uses for vexdb never uses any of the advanced features, and basically only needs caching, nice type support, and getting all records on a request. Right now, vexdb is trying to do too many things, and it's inflating the source code.

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.