Skip to content

shaun-sweet/Active-Fire

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Active-Fire

In Development

#Installing

  • npm install activefire

Setup

  • 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

Config

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.

Usage

Creating a model

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'
	}
})

Adding Data to Models

var user = new User();

user.create('ssweet06', {
	username: 'shaun',
	comments: {

	}
})
var comment = new Comment();
comment.create("comment1", {
  body: "this is a comment",
  user: 'ssweet06',
})

Querying

user.find("ssweet06").then((snapshot) => {
	//do stuff to the object
})
user.findBy("username", "shaun").then((snapshot) => {
	//do stuff to the object
})

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published