Code Monkey home page Code Monkey logo

redis-filtered-sort's Introduction

redis-filtered-sort

Build Status

Exports LUA script, which is able to perform multi filter operations, as well as sorts

This basically replicates http://redis.io/commands/sort but with extra features and ability to run it in clustered mode with hashed keys, which resolve to the same slot

Installation

npm i redis-filtered-sort -S

Usage

const { filter: strFilter, attach } = require('redis-filtered-sort');
const Redis = require('ioredis');
const redis = new Redis();

// adds redis.sortedFilteredList command to redis instance
attach(redis, 'fsort');

// raw Buffer of lua script
// filteredSort.script

const filter = strFilter({
  // only ids with `!mamba%` in them will be presented. Internally it uses lua string.find, so regexp is possible. Escape special chars
  // with % or use escape helper for that
  '#': '!mamba%',
  priority: {
    gte: 10, // only ids, which have priority greater or equal to 10 will be returned
  },
  name: 'love', // only ids, which have 'name' containing 'love' in their metadata will be returned
});
const offset = 10;
const limit = 20;
const sortBy = 'priority';
const expiration = 30000; // ms

// perform op
const currentTime = Date.now();

redis
  .fsort('set-of-ids', 'metadata*', sortBy, 'DESC', filter, currentTime, offset, limit, expiration)
  .then(data => {
    // how many items in the complete list
    // rest of the data is ids from the 'set-of-ids'
    const sortedListLength = parseInt(data.pop(), 10);

    // at this point you might want to populate ids with actual data about them
    // for instance, like this:
    return Promise.map(data, function populateData(id) {
      return Promise.props({
        id,
        data: redis.hgetall('metadata' + id),
      });
    });
  });

Aggregate functions

  1. use fsort to generate list of ids
  2. pass that id to aggregate function and receive results back
redis
  .fsortAggregate(ID_LIST_KEY, META_KEY_PATTERN, mod.filter({
    age: 'sum'
  }))
  .then(JSON.parse)
  .get('age')

redis-filtered-sort's People

Contributors

avvs avatar felipegcampos avatar jenyapoyarkov avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  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.