Starbase Channels
npm install @starbase/channelsStarbase Channels adds a unix-like directory structure to the Starbase Database and handles the creation and removal of parent and child documents based on channel paths. This allows for documents (channels) with sub-documents, which in turn can have child documents of their own. Channels is great for building APIs based on a path structure.
HTML
`$3
`HTML
`$3
`bash
npm install @starbase/channels @starbase/database
`
Using Channels
$3
`javascript
var database = Database('testdb');
var db = Channels(database);
`$3
`javascript
var Database = require('@starbase/database');
var Channels = require('@starbase/channels');
var database = Database('/path/to/testdb');
var db = Channels(database);
`API Methods
- db.path()
- db.onEvent()
- db.path().channel()
- db.path().put()
- db.path().get()
- db.path().del()
- db.path().list()
$3
#### The path method returns the CRUD operations for the specified channel path
`javascript
var root = db.path('/');
`
By default, the db object will point to the root path '/'$3
#### listen for and react to write and delete events
`javascript
db.onEvent(e => { // an object with the event information that occured
console.log(e);
});
`
NOTE: onEvent is only available as a method on base channels object.$3
#### Return the current channel path in use
`javascript
db.path('/users/mike').channel();
`#### Response
`JSON
"/users/mike"
`
$3
#### Put data into a document at the path specified and create all parent channels.
`javascript
db.path('/users/mike').put({"name":"Mike"}).then(result => {
console.log(result);
});
`NOTE: If the parent channel (in this case "/users") does not exist, this operation will create it. The PUT operation creates all corresponding parents for a child document.
#### Response
`JSON
{
"event": "write",
"path": "/users/mike",
"channel": "/users",
"key": "mike",
"timestamp": 1543119035522
}
`
$3
#### Get data from the document at the path specified.
`javascript
db.path('/users/mike').get().then(result => {
console.log(result);
});
`#### Response
`JSON
{
"path": "/users/mike",
"channel": "/users",
"key": "mike",
"data": {
"name": "Mike"
}
}
`
$3
#### Delete the document, data, and all children from the path specified.
`javascript
db.path('/users').del().then(result => {
console.log(result);
});
`
NOTE: This operation will DELETE ALL CHILD KEYS below it.
#### Response
`JSON
{
"event": "delete",
"paths": [
"/users",
"/users/mike"
],
"timestamp": 1543119519439
}
`
$3
#### List the child keys at a specific path
`javascript
db.path('/users').list().then(result => {
console.log(result);
});
`#### Response
`JSON
{
"data": [
{
"path": "/users/dracula",
"channel": "/users",
"key": "dracula"
},
{
"path": "/users/ghost",
"channel": "/users",
"key": "ghost"
},
{
"path": "/users/mike",
"channel": "/users",
"key": "mike"
}
]
}
`#### List keys with values, ranges, and limits
`javascript
db.path('/users').list({ // Range in reverse lexicographical order
"reverse": true,
// Limit results to 2
"limit":2,
// Return results as objects containing the key and value
"values": true,
// key must be greater than 'alien'
"gt": "alien",
// key must be less than 'werewolf'
"lt": "werewolf"
}).then(result => {
console.log(result);
});
`#### Response
`JSON
{
"data": [
{
"path": "/users/mike",
"channel": "/users",
"key": "mike",
"data": {
"name": "Mike"
}
},
{
"path": "/users/ghost",
"channel": "/users",
"key": "ghost",
"data": {
"name": "ghost"
}
}
]
}
`More Information
$3
The slash in paths denotes the separation of parent channels from child channels. The path method can be chained to achieve the same effect as specifying the complete path.These two reference objects point to the same location:
`javascript
var comments = db.path('/users/mike/posts/1/comments');
``javascript
var comments = db.path('users/mike').path('posts').path('1/comments');
``Starbase Database is great for storing data based on single keys. Starbase Channels expands on that idea and allows for keys to have sub-keys. Parent documents to contain child documents. Starbase Channels combined with the Starbase Rules Engine can provide an API gateway solution.