# magma

> A AMQP-powered scalable back end framework.

Latest version **0.1.2-rc0** (published 2015-05-17) · Closed Source license · 0 weekly downloads

## Install

```sh
npm install magma
pnpm add magma
yarn add magma
bun add magma
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2-rc0 |
| Published | 2015-05-17 |
| First published | 2015-05-12 |
| Weekly downloads | 0 |
| License | Closed Source |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Hydrais |
| Maintainers | hydrais |

## Links

- npm: https://www.npmjs.com/package/magma
- npm.io page: https://npm.io/package/magma

## Dependencies (5)

- [amqp](https://npm.io/package/amqp.md) ^0.2.4
- [merge](https://npm.io/package/merge.md) ^1.2.0
- [express](https://npm.io/package/express.md) ^4.12.3
- [node-uuid](https://npm.io/package/node-uuid.md) ^1.4.3
- [body-parser](https://npm.io/package/body-parser.md) ^1.12.4

## Recent versions

- 0.1.2-rc0 (latest) — 2015-05-17
- 0.1.1 — 2015-05-12
- 0.1.0 — 2015-05-12

## README

magma
=====

magma is a framework for creating scalable AMQP back ends.

### Install Magma

```bash
npm install magma
```

### Create a Router

In magma, everything starts with a Router. Routers take in incoming commands and dispatch them to Workers using RabbitMQ. Currently, magma only has one type of Router - the HTTP router. The HTTP router uses [express](http://expressjs.com) to take incoming HTTP requests and transmit them to a Worker.

```js
var magma  = require('magma');
var router = magma.router();

router.connect(function () {
  router.resource('article');
});

var server = router.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Listening at http://%s:%s', host, port);
});
```

#### Resources

The Router above declares one resource: `article`. In magma, a resource acts as a type of object. Each resource can be created, read, updated, and deleted using HTTP `POST`, `GET`, `PUT`, and `DELETE` requests respectively. When a resource is declared on the Router, endpoints matching the name of that resource are automatically created. For example:

```
GET http://.../network
```

#### Access Express

The HTTP Router is based around the [express framework](http://expressjs.com). You can access the express instance using the `Router.app` property. This allows you to add helpful features, such as authentication middleware. On a similar note, the `Router.listen` method is basically a direct proxy to the express `listen` method.

### Create a Worker

The next piece of a magma application is the Worker. Workers are in charge of performing a specific set of tasks related to a resource. While a Worker can handle more than one resource at a time, it's typically best practice to make a new worker for each declared resource. Workers can be spawned and despawned as the workload for the resource increases, and RabbitMQ will automatically handle load balancing between them.

It's important to rememeber that Workers should be&mdash;on their own&mdash;stateless. Any session or long term storage should be handled using a caching system such as Redis, or a database such as MongoDB. You should write your worker with the assumption that it can be spun up or down at any time.

```js
var magma  = require('magma');
var worker = magma.worker();

worker.connect(function () {
  worker.resource('article', function () {
    this.create = function (req, res) {
      var articleId = req.data.id;                // Access data using `req.data`
      var authHeader = req.headers.authorization; // Access headers using `req.headers`
    };

    this.read = function (req, res) {
      res.success('good job!'); // Send successful responses using `res.success`
    };

    this.update = function (req, res) {
      res.fail('something went wrong!'); // Send failure responses using `res.fail`.
    };

    this.delete = function (req, res) {
      // And so on...
    };
  });
});
```

When binding a Worker to a Resource, you must implement the `create`, `read`, `update`, and `delete` methods for that resource. These methods are automatically invoked corresponding to the `POST`, `GET`, `PUT`, and `DELETE` HTTP requests from the Router.

You can access request data using the `req.data` object. Query string data as well as POSTed body data will be combined into this key value array.

### Remote Procedure Calling

Traditionally it's best to stick with REST-ful API endpoints, which the default
router/worker system provides. However, occasionally you need to divert from
that path, which is where Magma's RPC feature comes in.

#### The Router

Start by declaring a router with a custom resource...

```js
var magma  = require('magma');
var router = magma.router();

router.connect(function () {
  router.resource('network', [
    'customAction:get'
  ]);
});

var server = router.listen(3000, funciton () {
  // ...
});
```

By including a second parameter to the `resource` method, we have declared a
new endpoint on the `network` resource called `customAction`. Because we suffixed
the endpoint with `:get`, you will be able to access the custom endpoint like so:

```
GET http://.../network/customAction
```

In addition to the `:get` suffix, you can use `:put`, `:post`, and `:delete` for
`PUT`, `POST`, and `DELETE` request respectively. You **must** include a suffix,
there is no default.

#### The Worker

Now that you have a router defined with your `customAction` endpoint declared,
creating a worker is pretty much business as usual except for one thing: in
addition to defining your worker's `create`, `read`, `update`, and `delete`
methods, you will also define a `customAction` method. The `customAction` will
behave in the same way as the other CRUD methods.

```js
var magma  = require('magma');
var worker = magma.worker();

worker.connect(function () {
  worker.resource('network', function () {
    this.customAction = function (req, res) {
      res.success('customAction');
    };

    this.create = function (req, res) {
      res.success('create');
    };

    this.read = function (req, res) {
      res.success('read');
    };

    this.update = function (req, res) {
      res.success('update');
    };

    this.delete = function (req, res) {
      res.success('delete');
    };
  });
});
```

---
_Source: https://npm.io/package/magma · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
