The firestore_goose is library for working with firestore. it makes it easy to work with firestore.
it built on top of firsbase-admin. You still need to understand the basics of firestore to use this library it main purpose is to make it easy using firestore
- make sure you have firebase-admin installed
npm i firebase-admin
- make sure you have initialized your firebase project
const firebaseAdmin = require("firebase-admin");
firebaseAdmin.initializeApp();
npm i firestore_goose
const FirestoreGoose = require("firestore_goose");
const fireSG = new FirestoreGoose(firebaseAdmin);
firebaseAdmin : The initialize firebase admin instance
let userCollection = "user";
let userID = "John";
let userDetails = {
lastName: "Doe",
age: 20,
kidsNames: ["Jam", "Mike"],
weeklyCars: { monday: "ferrari", tuesday: "benz" },
};
let query = {
path: userCollection, //firestore collection
key: userID, // firestore fieldKey
value: userDetails, // firestore fieldValue
};
fireSG.add(query).then((response) => {
console.log(response);
});
// save another data
let query2 = { path: userCollection, key: "Jane", value: "Doe" };
fireSG.add(query2).then((response) => {
console.log(response);
});
/*
--response--
{
id: '5t7AyoVQOgOhXap63wtW',
data: {
John: {
kidsNames: [Array],
lastName: 'Doe',
weeklyCars: [Object],
age: '20'
},
}
}
*/
NOTE
To overwrite a field you use fireSG.add(query,{force:true})
This will overwrite the existing field data
- id : The document id
- data : The document data
let detailsToUpdate = "random data";
query = {
path: userCollection, //firestore collection
value: detailsToUpdate // firestore nested field value
};
fireSG.addWithTransaction(query);
/*
it stores items in an array using firebase transaction
*/
- value : The data we want to add to transaction array
Note: Only existing document can be updated
let detailsToUpdate = "age";
let updatedAge = 35;
query = {
path: userCollection, //firestore collection
key: userID, // firestore fieldKey
childObject: detailsToUpdate, // firestore nested field Key
value: updatedAge, // firestore nested field value
};
fireSG.updateByID(query);
/*
--response--
John.age updated successfully
--output--
John.age = 35
*/
- childObject : The nested field object key we want to update
detailsToUpdate = "weeklyCars.wednesday";
let updatedCar = "bmw";
query = {
path: userCollection, //firestore collection
key: userID, // firestore fieldKey
childObject: detailsToUpdate, // firestore nested field Key
value: updatedCar, // firestore nested field value
};
fireSG.updateByID(query);
/*
--response--
John.weeklyCars.wednesday updated successfully
--output--
John.weeklyCars = { monday: 'ferrari', tuesday: 'benz', wednesday: 'bmw' }
*/
detailsToUpdate = "kidsNames";
let childToAdd = "Jerry";
query = {
path: userCollection, //firestore collection
key: userID, // firestore fieldKey
childArrayAdd: detailsToUpdate, // firestore nested field array Key
value: childToAdd, // firestore nested field array value
};
fireSG.updateByID(query);
/*
--response--
John.kidsNames updated successfully
--output--
John.kidsNames = ['Jam', 'Mike', 'Jerry']
*/
detailsToUpdate = "kidsNames";
let childToRemove = "Mike";
query = {
path: userCollection, //firestore collection
key: userID, // firestore fieldKey
childArrayRemove: detailsToUpdate, // firestore nested field array Key
value: childToRemove, // firestore nested field array value
};
fireSG.updateByID(query);
/*
--response--
John.kidsNames updated successfully
--output--
John.kidsNames = ['Jam', 'Jerry']
*/
query = {
path: userCollection, //firestore collection
key: userID, // firestore fieldKey
};
fireSG.getByID(query);
/*
--response--
{
lastName: 'Doe',
weeklyCars: { tuesday: 'benz', monday: 'ferrari' },
age: 35,
kidsNames: [ 'Jam', 'Jerry' ]
}
*/
- response : it returns only one field data
query = {
path: userCollection, //firestore collection
key: userID, // firestore fieldKey
};
fireSG.getByID(query);
/*
--response--
{
lastName: 'Doe',
weeklyCars: { tuesday: 'benz', monday: 'ferrari' },
age: 35,
kidsNames: [ 'Jam', 'Jerry' ]
}
*/
- response : it returns only one field data
query = {
path: userCollection, //firestore collection
value: detailsToFind // string ::
};
fireSG.findValueInTransaction(query);
/*
--response--
{
true
}
*/
query = {
path: userCollection, //firestore collection
};
fireSG.getAllFromTransaction(query);
/*
--response--
{
John.txId: [...],
}
*/
query = {
path: userCollection, //firestore collection
};
fireSG.getLast(query);
/*
--response--
{
John: {...},
Jane: ...
}
*/
userID = "Jane";
query = {
path: userCollection, //firestore collection
key: userID, // firestore fieldKey
};
fireSG.delete(query);
/*
--response--
Jane deleted successfully
*/
userID = "John";
detailsToUpdate = "weeklyCars.tuesday";
query = {
path: userCollection, //firestore collection
key: userID, // firestore fieldKey
childObject: detailsToUpdate, // firestore nested field Key
};
fireSG.delete(query);
/*
--response--
John.weeklyCars.tuesday deleted successfully
--output--
John.weekly = { monday: 'ferrari' }
*/