localbase icon indicating copy to clipboard operation
localbase copied to clipboard

How to use fk in collections?

Open renanjoppert1 opened this issue 3 years ago • 1 comments

I using localbase in a off-line project, and I have a question. Is It possible link a data in a collection with data in another collection?

renanjoppert1 avatar Feb 27 '21 23:02 renanjoppert1

Yes, but you have the responsibility of defining these links and recovering them.

Say for instance you have a collection of 'parents' and a collection of 'children' with some simple properties like an Id and a Name. If you want to create a link, you would have to set your documents like this:

// adds a parent to the database
db.collection('parents').add({id:0, name:"John"}); 
// adds Lucie as a children of John (id=0 in this example)
db.collection('children').add({id:0, name:"Lucie", parentId: 0}); 

To then retrieve them from the database, you would have to:

const john = db.collection('parents').doc({id=0}).get();
// the following line returns all entries in the children collection that contain the parentId of 0.
const johnChildren = db.collection('children').doc({parentId:0}).get(); 

It's up to you then to do whatever you want with this data. ;)

philippedasilva-orizone avatar May 14 '21 16:05 philippedasilva-orizone