Git Product home page Git Product logo

sails-redis-schema's Introduction

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

sails-redis-schema's People

Contributors

mjlescano avatar matiasdecarli avatar smvilar avatar ignusmart avatar rcrsvryan avatar khai-github-rcrsv avatar

Watchers

James Cloos avatar

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.