Rich document ids for CouchDB
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Go to file
Johannes J. Schmidt 4da8a2b0a2 (closes #7) - implements arity and parts
`docuri.parts` array of components, without trailing `undefined` components
`docuri.arity` length of parts.
11 years ago
test (closes #7) - implements arity and parts 11 years ago
.gitignore first commit 11 years ago
.travis.yml test on current versions 11 years ago
README.md (closes #7) - implements arity and parts 11 years ago
index.js (closes #7) - implements arity and parts 11 years ago
package.json (closes #7) - implements arity and parts 11 years ago

README.md

docuri Build Status

Rich document ids for CouchDB:

type/id/subtype/index/version

For example: movie/blade-runner/gallery-image/12/medium

Docuris have many advantages:

  • You can access the doc type (eg. in changes feed)
  • They sort well in Futon
  • They tell a lot about the document
  • You can rely on a schema and construct ids of dependend documents (eg. a specific version of an image)
  • You can easily delete related documents (eg. by requesting a range from _all_docs)

...and I'm sure I forgot to mention the best.

Give Docuris a try!

Usage

parse(string)

Parse id string:

var docuri = require('docuri');
docuri.parse('mytype/myid/mysubtype/myindex/myversion');
// {
//   type: 'mytype',
//   id: 'myid',
//   subtype: 'mysubtype',
//   index: 'myindex',
//   version: 'myversion'
// }

stringify(object)

Build id string from object:

docuri.stringify({
  type: 'mytype',
  id: 'myid',
  subtype: 'mysubtype',
  index: 'myindex',
  version: 'myversion'
});
// 'mytype/myid/mysubtype/myindex/myversion'

merge(objectOrString)

Change id string components:

docuri.merge('mytype/myid/mysubtype/myindex/myversion', {
  type: 'my_new_type',
});
// 'my_new_type/myid/mysubtype/myindex/myversion'

parts(objectOrString)

Array of components. Trailing undefined components are stripped off:

docuri.parts('mytype/myid/');
// ['mytype', 'myid']
docuri.parts({ type: 'mytype', subtype: 'mysubtype' });
// ['mytype', undefined, 'mysubtype']

arity(objectOrString)

Length of parts:

docuri.arity('mytype/myid/');
// 2 
docuri.arity({ type: 'mytype', subtype: 'mysubtype' });
// 3

docuri([definition])

Use custom definition:

docuri();
// ['type', 'id', 'subtype', 'index', 'version']
docuri(['id', 'meta']);
docuri();
// ['id', 'meta']
docuri.parse('42/answer');
// {
//   id: '42',
//   meta: 'answer'
// }

Note: definition MUST be an array of strings with at least one item.

Browser support

To use docid in your client-side application, browserify it like this:

browserify -s DocURI path/to/docuri/index.js > path/to/your/assets

Once added to your DOM, this will leave you with a global DocURI object for use in your e.g. Backbone Models/Collections.

Development

To run the unit tests:

npm test

License

Copyright (c) 2014 Johannes J. Schmidt, null2 GmbH
Licensed under the MIT license.