GithubHelp home page GithubHelp logo

Comments (12)

jlami avatar jlami commented on July 3, 2024 2

Ok, the error is in the final step. You did not return the final promise.

}).then(function() {
  var result = db.rel.find('author', 1);
  return result.then(function(data) {
    console.log(data)
  });
}).catch(console.log.bind(console));

I inserted an extra return there. Then you will see an error. The error is due to the fact that pouchdb-find is not loaded. If you insert the following line at the top it will work correctly:
PouchDB.plugin(require('pouchdb-find'));

We will have to look into whether this should be included in the setup code of relational-pouch itself, or whether it is the job of the user. Plugin hierarchy is a bit tricky to do correctly. But for now this should fix your problem.

from relational-pouch.

fransyozef avatar fransyozef commented on July 3, 2024 1

Hmm I really like the "queryInverse" solution. But now I've noticed that when I look up the author and it will get the books, but in a seperate key outside "author". Is it possible to return like this:

{ author : { name :"my author", books : [ { title : "book 1" }, { title : "book 2" } ] } }

Or do I need to sort that out on my code logic side?

from relational-pouch.

broerse avatar broerse commented on July 3, 2024

You can set the dontsave option. See https://github.com/nolanlawson/ember-pouch#dont-save-hasmany-child-ids

This new mode can be selected for a hasMany relationship by specifying the option dontsave: true on the relationship.

from relational-pouch.

oleersoy avatar oleersoy commented on July 3, 2024

Would I do that on the schema like this:

    singular: 'author',
    plural: 'authors',
    relations: {
      books: {
        hasMany: 'book',
        dontsave: true
      }
    }

Also should it be true ... I want the books to the update on the author when I save a new book with the author specified ...?

from relational-pouch.

broerse avatar broerse commented on July 3, 2024

Sorry I think you are searching for this:

  {
    singular: 'author',
    plural: 'authors',
    relations: {
      books: {hasMany: {type: 'book', options: {queryInverse: 'author'}}}
    }
  },

from relational-pouch.

oleersoy avatar oleersoy commented on July 3, 2024

I tried it and ended up getting this:

ole@MKI:~/Junk/pouch$ rm -fr mydb/ && node index.js 
/home/ole/Junk/pouch/node_modules/relational-pouch/lib/index.js:78
          throw new Error('Invalid relationship definition for: ' + field);
          ^

Error: Invalid relationship definition for: books
    at /home/ole/Junk/pouch/node_modules/relational-pouch/lib/index.js:78:17
    at Array.forEach (native)
    at /home/ole/Junk/pouch/node_modules/relational-pouch/lib/index.js:75:35
    at Array.forEach (native)
    at PouchDB$5.exports.setSchema (/home/ole/Junk/pouch/node_modules/relational-pouch/lib/index.js:70:10)
    at Object.<anonymous> (/home/ole/Junk/pouch/index.js:5:4)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

This is the full script:

var PouchDB = require('pouchdb');
PouchDB.plugin(require('relational-pouch'));

var db = new PouchDB('mydb');
db.setSchema([
  {
    singular: 'author',
    plural: 'authors',
    relations: {
      books: {
        hasMany: 'book',
        options: {
          queryInverse: 'author'
        }
      }
    }
  },
  {
    singular: 'book',
    plural: 'books',
    relations: {
      author: {
        belongsTo: 'author'
      }
    }
  }
]);
db.rel.save('author', {
  name: 'George R. R. Martin',
  id: 1,
  books: [6, 7]
}).then(function() {
  return db.rel.save('book', {
    title: 'A Game of Thrones',
    id: 6,
    author: 1
  });
}).then(function() {
  return db.rel.save('book', {
    title: 'The Hedge Knight',
    id: 7,
    author: 1
  });
}).then(function() {
  return db.rel.save('book', {
    title: 'Winny the Pooh',
    id: 8,
    author: 1
  })
}).then(function() {
  var result = db.rel.find('author', 1);
  result.then(function(data) {
    console.log(data)
  });
}).catch(console.log.bind(console));

from relational-pouch.

jlami avatar jlami commented on July 3, 2024

@broerse did it correctly, you should make it

  {
    singular: 'author',
    plural: 'authors',
    relations: {
      books: {hasMany: {type: 'book', options: {queryInverse: 'author'}}}
    }
  },

You have the name of the type as the value of hasMany but to give options you have to give a hash to hasMany with the name in type and extra options in options. I agree it is a bit weird, but it is in line with the other options like async.

And by the way, to improve performance while doing this, make sure you setup an index on the key you are querying (+ the id field) as mentioned here: https://github.com/nolanlawson/relational-pouch#dont-save-hasmany

from relational-pouch.

oleersoy avatar oleersoy commented on July 3, 2024

@jlami - ah got it. I only pasted in the options object initially. However if I run the script like this:

Your Snippet

var PouchDB = require('pouchdb');
PouchDB.plugin(require('relational-pouch'));

var db = new PouchDB('mydb');
db.setSchema([
  {
    singular: 'author',
    plural: 'authors',
    relations: {
      books: {
        hasMany: {
          type: 'book',
          options: {
            queryInverse: 'author'
          }
        }
      }
    }
  }, {
    singular: 'book',
    plural: 'books',
    relations: {
      author: {
        belongsTo: 'author'
      }
    }
  }
]);
db.rel.save('author', {
  name: 'George R. R. Martin',
  id: 1,
  books: [6, 7]
}).then(function() {
  return db.rel.save('book', {
    title: 'A Game of Thrones',
    id: 6,
    author: 1
  });
}).then(function() {
  return db.rel.save('book', {
    title: 'The Hedge Knight',
    id: 7,
    author: 1
  });
}).then(function() {
  return db.rel.save('book', {
    title: 'Winny the Pooh',
    id: 8,
    author: 1
  })
}).then(function() {
  var result = db.rel.find('author', 1);
  result.then(function(data) {
    console.log(data)
  });
}).catch(console.log.bind(console));

I get this:

ole@MKI:~/Junk/pouch$ rm -fr mydb/ && node index.js 

So it does not look as if the author was added at all. Thoughts?

from relational-pouch.

jlami avatar jlami commented on July 3, 2024

Since you now specify that you don't want to save the hasmany side of the relationship, the line books: [6, 7] is not needed anymore, and maybe even harmful. I don't know if this is really the culprit. But it should be safe to remove, as now the reverse key author on the book type should be used.

Why the complete author document is not saved I don't really know. But maybe you can report back with the books key removed?

from relational-pouch.

oleersoy avatar oleersoy commented on July 3, 2024

OK I removed the books part:

db.rel.save('author', {
  name: 'George R. R. Martin',
  id: 1
}).then(function() {
  return db.rel.save('book', {
    title: 'A Game of Thrones',
    id: 6,
    author: 1
  });
}).

However the author is still not saved ...?

from relational-pouch.

oleersoy avatar oleersoy commented on July 3, 2024

OK Awesome!! It works now. BTW - Do you think the configuration for hasMany relationships should be made to be more symmetrical. The initial example configuration looks like this:

{singular: 'author', plural: 'authors', relations: { books: {hasMany: 'book'}}}

But if we add the inverse query option then it looks like this:

  {
    singular: 'author',
    plural: 'authors',
    relations: {
      books: { hasMany: {type: 'book', options: { queryInverse: 'author'}}}}},

So in other words it goes from { books: {hasMany: 'book'} to { books: {hasMany: {type: 'book'}

I think the type: 'book' version is also easier to read.

from relational-pouch.

jlami avatar jlami commented on July 3, 2024

The results are currently oriented a bit towards ember-data, which uses the JSON api internally. I guess we could give the caller extra options to configure whether to return only the ids and the objects in a seperate array as is the case now, or to return the objects embedded. But for now you should do it on your side (or you could always write a PR ;)

from relational-pouch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.