最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Creating Schema in Firebase database with NodeJS - Stack Overflow

programmeradmin3浏览0评论

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: ""
})

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

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);
Share Improve this question edited Jan 8, 2019 at 16:21 Alwaysblue asked Jan 8, 2019 at 14:20 AlwaysblueAlwaysblue 11.8k44 gold badges139 silver badges251 bronze badges 4
  • I answered Q1 below. Please open separate questions for each... question. – Frank van Puffelen 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 :) – nanobar 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 ? – Alwaysblue 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 :) – Alwaysblue Commented Jan 8, 2019 at 16:23
Add a comment  | 

6 Answers 6

Reset to default 6

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:

  • https://www.npmjs.com/package/fireorm
  • https://www.npmjs.com/package/firebase-firestorm
  • https://www.npmjs.com/package/typesaurus
  • https://www.npmjs.com/package/@typeheim/orm-on-fire

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.

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()

        };

发布评论

评论列表(0)

  1. 暂无评论