Google Cloud Firestore API Wrapper Class for Node.JS
import { FirestoreRepository } from './FirebaseRepository';
// Create user repository object from instantiating FirestoreRepository class
const userRepository = new FirestoreRepository();
userRepository.setCollection('users');
// Get all users
userRepository.get().then(users => {
console.log(users);
});
Available Methods: get(), getById(), add(), update(), delete()
Please follow below steps to setup this project in your local machine.
- Clone from Github repository
git clone https://github.com/RolandBanguiran/node-firestore-wrapper.git
- Select project folder
- Install project dependencies
cd node-firestore-wrapper
npm install
- Add
serviceAccountKey.json
file to your project root folder - Paste your generated private key configuration like below
{
"type": "service_account",
"project_id": "node-firestore-repository",
"private_key_id": "",
"private_key": "",
"client_email": "firebase-adminsdk-m693l@node-firestore-repository.iam.gserviceaccount.com",
"client_id": "",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-m693l%40node-firestore-repository.iam.gserviceaccount.com"
}
npm run-script start
Set collection name.
Parameters:
collectionName
The collection name
get(conditions: Array<Condition> = null, orderBy: Array<OrderBy> = null, limit: number = null): Promise<Document[]>
Get documents in a collection.
Parameters:
conditions
Array of conditionsorderBy
Field name to order bylimit
Number of documents to retrieve
Returns:
- Array of
Document
objects
Sample Code:
// Get all users
userRepository.get().then(users => {
console.log("All users:", users);
});
// Get all male users
const condition: Condition = {
field: 'gender',
operator: Operators.Equal,
value: 'male'
};
userRepository.get([condition]).then(users => {
console.log("All male users:", users);
});
// Get first male user order by name (need to create an index)
const condition2: Condition = {
field: 'gender',
operator: Operators.Equal,
value: 'male'
};
const orderBy: OrderBy = {
field: 'name',
sort: SortType.Ascending
};
userRepository.get([condition2], [orderBy], 1).then(user => {
console.log("First male user:", user);
});
Get a document by ID
Parameters:
id
The document ID
Returns:
- A
Document
object ornull
Sample Code:
userRepository.get('helloworld123').then(user => {
console.log("User:", user);
});
Add a document.
Parameters:
data
A document objectid
The document ID
Returns:
ResultData
object ornull
Sample Code:
// Add a user with auto-generated ID
const user: Object = {
name: 'Kat',
email: '[email protected]',
gender: 'female'
};
userRepository.add(user).then(result => {
console.log("Add Result (auto-generated ID):", result);
});
// Add a user with custom ID
const user2: Object = {
name: 'Gary',
email: '[email protected]',
gender: 'male'
};
userRepository.add(user2, 'helloworld123').then(result => {
console.log("Add Result (custom ID):", result);
});
Update a document.
Parameters:
data
The document objectid
The document IDisMerge
If true, data will merge into existing document. Default is false.
Returns:
ResultData
object ornull
Sample Code:
// Update a user
const user3: Object = {
name: 'Peter',
email: '[email protected]',
gender: 'male'
};
userRepository.update(user3, 'helloworld123').then(result => {
console.log("Updated Result:", result);
});
// Update user email only
const user4: Object = {
email: '[email protected]'
};
userRepository.update(user4, 'helloworld123', true).then(result => {
console.log("Updated Result:", result);
});
Delete a document
Parameters:
id
The document ID
Returns:
ResultData
object ornull
Sample Code:
// Delete a user by ID
userRepository.delete('helloworld123').then(result => {
console.log("Delete Result:", result);
});