Neurosis Prevention Mechanism

    rilatigi

    0.0.3 • Public • Published

    Rilatigi

    NPM Version David David Build Status codecov Twitter Follow

    Rilatigi means to relate in esperanto.

    Rilatigi is a promise-based relationship CRUD library for Node.js. It aims to streamline relationship CRUD operations when you have to keep multiple data sources synced (i.e. caching layer and persistence layer).

    Rilatigi is data source agnostic and delegates resolution of CRUD operations to resolvers.

    CRUD operations are start, end, get and sync. Those operations can be performed on mutual and unmutual relationships.

    Installation

    npm install rilatigi --save

    Usage

    Plug in your favourite promise library

    Set rilatigi.Promise to your favourite ES6-style promise constructor and rilatigi will use it.

    npm install bluebird --save
    import Promise from 'bluebird'
    import rilatigi from 'rilatigi'
     
    rilatigi.Promise = Promise

    Define resolvers

    Rilatigi doesn't make any assumption about your resolvers but having start, end, get and sync methods.

    Signatures for these methods differ depending on the type of relationship (mutual, unmutual) a resolver manages.

    Mutual resolvers

    class MutualResolver {
      start (subjects) {
     
      }
      end (subjects) {
     
      }
      get (subject) {
     
      }
      sync (subject, result) {
     
      }
    }

    Unmutual resolvers

    class UnmutualResolver {
      start (subject, objects) {
     
      }
      end (subject, objects) {
     
      }
      get (subject) {
     
      }
      sync (subject, result) {
     
      }
    }

    Define and use relationships

    You can define mutual and unmutual relationships.

    The Following examples show you both mutual and unmutual relationship definition and usage in the context of multiple data sources (Redis and Mongo).

    Definition of resolvers is omitted but you can take a look at the examples

    Mutual relationships

    import { mutual } from 'rilatigi'
    import { redisFriendshipResolver, mongoFriendshipResolver } from './resolvers'
     
    const friendship = mutual([redisFriendshipResolver, mongoFriendshipResolver])
     
    friendship
      .start(['Tom', 'Jerry'])
      .then(() => friendship.get('Tom'))
      .then(result => console.log(result)) // ['Jerry']

    Unmutual relationships

    import { unmutual } from 'rilatigi'
    import { redisSpeakToResolver, mongoSpeakToResolver} from './resolvers'
     
    const speakTo = unmutual([redisSpeakToResolver, mongoSpeakToResolver])
     
    speakTo
      .start('teacher', ['a student', 'another student'])
      .then(() => speakTo.get('teacher'))
      .then(result => console.log(result)) // ['a student', 'another student']

    Operations lifecycle

    start

    When you invoke start on a relationship, rilatigi will sequentially invoke start on its resolvers, starting from the last resolver.

    end

    When you invoke end on a relationship, rilatigi will sequentially invoke end on its resolvers, starting from the last resolver.

    get

    When you invoke get on a relationship, rilatigi will sequentially invoke get on its resolvers, starting from the first resolver, until a valid result is returned or all resolvers' get got invoked. Non-valid results are null, undefined, empty strings, empty arrays.

    Rilatigi will also invoke sync on all resolvers that returned a non-valid value in order to keep data sources synced.

    sync

    You shouldn't invoke sync on a relationship. It's for internal use.

    License

    The project is licensed under the MIT license.

    Install

    npm i rilatigi

    DownloadsWeekly Downloads

    11

    Version

    0.0.3

    License

    MIT

    Last publish

    Collaborators

    • adigiovanni