GithubHelp home page GithubHelp logo

seneca-vote's Introduction

seneca-vote

A voting plugin for Seneca.js

Contents:

Requirements

The following Seneca plugins must be plugged in before this plugin can be used:

  • seneca-entity
  • seneca-promisify

Normally, your code would look like this:

const Seneca = require('seneca')
const Entities = require('seneca-entity')
const SenecaPromisify = require('seneca-promisify')
const VotePlugin = require('seneca-vote')

Seneca()
  .use(Entities)
  .use(SenecaPromisify)
  .use(VotePlugin)

Actions

Action Descriptions

Upvote Action

Pattern

sys:vote,vote:up

Params

  • fields.poll_id_ : ID! : The ID of the poll to upvote on.
  • fields.voter_id_ : ID! : The ID of the voter.
  • fields.voter_type_ : "sys/user"! : The type of the voter. Currently only "sys/user" is supported.

Description

Creates an upvote for a poll. If the voter has already downvoted on the poll,
the downvote will be replaced by an upvote. On success, the number of upvotes
and downvotes are counted and included in the response.

Responses

Upon successful upvote:

{ ok: true, data: { num_upvotes: Int!, num_downvotes: Int! } }

Upon failed validation of the request params:

{
  ok: false,
  why: String,
  details?: { path: Array<Any>?, why_exactly: String? }
}

When the poll does not exist:

{ ok: false, why: String, details?: { what: String? } }

Downvote Action

Pattern

sys:vote,vote:down

Params

  • fields.poll_id_ : ID! : The ID of the poll to upvote on.
  • fields.voter_id_ : ID! : The ID of the voter.
  • fields.voter_type_ : "sys/user"! : The type of the voter. Currently only "sys/user" is supported.

Description

Creates an downvote for a poll. If the voter has already upvoted on the poll,
the upvote will be replaced by a downvote. On success, the number of upvotes
and downvotes are counted and included in the response.

Responses

Upon successful downvote:

{ status: "success", data: { num_upvotes: Int!, num_downvotes: Int! } }

Upon failed validation of the request params:

{
  ok: false,
  why: String,
  details?: { path: Array<Any>?, why_exactly: String? }
}

When the poll does not exist:

{ ok: false, why: String, details?: { what: String? } }

Open Poll Action

Pattern

sys:vote,open:poll

Params

  • fields.title_ : string! : The title of the poll.

Description

Creates a new poll with the given title. If a poll with the given title already exists,
then action will nontheless succeed, but a new poll will not be created. On success,
the poll data is returned.

Responses

Upon success:

{
  status: "success",
  
  data: {
    poll: {
      id: ID!,
      title: String!,
      created_at: Date!,
      updated_at: Date?
    }
  }
}

Upon failed validation of the request params:

{
  ok: false,
  why: String,
  details?: { path: Array<Any>?, why_exactly: String? }
}

Get Poll Action

Pattern

sys:vote,get:poll

Params

  • poll_id_ : ID! : The ID of the poll to get.

Description

Upon success, returns the poll data. Returns an error message if the poll with
the given ID does not exist.

Responses

Upon success:

{
  status: "success",
  
  data: {
    poll: {
      id: ID!,
      title: String!,
      created_at: Date!,
      updated_at: Date?
    }
  }
}

Upon failed validation of the request params:

{
  ok: false,
  why: String,
  details?: { path: Array<Any>?, why_exactly: String? }
}

When the poll does not exist:

{ ok: false, why: String, details?: { what: String? } }

Plugin Options

locks_disabled : Boolean? By default, this option is set to true, i.e. locks are disabled by default. If set to false, re-enabled locks which help prevent potential race conditions in actions.

During implementation work on seneca-vote, in order to meet certain MVP requirements, such as that there can only ever be a single poll record with the same title, - locks were implemented as part of seneca-vote to prevent race conditions, that may otherwise occur under high loads.

It was ultimately decided to disable the locks by default in order to comply with the rest of the Seneca eco-system. Entity upserts are planned to be added to Seneca entities in the future. They will work as upserts normally do:
https://docs.mongodb.com/drivers/node/fundamentals/crud/write-operations/upsert/
https://www.postgresqltutorial.com/postgresql-upsert/

Further work on the data model of seneca-vote is expected in order to both prevent race conditions and remain compliant with the rest of the Seneca eco-system.

Dev Scripts

$ npm test
Runs the automated tests.

$ npm run check-coverage
Generates a test coverage report.

seneca-vote's People

Contributors

lilsweetcaligula avatar rjrodger avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar  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.