Services (Firebase)

Understanding SwallowJS (Firebase) Services

Services is fully responsible for managing almost everything regarding your data, its interactions, as well as the evolution of information workflow.Understanding that this connects mainly with Firebase data system reliable for storing and syncing data in real time across all connected clients

findOne function

FirebaseService.findOne

Retrieve a node from Firebase server.


FirebaseService.findOne( params, [ [success],[error] ] )

Retrieve a single resource from the Firebase server.

Parameters

params {Object}
Values to filter the request or results with. params.path required
params.listenerType optional
params.eventType optional
function (response)

Returns

response
A callback on successful retrieval. The callback receives the retrieved resource as response
response.error
A callback when an error occurs. The callback receives an object response.error

Use

FirebaseService.findOne() is used to retrieve a model instance from the server.

Use findOne like:

                                        
                                            FirebaseService.findOne({
                                                path: 'posts/post_one',
                                            }, function(response) {
                                                if(!response.error) {
                                                    // success
                                                    console.log(response);
                                                } else {
                                                    // error
                                                    console.log(response);
                                                }
                                            });
                                        
                                    
                                        
                                            listenerType: 'once', // 'on','once'
                                            eventType: 'value', // 'value', child_added'
                                        
                                    

The return response should look like:

                                        
                                            {
                                               author: "John Deauthor",
                                               body: "Conveniently harness fictionalized communities high-impact markets under whelm go.",
                                               title: "The post title ", node_id: "post_one"
                                            }
                                        
                                    

findAll function

FirebaseService.findAll

Retrieve multiple nodes from Firebase server.


FirebaseService.findAll( params, [[success],[error]] )

Retrieve multiple resource from the Firebase server.

Parameters

params {Object}
Values to filter the request or results with. params.path required
params.limit required
params.listenerType optional
params.eventType optional
function (response)

Returns

response
A callback on successful retrieval. The callback receives the retrieved resource as response
response.error
A callback when an error occurs. The callback receives an object response.error

Use

FirebaseService.findAll() is used to retrieve a model instance from the server.

Use findAll like:

                                        
                                            FirebaseService.findAll({
                                                path: '/author/posts'
                                                limit: '4'
                                            }, function(response) {
                                                if(!response.error) {
                                                    // success
                                                    console.log(response);
                                                } else {
                                                    // error
                                                    console.log(response);
                                                }
                                            });
                                        
                                    
                                        
                                            listenerType: 'once', // 'on','once'
                                            eventType: 'value', // 'value', child_added'
                                        
                                    

The return data should look like:

                                        
                                            {
                                                data: [
                                                    {author: "John Deauthor", body: "post body content.", title: "The post title ", node_id: "post_one"},
                                                    {author: "John Deauthor", body: "post body content.", title: "Another post title ", node_id: "post_two"},
                                                    {author: "John Deauthor", body: "post body content.", title: "Another post title ", node_id: "post_three"},
                                                ]
                                            {
                                        
                                    

saveData function

FirebaseService.saveData

Save node to Firebase server.


FirebaseService.saveData( params, [[error]] )

Retrieve a single resource from the Firebase server.

Parameters

params {Object}
Values to filter the request or results with. params.path required
params.data required
function (response)

Returns

response
A callback on successful retrieval. The the node id of the saved data -KT-kz68IJrOajjwg865
response.error
A callback when an error occurs. The callback receives an object response.error

Using saveData to create an instance.

FirebaseService.saveData() is used to retrieve a model instance from the server.

If saveData() is called an instance is created in Firebase.

                                        
                                            FirebaseService.saveData({
                                                path: '/author/posts',
                                                data: ({author:"John Deauthor", body: "post body content.", title: "Another post title "})
                                            }, function(data) {
                                                if(!data.error) {
                                                    // success
                                                    console.log(data);
                                                } else {
                                                    // error
                                                    console.log(data);
                                                }
                                            });
                                        
                                    
                                        
                                            listenerType: 'once', // 'on','once'
                                            eventType: 'value', // 'value', child_added'
                                        
                                    

If updateData() is update the instance created on Firebase base on the node key in this case . -KT-kz68IJrOajjwg865

                                        
                                            FirebaseService.updateData({
                                                path: '/author/posts/-KT-kz68IJrOajjwg865',
                                                data: ({author:"John Deauthor", body: "post body content.", title: "Another post title "})
                                            }, function(data) {
                                                if(!data.error) {
                                                    // success
                                                    console.log(data);
                                                } else {
                                                    // error
                                                    console.log(data);
                                                }
                                            });
                                        
                                    

deleteData function

FirebaseService.deleteData

Delete node from Firebase server.


FirebaseService.deleteData( params, [[success],[error]] )

Destroy resource from the Firebase server.

Parameters

params {Object}
full path values of the resource to destroy with. params.path required
function (response)

Returns

response
A callback on successful retrieval. The callback receives the retrieved resource as response
response.error
A callback when an error occurs. The callback receives an object response.error

Use

FirebaseService.deleteData() is used to destroy resource from the server.

Use deleteData like:

                                        
                                            FirebaseService.deleteData({
                                                path: '/author/posts/-KT-kz68IJrOajjwg865'
                                            }, function(response) {
                                                if(!response.error) {
                                                    // success
                                                    console.log(response);
                                                } else {
                                                    // error
                                                    console.log(response);
                                                }
                                            });
                                        
                                    

customQuery function

FirebaseService.customQuery

Retrieve multiple nodes from Firebase server dynamic query.


FirebaseService.customQuery( params, [[success],[error]] )

Dynamic query to retrieve multiple resource from the Firebase server.

Parameters

params {Object}
Values to filter the request or results with. params.query required
params.listenerType optional
params.eventType optional
function (response)

Returns

response
A callback on successful retrieval. The callback receives the retrieved resource as response
response.error
A callback when an error occurs. The callback receives an object response.error

Use

FirebaseService.customQuery() is used to retrieve a model instance from the server.

Use customQuery like:

                                        
                                            FirebaseService.customQuery({
                                                query: firebaseBaseDatabase.ref('author').limitToLast(2).orderByKey(),
                                            }, function(response) {
                                                if(!response.error) {
                                                    // success
                                                    console.log(response);
                                                } else {
                                                    // error
                                                    console.log(response);
                                                }
                                            });
                                        
                                    
                                        
                                            listenerType: 'once', // 'on','once'
                                            eventType: 'value', // 'value', child_added'
                                        
                                    

The return data should look like:

                                        
                                            {
                                                data: [
                                                    {author: "John Deauthor", body: "post body content.", title: "The post title ", node_id: "post_one"},
                                                    {author: "John Deauthor", body: "post body content.", title: "Another post title ", node_id: "post_two"},
                                                ]
                                            {
                                        
                                    

incrementValue function

FirebaseService.incrementValue

Increment nodes value in the Firebase server.


FirebaseService.incrementValue( params, [[success],[error]] )

Dynamic query to retrieve multiple resource from the Firebase server.

Parameters

params {Object}
Values to filter the request or results with. params.path required
params.incrementBy (int) required
function (response)

Returns

response
A callback on successful retrieval. The callback receives the retrieved resource as response

Use

FirebaseService.incrementValue() is used to increment a model value instance on the server.

Use incrementValue like:

                                        
                                            FirebaseService.incrementValue({
                                                path: '/author/posts/-KT-kz68IJrOajjwg865/view',
                                                incrementBy: 1
                                            }, function(response) {
                                                if(!response.error) {
                                                    // success
                                                    console.log(response);
                                                } else {
                                                    // error
                                                    console.log(response);
                                                }
                                            });
                                        
                                    

The return data should look like:

                                        
                                            incrementValue completed
                                        
                                    

fileUpload function

FirebaseService.fileUpload

Retrieve multiple nodes from Firebase server.


FirebaseService.findAll( params, [[success],[error]] )

Retrieve multiple resource from the Firebase server.

Parameters

params {Object}
Values to filter the request or results with. params.path required
params.limit required
params.listenerType optional
params.eventType optional
function (response)

Returns

response
A callback on successful retrieval. The callback receives the retrieved resource as response

Use

FirebaseService.fileUpload() is used to upload files instance from the firebase storage.

Use fileUpload like:

                                        <input type="file" name="img_src" id="src_input_id">
                                    
                                        
                                            // returns file in input field
                                            getInputFile('src_input_id');
                                        
                                    
                                        
                                            FirebaseService.fileUpload({
                                                'file': getInputFile('img_src'),
                                                'path': '/author/posts/-KT-kz68IJrOajjwg865/image
                                            }, function (data) {
                                                console.log(data);
                                                if (data.downloadURL) {
                                                   // data.downloadURL
                                                }
                                            });
                                        
                                    

Progress status:

                                        
                                            {
                                                progress: 82.1
                                                element: "hklwzqjhguax"
                                                fileName: "hklwzqjhguax.mp3"
                                                fileSize: "1.646 MB"
                                                fileType: "audio/mp3"
                                            }

                                        
                                    

The return data should look like:

                                        
                                            {
                                                downloadURL:  "https://firebasestorage.googleapis.com/v0/b/app-gen.appspot.com/o/-KXeLTodVdfAcMNTmVVr%2F-KaGf2Y2jJO9h_p_49g_0Skitmp3.extention"
                                                element: "hklwzqjhguax"
                                                fileName: "hklwzqjhguax.mp3"
                                                fileSize: "1.646 MB"
                                                fileType: "audio/mp3"
                                            }
                                        
                                    

Getting Help

If you’re stuck, We are her to to help you out,we are always ready to listen on Slack

We’ve created a framework to help you kick start your firebase project faster and better

The most important parts of the Swallow.Js Framework are the simple included set of different functions. Swallow.Js is built on these components PathJs for routing, MustacheJs for view and depend more on Firebase for data-source, and also build-in server side request for your regular JSON calls.