npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis-modelize

v0.1.0

Published

Represent your model as a json object, and use redis as backend

Downloads

3

Readme

redis-modelize

Represent your model as a json object, using redis as backend.

This tool was thought for implementing complex models on a fast backend as redis.

Install

To install with npm, type

npm install redis-modelize

Usage

Basics

Inside your project, type

var rm = require('redis-modelize');
var model = rm.init(modelObj, {prefix: 'optional:some:redis:prefix'});
var redis-client = rm.client;

rm.client part of the code returns the instance of redis client that this module uses internally.

modelObj is the object that describes your model. For example:

//Define your model
var modelObj = {
	global: {
		keywords: {type: 'set'}
	}
};

//modelize
var model = rm.init(modelObj, {prefix: 'prefix'});

//Now you have methods to interact with redis
new model.gobal().setKeywords(['any', 'array', 'of', 'keywords'], function(err, resp) {
  /*this is a callback*/
});

The hierarchy of the model object is:

modelObj => namespace => property

So in this example, global is the namespace, and keywords is the property. When you modelize, you get a constructor with each namespace defined, and get several methods for each property.

As you can imagine, what setKeywords method of the example really does under the hood, is

redis.sadd('prefix:global:keywords', ['any', 'array', 'of', 'keywords'], callback);

How does it know that you want to use sadd?

Because we defined that the property keywords is of type set.

Posible values for type are (the redis types): string, set, list, zset and hash.

Depending on which type you define your property, are the ammount of methods you get to manipulate it.

The last parameter of all methods is a callback function with the signature function(err, resp).

Complicating things

Ok, this example is kind of too simple and really it's an overkill when you can simply use redis module.. lets make things a little more complicated.

Imagine we have

var modelObj = {
	user: {
		_obj: {
			type: 'hash',
			reverse: ['email'],
			props: {
				fullname: {mandatory: true},
				email: {mandatory: true},
				password: {mandatory: true}
				phone: {mandatory: false}
			}
		},
		projects: {type: 'set', refs: true}
	},
	project: {
		_obj: {
			type: 'hash',
			props: {
				name: {mandatory: true},
				user: {refs: true},
				description: {mandatory: false}
			}
		}
	},
	global: {
                keywords: {type: 'set'}
	}
}

var model = rm.init(modelObj);

Now things get interesting...

Fist of all, the only type that requires different ammount of parameters is hash.

Parameters for properties of type:

  • hash:
    1. type: mandatory. Defines the type. In this case 'hash'.
    2. props: mandatory. Represent the fields of this hash object. You can define each field with {refs: true|false}. If refs is true, your property will get two extra methods, called 'getRef'+<field name with first letter in upper case> and 'setRef'+<field name with first letter in upper case>. I'll explain them later.
  • any other type:
    1. type: mandatory. Defines the type.
    2. refs: optional. Same as above, but the methods will be named with property name.

Of coure, you can add any parameter you like, but they will not be considered.

WTF is _obj?

Ok, now lets explain the _obj property that you see in user and project.

The _obj property is the only one that has special meaning. You can think of it as the constructor of a class.

With the model of the previous example, you could do the following:

new model.user({
  fullname: 'John Doe', 
  email: '[email protected]', 
  password: hashlib.md5('password')
});

Simple, isn't it? But there are some special considerations of the _obj property:

  • _obj property must be of type 'hash'.
  • Fields in _obj property may be defined with refs, like a normal hash, and also with the optional parameter mandatory. If a field is defined as mandatory, and it wasn't included in the constructor, it will thow an error.
  • _obj property may include the parameter reverse. This parameter can be a string, or an array of strings and they must match the name of a field defined in props. If this parameter is present, you get the static method reverse. You could use it like this:
model.user.reverse('[email protected]', function(err, id) { 
	var johnDoeUser = this 
});
  • If you defined _obj property, you get the method get that returns all the fields of the instance, defined in the _obj, as a json object. You could use it like this:
model.user.reverse('[email protected]', function(err, id) { 
	//'this' references an instance of user.
        this.get(function(err, userFields) {
		console.log(userFields.fullname); //Prints 'John Doe'
	});
});
  • As a final offer, you also get a new method called getParams. It simply returns all the fields as you defined them in the model object. This may be useful if you want to process something according to parameters.. for example, if you want to dynamically create forms according the parameters you defined in _obj.

Manipulating data

Ok, suppose now that you know John Doe's id is 1... an other way of getting an instance of user with John Doe's data is

new model.user(1, function(err, id) { ... });

This means, that if the parameter you pass to the constructor is not an object, it takes it as an identifier.

Fields of hash objects also get methods to manipulate them. In this example, you could add phone number to John doing

//set phone number within callback function
new model.user(1, function(err, id) {
	this.setPhone('555-555555');
});

//or set phone number directly
new model.user(1).setPhone('555-555555');

Refs

If a property or a field was defined with refs: true, it means that it gets two special methods: setRef+ and getRef+. What it means is that the property/field references an object in the model. You could use it like this:

//add a project to John Doe
var jdUser = new model.user(1);
var awproj = new model.project({name: 'Awesome Project', description: 'This project will be awesome'}, function(err, id) {
	this.setRefUser(jdUser);
	jdUser.setRefProjects(this);
});

If you later do awproj.getRefUser(function(err, refUserInstance){ ... }) the second argument of the callback function is an instance of the referenced user.