creating tables relationships #1435
-
Hi there, i'm building a recipe library app and i've created a few tables in Rowy. I've set up algolia to enable the connect table column type. The goal is for me to link a tag in "recipeTags" table to "recipeList" table using recipeID. My question is:
Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @ounced. Answering your questions:
Here's an example of how you can create the relationship using a JavaScript GCP Cloud Function: const admin = require('firebase-admin');
exports.createRelationship = async (req, res) => {
const recipeID = req.body.recipeID; // RecipeID to link the tables
const tagID = req.body.tagID; // ID of the tag in the "recipeTags" table
try {
// Get the recipe document from the "recipeList" table
const recipeDoc = admin.firestore().collection('recipeList').doc(recipeID);
const recipeSnapshot = await recipeDoc.get();
const recipeData = recipeSnapshot.data();
// Add the tagID to the recipe document
await recipeDoc.update({
tagID: tagID
});
res.status(200).send('Relationship created successfully');
} catch (error) {
console.error('Error creating relationship:', error);
res.status(500).send('Error creating relationship');
}
};
exports.createRelationshipBulk = async (req, res) => {
const relationships = req.body.relationships; // Array of recipeID-tagID pairs
try {
for (const { recipeID, tagID } of relationships) {
const recipeDoc = admin.firestore().collection('recipeList').doc(recipeID);
const recipeSnapshot = await recipeDoc.get();
const recipeData = recipeSnapshot.data();
await recipeDoc.update({
tagID: tagID
});
}
res.status(200).send('Bulk relationships created successfully');
} catch (error) {
console.error('Error creating bulk relationships:', error);
res.status(500).send('Error creating bulk relationships');
}
};
I hope the above solutions/answers will be helpful. Feel free to reach out if you have more questions or feedback. You can also join one of our upcoming office hours and Discord to interact with the community. -- |
Beta Was this translation helpful? Give feedback.
-
Hey Gaurav, thanks for this. let me try this out. i'm not very technical person but is learning. will need some time to implement this solution. |
Beta Was this translation helpful? Give feedback.
Hi @ounced. Answering your questions:
Here's an example of how you can create the relationship using a JavaScript GCP Cloud Function: