A flexible easy to use Google Cloud Firestore API Wrapper class for Node.JS
npm install swallowstore
import * as swallowInstance from "swallowstore";
const swallowInstance = require('swallowstore');
//swallowstore initialize with firestore config
swallowInstance.initialize({
databaseURL: "https://my-first-project.com",
projectId: "my-first-project",
})
// Get all users
swallowInstance.findAll('users').then(response => {
console.log(response);
});
Available Methods: findById(), findOne(), findAll(), saveAndUpdate(), paginator(), delete()
Set collection firebase Config.
Parameters:
-
object
firebase config setting//swallowstore initialize with firestore config swallowInstance.initialize({ databaseURL: "https://my-first-project.com", projectId: "my-first-project", })
Get One document in a collection by ID.
Parameters:
id
The document ID /ids
The document IDs
Returns:
- A
Document
object ornull
Sample Code:
// Get user by id
swallowInstance.findById('users', '5ixWj00cZYDirdz9CCJd').then(response => {
console.log("users with id '5ixWj00cZYDirdz9CCJd' detail:", response);
});
// Get user by ids
swallowInstance.findById('users', ['0Qq4GEfXPsc2ixvDZv8MFEcu2ek1', '0Qq4GEfXPsc2ixvDZv8MFEcu2ek1']).then(response => {
console.log("users with ids '['0Qq4GEfXPsc2ixvDZv8MFEcu2ek1', '0Qq4GEfXPsc2ixvDZv8MFEcu2ek1']' detail:", response);
});
Get One document in a collection by ID or by conditions.
Parameters:
conditions
Array of conditionsid
The document ID
Returns:
- A
Document
object ornull
Sample Code:
// Get user by id
swallowInstance.findOne('users', { id: '5ixWj00cZYDirdz9CCJd' }).then(response => {
console.log("users with id '5ixWj00cZYDirdz9CCJd' detail:", response);
});
// Get user by condition(s)
const conditions = {
'where': [
['email', '==', 'hurlatunde@gmail.com']
['password', '==', 'password']
]
};
swallowInstance.findOne('users', conditions).then(response => {
console.log("user:", response);
});
Add / Update a document.
Parameters:
data
A document objectid
The document ID
Returns:
ResultData
object ornull
Sample Code:
// Add a user with auto-generated ID
const userObject = {
name: 'Kat',
email: 'kat@email.com',
gender: 'female'
};
swallowInstance.saveAndUpdate('users', userObject).then(response => {
console.log("Add Result (auto-generated ID):", response);
});
// response { node_id: 'BQFNY9pQDhZOarvmoMSB' }
// Add a user with custom ID
const userObject = {
node_id: 'BQFNY9pQDhZOarvmoMSB',
name: 'Gary',
email: 'gary@email.com',
gender: 'male'
};
swallowInstance.saveAndUpdate('users', userObject).then(response => {
console.log("Add Result (custom ID):", response);
});
// response { node_id: 'BQFNY9pQDhZOarvmoMSB' }
// Update a user with ID
swallowInstance.saveAndUpdate('users', userObject, 'BQFNY9pQDhZOarvmoMSB').then(response => {
console.log("Updated Result:", response);
});
// response { node_id: 'BQFNY9pQDhZOarvmoMSB' }
.findAll(collectionName: string, { conditions: Array<Condition> = null, orderBy: Array<OrderBy> = null, limit: number = null } ): Promise<Document[]>
Get document in a collection.
Parameters:
conditions
Array of conditionsorderBy
Field name to order bylimit
Number of documents to retrieve || default is 20
Returns:
- Array of
Document
object
Sample Code:
// Get all users
swallowInstance.findAll('users').then(response => {
console.log("All users: ", response);
});
// Get all users by condition(s)
const conditions = {
'where': [
['age', '>=', 20]
['likes', '>=', 200]
],
'limit': 5
};
swallowInstance.findAll('users', conditions).then(response => {
console.log("All users with age >= '20' & likes >= '200', limit '5':", response);
});
Get paginate document in a collection.
Parameters:
conditions
Array of conditionsorderBy
Field name to order bylimit
Number of documents to retrieve || default is 20
Returns:
- Array of
Document
object
Sample Code:
// Get all users
let paginateInit;
paginateInit = swallowInstance.paginator('users');
paginateInit.params({'limit': 2, 'orderBy': 'full_name'}).then(({data}) => {
console.log(data);
});
// Get the next index of user collection by condition(s)
paginateInit.next().then(({data}) => {
console.log("next users collection: ",data);
});
// Get the previous index of user collection by condition(s)
paginateInit.previous().then(({data}) => {
console.log("previous users collection: ",data);
});
Delete a document
Parameters:
id
The document ID
Returns:
ResultData
object ornull
Sample Code:
// Delete a user by ID
swallowInstance.delete('users', {'id' : 'HnjIzeysNmi4DLL2tFUJ'}).then((res) => {
console.log("Delete Result:", result);
});