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.
docuri/README.md

105 lines
2.4 KiB
Markdown

10 years ago
# docuri [![Build Status](https://travis-ci.org/jo/docuri.svg?branch=master)](https://travis-ci.org/jo/docuri)
Rich document ids for CouchDB:
10 years ago
10 years ago
```
type/id/subtype/index/version
```
10 years ago
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`)
10 years ago
...and I'm sure I forgot to mention the best.
Give Docuris a try!
10 years ago
## Usage
### `parse(string)`
10 years ago
Parse id string:
```js
var docuri = require('docuri');
docuri.parse('mytype/myid/mysubtype/myindex/myversion');
10 years ago
// {
// type: 'mytype',
// id: 'myid',
// subtype: 'mysubtype',
// index: 'myindex',
// version: 'myversion'
10 years ago
// }
```
### `stringify(object)`
10 years ago
Build id string from object:
```js
docuri.stringify({
10 years ago
type: 'mytype',
id: 'myid',
subtype: 'mysubtype',
index: 'myindex',
version: 'myversion'
10 years ago
});
// 'mytype/myid/mysubtype/myindex/myversion'
10 years ago
```
### `merge(objectOrString)`
Change id string components:
```js
docuri.merge('mytype/myid/mysubtype/myindex/myversion', {
type: 'my_new_type',
});
// 'my_new_type/myid/mysubtype/myindex/myversion'
10 years ago
```
### `parts(objectOrString)`
Array of components. Trailing `undefined` components are stripped off:
```js
docuri.parts('mytype/myid/');
// ['mytype', 'myid']
docuri.parts({ type: 'mytype', subtype: 'mysubtype' });
// ['mytype', undefined, 'mysubtype']
```
### `docuri.definition([array])`
Access or use custom definition:
```js
docuri.definition();
// ['type', 'id', 'subtype', 'index', 'version']
docuri
.definition(['id', 'meta'])
.parse('42/answer');
10 years ago
// {
// id: '42',
// meta: 'answer'
// }
```
Note: the (optional) argument to `docuri.definition` MUST be an array of strings
containing at least one item.
## Browser support
To use docid in your client-side application, browserify it like this:
```shell
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.
10 years ago
## Development
To run the unit tests:
```shell
npm test
```
## License
Copyright (c) 2014 Johannes J. Schmidt, null2 GmbH
Licensed under the MIT license.