localbase
localbase copied to clipboard
How to use fk in collections?
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?
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. ;)