feathersjs-ecosystem / feathers-memory

An in memory feathers service
MIT License
44 stars 18 forks source link

How properly append values to an existing memory element #9

Closed EderSantana closed 8 years ago

EderSantana commented 8 years ago

Hi,

I'm learning Feathers by creating a real time data visualization app. For my demo I'll start using the memory service. I'd like to know what is the proper way to append values do a list using the REST api Feathers provides us. For example. given memory as the service, I create an element like

memory.create {'name': 'train-cost',
'type': 'cost_line', 'value': []}, (error, data) ->
    console.log 'created data: ',  data

I want to send a request to append numbers to the value list. I didn't want the client to GET the entire list append the new value and resend everything again. I'd really appreciate if you could give me some advice on how to do that. Right now, I'm thinking about creating a second service that only deals with the updates. But if you have any other idea. Please let me know.

marshallswain commented 8 years ago

I don't think the memory adapter supports this type of query. You would need to read, modify, then write. The MongoDB adapter would support this, but you might consider using the NeDB adapter, instead, which would allow for the flexibility that you want. It does persist to disk, but it's extremely fast because all operations are appends, then it periodically compacts the data file to save space. When you want to delete the data, you simply delete the file from the db-data folder. It supports nearly all of MongoDB's basic operations, so I'm pretty sure you'd be able to accomplish what you want with it.

daffl commented 8 years ago

.patch extends an existing resource but it does not do that recursively which means you still have to provide the entire value array. If you have two entities that you want to update individually the best way is probably to use two separate services and associate them via their ids like this:

var app = feathers()
  .use('/cost', memory())
  .use('/values', memory());

app.service('cost').create({
  'name': 'train-cost',
  'type': 'cost_line'
}, function(error, cost) {
  var valueService = app.service('values');

  valueService.create({
    value: 10,
    cost_id: cost.id
  });

  valueService.create({
    value: 15,
    cost_id: cost.id
  });
});

Now you can just retrieve all values for a cost by requesting values?cost_id=<costid> and delete, remove and update them separately.

If you still want to provide a values array if someone e.g. does a get for a cost you can use a feathers-hooks after hook like this:

var hooks = require('feathers-hooks');
app.configure(hooks())
  .service('cost').after({
    get: function(hook, next) {
      var cost = hook.result;
      app.service('values').find({
        query: { cost_id: cost.id }
      }, function(error, values) {
        cost.values = values;
        next();
      });
    }
  });

There is also the feathers-collections plugin but @ebourmalo can give a better status on where that is.

EderSantana commented 8 years ago

I decided to try that with mongodb. Now the issue is a little bit different and moved to here: https://github.com/feathersjs/feathers-mongodb/issues/14