File: test/js/nosql/redis.js

Recommend this page to a friend!
  Classes of Nikos M.  >  Dialect PHP ORM Library  >  test/js/nosql/redis.js  >  Download  
File: test/js/nosql/redis.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Dialect PHP ORM Library
Store and retrieve objects in database using ORM
Author: By
Last change:
Date: 1 month ago
Size: 2,110 bytes
 

 

Contents

Class file image Download
"use strict";

module.exports = function( DialectNoSql ) {

var redis = null;

// requires redis node module
// https://github.com/NodeRedis/node-redis
try {
    redis = require('redis');
} catch(e) {
    redis = null;
}

function is_obj(arg)
{
    return Object.prototype.toString.call(arg) === '[object Object]';
}

class RedisStorage extends DialectNoSql.INoSql
{
    client = null;
    keyPrefix = '';

    constructor(options = {})
    {
        super();
        this.client = redis.createClient(options['port']||6379, options['host']||'127.0.0.1');
        this.keyPrefix = String(options['namespace']||'');
        this.client.on('error', function(err) { throw err; });
    }

    vendor()
    {
        return 'redis';
    }

    supportsPartialUpdates()
    {
        return false;
    }

    supportsConditionalQueries()
    {
        return false;
    }

    insert(collection, key, data)
    {
        return new Promise((resolve,reject) => {
            this.client.SET(this.getKeyName(collection, key), JSON.stringify(data), function(err, res){
                if (err) reject(err);
                else resolve(1);
            });
        });
    }

    update(collection, key, data)
    {
        return this.insert(collection, key, data);
    }

    del(collection, key)
    {
        return new Promise((resolve,reject) => {
            this.client.UNLINK(this.getKeyName(collection, key), function(err, res){
                if (err) reject(err);
                else resolve(1);
            });
        });
    }

    find(collection, key)
    {
        return new Promise((resolve,reject) => {
            this.client.GET(this.getKeyName(collection, key), function(err, data){
                if (err || !data) resolve(null);
                else resolve(JSON.parse(data));
            });
        });
    }

    findAll(collection, conditions)
    {
        return null;
    }

    getKeyName(collection, key = null)
    {
        return this.keyPrefix + String(collection) + ':' + (!key ? '' : (is_obj(key) ? Object.values(key).join(':') : String(key)));
    }
}

return RedisStorage;
};

For more information send a message to info at phpclasses dot org.