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

camfou-modinha-redis

v1.4.0

Published

Fork of Redis persistence mixins for Modinha models

Downloads

37

Readme

camfou-modinha-redis

Actions Status npm Coverage Status semantic-release

The RedisDocument mixin defines a collection of persistence methods that map cleanly between HTTP semantics and Redis data structures.

Usage

Suppose we've defined an Account model with Modinha like so:

var Modinha = require('camfou-modinha')
  , RedisDocument = require('camfou-modinha-redis').RedisDocument

var Account = Modinha.define('accounts', {
  email: { type: 'string', required: true, unique: true },
  role:  { type: 'string', secondary: true, enum: ['admin', 'editor', 'author'] },
  hash:  { type: 'string', private: true }
});

Account.extend(RedisDocument);

RedisDocument will add the following persistence methods to the Account document.

HTTP                     MODEL METHOD

GET    /accounts         Account.list(options, callback)
GET    /accounts/id      Account.get(ids, options, callback)
POST   /accounts         Account.insert(data, options, callback)
PUT    /accounts/id      Account.replace(id, data, options, callback)
PATCH  /accounts/id      Account.patch(id, data, options, callback)
DELETE /accounts/id      Account.delete(id, callback)

Extending Account with RedisDocument will also define the following properties on Account.schema:

_id:      { type: 'string', required: true, default: Model.defaults.uuid },
created:  { type: 'number', order: true, default: Model.defaults.timestamp },
modified: { type: 'number', order: true, default: Model.defaults.timestamp }

Since we defined unique and secondary properties on email and role, respectively, the mixin will also generate property specific methods for those indexes.

Account.getByEmail(email, callback)
Account.listByRole(role, callback)

More about indexing

We can index in a variety of ways with Redis hashes and sorted sets. For example, we could explicitly define our unique email index like so:

Account.defineIndex({
  type:  'hash',
  key:   'accounts:email',
  field: 'email',
  value: '_id'
});

This tells the model to store an account's _id property in a hash named accounts:email with email as the field name. Because this is a very common use of the hash type index, the mixin also provides a helper method for defining unique indices:

Account.indexUnique('email');

This is equivalent to adding unique: true to the property definition in our schema.

Sorted set indices get a little more interesting. We have a great deal of flexibility in how we can index our models. For example, suppose we have a Video model that has a category property and a likes property. We want to retrieve a list of videos for a specific category, sorted by the number of likes.

Video.defineIndex({
  type:   'sorted',
  key:    ['videos:#:$', 'category', 'category'],
  score:  'likes',
  member: '_id'
});

When we index the following instance...

{
  _id: 'r4nd0m',
  name: 'Awesome Presentation',
  url: 'https://youtube.com/wh4t3v3r'
  category: 'conferences',
  likes: 777
}

... the object's _id will be added to a sorted set in Redis called videos:category:conferences, with a score of 777. Notice the key property of the index definition: ['videos:#:$', 'category', 'category']. The first element of this array is a template for a key name. In the template, the placeholders # and $ will be replaced in order according to the remaining elements of the array. # will be replaced literally with element and $ will be used to access a property on the object being indexed.

Like the hash-type index, there are a few very common indexing patterns for sorted sets. The mixin provides higher level methods for defining these, and in some cases, they can be created as part of a schema definition. Some examples:

Model.indexSecondary(propertyName, [score]);
Video.indexSecondary('category', 'likes');                     // Same as previous example


Model.indexReference(propertyName, ReferencedModel, [score]);
Comment.indexReference('videoId', Video);                      // multi.zadd('videos:ID:comments', comment.created, comment._id);


Model.indexOrder(propertyName);
Comment.indexOrder('likes');


// video schema
{
  name:     { type: 'string', unique: true },
  url:      { type: 'string', unique: true },
  category: { type: 'string', enum: ['tutorial', 'presentation'], secondary: true },
  likes:    { type: 'string', order: true }
}

Unique values are enforced by the insert, replace, and patch methods. If you write custom methods, you can use Account.enforceUnique(callback) (for example) to generate a UniqueValueError.

The default timestamp methods define an ordered index for created and modified. Account.list(options, callback) uses the accounts:created index by default to deliver reverse chronological account listings.

The MIT License

Copyright (c) 2015 Anvil Research, Inc. http://anvil.io

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.