# hypercache

> Simple but powerful caching.

Latest version **0.2.0** (published 2017-07-10) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install hypercache
pnpm add hypercache
yarn add hypercache
bun add hypercache
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2017-07-10 |
| First published | 2017-06-27 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Maciej Krüger |
| Maintainers | mkg20001 |
| Keywords | db, caching, sync, fast |

## Links

- npm: https://www.npmjs.com/package/hypercache
- Repository: https://github.com/mkg20001/hypercache
- Homepage: https://github.com/mkg20001/hypercache#readme
- Issues: https://github.com/mkg20001/hypercache/issues
- npm.io page: https://npm.io/package/hypercache

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.2.0 (latest) — 2017-07-10
- 0.1.0 — 2017-07-05
- 0.0.2 — 2017-06-27
- 0.0.1 — 2017-06-27
- 0.0.1-pre — 2017-06-27

## README

# Hypercache [![Build Status](https://travis-ci.org/mkg20001/hypercache.svg?branch=master)](https://travis-ci.org/mkg20001/hypercache) [![codecov](https://codecov.io/gh/mkg20001/hypercache/branch/master/graph/badge.svg)](https://codecov.io/gh/mkg20001/hypercache)

Simple but powerful caching.

Hypercache caches async db operations and gives you the abbility to get things by id or search the database completly sync and with speeds otherwise impossible.

# Why hypercache?

-   ## Fast
-   ## Zero-Dependencies
-   ## 100% Test Coverage

# Usage

`new Hypercache(fnc, options)`

-   `fnc`: A function that is called as `fnc(cb)`
    -   `cb(err, res)`
    -   `err`: The error that occured, if any
    -   `res`: An array of objects
-   `options`
    -   `name`: An optional name.
        If not provided the name will be guessed based on the function
    -   `keys`: A list of unique properties like `["id"]`
    -    `sets`: A list of non-unique (also array) properties like `["aliases"]`
    -   `interval`: The interval to refresh in ms (default `2500`)
    -   `manual`: This will disable the `fnc` and `interval` and will expose an `update` function that is called as `update(res)`
    -   `sync`: A hypercache to sync with. This will override the call for fnc to `fnc(opt.sync.getAll(), cb)` and disable the interval.

# Events

`ready`: Emitted when the first update occurs

`update`: Emitted when an update occurs

`error`: Emitted when a function without a callback throws an error

# Methods

`getAll()`: Returns an array of all elements

`getMap(name)`: Returns the id->object map for key `name`

`getBy(id, value)`: Returns an object with `[id]==value`

`getSetMap(name)`: Returns the id->set map for key `name`

`getSet(id, value)`: Returns an array with `[id]==value || [id].indexOf(value)`

`search(str)`: Searches through all keys and returns the first with `[key]==str`

`searchSets(str)`: Seracher through all sets and returns the first with `[key]==value || [key].indexOf(value)`

# Examples

Fetch and cache data from mongoDB

```js
const cache = new Hypercache(cb => Users.find({}, cb), {
  keys: ["id", "username"],
  sets: ["aliases"]
})
cache.once("ready", () => {
  cache.getBy("username", "mkg20001") // {username: "mkg20001", id: "1234", aliases: ["mkg", "krüger"], ...}
  cache.search("1234") // {username: "mkg20001", id: "1234", aliases: ["mkg", "krüger"], ...}
  cache.searchSets("mkg") // {username: "mkg20001", id: "1234", aliases: ["mkg", "krüger"], ...}
})
```

---
_Source: https://npm.io/package/hypercache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
