Swallowstore
A flexible easy to use Google Cloud Firestore API Wrapper class for Node.JS
Installation
npm install swallowstore
;
Sample Usage
const swallowInstance = ; //swallowstore initialize with firestore configswallowInstance // Get all usersswallowInstance;
Available Methods: findById(), findOne(), findAll(), saveAndUpdate(), paginator(), delete()
API Reference
.initialize(firebaseConfig: object)
Set collection firebase Config.
Parameters:
-
object
firebase config setting//swallowstore initialize with firestore configswallowInstance
.findById(collectionName: string, id: string ): Promise<Document[]>
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; // Get user by idsswallowInstance;
.findOne(collectionName: string, { conditions: Array<Condition> = null } ): Promise<Document[]>
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 idswallowInstance; // Get user by condition(s)const conditions = 'where': 'email' '==' 'hurlatunde@gmail.com' 'password' '==' 'password' ; swallowInstance;
.saveAndUpdate(collectionName: string, data: Object, id: String ): Promise<ResultData>
Add / Update a document.
Parameters:
data
A document objectid
The document ID
Returns:
ResultData
object ornull
Sample Code:
// Add a user with auto-generated IDconst userObject = name: 'Kat' email: 'kat@email.com' gender: 'female'; swallowInstance;// response { node_id: 'BQFNY9pQDhZOarvmoMSB' } // Add a user with custom IDconst userObject = node_id: 'BQFNY9pQDhZOarvmoMSB' name: 'Gary' email: 'gary@email.com' gender: 'male'; swallowInstance;// response { node_id: 'BQFNY9pQDhZOarvmoMSB' } // Update a user with IDswallowInstance;// 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 usersswallowInstance; // Get all users by condition(s)const conditions = 'where': 'age' '>=' 20 'likes' '>=' 200 'limit': 5; swallowInstance;
.paginator(collectionName: string): paginatorDocument{}
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 userslet paginateInit;paginateInit = swallowInstance;paginateInit; // Get the next index of user collection by condition(s)paginateInitnext; // Get the previous index of user collection by condition(s)paginateInitprevious;
delete(collectionName: string, id: string): Promise<ResultData>
Delete a document
Parameters:
id
The document ID
Returns:
ResultData
object ornull
Sample Code:
// Delete a user by IDswallowInstance;