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

@miroculus/sails-redis-schema

v1.4.1

Published

A redis adapter for Sails / Waterline with basic schema support

Downloads

43

Readme

sails-redis-schema

A redis adapter for Sails / Waterline with basic schema and indexing support.

This adapter is intended to be used with models with very simple attributes (only number, boolean, string and json types are supported) and to do very simple queries, if you need something more complex you should be considering not using redis and move to a full featured DB like MongoDB, PostgreSQL, etc.

Installation

To install this adapter, run:

npm install sails-redis-schema

Then, add the adapter to the config/datastores.js file like this:

module.exports.datastores = {
  'redis-schema': {
    adapter: require('sails-redis-schema'),
    url: 'redis://user:[email protected]:6379' // defaults to '127.0.0.1:6379'
  }
};

You could also use the options field with any of the values listed here: https://github.com/luin/ioredis/blob/HEAD/API.md#new-redisport-host-options

module.exports.datastores = {
  'redis-schema': {
    adapter: require('sails-redis-schema'),
    options: {
      host: '192.168.1.1',
      port: 5412,
      db: 3,
      enableOfflineQueue: false
    }
  }
};

More info about datastores at: https://sailsjs.com/documentation/reference/configuration/sails-config-datastores

Usage

In most cases I added easy to understand errors, but as a rule of thumb, if some functionality is not listed here is probably because is not implemented. Keep in mind that I tried to implement this adapter as minimal as possible.

Model Definition

First, define your model and configure it to use the redis-schema datastore like this:

module.exports = {
  identity: 'user',
  datastore: 'redis-schema',
  attributes: {
    id: {
      type: 'string'
    },
    age: {
      type: 'number',
      defaultsTo: 18,
      meta: { index: true }
    },
    firstName: {
      type: 'string',
      required: true,
      meta: { index: true }
    },
    lastName: {
      type: 'string'
    }
  }
}

Things to keep in mind:

  • For now, only the type's number, boolean, string and json are allowed.
  • If you want to query your model using other attribute than the primaryKey, you have to index it, to do so, do the same as we did with firstName, and add:
      meta: { index: true }

.find()

The find method only allows to query items using only 1 attribute; you won't be able to do composed querys like: { firstName: 'Ada', age: 20 }.

Also, don't forget to add the meta: { index: true } to all the attributes that you want to use to query items.

Examples:

// Correct
User.find({ id: '123123123' }) // Find users with id '123123123'
User.find({ firstName: 'Mark' }) // Find users named 'Mark'
User.find({ firstName: ['Mark', 'Ada'] }) // Find users named `Mark` or `Ada`

// Select fields
User.find({
  where: { firstName: ['Mark', 'Ada'] }
  select: ['firstName', 'lastName']
})

// Incorrect
User.find({ firstName: 'Mark', age: 20 }) // Throws an error

.create()

The create method works as expected. And, if you don't define an id, we will create one for you using nanoid.

Examples:

const ada = await User.create({
  firstName: 'Ada',
  lastName: 'Lovelace',
  age: 36
}).fetch()

.destroy()

Destroys one or multiple items, query rules apply the same as .find().

const deletedItems = await User.destroy({
  firstName: ['Mark', 'Ada']
}).fetch()

👆 Deletes all users named "Mark" or "Ada", and, in this case we added the .fetch() call so it will bring back all the deleted items.

.update()

Updates one or multiple items, query rules apply the same as .find().

await User.update({
  firstName: 'Mark',
}, {
  age: 32
})

👆 Changes the age of all users named "Mark" to be 32 years old.

.count()

The same as .find(), but returns the amount of queried items.

const marksCount = await User.count({ firstName: 'Mark' })

// `There are ${marksCount} marks.`

More Info

Visit Models & ORM in the docs for more information about using models, datastores, and adapters in your app/microservice.

Tests

To run the tests, first make sure to have a redis server running. Or start one using Docker with:

docker-compose up

And then, to execute the tests run (this will connect to the running redis on 127.0.0.1:6378):

npm test

Or, you can use a custom redis instance setting the environment variable like this:

TEST_REDIS_URL=127.0.0.1:6379 npm test

License

MIT