pouchdb-community / relational-pouch

Store relational data in PouchDB/CouchDB
Apache License 2.0
405 stars 68 forks source link

why pouchdb.rel.find() works only with integer id? #170

Open bilalswiftsolutions opened 1 year ago

bilalswiftsolutions commented 1 year ago

I am trying to fetch related record with the help of pouchdb.rel.find('') it works fine when i give integer id it fetch the related data, but i don't give integer id while storing and give same id generated by couch db it is not working then

this will work

pouchdb.rel
  .save('staff', {
    title: 'Rails is Unagi',
    text: 'Delicious unagi. Mmmmmm.',
    id:5
  })
  .then((data) => {
    console.log(data);
    return pouchdb.rel.save('agent', {
      description: 'nice masculine jawline',
      staff: data.id
    });
  });

but this will not work

pouchdb.rel
  .save('staff', {
    title: 'Rails is Unagi',
    text: 'Delicious unagi. Mmmmmm.',
  })
  .then((data) => {
    console.log(data);
    return pouchdb.rel.save('agent', {
      description: 'nice masculine jawline',
      staff: data.id
    });
  });

As i can not give id manually every time, maybe i am missing some please guide me about it what i am doing wrong here

this is full file i am working on


import PouchDB from 'pouchdb';
//import some adapter
import find from 'pouchdb-find';
import rel from 'relational-pouch';

// import { bulkStaaff } from './bulk';

PouchDB.plugin(require('pouchdb-adapter-http').default)
  .plugin(require('pouchdb-authentication').default)
  .plugin(require('pouchdb-adapter-indexeddb'))
  .plugin(find)
  .plugin(rel)
  .plugin(require('pouchdb-replication'));

const remotedb = new PouchDB('http://localhost:5984/siloc');
const pouchdb = new PouchDB('siloc');

// const db = new PouchDb('http://localhost:5984/siloc');
pouchdb.setSchema([
  { singular: 'staff', plural: 'staffs', relations: { agent: { belongsTo: 'agent' } } },
  { singular: 'agent', plural: 'agents', relations: { staff: { belongsTo: 'staff' } } },
  { singular: 'ranchOwner', plural: 'ranchOwners', relations: { staff: { belongsTo: 'staff' } } },
  { singular: 'rightHandMan', plural: 'rightHandMen', relations: { staff: { belongsTo: 'staff' } } },
  { singular: 'independentContractor', plural: 'independentContractors', relations: { staff: { belongsTo: 'staff' } } },
  { singular: 'packagingCompany', plural: 'packagingCompanies', relations: { staff: { belongsTo: 'staff' } } },
  { singular: 'cuttingCompany', plural: 'cuttingCompanies', relations: { staff: { belongsTo: 'staff' } } },
  { singular: 'truck', plural: 'trucks', relations: { staff: { belongsTo: 'staff' } } },
  { singular: 'boss', plural: 'bosses', relations: { staff: { belongsTo: 'staff' } } }
]);

remotedb
  .sync(pouchdb, {
    live: true,
    retry: true
  })
  .on('change', function (info) {
    // handle change
    console.log(info);
  })
  .on('paused', function (err) {
    // replication paused (e.g. replication up to date, user went offline)
    console.log(err);
  })
  .on('active', function () {
    // replicate resumed (e.g. new changes replicating, user went back online)
  })
  .on('denied', function (err) {
    // a document failed to replicate (e.g. due to permissions)
    console.log(err);
  })
  .on('complete', function (info) {
    // handle complete
    console.log(info);
  })
  .on('error', function (err) {
    // handle error
    console.log(err);
  });

// bulkStaaff.forEach((staff) => {
//   pouchdb.rel.save('staff', staff);
// });

// pouchdb.rel
//   .save('staff', {
//     title: 'Rails is Unagi',
//     text: 'Delicious unagi. Mmmmmm.',
//     id:5
//   })
//   .then((data) => {
//     console.log(data);
//     return pouchdb.rel.save('agent', {
//       description: 'nice masculine jawline',
//       id: 1,
//       staff: data.id
//     });
//   });
console.log(pouchdb.rel.find('staff','F8AD62DF-C70B-BEA3-B42F-639013CECC23'))

export { pouchdb, remotedb };

@broerse 
broerse commented 1 year ago

The ID's Relational Pouch uses contain the model in the PouchDB _id like author_2_NonIntregerID See:

https://my.cloudstation.com/bloggr/author_2_01991B5B-653A-723F-A8F4-C80030E10C3B

https://github.com/pouchdb-community/relational-pouch#dbrelmakedocidparsedid

bilalswiftsolutions commented 1 year ago

Problem is that i dont have id to makeDocId(). Like in mysql we dont have to give id because it autoincrement id. But in this case i have to give id everytime which i dont have. How to do that . You idea will be appropriated. One idea is to get count of records and increment in it and place that id. But i dont think it should work like that. And relation with auto generated uuid are not working. I am stuck here @broerse

broerse commented 1 year ago

@bilalswiftsolutions In an eventually consistent database like CouchDB/PouchDB there is no way to know the next number.

We use uuid's for recorded ID's and listen with a Node.js or OpenWhisk to db.changes in CouchDB and update order numbers or invoice numbers if they are empty. This way only one process determines the numbers. So if users are offline they can create new Orders or Invoices and when they come online the order numbers and invoice numbers are automatically assigned. We store the next ID's in the database like this:

{
"_id":"appconfig_2_referencelist",
"_rev":"6577-f4117ec3b0e8bc0caa1d72a203650ecc",
"data":{
  "nextOrderReferencenr":"45080",
  "nextInbookReferencenr":"1230054",
  "nextDefectivesReferencenr":"1570",
  "nextProformainvoicesReferencenr":"3230229",
  "since":"544707-g1AAAAJ7eJzLYWBg4MhgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUoxJTIkyf___z8rgzmJgZGpMxcoxm6eapRkammCTQ8ek5IUgGSSPdww1odgwywMjZKM0gxJNcwBZFg8wmX7wIaZWaYZG5hYkmpYAsiwerhhjOpgw9LSjIxNDCxINCyPBUgyNAApoHnzIQaqPAYbaJJqnGJuZECWgQsgBu6HGMjLDjbQMDExydTImCwDD0AMvA8xkCsT4mVDQ2MzA3OyDHwAMRAahuyKEBeampknmmN1YRYAuBTC7Q"
}}
bilalswiftsolutions commented 1 year ago

So if i need to access from agent relation to staff i need to have staff if in agent and if i want to access agent from staff then i need to have agent id in staff? It is bidirectional?

I can not access agent from staff while having staff id in agent?

On Mon, Jun 19, 2023, 1:28 AM Martin Broerse @.***> wrote:

@bilalswiftsolutions https://github.com/bilalswiftsolutions In an eventually consistent database like CouchDB/PouchDB there is no way to know the next number.

We use uuid's for recorded ID's and listen with a Node.js or OpenWhisk to db.changes in CouchDB and update order numbers or invoice numbers if they are empty. This way only one process determines the numbers. So if users are offline they can create new Orders or Invoices and when they come online the order numbers and invoice numbers are automatically assigned. We store the next ID's in the database like this:

{"_id":"appconfig_2_referencelist","_rev":"6577-f4117ec3b0e8bc0caa1d72a203650ecc","data":{ "nextOrderReferencenr":"45080", "nextInbookReferencenr":"1230054", "nextDefectivesReferencenr":"1570", "nextProformainvoicesReferencenr":"3230229", "since":"544707-g1AAAAJ7eJzLYWBg4MhgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUoxJTIkyf___z8rgzmJgZGpMxcoxm6eapRkammCTQ8ek5IUgGSSPdww1odgwywMjZKM0gxJNcwBZFg8wmX7wIaZWaYZG5hYkmpYAsiwerhhjOpgw9LSjIxNDCxINCyPBUgyNAApoHnzIQaqPAYbaJJqnGJuZECWgQsgBu6HGMjLDjbQMDExydTImCwDD0AMvA8xkCsT4mVDQ2MzA3OyDHwAMRAahuyKEBeampknmmN1YRYAuBTC7Q" }}

— Reply to this email directly, view it on GitHub https://github.com/pouchdb-community/relational-pouch/issues/170#issuecomment-1596256856, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANXTZUOWDOVTMYU67G62RX3XL5QGBANCNFSM6AAAAAAZK3CNQI . You are receiving this because you were mentioned.Message ID: @.***>

broerse commented 1 year ago

@bilalswiftsolutions I don't completely follow. Maybe take a look at the data from https://bloggr.exmer.com/

https://my.cloudstation.com/bloggr/post_2_C7F645EA-12FA-4744-8CC5-698945C3C6D1 https://my.cloudstation.com/bloggr/author_2_DDEEDA45-9766-A8F1-BEF3-30C53BC663D4

broerse commented 1 year ago

@bilalswiftsolutions Not in the readme I see but db.rel.uuid(); is public if you need a new ID.

    let id = data.id;
    if (!id) {
      id = data.id = rel.uuid();
    }

See how it is done for EmberData createRecord : https://github.com/pouchdb-community/ember-pouch/blob/master/addon/adapters/pouch.js#L520