Skip to content

AyoAyomide/firestore_goose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

firestore_goose

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

Prerequisite

  • 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();

Installation

Using npm

npm i firestore_goose

Node.js

const FirestoreGoose = require("firestore_goose");

Basic Usage

const fireSG = new FirestoreGoose(firebaseAdmin);

firebaseAdmin : The initialize firebase admin instance

Example 1 - Adding data

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

Example 2 - Adding data using transaction

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

Example 3 - Updating data

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

Example 4 - Update : add to nested object

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' }
*/

Example 5 - Update : add new value to array

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']
*/

Example 6 - Update : remove value from array

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']
*/

Example 7 - Get : field by id

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

Example 8 - Get : all field in a collection

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

Example 9 Find- value in transaction

query = {
  path: userCollection, //firestore collection
  value: detailsToFind // string ::
};
fireSG.findValueInTransaction(query);
/*
--response--
{
  true
}
*/

Example 10 - Get : all value in transaction

query = {
  path: userCollection, //firestore collection
};
fireSG.getAllFromTransaction(query);
/*
--response--
{
  
  John.txId: [...],
}
*/

Example 10 - Get : last document in the collection

query = {
  path: userCollection, //firestore collection
};
fireSG.getLast(query);
/*
--response--
{
  
  John: {...},
  Jane: ...
}
*/

Example 11 - Delete : field by id

userID = "Jane";
query = {
  path: userCollection, //firestore collection
  key: userID, // firestore fieldKey
};
fireSG.delete(query);
/*
--response--
Jane deleted successfully
*/

Example 12 - Delete : a value in a nested field

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' }
*/

About

ORM library for firestore

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published