@oddnetworks/oddworks-example-data
Example seed functions that help populate our example applications with data.
Installation
$ npm install --save @oddnetworks/oddworks-example-data
Usage
Require the example data package in your script
const exampleData = require('@oddnetworks/oddworks-example-data');
Functions on the module exampleData
will take an oddcast bus, like so:
const oddcast = require('oddcast');
const bus = oddcast.bus();
exampleData.nasa(bus);
These functions return promises.
In reality, these functions can do whatever you like. We use them to send messages on the oddcast bus so that our example data is sent to any observing "stores". Read more about stores in oddworks
Seed Script
In the nasa example, we're loading all the relative JSON files, which each contain a single object, and sending them into our stores.
Note: You don't have to use JSON files here. This data can come from anywhere. Any objects passed to the bus should be in the oddworks entity data format, however.
module.exports = bus => {
return glob('./+(channel|platform)/*.json', {cwd: __dirname})
.then(loadFiles)
.then(objects => {
return Promise.all(seedData(bus, objects));
})
.then(() => {
return glob('./+(collection|promotion|video|view)/*.json', {cwd: __dirname});
})
.then(loadFiles)
.then(objects => {
return Promise.all(seedData(bus, objects))
});
};
In a nutshell, we're loading all of the channel
and platform
JSON objects first, then we're loading all the collection
, promotion
, video
, and view
JSON objects.
Let's break down the seedData
method:
function seedData(bus, objects) {
const promises = [];
for(let object of objects) {
const searchable = Boolean(searchableTypes.indexOf(object.type) + 1);
let pattern = {role: 'store', cmd: 'set', type: object.type};
if (searchable) {
pattern = {role: 'catalog', cmd: 'create', searchable: true};
}
const payload = {
version: 1,
channel: object.channel,
platform: object.id,
scope: ['platform']
};
promises.push(bus.sendCommand(pattern, object));
}
return promises;
}
Contributing
Feel free to contribute and create additional sample data/seeds. Please make sure any data is in the public domain.