Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nhz-io/nhz-io-pouch-db-manager-registry
https://github.com/nhz-io/nhz-io-pouch-db-manager-registry
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/nhz-io/nhz-io-pouch-db-manager-registry
- Owner: nhz-io
- License: mit
- Created: 2017-11-17T21:52:18.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-18T22:50:16.000Z (about 7 years ago)
- Last Synced: 2024-11-21T10:40:43.006Z (about 1 month ago)
- Language: CoffeeScript
- Size: 39.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.litcoffee
- License: LICENSE
Awesome Lists containing this project
README
# PouchDB Manager Registry
[![Travis Build][travis]](https://travis-ci.org/nhz-io/nhz-io-pouch-db-manager-registry)
[![NPM Version][npm]](https://www.npmjs.com/package/@nhz.io/pouch-db-manager-registry)## Install
```bash
npm i -S @nhz.io/pouch-db-manager-registry
```## Usage
```js
const Registry = require('@nhz.io/pouch-db-manager-registry')...
```## Literate Source
### Imports
{ urlname, allPass, mkkey, priority } = require '@nhz.io/pouch-db-manager-helpers'
### Resource
class Resource
constructor: ({ @uid, @type, @queue, @name, @local, @remote }) ->
['uid', 'type', 'queue' ].forEach (name) =>
throw TypeError "Missing #{ name }" unless @[name]throw TypeError 'Missing name' unless @name or @local
### Registry
class Registry
@Resource = Resource
constructor: ({ @resources = {}, @types, @queues }) ->
throw TypeError 'Missing types' unless @types?.length
throw TypeError 'Missing queues' unless @queues?.lengthsanitize: (resource) ->
['uid', 'type', 'queue' ].forEach (name) ->
throw TypeError "Missing resource #{ name }" unless resource[name]throw TypeError 'Unknown resource type: #{ resource.type }' unless resource.type in @types
throw TypeError 'Unknown resource queue: #{ resource.queue }' unless resource.queue in @queues
{ name, local, remote } = resource
resource.local ?= resource.name
if resource.local.match /^\s*https?:\/\//
resource.remote = resource.local
resource.name = resource.local = urlname resource.local
resource.key = mkkey resource
resource
register: (resource) ->
resource = @sanitize resource
return existing if existing = @resources[resource.uid]
@resources[resource.uid] = resource
unregister: (resource) ->
uid = resource.uid or resource
return unless resource = @resources[uid]
delete @resources[uid]
resource
find: (query, predicates = []) ->
match = allPass [
if query.type then (resource) -> resource.type?.match query.type
if query.queue then (resource) -> resource.queue?.match query.queue
if query.name then (resource) -> resource.name?.match query.name
if query.local then (resource) -> resource.local?.match query.local
if query.remote then (resource) -> resource.remote?.match query.remote
if query.job then (resource) -> resource.job is job
predicates...
]
resources = (Object.keys @resources).map (uid) => @resources[uid]
resources.find (resource) -> match resource
### Exports
module.exports = Registry
## Tests
test = require 'tape-async'
test 'Resource constructor', (t) ->
t.plan 6
t.throws -> new Resource {}
t.throws -> new Resource { uid: 1 }
t.throws -> new Resource { uid: 1, name: 'foo' }
t.throws -> new Resource { uid: 1, name: 'foo', type: 'a' }
t.ok new Resource { uid: 1, name: 'foo', type: 'a', queue: 'b' }
t.ok new Resource { uid: 1, local: 'foo', type: 'a', queue: 'b' }
test 'Registry constructor', (t) ->
t.plan 4
t.throws -> new Registry {}
t.throws -> new Registry { types: [] }
t.throws -> new Registry { types: [], queues: [] }
t.ok new Registry { types: ['a'], queues: ['b'] }
## Version 0.1.3
## License [MIT](LICENSE)
[travis]: https://img.shields.io/travis/nhz-io/nhz-io-pouch-db-manager-registry.svg?style=flat
[npm]: https://img.shields.io/npm/v/@nhz.io/pouch-db-manager-registry.svg?style=flat