I am new to falcor data fetching framework. I tried with few example when I request for something like
model.get(["contacts", {0..2}, "name"])
.then(response => {
this.state.list = response.json.contacts;
this.setState(this.state);
});
at server side
let data = {
contacts: [
{name: "ABC"},
{name: "XYZ"},
{name: "PQR"}
]
};
let contactsRouter = Router.createClass([
{
route: 'contacts[{integers:contactIndexes}]',
get: (pathSet) => {
let results = [];
pathSet.contactIndexes.forEach(contactIndex => {
if (data.contacts.length > contactIndex) {
results.push({
path: ["contacts", contactIndex, "name"],
value: data.contacts[contactIndex].name
});
}
});
return results;
}
},
{
route: 'contacts.add',
call: (callPath, args) => {
var newContact = args[0];
data.contacts.push({name: newContact})
return [
{
path: ['contacts', data.contacts.length-1, 'name'],
value: newContact
},
{
path: ['contacts', 'length'],
value: data.contacts.length
}
]
}
}
]);
I'm getting data & able to do other operations too. My question is I want to do same CRUD operations with MongoDB instead from
data.contacts
how i construct JSON Graph object data should come from database schema. hope my question is cleared.
The simplest way is to simply do a database query inside the route's get
function:
{
route: 'contacts[{integers:contactIndexes}]',
get: (pathSet) => {
const data = db.get('myModel', (err, res) => {
return res
})
let results = [];
pathSet.contactIndexes.forEach(contactIndex => {
if (data.contacts.length > contactIndex) {
results.push({
path: ["contacts", contactIndex, "name"],
value: data.contacts[contactIndex].name
});
}
});
return results;
}
}
Made a simple repo using Falcor and CouchDB. It should be enough to understand how it should be done in MongoDB.