Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Schema in Firebase database with NodeJS

I am not a pro in JS/NodeJS and I have some experience using express-mongoose.

Now, I prefer MVC approach, meaning creating schema and importing it.

Since, Firebase database also happens to be no sql database, I decided on doing exactly what I am suppose to do in mongoose but unfortunately I am unable to understand.

To say the least, I have created index.js which is my entry point of the app.

And as per firebase docs, I am initialising it like this (index.js)

const admin = require("firebase-admin");

//Firbade config file 
const firebaseConfig = require("./functions-config.json")

admin.initializeApp({
    credential: admin.credential.cert(firebaseConfig),
    databaseURL: "https://functions-firebase-43a59.firebaseio.com"
})

const db = admin.firestore()

.

Question: Can we create schema in firebase db as we are used to creating in mongoose

i.e this is the example of creating mongoose schema in NodeJS (models/user.js)

const mongoose = require('mongoose')

const userSchema = new mongoose.Schema({
    fullName: String,
    email: String,
    image: String, 
    gender: String,
    profile_id: String,
    createdAt: {type: Date, default: Date.now}
}) 


module.exports = mongoose.model('User', userSchema);
like image 215
anny123 Avatar asked Jan 08 '19 14:01

anny123


1 Answers

Can we create schema in firebase db as we are used to creating in mongoose?

Firebase comes with two databases: the Realtime Database, and Cloud Firestore. Both of those are schemaless NoSQL databases. But in both cases, you can use the database's server-side security rules to control what type of data can be written to it.

For Realtime Database, see the documentation, and this classic video for an introduction. You'll specifically want to look at the .validate rules here.

For Cloud Firestore, see the documentation, and episode 6 in the Getting to know Cloud Firestore video series.

like image 192
Frank van Puffelen Avatar answered Sep 16 '22 15:09

Frank van Puffelen