Vue.js Firestore example – Vue.js CRUD serverless with Firebase Cloud Firestore

vuejs-firestore-example-note-app-feature-image

Cloud Firestore helps us store data in the cloud. It supports offline mode so our app will work fine (write, read, listen to, and query data) whether device has internet connection or not, it automatically fetches changes from our database to Firebase Server. We can structure data in our ways to improve querying and fetching capabilities. This tutorial shows you a Vue.js app that can do Firebase Firestore CRUD Operations.

Related Post: Vue.js CRUD example – a simple Note App

Vue.js Firestore example Overview

Goal

Our Vue App can help us write new Notes, then it displays a list of Notes and each Note page (containing title and content) can be modified easily. This App also supports offline mode – we can create/update/delete Note without internet connection.

vuejs-firestore-example-crud-note-app-overview

And, of course, this App interacts with Firebase Firestore as backend infrastructure:

vuejs-firestore-example-note-app-firebase-firestore-result

Demo

Project Structure

vuejs-firestore-example-crud-note-app-project-structure

We have 3 components:
App.vue holds all of the other components.
NotesList.vue contains all of notes in a List with + Note button.
Note.vue display a single Note in the List that allows us to create new Note or edit current Note.

Technologies

– Vue CLI 3.0.1
– Vue 2.5.17
– Firebase SDK for Javascript 5.4.2

Practice

Setup Vue Project
Install vue-cli

For use Vue CLI anywhere, run command:
npm install -g vue-cli

Init Project

Point cmd to the folder you want to save Project folder, run command:
npm create vue-note-app

You will see 2 options, choose default:

vuejs-firestore-example-note-app-project-setup

Install Firebase SDK

Run command: npm install firebase
Once the process is done, you can see firebase in package.json:

Setup Firebase Project
Create Project

Go to Firebase Console, login with your Google Account, then click on Add Project.

Enter Project name, select Location:

vuejs-firestore-example-note-app-add-firebase-project

Then press CREATE PROJECT.

Config Rules for Firebase Cloud Firestore

On the left tab, click on Database.
Choose Cloud Firestore. Click on Create Database, a window is shown, choose Start in test mode:

vuejs-firestore-example-note-app-add-firebase-database-rule

This action is equivalent to set Database Rules:

Child Components

To understand child Components’ code, please read description in the tutorial:
Vue.js CRUD example – a simple Note App

Note Component

components/Note.vue

NotesList Component

components/NotesList.vue

App Component

In this Component, we have to do these things:
– import Firebase, initialize Firestore Database, then make reference to the Database Collection.
– import and interact 2 components above (NotesList and Note).
– use Firebase SDK function to do CRUD operations and listen to the Firestore events (create, delete, update).

Import Firebase Firestore

Go to Firebase Console, choose the Project that we have created before -> Project Overview.
Click on Web App icon:

vuejs-firestore-example-note-app-add-firebase-web-app

A Popup will be shown:

vuejs-firestore-example-note-app-add-firebase-config-project

Copy the code and paste in App.vue, don’t forget to import, initialize Firebase and create Reference to the Notes Collection:

Import Child Components

We define the data() function that returns array of Notes (notes[]) and a number for current Note’s index in the array (index).

Please remember that we have used $emit in 2 child Components before:
+ Note Component: $emit("app-removeNote") & $emit("app-saveNote")
+ NotesList Component: $emit("app-changeNote", index) & $emit("app-addNote")

Now we have 4 event handlers with @app- prefix that point to corresponding methods inside App component:

Do Firebase Firestore CRUD Operations

– Create Note using collectionRef.add({data}):

– Update Note using nodeRef.update({data}):

– Delete Note using nodeRef.delete():

– Read list of Notes using collectionRef.get().then(function(snapshot)):

Listen to the data changes

To listen for changes, use onSnapshot() methods of firebase.firestore.CollectionReference to observe events:

Full code

App.vue

Run

– Run Vue.js App with command: npm run serve.
– Open browser with url: http://localhost:8080/.

Source Code

vue-firestore-example

By grokonez | September 7, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*