简体   繁体   中英

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

ie 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);

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 .

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

You dont need to create a scheme beforehand.

You can just set the data like:

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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM