0

Can't fetch data from table I've just created in rethinkDB.


I've create a new table in retinkDB - items.

And fill it with data:

r.db('test').table('items').insert([
    {name: 'qqq'},
    {name: 'www'},
    {name: 'eee'}
])

BUT .getList() never returns table's data:

client.record.getList('items') // '.getEntries()' always return []

I don't really understand why .getList('items') didn't return data from items table.


I assume that this is because entries structure: when you are create entry via deepstream, entry's structure be like:

[
 ...
  {
    "_d": {
     "id": "qqq"
     "name": 'qqq' 
    },
    "_v": 0,
    "ds_id":  "qqq"
  }
 ...
]

But mine structure is just:

[
  {
    "id": 'qqq'
    "name": 'qqq'
  }
]

My question is: How to create table with data in rethinkDB (via script) that would we worked with deepstream?

S Panfilov
  • 16,641
  • 17
  • 74
  • 96

2 Answers2

-1

I've find a solition.

First of all table should be created with proper primaryKey === "ds_id":

r.db('test')
  .tableCreate('items',
    {
      primaryKey: 'ds_id'
    })

And after that you have to insert data with deepstream-like structure:

r.db('test').table('items').insert([
  {
    "_d": {
      "id": r.uuid(),
      "name": "qqq"
    },
    "_v": 0,
    "ds_id": r.uuid()
}
])

P.S. to make _d.id equal to ds_id use this:

r.db('test').table('items').forEach(function(c) {
  return r.db('test').table('items').get(c('ds_id')).update(function(row) {
    return {_d: {id: row('ds_id')}};
  });
})

This is stupid but I don't know how to do it in more elegant way.

S Panfilov
  • 16,641
  • 17
  • 74
  • 96
-1

deepstream is designed to manage data for you. Simply by saying

var bmw = ds.record.getRecord( 'cars/bmw' )

deepstream will create a table called cars and store an entry with a primary key of bmw in it. You wouldn't be expected to create the table yourself.

wolframhempel
  • 1,094
  • 10
  • 12
  • And how could you create table with data in this case? Hardcode all the data inside code? I asked about script, cause I have to make a migration for DB and need a table filled with data from the begining – S Panfilov Sep 30 '16 at 07:26
  • 1
    For that I'd suppose your solution below would work. Although you could also iterate through your data and load it into deepstream via the client library – wolframhempel Oct 02 '16 at 09:03