Git Product home page Git Product logo

redis-hashes's Introduction

redis-hashes

Build Status Dependency Status npm version

redis hash utilities for nodejs

Note!: From v0.5+ all indexes keys will be using format :indexes::...

Requirements

Installation

$ npm install --save redis-hashes

Usage

//initialize redis-hashes with default options
const redis = require('redis-hashes')([options]);

//save single user
const user = ...;
hash.save(user, {collection: 'users' }, done);

//save multiple users
const users = ...;
hash.save(users, {collection: 'users' }, done);


//get single user
hash.get(<id>, function(error, user){
   ...
});

//get single user and select specified fields
hash.get(<id>, { fields: 'name, email'}, function(error, user){
   ...
});


//get multiple users
hash.get(<id>, <id>, function(error, users){
   ...
});

//search users collection
hash.search(<search_query>, function (error, users) {
    ...
});


//count number of users
hash.count('users', function(error, counters){
  ...
});


//count number of users and orders
hash.count('users', 'orders', function(error, counters){
  ...
});


//remove single users
hash.remove(<id>, function(error, results){
   ...
});


//remove multiple users
hash.remove(<id>, <id>, function(error, results){
   ...
});

...

Options

  • prefix:String - redis key prefix. default to r
  • separator:String - redis key separator. default to :
  • redis:Object|String - redis connections options or string.

To initialize redis with custom options use

const redis = require('redis-hashes')({
    prefix:'q',
    separator:'-',
    redis: {
    port: 6379,
    host: '127.0.0.1'
  }
});

...

or

const redis = require('redis-clients')({
    prefix:'q',
    separator:'-',
    redis: 'redis://localhost:6379'
});

API

save(objects:Object|Object[],[options:Object],done:Fuction)

Store(create or update) object(s) into redis hash datatype. Before saving the whole of object is flatten'ed and serialized. i.e all dates will be converted to timestamps etc.

Options:

  • index:Boolean - whether to index the object or not for search. default to true.
  • collection:String - name of collection or namespace used in prefix hash keys. default to hash
  • ignore: Array[String] - Collection of object fields to ignore when indexing
const object = ...;
hash.save(object, function (error, saved) {
    ...
});

const user = ...;
hash.save(user, {collection: 'users' }, function (error, saved) {
    ...
});

//or bulk save
const objects = ...;
hash.save(objects, function (error, objects) {
    ...
});

const users = ...;
hash.save(users, {collection: 'users' }, function (error, users) {
    ...
});

get(...keys,[{ fields: String|String[] }],done:Function)

Get single or multiple saved object using their keys

//get single
hash.get(<id>, function(error, object){
   ...
});

//get single
hash.get(<id>, { fields: 'name, email'}, function(error, object){
   ...
});

//get multiple object
hash.get([<id>, <id>], function(error, objects){
   ...
});

//get multiple object
hash.get([<id>, <id>], { fields: 'name, email'},  function(error, objects){
   ...
});

//get multiple object
hash.get(<id>, <id>, function(error, objects){
   ...
});

//get multiple object
hash.get(<id>, <id>, { fields: 'name, email'}, function(error, objects){
   ...
});

search(options:String|Object,[{ fields: String|String[]}],done:Function)

Search existing objects.

Options:

  • type:String - type of reds search. default to or
  • collection:String - name of collection used in searching. default to hash
  • q: String - query string. default to ''
//search default collection
hash.search(<search_query>, function (error, objects) {
    ...
});

//search default collection and select specified fields
hash
  .search({q:<search_query>, fields: 'name, email'}, function (error, objects) {
    ...
});

//search specific collection
hash.search({
    q: <search_query>,
    collection: 'users',
    type:'or'
  }, function (error, objects) {
    ...
});

//search specific collection and select specified fields
hash.search({
    q: <search_query>,
    collection: 'users',
    type:'or',
    fields: 'name, email'
  }, function (error, objects) {
    ...
});

remove(...keys,done:Function)

Remove single or multiple saved object using their keys

//remove single
hash.remove(<id>, function(error, object){
   ...
});

//remove multiple object
hash.remove([<id>, <id>], function(error, objects){
   ...
});


//remove multiple object
hash.remove(<id>, <id>, function(error, objects){
   ...
});

count(...collections, done:Function)

Count number of saved object(s) on specified collection(s)

//count in single collection
hash.count('users', function(error, counters){
   ...
});

//count in multiple collections
hash.count('users', 'orders', 'contacts', function(error, counters){
   ...
});

References

Testing

  • Clone this repository

  • Install all development dependencies

$ npm install
  • Then run test
$ npm test

Contribute

It will be nice, if you open an issue first so that we can know what is going on, then, fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.

Licence

The MIT License (MIT)

Copyright (c) 2017 lykmapipo & Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

redis-hashes's People

Contributors

lykmapipo avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

redis-hashes's Issues

"hash.count" issue

When I call

hash.count('users', function(error, counters){
  console.log(counters);
});

It error:

TypeError: redis.key is not a function
    at Object.exports.key (/Users/wangzhang/git/stcpd-node-app/node_modules/redis-hashes/src/hash.js:195:22)
    at /Users/wangzhang/git/stcpd-node-app/node_modules/redis-hashes/src/hash.js:673:22
    at arrayMap (/Users/wangzhang/git/stcpd-node-app/node_modules/lodash/lodash.js:631:23)
    at Function.map (/Users/wangzhang/git/stcpd-node-app/node_modules/lodash/lodash.js:9546:14)

Search and count methods throws errors

The search and count methods throw errors if used immediately after module import

const redis = require('redis-hashes')({
    prefix: 'r',
    separator: ':',
    redis: {
        port: 6379,
        host: '127.0.0.1'
    }
});

//search specific collection
redis.search({
    q: '1',
    collection: 'users',
    type: 'or',
}, function (error, objects) {
    if (error) {
        console.log('Error in query', error);
    } else {
        console.log(`result of query`, objects);
    }
});

Throws this error

const _key = redis.key([].concat(...args));
                   ^

TypeError: redis.key is not a function
    at Object.exports.key (/Users/DemoUser/Projects/redis/node_modules/redis-hashes/src/hash.js:195:22)

But this works


const redis = require('redis-hashes')({
    prefix: 'r',
    separator: ':',
    redis: {
        port: 6379,
        host: '127.0.0.1'
    }
});

//get single
redis.get('1', function (error, object) {
    if (error) {
        console.log(`error while getting data`, error);
    } else {
        console.log(`Success getting data`, object);
    }
});

//search specific collection
redis.search({
    q: '1',
    collection: 'users',
    type: 'or',
}, function (error, objects) {
    if (error) {
        console.log('Error in query', error);
    } else {
        console.log(`result of query`, objects);
    }
});

Deleting keys does not work

redis.all({
    collection: 'users',
    fields: '_id'
}, function (error, data) {
    console.log(`Success getting all data`, data);
    redis.remove(...data, function (error, objects) {
        console.log('remove success', objects);
    });
});


Returns this error:

node_redis: Deprecated: The DEL command contains a argument of type Object.
This is converted to "[object Object]" by using .toString() now and will return an error from v.3.0 on.

This is the output generated on redis-cli monitor

"del" "[object Object]" "[object Object]" "[object Object]" "[object Object]"

However these items still exist in redis, and running get all returns the keys that should have been deleted.

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.