4.0.0-rc4 • Published 4 months ago

seneca v4.0.0-rc4

Weekly downloads
9,829
License
MIT
Repository
github
Last release
4 months ago

Logo

A Node.js toolkit for Microservice architectures

VoxgigThis open source module is sponsored and supported by Voxgig.

Seneca

Npm NpmFigs Travis Coveralls DeepScan CodeClimate Gitter

Seneca is a toolkit for writing microservices and organizing the business logic of your app. You can break down your app into "stuff that happens", rather than focusing on data models or managing dependencies.

Seneca provides,

  • pattern matching: a wonderfully flexible way to handle business requirements

  • transport independence: how messages get to the right server is not something you should have to worry about

  • maturity: 8 years in production (before we called it microservices), but was once taken out by lightning

  • plus: a deep and wide ecosystem of plugins

  • book: a guide to designing microservice architectures: taomicro

Use this module to define commands that work by taking in some JSON, and, optionally, returning some JSON. The command to run is selected by pattern-matching on the the input JSON. There are built-in and optional sets of commands that help you build Minimum Viable Products: data storage, user management, distributed logic, caching, logging, etc. And you can define your own product by breaking it into a set of commands - "stuff that happens". That's pretty much it.

If you're using this module, and need help, you can:

If you are new to Seneca in general, please take a look at senecajs.org. We have everything from tutorials to sample apps to help get you up and running quickly.

Seneca's source can be read in an annotated fashion by running npm run annotate. An annotated version of each file will be generated in ./docs/.

Install

To install via npm,

npm install seneca

Quick Example

'use strict'

var Seneca = require('seneca')


// Functionality in seneca is composed into simple
// plugins that can be loaded into seneca instances.


function rejector () {
  this.add('cmd:run', (msg, done) => {
    return done(null, {tag: 'rejector'})
  })
}

function approver () {
  this.add('cmd:run', (msg, done) => {
    return done(null, {tag: 'approver'})
  })
}

function local () {
  this.add('cmd:run', function (msg, done) {
    this.prior(msg, (err, reply) => {
      return done(null, {tag: reply ? reply.tag : 'local'})
    })
  })
}


// Services can listen for messages using a variety of
// transports. In process and http are included by default.


Seneca()
  .use(approver)
  .listen({type: 'http', port: '8260', pin: 'cmd:*'})

Seneca()
  .use(rejector)
  .listen(8270)


// Load order is important, messages can be routed
// to other services or handled locally. Pins are
// basically filters over messages


function handler (err, reply) {
  console.log(err, reply)
}

Seneca()
  .use(local)
  .act('cmd:run', handler)

Seneca()
  .client({port: 8270, pin: 'cmd:run'})
  .client({port: 8260, pin: 'cmd:run'})
  .use(local)
  .act('cmd:run', handler)

Seneca()
  .client({port: 8260, pin: 'cmd:run'})
  .client({port: 8270, pin: 'cmd:run'})
  .use(local)
  .act('cmd:run', handler)


// Output
// null { tag: 'local' }
// null { tag: 'approver' }
// null { tag: 'rejector' }

Running

To run normally, say in a container, use

$ node microservice.js

(where microservice.js is a script file that uses Seneca). Logs are output in JSON format so you can send them to a logging service.

To run in test mode, with human-readable, full debug logs, use:

$ node microservice.js --seneca.test

Why we built this?

So that it doesn't matter,

  • who provides the functionality,
  • where it lives (on the network),
  • what it depends on,
  • it's easy to define blocks of functionality (plugins!).

So long as some command can handle a given JSON document, you're good.

Here's an example:

var seneca = require('seneca')()

seneca.add({cmd: 'salestax'}, function (msg, done) {
  var rate  = 0.23
  var total = msg.net * (1 + rate)
  done(null, {total: total})
})

seneca.act({cmd: 'salestax', net: 100}, function (err, result) {
  console.log(result.total)
})

In this code, whenever seneca sees the pattern {cmd:'salestax'}, it executes the function associated with this pattern, which calculates sales tax. There is nothing special about the property cmd . It is simply the property we want to pattern match. You could look for foo for all seneca cares! Yah!

The seneca.add method adds a new pattern, and the function to execute whenever that pattern occurs.

The seneca.act method accepts an object, and runs the command, if any, that matches.

Where does the sales tax rate come from? Let's try it again:

seneca.add({cmd: 'config'}, function (msg, done) {
  var config = {rate: 0.23}
  var value = config[msg.prop]
  done(null, {value: value})
})

seneca.add({cmd: 'salestax'}, function (msg, done) {
  seneca.act({cmd: 'config', prop: 'rate'}, function (err, result) {
    var rate  = parseFloat(result.value)
    var total = msg.net * (1 + rate)
    done(null, {total: total})
  })
})

seneca.act({cmd: 'salestax', net: 100}, function (err, result) {
  console.log(result.total)
})

The config command provides you with your configuration. This is cool because it doesn't matter where it gets the configuration from - hard-coded, file system, database, network service, whatever. Did you have to define an abstraction API to make this work? Nope.

There's a little but too much verbosity here, don't you think? Let's fix that:

seneca.act('cmd:salestax,net:100', function (err, result) {
  console.log(result.total)
})

Instead of providing an object, you can provide a string using an abbreviated form of JSON. In fact, you can provide both:

seneca.act('cmd:salestax', {net: 100}, function (err, result) {
  console.log(result.total)
})

This is a very convenient way of combining a pattern and parameter data.

Programmer Anarchy

The way to build Node.js systems, is to build lots of little processes. Here's a great talk explaining why you should do this: Programmer Anarchy.

Seneca makes this really easy. Let's put configuration out on the network into its own process:

seneca.add({cmd: 'config'}, function (msg, done) {
  var config = {rate: 0.23}
  var value = config[msg.prop]
  done(null, { value: value })
})

seneca.listen()

The listen method starts a web server that listens for JSON messages. When these arrive, they are submitted to the local Seneca instance, and executed as actions in the normal way. The result is then returned to the client as the response to the HTTP request. Seneca can also listen for actions via a message bus.

Your implementation of the configuration code stays the same.

The client code looks like this:

seneca.add({cmd: 'salestax'}, function (msg, done) {
  seneca.act({cmd: 'config', prop: 'rate' }, function (err, result) {
    var rate  = parseFloat(result.value)
    var total = msg.net * (1 + rate)
    done(null, { total: total })
  })
})

seneca.client()

seneca.act('cmd:salestax,net:100', function (err, result) {
  console.log(result.total)
})

On the client-side, calling seneca.client() means that Seneca will send any actions it cannot match locally out over the network. In this case, the configuration server will match the cmd:config pattern and return the configuration data.

Again, notice that your sales tax code does not change. It does not need to know where the configuration comes from, who provides it, or how.

You can do this with every command.

Keeping the Business Happy

The thing about business requirements is that they have no respect for common sense, logic or orderly structure. The real world is messy.

In our example, let's say some countries have single sales tax rate, and others have a variable rate, which depends either on locality, or product category.

Here's the code. We'll rip out the configuration code for this example.

// fixed rate
seneca.add({cmd: 'salestax'}, function (msg, done) {
  var rate  = 0.23
  var total = msg.net * (1 + rate)
  done(null, { total: total })
})


// local rates
seneca.add({cmd: 'salestax', country: 'US'}, function (msg, done) {
  var state = {
    'NY': 0.04,
    'CA': 0.0625
    // ...
  }
  var rate = state[msg.state]
  var total = msg.net * (1 + rate)
  done(null, {total: total})
})


// categories
seneca.add({ cmd: 'salestax', country: 'IE' }, function (msg, done) {
  var category = {
    'top': 0.23,
    'reduced': 0.135
    // ...
  }
  var rate = category[msg.category]
  var total = msg.net * (1 + rate)
  done(null, { total: total })
})


seneca.act('cmd:salestax,net:100,country:DE', function (err, result) {
  console.log('DE: ' + result.total)
})

seneca.act('cmd:salestax,net:100,country:US,state:NY', function (err, result) {
  console.log('US,NY: ' + result.total)
})

seneca.act('cmd:salestax,net:100,country:IE,category:reduced', function (err, result) {
  console.log('IE: ' + result.total)
})

In this case, you provide different implementations for different patterns. This lets you isolate complexity into well-defined places. It also means you can deal with special cases very easily.

Contributing

The Senecajs org encourages participation. If you feel you can help in any way, be it with bug reporting, documentation, examples, extra testing, or new features feel free to create an issue, or better yet, submit a Pull Request. For more information on contribution please see our Contributing guide.

Test

To run tests locally,

npm run test

To obtain a coverage report,

npm run coverage; open docs/coverage.html

License

Copyright (c) 2010-2018 Richard Rodger and other contributors; Licensed under MIT.

@flexdrive/seneca-nats-streaming@flexdrive/seneca-schemablocked-frameseneca-service-areaseneca-dynamodb@infinitebrahmanuniverse/nolb-seneseneca-sqs-transport@everything-registry/sub-chunk-2731tclmoongotcmoongodbtalk-to-senecawzrdryrnsell-seneca1service-discovery-cfservice-discovery-cloudfoundryservicebaseschemas-files-serviceseneca-awaitseneca-aws-adapterseneca-cache-autoexpseneca-cardseneca-cassandra-storeseneca-cliseneca-hapijsseneca-import-csvseneca-in-practiceseneca-kinde-authseneca-knex-storeseneca-kueseneca-activemq-transportseneca-archiveseneca-db-test-harnessseneca-db-webseneca-docseneca-dynamo-storeseneca-edissonsseneca-clientseneca-cmsseneca-couchdb-changesseneca-couchdb-storeseneca-cronseneca-hana-storeseneca-extendseneca-extendedseneca-zipkin-tracerseneca-zyre-transportsenegraphseneca-utilsseneca-viewsseneca-voteseneca-watchseneca-wflowseneca-salesforce-storeseneca-sentinelseneca-serviceseneca-mysqlseneca-nats-streamingseneca-nftraceseneca-ng-cardseneca-ng-webseneca-nservicebus-transportseneca-orientdb-storeseneca-service-registryseneca-service-registry-clientseneca-servicebus-transportseneca-shard-cacheseneca-sqlite-storeseneca-sqs-queueseneca-ultilsseneca-logtrans-pluginseneca-merge-payloadseneca-merge-validateseneca-stripeseneca-sybase-storeseneca-telldus-liveseneca-r-scriptseneca-redis-store-expiresseneca-msaccess-storeseneca-mssql-storetfk-seneca-seeiendom-lookupvoodoo-corevlog-apivastifyvidi-dashboardvidi-metricsvidi-seneca-metricsvidi-toolbag-metricslibtesttclmms-coremocoolka-toolslihanbokprohapi-seneca-pluginitdragon-ormiqpass-label-composer-servicemicroconnectmicroplumkryptstorm-servicehookmeuphookmeup1giri-cluster-control
4.0.0-rc4

4 months ago

4.0.0-rc2

6 months ago

4.0.0-rc3

4 months ago

3.37.0

6 months ago

3.37.1

4 months ago

3.37.2

4 months ago

4.0.0-rc.2

6 months ago

3.36.0

7 months ago

3.35.3

7 months ago

3.35.2

9 months ago

3.34.1

10 months ago

3.34.0

10 months ago

3.33.0

1 year ago

3.31.0

1 year ago

3.31.1

1 year ago

3.31.2

1 year ago

3.32.0

1 year ago

3.32.1

1 year ago

3.30.1

1 year ago

3.30.0

2 years ago

3.29.0

2 years ago

3.28.0

3 years ago

3.28.2

2 years ago

3.27.2

3 years ago

3.27.1

3 years ago

3.27.0

3 years ago

3.26.0

3 years ago

3.26.2

3 years ago

3.26.1

3 years ago

3.25.0

3 years ago

3.24.1

3 years ago

3.24.0

3 years ago

3.23.3

4 years ago

3.23.2

4 years ago

3.23.1

4 years ago

3.23.0

4 years ago

3.22.0

4 years ago

3.21.2

4 years ago

3.21.1

4 years ago

3.21.0

5 years ago

3.20.6

5 years ago

3.20.4

5 years ago

3.20.5

5 years ago

3.20.3

5 years ago

3.20.2

5 years ago

3.20.1

5 years ago

3.20.0

5 years ago

3.19.0

5 years ago

3.18.0

5 years ago

3.17.0

5 years ago

3.16.1

5 years ago

3.16.0

5 years ago

3.15.4

5 years ago

3.15.3

5 years ago

3.15.2

5 years ago

3.15.1

5 years ago

3.14.1

5 years ago

3.14.0

5 years ago

3.13.3

5 years ago

3.13.2

5 years ago

3.13.1

5 years ago

3.13.0

5 years ago

3.12.1

5 years ago

3.12.0

5 years ago

3.11.0

6 years ago

3.10.0

6 years ago

3.9.0

6 years ago

3.8.4

6 years ago

3.8.3

6 years ago

3.8.2

6 years ago

3.8.1

6 years ago

3.8.0

6 years ago

3.8.0-rc3

6 years ago

3.7.0

6 years ago

3.6.0

6 years ago

3.5.0

7 years ago

3.4.3

7 years ago

3.4.2

7 years ago

3.4.1

7 years ago

3.4.0

7 years ago

3.4.0-rc0

7 years ago

3.3.0

8 years ago

3.2.2

8 years ago

3.2.1

8 years ago

3.2.0

8 years ago

3.1.0

8 years ago

3.0.0

8 years ago

2.1.0

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.4.0

9 years ago

1.3.0

9 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago

0.9.3

9 years ago

0.9.2

9 years ago

0.9.1

9 years ago

0.9.0

9 years ago

0.8.0

9 years ago

0.7.2

9 years ago

0.7.1

9 years ago

0.7.0

9 years ago

0.6.5

9 years ago

0.6.4

9 years ago

0.6.3

9 years ago

0.6.2

9 years ago

0.6.1

10 years ago

0.6.1-rc2

10 years ago

0.6.1-rc1

10 years ago

0.6.1-rc0

10 years ago

0.6.0-rc2

10 years ago

0.6.0

10 years ago

0.5.21

10 years ago

0.5.20

10 years ago

0.5.19

10 years ago

0.5.18

10 years ago

0.5.17

11 years ago

0.5.16

11 years ago

0.5.15

11 years ago

0.5.14

11 years ago

0.5.13

11 years ago

0.5.12

11 years ago

0.5.11

11 years ago

0.5.10

11 years ago

0.5.9

11 years ago

0.5.8

11 years ago

0.5.7

12 years ago

0.5.6

12 years ago

0.5.5

12 years ago

0.5.4

12 years ago

0.5.3

12 years ago

0.5.2

12 years ago

0.5.1

12 years ago

0.5.0

12 years ago

0.4.4

12 years ago

0.4.3

12 years ago

0.4.2

12 years ago

0.4.1

12 years ago

0.0.1

12 years ago