In Development
#Installing
- npm install activefire
- Download your service.json from google Instructions here
- In the root directory of your app, make an ENV folder and put the file you got from google (service.json) in that folder
In a separate file add the following:
//config.js
var af = require('activefire')
module.exports = {activeFive: new af(*service.json*, https://*yourURL*.firebaseio.com)}
Where you want to run the code: imports the object with the ActiveFire instance/Firebase Connection var config = require('./config') var activeFire = config.activeFire;
Note: Because firebase uses websockets with MongoDB it doesn't open and close database connections on every call, it stays open. For that reason you have to have the same connection open throughout your application.
Create a new javascript constructor or class. I will be using examples in ECMAScript 6.
var config = require('../config')
var activeFire = config.activeFire;
class User extends activeFire.base {
constructor(){
super();
this.modelName = 'users';
}
}
module.exports = User;
Another example:
var config = require('../config')
var activeFire = config.activeFire;
class Comment extends activeFire.base {
constructor(){
super();
this.modelName = 'comments';
}
}
module.exports = Comment;
IMPORTANT: this.modelName value MUST be a string that matches the following step
activeFire.newModel('users',{
attributes: {
username: 'string'
},
relationships: {
comments: 'has_many'
}
})
comments belonging to the users:
activeFire.newModel('comments',{
attributes: {
user: 'string',
body: 'string',
},
relationships: {
users: 'belongs_to'
}
})
var user = new User();
user.create('ssweet06', {
username: 'shaun',
comments: {
}
})
var comment = new Comment();
comment.create("comment1", {
body: "this is a comment",
user: 'ssweet06',
})
user.find("ssweet06").then((snapshot) => {
//do stuff to the object
})
user.findBy("username", "shaun").then((snapshot) => {
//do stuff to the object
})