Starbase Channels
Starbase 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.
<script src="/path/to/database.min.js"></script>
<script src="/path/to/channels.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@starbase/database/database.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@starbase/channels/channels.min.js"></script>
npm install @starbase/channels @starbase/database
var database = Database('testdb');
var db = Channels(database);
var Database = require('@starbase/database');
var Channels = require('@starbase/channels');
var database = Database('/path/to/testdb');
var db = Channels(database);
- db.path()
- db.onEvent()
- db.path().channel()
- db.path().put()
- db.path().get()
- db.path().del()
- db.path().list()
var root = db.path('/');
By default, the db object will point to the root path '/'
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.
db.path('/users/mike').channel();
"/users/mike"
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.
{
"event": "write",
"path": "/users/mike",
"channel": "/users",
"key": "mike",
"timestamp": 1543119035522
}
db.path('/users/mike').get().then(result => {
console.log(result);
});
{
"path": "/users/mike",
"channel": "/users",
"key": "mike",
"data": {
"name": "Mike"
}
}
db.path('/users').del().then(result => {
console.log(result);
});
NOTE: This operation will DELETE ALL CHILD KEYS below it.
{
"event": "delete",
"paths": [
"/users",
"/users/mike"
],
"timestamp": 1543119519439
}
db.path('/users').list().then(result => {
console.log(result);
});
{
"data": [
{
"path": "/users/dracula",
"channel": "/users",
"key": "dracula"
},
{
"path": "/users/ghost",
"channel": "/users",
"key": "ghost"
},
{
"path": "/users/mike",
"channel": "/users",
"key": "mike"
}
]
}
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);
});
{
"data": [
{
"path": "/users/mike",
"channel": "/users",
"key": "mike",
"data": {
"name": "Mike"
}
},
{
"path": "/users/ghost",
"channel": "/users",
"key": "ghost",
"data": {
"name": "ghost"
}
}
]
}
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:
var comments = db.path('/users/mike/posts/1/comments');
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.