Firebase: how to create nested object - javascript

There are questions on how to update nested properties for a Firebase record, but no answers on how to create records with nested properties.
This and this were similar but did not help.
From the web, the goal is to create a Firebase record with nested properties.
Using dot notation works for updates, but a nested hierarchy doesn't get created when reusing the same key for creating the record.
Which makes sense because the key doesn't impart any information about the data types of the child properties.
What is the right way to create an object with nested properties?
async test(serviceId, numCredits, emailAddress) {
// Set credits key.
let creditsKey = `credits.${serviceId}.numAllowed`;
try {
// Get user matching #emailAddress.
let user = await this.getUser(emailAddress);
// New user? Create database record.
if (!user) {
this.db_
.collection('users')
.add(
{
emailAddress: emailAddress,
[{creditsKey}]: numCredits
}
);
// Nope, user exists so update his/her record.
} else {
// Set update query.
let query = this.db_
.collection('users')
.where('emailAddress', '==', emailAddress);
// Run update query.
const querySnapshot = await query.get();
return querySnapshot.docs[0].ref.update({
[creditsKey]: firebase.firestore.FieldValue.increment(numCredits)
});
}
} catch(e) {
debug('Error in test(): ' + e);
}
}

If I correctly understand your question, the following would do the trick. (There are probably more elegant solutions however...)
const obj = {};
obj.numAllowed = numCredits;
const obj1 = {};
obj1[serviceId] = obj;
// ...
this.db_.collection('users')
.add(
{
emailAddress: emailAddress,
credits: obj1
})

Related

Deep copy of the Object to add a key : value

I am pre-fetching a product from a database using mongoose with next.js and react-query. I was wondering why I need to do a deep copy of a nested object in order to add a key:value to it. Otherwise it does not work. Let me know what I am not understanding.
await queryClient.prefetchQuery(['productSlug', slug], async () => {
const product = await read(slug);
const existingRatingObject = product.ratings.find(
(item) => item.postedBy.toString() === user._id.toString()
);
const copyProduct = JSON.parse(JSON.stringify(product));
if (existingRatingObject) {
copyProduct.star = existingRatingObject.star;
} else {
copyProduct.star = 0;
}
console.log({ copyProduct });
return JSON.stringify(copyProduct);
});
The reason is that the product fetched is a Mongoose document not a plain old JavaScript object.
When you convert it to plain old javascript Object, you will be able to add any key to it.
You can add .lean() to you query or add toObject/toJSON to you the fetched document

sequelize ignore "save()" method on JSON object

I am trying to take benefits of model instance methods, as stated in the doc. So I defined User class as following:
class User extends Model {
addRole(role){
let roles = this. roles;
roles[role] = true;
this.roles = roles;
this.save();
}
removeRole (role) {
let roles = this.roles;
delete roles[role];
this.save();
}
hasRole (role){
return this.roles[role] != null;
}
}
User.init({
// some attributes
,
roles:{
type: DataTypes.JSON,
allowNull: false,
}
}, { sequelize});
I expected to use methods addRole(), removeRole() and hasRole() in any User instance.
The problem that all the methods can't save their changes to database. (Can read only!)
// example
let user = null;
// get the first user to test.
User.findAll()
.then(users =>{
user = users[0];
user.addRole("admin");
console.log(user.roles); // {admin: true}
user.save();
// However the changes don't appear in the database.
});
I had found the answer.
For some reasons, sequelise can't detect the changes of the json object properly. As sequelise is optimised internally to ignore call to model.save() if there is no changes of the model. So, sequelize randomly ignore the save method.
This behavior had no relation with instance method as I believed when I face this problem first time.
To get out of this problem, I had to use :
user.addRole("admin");
user.changed("roles", true); // <<<< look at this;
console.log(user.roles); // {admin: true}
user.save();
Please note that this function will return false when a property from a nested (for example JSON) property was edited manually, you must call changed('key', true) manually in these cases. Writing an entirely new object (eg. deep cloned) will be detected.
Example:
const mdl = await MyModel.findOne();
mdl.myJsonField.a = 1;
console.log(mdl.changed()) => false
mdl.save(); // this will not save anything
mdl.changed('myJsonField', true);
console.log(mdl.changed()) => ['myJsonField']
mdl.save(); // will save
changed method usage

How do to retrieve all documents in a collection and see if a document exists in firebase Firestore?

Schema:
This is how my schema looks
Current Implementation:
for (let i=0; i<data.length; i++) {
try
{
var ifPresent = db.collection("Safes-Hardware").doc(data[i]['Mac address Check']);
ifPresent.get()
.then(async (doc)=>{
if (!doc.exists)
{
// Do stuff
}
else
{
//Do stuff
}
return { message: "Success is within the palm of our hands." }
}
}
Problem:
Even though this code does the job, for each data in the array I'm doing a lookup, and this results in a socket hang-up.(sometimes)
So I'm thinking I'll get all the documents in the collection in one go, store it locally and look up if a documents exists locally instead of querying the database every time.
Question:
How do I implement this?
You can just use collection("Safes-Hardware").get().then() and you can save the data locally.
let collection = []
db.collection("Safes-Hardware").get().then(function(querySnapshot) {
collection = querySnapshot.map((doc) => ({
id: doc.id,
...doc.data()
}))
});
then you can use collection to search for what you want, maybe like this
data.forEach( doc => {
let x = collection.find(v => v.id === doc['Mac address Check'])
if(x){
//it exists
}else{
// not exists
}
})
But take care you are compromising bandwidth or number of requests with o(n^2) operation in the client side

Firebase - Update specific fields simultaneously without replacing all of the data

I would like to be able to publish simultaneously in two directories of my Firebase database. I created a function for this, according to the example proposed in the "Update specific fields" section of the Firebase Javascript documentation:
function linkTwoUsers(user1, user2) {
// The two users are "connected".
var user1Data = {
userLink: user2
};
var user2Data = {
userLink: user1
};
var updates = {};
updates["/users/" + user1] = user1Data;
updates["/users/" + user2] = user2Data;
return database
.ref()
.update(updates)
.then(() => {
return res.status(200).end();
})
.catch(error => {
return res.status(500).send("Error: " + error.message);
});
}
The problem is that when I run the function, instead of uploading the directories, it replaces all the data present in it.
Here are the user directories before the function:
And then:
How do we make sure the data doesn't overwrite the others? Thank you for your help.
Try to narrow your path to just the property you are trying to update:
updates["/users/" + user1 + "/userLink/"] = user1;
updates["/users/" + user2 + "/userLink/"] = user2;
It seems as though you're creating an entirely new object when you set:
var userData = { someThing: stuff }
When you pass that in, it will override the original object. One way you might solve this (there might be a more efficient way) is to grab the objects from Firebase, add the new property and value to the object, then send the entire object back into Firebase.
In some javascript frameworks, you should be able to use the spread operator to set all of an object's props to another object like this:
var newObject = { ...originalObject }
newObject.userData = "something"
// then save newObject to firebase

Creating a field in a Firestore collection from a field referenced in another Firestore collection using Google Cloud Functions

I am trying to create a field ("artistName") in a Firestore collection ("shows") that is pulling from a field ("name") in another Firestore collection ("artists"). The "shows" collection has a reference field ("artist") that points to the a document in the "artists" collection. To create the field, Im using Google Cloud Functions. Here is my code:
exports.addReferenceDataToCollection = functions.firestore
.document('shows/{showId}').onWrite(event => {
var newValue = event.data.data();
var artistId = newValue.artist.id;
var artistRef = firestore.collection('artists').doc(artistId);
return event.data.ref.set({
artistName: artistRef.get().then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
var artistName = doc.data().name;
console.log('Artist Name:', artistName);
return Promise.resolve(artistName);
}
})
}, {merge: true});
});
I cant seem to get the data out of the promise.
You need to do artistRef.get() first, then after you have the document you need, use its data in event.data.ref.set(). As you've written it now, you're assigning a promise object to the artistName property.
The general form of this type of pattern looks like this:
// First get the artist doc
return artistRef.get().then(doc => {
return event.data.ref.set({
// use the properties of the doc in here
})
})

Categories