(similar to racer-rpc but more advanced)
//const derby = require('derby');
const racer = require('racer');
const RPC = require('racer-model-rpc');
//const backend = derby.createBackend();
const backend = racer.createBackend();
RPC.backend(backend); // define additional methods to setup a list of extra DBs
Racer uses sharedb
under the hood to handle all operations at server side
// Define RPC handler to process requests from the client side
const rpcHandlerMap = new Map();
const rpcEvent = 'user-finder';
const handler = async ({ email }) => Usecase.findUser({ email });
// Example to define RPC-handler with some processing
rpcHandlerMap.set(rpcEvent, handler);
// Define result formatter
const format = result => {
console.log(result);
return result;
};
// Setub a new extra DB
const db = new RPC.ExtraDB(rpcHandlerMap, format);
backend.addExtraDb(db);
// Check extra DB (just for testing)
const found = backend.getExtraDb(db.name);
console.log('Found extra DB: ', found);
//const client = require('derby');
const client = require('racer');
const extension = require('racer-model-rpc/src/client');
extension(client);
const model = client.createModel();
// For Derby's component just use
//const model = this.model;
const rpcEvent = 'user-finder';
const criteria = { email: 'example@mail.com' };
const success = result => console.log(result);
const failure = error => console.error(error);
model.rpc(rpcEvent, criteria).then(success).catch(failure);
Example of calling with async/await
(async () => {
try {
const result = await model.rpc(rpcEvent, criteria);
success(result);
} catch (error) {
failure(error);
}
})();