npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angularfire-resource

v0.0.3

Published

Resource factory built on top of AngularFire

Downloads

11

Readme

angularfire-resource

Resource factory built on top of AngularFire

Alpha release, feedback apreciated

Abstract

Resource instances are extended firebaseObject, so you'll find everything you're used to with firebase object, plus

  • createdAt / updatedAt timestamps
  • hooks (beforeCreate, afterCreate, beforeSave, afterSave)
  • a global instances map for each resource to ensure an instance is not retrieved 2 times from firebase
  • associations (hasOne, hasMany, hasAndBelongsToMany returning respectively a firebaseObject instance or association collections)

AssociationCollection instances are extended firebaseArray, plus

  • they are made of Resource instances
  • they can preload their own instances associations
  • they deal nicely with the firebase-util librairy for pagination & infinite scrolling work

Usage

Add the 'angularfire-resource' module as a dependency to your application module, and then define your model classes and their relations through the FireResource factory


angular.module('myApp', ['angularfire-resource'])

  .factory('$firebase', function() {
    return new Firebase('https://fireresourcetest.firebaseio.com/');
  })

  .factory('User', function(FireResource, $firebase) {
    return FireResource($firebase.child('users'))
      .hasMany('conversations')
      .hasOne('displayedConversation', {className: 'Conversation', inverseOf: false, foreignKey: 'displayedConversationId' })
  })
  
  .factory('Conversation', function(FireResource, $firebase) {
    return FireResource($firebase.child('conversations'), function(){
      this.hasMany('users');
      // customize the way you store foreign keys to be able to sort your association collection
      this.hasMany('messages', {storedAt: 'createdAtDesc' }, function(baseRef, init){
        // use firebase util to handle the pagination
        init(new Firebase.util.Scroll(baseRef, '$value')).$next(5);  
      });
      // use hooks
      this.beforeCreate(function(){        
        this.createdAtDesc = - Date.now()
      });
    });
  })
  
  .factory('Message', function(FireResource, $firebase) {
    return FireResource($firebase.child('messages'), function(){
      // define one sided association (ie here no message foreign key will be set into the user model)
      this.hasOne('user', { inverseOf: false }); 
      this.hasOne('conversation');
    });
  })
  

The above code will maintain a deserialized data model with duplicated foreign keys, to allow security enforcement and easy admin queries.

root
|_ users
| |_ userId1
| | |_ displayedConversationId: conversationId1
| | |_ conversations
| |   |_ conversationId1: true
| |_ userId2
|   |_ conversations
|     |_ conversationId1: true
|
|
|_ conversations
| |_ conversationId1
|   |_users
|   | |_ userId1: true
|   | |_ userId2: true
|   |_ messages
|     |_ messageId1: aCustomValue 
|     |_ messageId2: aCustomValue
|
|
|_ messages
  |_ messageId1
  | |_ userId: userId1
  | |_ conversationId: conversationId1
  |_ messageId2
    |_ userId: userId2
    |_ conversationId: conversationId1
    

And now you can write some clean controllers :-)


angular.module('myApp')

  // Let's asume $currentUser is an instance of User retrieved from a resolve
  .controller('ExamplesController', function($scope, Message, $currentUser){
    
    // $query on all ( rootUrl/users ) 
    $scope.allUsers = User.$query()
    
    // $query on some, customizing your ref
    $scope.someUsers = User.$query(function(baseRef, init){
      init(new Firebase.util.Scroll(baseRef, 'presence')).$next(10)
    });
    
    // use $next and $prev to access the scroll instance of your custom ref (if firebase util is used)
    $scope.loadMoreUsers = function(){
      $scope.someUsers.$next(10)
    };

    // preload associations
    $scope.conversations = $currentUser.$conversations().$include('messages')
    
    // Instanciate new model 
    $scope.newMessage = Message.$new()
    $scope.saveMessage = function(){
      angular.extend($scope.newMessage, { userId: $currentUser.$id });

      // save a new instance by adding it to a collection 
      $currentUser.$displayedConversation().$messages().$add($scope.newMessage)
      $scope.newMessage = Message.$new();
    };
    
    // do sequential operations
    $scope.createConversationWith = function(user) {
      // create an instance into a collection
      return $currentUser.$conversations().$create()
        .then(function (conversation) {
          // then add it to an other collection
          conversation.$users().$add(user);
        })
  })
    

Demo

To have a more in depth look over a practical case, check out the demo

API / Documentation

In progress here