devlucky/Kakapo.js

Support for 1-to-1 and many-to-many relationships

devanp92 opened this issue · 5 comments

In the README.md it states that the DB supports "belongsTo and hasMany" features. Although, it would be nice to have 'strict' checking for 1-to-1 or many-to-many relationships.

I believe that this represents the many-to-many relationship:

const kakapoBreedingFactory = () => ({
  humans: db.hasMany('human'),
  kakapos: db.hasMany('kakapo')
});

const humanFactory = () => ({
  name: 'Billy Bob',
  kokapo: db.belongsTo('kokapo')
});

const kokapoFactory = () => ({
  name: 'Kokapo1',
  owner: db.belongsTo('human')
});

But explicitly stating the relationship preserves constraints.

For 1-to-1 relationships, having db.hasOne(collectionName) would be useful in this situation.

Hi @devanp92 I'm not sure if I understand what you wanna achieve, but if I get it right you want to make sure that a specific record is being returned? You can achieve that doing:

const humanFactory = () => ({
  name: 'Billy Bob',
  kokapo: db.belongsTo('kokapo', {name: 'Kokapo1'})
});

That way the human record will ensure you return the right record. Please let me know if this is valid for you, if not I'll be happy to implement your feature!

Hi @zzarcon Thanks for responding. Yes, that looks valid!

Closing issue.

@devanp92 you welcome, please let me know if you find any other issue or if you think we have to improve something from the docs ;)

Not critical - but how about detailing how to design the relationships using hasMany, and belongsTo?

I can write the docs if necessary.

Sure, please feel free to improve them if you like, those are the current ones for the Database, http://devlucky.github.io/kakapo-js#database, you can find all of them here: