Friday 5 May 2017

node RESTful API model with firebase

I am trying to create a RESTful API using Node.js, which is to serve for an android application. I am using firebase for my database. I am following a tutorial tutorial to create the API. It uses mongoose and has routes, models and controllers defined separately. This looks like a very good separation of concerns and wanted to follow something similar for my code as well. But wanted to know how I can create my model in this case. I am able to read data from firebase.

//my code so far

var admin = require('firebase-admin');

var serviceAccount = require('../../<firebase.json>');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "<path_to_firebase_db",

});

var db = admin.database();
var ref = db.ref();
var item = ref.child('Item');


item.orderByChild("price").on("child_added", function(snapshot) {
  console.log(snapshot.key + " was " + snapshot.val().price + "$");
});


// my reference as given in the following link:

'use strict';

var mongoose = require('mongoose');

var Schema = mongoose.Schema;


var TaskSchema = new Schema({
  name: {
    type: String,
    Required: 'Kindly enter the name of the task'
  },
  Created_date: {
    type: Date,
    default: Date.now
  },
  status: {
    type: [{
      type: String,
      enum: ['pending', 'ongoing', 'completed']
    }],
    default: ['pending']
  }
});


module.exports = mongoose.model('Tasks', TaskSchema);


I want to create model similar to this, how can I do this? I am new to node and firebase and I have trying to find out some resource for it but have not been successful so far? Or is there another approach?



via roshan

No comments:

Post a Comment