Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bilalswiftsolutions opened this issue Jun 18, 2023 · 6 comments
Open

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

bilalswiftsolutions opened this issue Jun 18, 2023 · 6 comments

Comments

@bilalswiftsolutions
Copy link

bilalswiftsolutions commented Jun 18, 2023

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 
@bilalswiftsolutions bilalswiftsolutions changed the title why parseRelDoc() works only with integer id? why pouchdb.rel.find() works only with integer id? Jun 18, 2023
@broerse
Copy link
Collaborator

broerse commented Jun 18, 2023

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
Copy link
Author

bilalswiftsolutions commented Jun 18, 2023

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
Copy link
Collaborator

broerse commented Jun 18, 2023

@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
Copy link
Author

bilalswiftsolutions commented Jun 18, 2023 via email

@broerse
Copy link
Collaborator

broerse commented Jun 19, 2023

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants