Everyplay / serverbone

Backbone node.js additions to serve backbone collections/models over express
MIT License
5 stars 5 forks source link

Using Redis collections #7

Closed mulderp closed 10 years ago

mulderp commented 10 years ago

Hi!

Say I have a hash in redis with key and values:

"users:1" "name" "patrick" "role" "admin"

etc.

How would I map this to a Serverbone.BaseCollection ? (Or IndexCollection ?)

mulderp commented 10 years ago

Ah, I see there is something here already: https://github.com/Everyplay/backbone-db-redis

mulderp commented 10 years ago

Just a ping, if there is an update to get started with Serverbone? It would be great to see an example query with Serverbone. E.g.

global.serverbone = require("serverbone");
var repl = require("repl").start({ useGlobal: true, ignoreUndefined: false })

And I start a REPL with `node repl-serverbone``

How could I query a Redis instance, e.g. add keys, list keys, add hashes, etc ?

I am looking into serverbone.models and serverbone.collections but some pointer would be great! Thanks!

mulderp commented 10 years ago

Ok, just was able to write my first data via Backbone-db-redis:

1) "mymodel:mymodels:12321321312"

However, not sure how the relationship between Backbone-db-redis and serverbone is.

mulderp commented 10 years ago

Next, I was trying to save a collection:

> collection = new Collection({a: 1, b:2}, {d: 3, e: 4})
{ length: 1,
  models:
   [ { cid: 'c3',
       attributes: [Object],
       collection: [Circular],
       _changing: false,
       _previousAttributes: {},
       changed: {},
       _pending: false,
       _events: [Object] } ],
  _byId:
   { c3:
      { cid: 'c3',
        attributes: [Object],
        collection: [Circular],
        _changing: false,
        _previousAttributes: {},
        changed: {},
        _pending: false,
        _events: [Object] } } }
> collection.save()
TypeError: Object [object Object] has no method 'save'

Then, tried to save the single models:

lodash.each(collection.models, function(m) { m.save() })

I get something in Redis:

1) "mymodel:mymodels"
2) "mymodel:mymodels:ids"
3) "mymodel:mymodels:1"
4) "mymodel:mymodels:12321321312"
mulderp commented 10 years ago

Interesting, so, the JSON from the models get saved as String not as Hash:

redis 127.0.0.1:6379> get "mymodel:mymodels:3"
"{\"d\":3,\"e\":4,\"id\":3}"
kosmikko commented 10 years ago

Hi,

You could have a look at tests to get better picture how its works. E.g. https://github.com/Everyplay/backbone-db-redis/blob/master/test/test.collection.js https://github.com/Everyplay/serverbone/blob/master/test_integration/redis_sync_spec.js

In your collection example you should give an array of models, just like backbone works. Collection does not have save method, use create for that.