# hoodie-plugins-api

> Hoodie interface to CouchDB

Latest version **0.4.1** (published 2015-03-20) · 0 weekly downloads

## Install

```sh
npm install hoodie-plugins-api
pnpm add hoodie-plugins-api
yarn add hoodie-plugins-api
bun add hoodie-plugins-api
```

## 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.4.1 |
| Published | 2015-03-20 |
| First published | 2013-09-12 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+6 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 13 |
| Maintainers | caolan, svnlto, gr2m, jan, espy, boennemann, hoodiebot |

## Links

- npm: https://www.npmjs.com/package/hoodie-plugins-api
- Repository: https://github.com/hoodiehq/hoodie-plugins-api
- Issues: https://github.com/hoodiehq/hoodie-plugins-api/issues
- npm.io page: https://npm.io/package/hoodie-plugins-api

## Dependencies (5)

- [uuid](https://npm.io/package/uuid.md) 1.4.1
- [async](https://npm.io/package/async.md) 0.2.8
- [couchr](https://npm.io/package/couchr.md) 0.0.13
- [moment](https://npm.io/package/moment.md) 2.1.0
- [underscore](https://npm.io/package/underscore.md) 1.4.4

## Recent versions

- 0.4.1 (latest) — 2015-03-20
- 0.4.0 — 2015-02-06
- 0.2.1 — 2014-02-07
- 0.2.0 — 2014-02-07
- 0.1.0 — 2014-02-04
- 0.0.6 — 2013-10-18
- 0.0.5 — 2013-09-30
- 0.0.4 — 2013-09-24
- 0.0.3 — 2013-09-17
- 0.0.2 — 2013-09-12

## README

# Hoodie Plugin API
[![Build Status](https://travis-ci.org/hoodiehq/hoodie-plugins-api.svg)](https://travis-ci.org/hoodiehq/hoodie-plugins-api)
[![Dependency Status](https://david-dm.org/hoodiehq/hoodie-plugins-api.svg)](https://david-dm.org/hoodiehq/hoodie-plugins-api)
[![devDependency Status](https://david-dm.org/hoodiehq/hoodie-plugins-api/dev-status.svg)](https://david-dm.org/hoodiehq/hoodie-plugins-api#info=devDependencies)


## File and directory structure

```
/hoodie-plugin-plugin_name
    /pocket
    hoodie.plugin_name.js
    index.js
    package.json
```

* __/pocket__ - Extends the Pocket admin interface (contains a HTML fragment with code and styles)
* __hoodie.plugin_name.js__ - Extends the hoodie.js front-end API
* __index.js__ - Node.js worker for handling tasks and other events (this is just the default location, you have more options here, see below)
* __package.json__ - The plugin's metadata and dependencies (since the plugin should function as an npm module)

The server-side component of the plugin can be left in an index.js for simplicity, but Hoodie will prefer the following, if present:
* Whatever you reference under `main` in the plugin's `package.json`
* Whatever you get when you `require()` the plugin root directory

## Writing workers

```javascript
// Initializing a worker
module.exports = function (hoodie, callback) {
    // hoodie object is client to hoodie backend, documented below.
    // call callback when setup complete (with optional error if worker failed to initialize).
    // ...
};


// make HTTP requests directly to CouchDB (ideally, you would never need to use this)
hoodie.request(method, path, options, callback)

// get / set plugin configuration
hoodie.config.get(key)
hoodie.config.set(key, value)

// list all databases
hoodie.database.findAll(callback)

// create a new database
hoodie.database.add(name, callback)

// remove a database
hoodie.database.remove(name, callback)

// get a database object to make calls against
hoodie.database(name) => db

// add a document to db
db.add(type, attrs, callback)

// update a document in db
db.update(type, id, changed_attrs, callback)

// get a document from db
db.find(type, id, callback)

// get all documents from db
db.findAll(callback)

// get all documents of a single type in db
db.findAll(type, callback)

// remove a document from db
db.remove(type, id, callback)

// remove all documents of type in db
db.removeAll(type, callback)

// grant read access to everyone on db by updating CouchDB security
db.grantPublicReadAccess(callback)

// grant write access to everyone on db
db.grantPublicWriteAccess(callback)

// grant read access to specific user on db by updating CouchDB security
db.grantReadAccess(account_type, account_id, callback)

// grant write access to specific user on db by adding role (checked by design doc in db)
db.grantWriteAccess(account_type, account_id, callback)

// update db security so it's no longer publicly readable
db.revokePublicReadAccess(callback)

// update db security so it's no longer publicly writable
db.revokePublicWriteAccess(callback)

// remove user from couchdb readers for db
db.revokeReadAccess(account_type, account_id, callback)

// remove role from user so they cannot write to db (checked by design doc)
db.revokeWriteAccess(account_type, account_id, callback)


// Index / Query API

// creates new design doc with CouchDB view on db
db.addIndex(name, {map: .., reduce: ..}, callback)

// removes design doc for couchdb view on db
db.removeIndex(name, callback)

// query a couchdb view on db
db.query(index, options, callback)


//
// hoodie.account API
//
hoodie.account.add(type, attrs, callback)
hoodie.account.update(type, id, changed_attrs, callback)
hoodie.account.find(type, id, callback)
hoodie.account.findAll(callback)
hoodie.account.findAll(type, callback)
hoodie.account.remove(type, id, callback)
hoodie.account.removeAll(type, callback)

// hoodie.account events
hoodie.account.on('change', handler)
hoodie.account.on('type:change', handler)

// use case: 
// handle password resets
hoodie.account.on('$passwordReset:change', function(object) {
  // set new password in user doc & send it via email
})


//
// listen to task document events
//
hoodie.task.on('change', function (db, doc) { ... })
hoodie.task.on('type:change', function (db, doc) { ... })

// add / remove sources (database) to listen for new tasks
hoodie.task.addSource( databaseName )
hoodie.task.removeSource( databaseName )

// mark task as complete
hoodie.task.success( databaseName, taskDoc, [callback] )

// mark task as errored
hoodie.task.error( databaseName, taskDoc, error, [callback] )

// send emails
hoodie.sendEmail({
    from: "Fred Foo ✔ <foo@blurdybloop.com>", // sender address
    to: "bar@blurdybloop.com, baz@blurdybloop.com", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
}, callback)

// sending emails uses nodemailer API:
// https://github.com/andris9/Nodemailer

// you can also pass attachments as dataURIs:
hoodie.sendEmail({
  to: 'test@example.com',
  from: 'hoodie@example.com',
  subject: 'test',
  text: 'blah blah',
  attachments: [
    {dataURI: 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D', ...}
  ]
}, callback);

// please note, the 'filePath' property usable on attachments in nodemail
// will be stripped in hoodie to prevent accidentally emailing out files
// from the server
```

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