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

How to use fk in collections? #27

Open
renanjoppert1 opened this issue Feb 27, 2021 · 1 comment
Open

How to use fk in collections? #27

renanjoppert1 opened this issue Feb 27, 2021 · 1 comment

Comments

@renanjoppert1
Copy link

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?

@philippedasilva-orizone

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. ;)

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