13

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);
4
  • I answered Q1 below. Please open separate questions for each... question. Commented Jan 8, 2019 at 15:00
  • 1
    Once you go schemaless you don't go back, as the saying goes. Whole point of NoSQL is to be schemaless, mongoose is just a bloated relational crutch in front of an already elegant driver. It also encourages you to stay in the relational mindset which doesn't always lend itself to optimal performance in a nosql world imo :) Commented Jan 8, 2019 at 16:09
  • @Dominic I was just typing other two questions when you shared your comment. But why schemaless is preferred over schema? Frankly, I am literally pondering over why firebase is schemaless ? Commented Jan 8, 2019 at 16:12
  • Frank, thanks a lot for the answer. Here is the second question stackoverflow.com/questions/54095750/… Also, I have edited current question and removed other two questions :) Commented Jan 8, 2019 at 16:23

6 Answers 6

7

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.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use an npm package "firefose". Documentation can be found here: https://www.npmjs.com/package/firefose

1 Comment

This looks very promising. I'll give it a try! :-)
1

You dont need to create a scheme beforehand.

You can just set the data like:

firebase.database()
  .ref('users')
  .set({
    email,
  })

Comments

0

Yes, you can create db schema for firestore in node js using class. There are a few third party libraries for firestore ORM (Object-relational Mapper) available:

1 Comment

hey thanks this is helpful, for me, since i have to work with flask and firebase, so I have searched with the same keywords and one extra keyword "pip" these packages are the one which i am looking for.
0

If you can stick to relatively flat data structure then, in my opinion, the best solution for bringing schema to Firestore is to use a separate collection for storing schemas as docs. You can then use cloud functions to validate data and write data. The biggest advantage of this approach is that you can create and modify schemas at run time. Overall it's a bit more work but saves a lot of hassle long therm. I've made a CMS using this approach, it works well and is very cost effective comparing to MongoDB.

2 Comments

how does one measure a relatively flat data structure?
You don't measure it. In this context "relatively flat" is a figure of speech and its purpose it to highlight that the proposed solution requires taking into consideration how nested the data structure is as this factor does affect the performance. In another words it's a suggestion: less nested data structure is better this solution works. But there's no clear cut here and it's up to whoever does the implementation to make the call.
-1

You dont need to create a scheme beforehand. but if you want to define the data type of your collection before sending to firebase, use;

const data = {
            title: fullName.trim(),
            email: email.trim()

        };

1 Comment

The OP is asking to use a scheme though. I don't think this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.