pouchdb-community/relational-pouch

Problem obtaining related records

Closed this issue · 2 comments

This is how I defined the scheme, but when I proceed to obtain the "work", the json that is returned to me brings to me all the works and reports. Not respecting the work id for which I am looking for.

@nolanlawson can you help me?, I'm thinking of using only pouchdb without this plugin.

//Schema definition
this.db.setSchema([
{
singular: 'cliente',
plural: 'clientes',
relations: {
trabajos: { hasMany: { type: 'trabajo', options: { queryInverse: 'cliente' } } }
}
},
{
singular: 'trabajo',
plural: 'trabajos',
relations: {
'cliente': { belongsTo: 'cliente' },
informes: { hasMany: { type: 'informe', options: { queryInverse: 'trabajo' } } }
}
},
{
singular: 'informe',
plural: 'informes',
relations: {
'trabajo': { belongsTo: 'trabajo' }
}
}
]);

//Index's
this.db.createIndex({
  index: {
    fields: ['data.cliente', '_id']
  }
}).then(function (result) {
  // handle result
  console.log('Index creado: ' + JSON.stringify(result));
}).catch(function (err) {
  console.log('Index creation error: ' + err);
});

this.db.createIndex({
  index: {
    fields: ['_id', 'data.trabajo']
  }
}).then(function (result) {
  // handle result
  console.log('Index creado: ' + JSON.stringify(result));
}).catch(function (err) {
  console.log('Index creation error: ' + err);
});

@luca147 I believe you need to use a Mango Query in order to take advantage of the Indexes you are creating. if you take a look at the API docs on this for PouchDB, you should be able to filter out the documents you want based on a property of a document: https://pouchdb.com/guides/mango-queries.html

@509dave16 thanks dave, in the same way I decided don't use the plugin and do it myself using pouch and the plugin find.

Thanks a lot for the reply!